/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketImpl.java

http://mobicents.googlecode.com/ · Java · 249 lines · 129 code · 48 blank · 72 comment · 13 complexity · d2a0bccb978d35daa5fb516205f5fe3a MD5 · raw file

  1. /**
  2. * Start time:10:17:34 2009-08-03<br>
  3. * Project: mobicents-media-server-test-suite<br>
  4. *
  5. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski </a>
  6. */
  7. package org.mobicents.media.server.testsuite.general.rtp;
  8. import java.io.IOException;
  9. import java.net.DatagramSocket;
  10. import java.net.InetAddress;
  11. import java.net.InetSocketAddress;
  12. import java.net.SocketException;
  13. import java.nio.channels.DatagramChannel;
  14. import java.nio.channels.SelectionKey;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Vector;
  18. import javax.sdp.Attribute;
  19. import org.apache.log4j.Logger;
  20. /**
  21. * Start time:10:17:34 2009-08-03<br>
  22. * Project: mobicents-media-server-test-suite<br>
  23. *
  24. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski</a>
  25. */
  26. public class RtpSocketImpl implements RtpSocket {
  27. private static final transient Logger log = Logger.getLogger(RtpSocketImpl.class);
  28. private int localPort;
  29. private String localAddress;
  30. private int remotePort;
  31. private String remoteAddress;
  32. private InetSocketAddress remoteInetSocketAddress = null;
  33. // holder for dynamic payloads.
  34. private Vector<Attribute> rtpMap = new Vector<Attribute>();
  35. private final List<RtpSocketListener> dataSinks = new ArrayList<RtpSocketListener>();
  36. private RtpSocketFactoryImpl rtpSocketFactoryImpl;
  37. private DatagramChannel channel;
  38. private DatagramSocket socket;
  39. private SelectionKey key;
  40. private String connectionIdentifier;
  41. /**
  42. *
  43. * @param transceiver
  44. * @param timer
  45. * @param formatMap
  46. * @param rtpSocketFactoryImpl2
  47. */
  48. public RtpSocketImpl(Vector<Attribute> formatMap, RtpSocketFactoryImpl rtpSocketFactory) {
  49. this.rtpSocketFactoryImpl = rtpSocketFactory;
  50. this.rtpMap.addAll(formatMap);
  51. this.localAddress = this.rtpSocketFactoryImpl.getBindAddress();
  52. }
  53. public int init(InetAddress localAddress, int lowPort, int highPort) throws SocketException, IOException {
  54. if (this.key != null) {
  55. this.key.cancel();
  56. this.key = null;
  57. }
  58. channel = DatagramChannel.open();
  59. channel.configureBlocking(false);
  60. socket = channel.socket();
  61. boolean bound = false;
  62. this.localAddress = localAddress.getHostAddress();
  63. this.localPort = lowPort;
  64. // looking for first unused port
  65. while (!bound) {
  66. try {
  67. // creating local address and trying to bind socket to this
  68. // address
  69. InetSocketAddress bindAddress = new InetSocketAddress(localAddress, localPort);
  70. socket.bind(bindAddress);
  71. bound = true;
  72. // if stunHost is assigned then stun ussage is supposed
  73. // discovering paublic address
  74. } catch (SocketException e) {
  75. // increment port number util upper limit is not reached
  76. localPort++;
  77. if (localPort > highPort) {
  78. throw e;
  79. }
  80. }
  81. }
  82. return localPort;
  83. }
  84. /*
  85. * (non-Javadoc)
  86. *
  87. * @see org.mobicents.media.server.testsuite.general.rtp.RtpSocket#addListener
  88. * (org.mobicents.media.server.testsuite.general.rtp.RtpSocketListener)
  89. */
  90. public void addListener(RtpSocketListener listener) {
  91. this.dataSinks.add(listener);
  92. }
  93. /*
  94. * (non-Javadoc)
  95. *
  96. * @see org.mobicents.media.server.testsuite.general.rtp.RtpSocket#removeListener
  97. * (org.mobicents.media.server.testsuite.general.rtp.RtpSocketListener)
  98. */
  99. public void removeListener(RtpSocketListener listener) {
  100. this.dataSinks.remove(listener);
  101. }
  102. public void resetRtpMap() {
  103. this.rtpMap.clear();
  104. this.rtpMap.addAll(this.rtpSocketFactoryImpl.getFormatMap());
  105. }
  106. public boolean isChannelOpen() {
  107. return (this.channel != null && this.channel.isOpen());
  108. }
  109. public void release() {
  110. this.resetRtpMap();
  111. if (socket != null) {
  112. socket.disconnect();
  113. // socket.close();
  114. }
  115. dataSinks.clear();
  116. this.connectionIdentifier = null;
  117. this.rtpSocketFactoryImpl.releaseSocket(this);
  118. }
  119. /**
  120. * Closes socket
  121. *
  122. */
  123. public void close() {
  124. if (channel != null) {
  125. try {
  126. channel.disconnect();
  127. } catch (IOException e) {
  128. }
  129. }
  130. }
  131. /**
  132. * Assigns remote end.
  133. *
  134. * @param address
  135. * the address of the remote party.
  136. * @param port
  137. * the port number of the remote party.
  138. */
  139. public void setPeer(InetAddress address, int port) throws IOException {
  140. remoteAddress = address.getHostAddress();
  141. remotePort = port;
  142. remoteInetSocketAddress = new InetSocketAddress(remoteAddress, remotePort);
  143. channel.connect(remoteInetSocketAddress);
  144. this.key = rtpSocketFactoryImpl.registerSocket(this);
  145. if (log.isDebugEnabled()) {
  146. log.debug("Connect RTP socket[" + localAddress + ":" + localPort + " to " + remoteAddress + ":"
  147. + remotePort);
  148. }
  149. }
  150. /**
  151. * Gets address to which this socked is bound.
  152. *
  153. * @return either local address to which this socket is bound
  154. */
  155. public String getLocalAddress() {
  156. return localAddress;
  157. }
  158. /**
  159. * Returns port number to which this socked is bound.
  160. *
  161. * @return port number or -1 if socket not bound.
  162. */
  163. public int getLocalPort() {
  164. return localPort;
  165. }
  166. public void notify(Exception e) {
  167. for (RtpSocketListener l : this.dataSinks)
  168. l.error(e);
  169. }
  170. /*
  171. * (non-Javadoc)
  172. *
  173. * @see org.mobicents.media.server.testsuite.general.rtp.RtpSocket#receive(org
  174. * .mobicents.media.server.testsuite.general.rtp.RtpPacket)
  175. */
  176. public void receive(RtpPacket rtpPacket) {
  177. rtpPacket.setConnectionIdentifier(this.connectionIdentifier);
  178. for (RtpSocketListener l : this.dataSinks)
  179. l.receive(rtpPacket);
  180. }
  181. /*
  182. * (non-Javadoc)
  183. *
  184. * @see org.mobicents.media.server.testsuite.general.rtp.RtpSocket#getSelectionKey()
  185. */
  186. public SelectionKey getSelectionKey() {
  187. return this.key;
  188. }
  189. public void setSelectionKey(SelectionKey key) {
  190. this.key = key;
  191. }
  192. public DatagramChannel getChannel() {
  193. return this.channel;
  194. }
  195. public String getConnectionIdentifier() {
  196. return connectionIdentifier;
  197. }
  198. public void setConnectionIdentifier(String connectionIdentifier) {
  199. this.connectionIdentifier = connectionIdentifier;
  200. }
  201. }