Revision: 7403
Author: baranowb
Date: Sun Aug  9 21:50:54 2009
Log: change:
 - make tool use different ports,
http://code.google.com/p/mobicents/source/detail?r=7403

Modified:
/trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/AbstractCall.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/AbstractTestCase.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/ann/AnnCall.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocket.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketFactory.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketFactoryImpl.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketImpl.java /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/Transceiver.java

=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/AbstractCall.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/AbstractCall.java Sun Aug 9 21:50:54 2009
@@ -143,7 +143,7 @@
        protected void releaseSocket() {
                try {
                        if (this.socket != null) {
-                               this.socket.close();
+                               this.socket.release();
                                this.socket = null;
                        }

=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/AbstractTestCase.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/AbstractTestCase.java Sun Aug 9 21:50:54 2009
@@ -131,7 +131,6 @@
                if(this.socketFactory==null)
                {
                        this.socketFactory = new RtpSocketFactoryImpl();
-                       this.socketFactory.setTimer(new TimerImpl());
                        //this.socketFactory.setPortRange("5000-10000");

                }
=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/ann/AnnCall.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/ann/AnnCall.java Sun Aug 9 21:50:54 2009
@@ -316,7 +316,7 @@
                                        
.getServerJbossBindAddress().getHostAddress()
                                        + ":" + 
super.testCase.getCallDisplayInterface().getRemotePort());

- CreateConnection crcx = new CreateConnection(this, this.callIdentifier, ei, ConnectionMode.SendRecv); + CreateConnection crcx = new CreateConnection(this, this.callIdentifier, ei, ConnectionMode.SendOnly);
                        // int localPort = 
this.datagramChannel.socket().getLocalPort();
                        int localPort = super.socket.getLocalPort();
                        String sdp = super.getLocalDescriptor(localPort);
=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocket.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocket.java Sun Aug 9 21:50:54 2009
@@ -9,6 +9,8 @@
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.SocketException;
+import java.nio.channels.DatagramChannel;
+import java.nio.channels.SelectionKey;

 /**
  * Start time:09:28:08 2009-08-03<br>
@@ -23,7 +25,7 @@
         */
        void close();

-
+       public SelectionKey getSelectionKey();


        public void setPeer(InetAddress address, int port) throws IOException;
@@ -54,4 +56,24 @@
         * @param rtpPacket
         */
        void receive(RtpPacket rtpPacket);
-}
+
+       /**
+        * @return
+        */
+       DatagramChannel getChannel();
+       public boolean isChannelOpen();
+
+       /**
+        *
+        */
+       void release();
+
+       /**
+        * @param bindAddress
+        * @param i
+        * @param j
+        */
+ int init(InetAddress bindAddress, int lowPOrt, int highPort) throws SocketException, IOException;
+
+
+}
=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketFactory.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketFactory.java Sun Aug 9 21:50:54 2009
@@ -39,14 +39,7 @@
        public void stop();

        public void start() throws SocketException, IOException;
-       /**
-        * Set execturo, this will read data from sockets
-        *
-        * @param executor
-        */
-       public void setTimer(Timer executor);
-
-       public Timer getTimer();
+

        /**
         * Gets the IP address to which trunk is bound. All endpoints of the 
trunk
@@ -70,15 +63,18 @@
         *
         * @return the string in format "lowPort-highPort".
         */
-       public int getPort();
-
+       public String getPortRange();
+
+
+       public int getLowPort();
+       public int getHighPort();
        /**
         * Modify port used to create RTP stream.
         *
         * @param portRange
         *            the string in format "lowPort-highPort"
         */
-       public void setPort(int port);
+       public void setPortRange(String portRange);

        // FIXME: should we reuse org.mobicents.media.Format?

=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketFactoryImpl.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketFactoryImpl.java Sun Aug 9 21:50:54 2009
@@ -14,10 +14,12 @@
 import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
 import java.nio.channels.DatagramChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.spi.SelectorProvider;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -29,7 +31,7 @@

 import org.apache.log4j.Logger;

-import org.mobicents.media.server.testsuite.general.Timer;
+

 /**
  * Start time:09:56:52 2009-08-03<br>
@@ -41,24 +43,49 @@

private static final Logger log = Logger.getLogger(RtpSocketFactoryImpl.class);

+
+       private String localPortRange="1024-6000";
+       private int lowPort = 1024;
+       private int highPort = 6000;
        private InetAddress bindAddress;
-
-       private int localPort=6006;
-
-
-       private Timer timer;
+
+
        private Vector<Attribute> formatMap;
-
- protected volatile HashMap<SocketAddress, RtpSocket> rtpSockets = new HashMap<SocketAddress, RtpSocket>();
+       private Selector readSelector;
+ private LinkedList<RtpSocket> notUsedSockets = new LinkedList<RtpSocket>(); + protected volatile HashMap<SelectionKey, RtpSocket> rtpSockets = new HashMap<SelectionKey, RtpSocket>();
        private Transceiver transceiver;
+
+
+
+       public RtpSocketFactoryImpl() throws IOException {
+               super();
+               readSelector = SelectorProvider.provider().openSelector();
+
+       }

        public RtpSocket createSocket() throws SocketException, IOException {
- RtpSocket rtpSocket = new RtpSocketImpl(transceiver, timer, formatMap, this);
+//             RtpSocket rtpSocket = new RtpSocketImpl(formatMap, this);
+//             return rtpSocket;
+
+               RtpSocket rtpSocket = notUsedSockets.poll();
+
+               if (rtpSocket == null) {
+
+                       rtpSocket = new RtpSocketImpl(formatMap, this);
+                       rtpSocket.init(bindAddress, this.lowPort, 
this.highPort);
+               } else if (!rtpSocket.isChannelOpen()) {
+ log.error("The RTPSocket's DatagramChannel is closed. Re init() RtpSocket ");
+                       rtpSocket.init(bindAddress, lowPort, highPort);
+               }
+
                return rtpSocket;
        }

        public void releaseSocket(RtpSocket rtpSocket) {
-               rtpSockets.remove(((RtpSocketImpl)rtpSocket).remoteAddress);
+
+               this.rtpSockets.remove(rtpSocket.getSelectionKey());
+               this.notUsedSockets.add(rtpSocket);
        }

        /*
@@ -72,17 +99,6 @@
        }


-
-       /*
-        * (non-Javadoc)
-        *
-        * @see
- * org.mobicents.media.server.testsuite.general.rtp.RtpSocketFactory#getTimer
-        * ()
-        */
-       public Timer getTimer() {
-               return this.timer;
-       }

        /*
         * (non-Javadoc)
@@ -105,18 +121,6 @@
                this.formatMap = originalFormatMap;

        }
-
-
-       /*
-        * (non-Javadoc)
-        *
-        * @see
- * org.mobicents.media.server.testsuite.general.rtp.RtpSocketFactory#setTimer
-        * (java.util.concurrent.ScheduledExecutorService)
-        */
-       public void setTimer(Timer executor) {
-               this.timer = executor;
-       }

        /*
         * (non-Javadoc)
@@ -130,6 +134,16 @@
                        this.transceiver.stop();
                        this.transceiver = null;
                }
+               for(RtpSocket socket:rtpSockets.values())
+               {
+                       socket.close();
+               }
+               rtpSockets.clear();
+               for(RtpSocket socket:this.notUsedSockets)
+               {
+                       socket.close();
+               }
+               this.notUsedSockets.clear();
                log.info("Stopped RTP Factory");

        }
@@ -138,19 +152,18 @@
* @see org.mobicents.media.server.testsuite.general.rtp.RtpSocketFactory#getBindAddress()
         */
        public String getBindAddress() {
-               // TODO Auto-generated method stub
-               return null;
+               return this.bindAddress.toString();
        }

        /* (non-Javadoc)
* @see org.mobicents.media.server.testsuite.general.rtp.RtpSocketFactory#start()
         */
        public void start() throws SocketException, IOException {
- InetSocketAddress address = new InetSocketAddress(bindAddress, localPort);
-
- log.info("Binding RTP transceiver to " + bindAddress + ":" + localPort);
-
-        transceiver = new Transceiver(rtpSockets, bindAddress, localPort);
+
+
+ log.info("Binding RTP transceiver to " + bindAddress + ":" + localPortRange);
+
+        transceiver = new Transceiver(rtpSockets, readSelector);
         transceiver.start();

        }
@@ -158,18 +171,51 @@
        /* (non-Javadoc)
* @see org.mobicents.media.server.testsuite.general.rtp.RtpSocketFactory#getPort()
         */
-       public int getPort() {
-               return this.localPort;
+       public String getPortRange() {
+               return this.localPortRange;
        }

        /* (non-Javadoc)
* @see org.mobicents.media.server.testsuite.general.rtp.RtpSocketFactory#setPort(int)
         */
-       public void setPort(int port) {
-               this.localPort = port;
+       public void setPortRange(String port) {
+               this.localPortRange = port;

        }

+       public int getLowPort() {
+               return lowPort;
+       }
+
+       public void setLowPort(int lowPort) {
+               this.lowPort = lowPort;
+       }
+
+       public int getHighPort() {
+               return highPort;
+       }
+
+       public void setHighPort(int hightPort) {
+               this.highPort = hightPort;
+       }
+
+       /**
+        * @param rtpSocketImpl
+        * @return
+        * @throws ClosedChannelException
+        */
+ public SelectionKey registerSocket(RtpSocketImpl rtpSocketImpl) throws ClosedChannelException {
+
+               SelectionKey key=rtpSocketImpl.getSelectionKey();
+               if(key==null)
+               {
+ key=rtpSocketImpl.getChannel().register(this.readSelector, SelectionKey.OP_READ);
+               }
+               this.rtpSockets.put(key, rtpSocketImpl);
+               return key;
+       }
+
+



=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketImpl.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/RtpSocketImpl.java Sun Aug 9 21:50:54 2009
@@ -10,9 +10,11 @@
 import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
+import java.net.Socket;
 import java.net.SocketException;
 import java.nio.ByteBuffer;
 import java.nio.channels.DatagramChannel;
+import java.nio.channels.SelectionKey;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Vector;
@@ -36,8 +38,6 @@

private static final transient Logger log = Logger.getLogger(RtpSocketImpl.class);

-
-
        private int localPort;
        private String localAddress;

@@ -51,8 +51,9 @@
private final List<RtpSocketListener> dataSinks = new ArrayList<RtpSocketListener>();

        private RtpSocketFactoryImpl rtpSocketFactoryImpl;
-       private Timer timer;
-
+       private DatagramChannel channel;
+       private DatagramSocket socket;
+       private SelectionKey key;
        /**
         *
         * @param transceiver
@@ -60,17 +61,47 @@
         * @param formatMap
         * @param rtpSocketFactoryImpl2
         */
- public RtpSocketImpl(Transceiver transceiver, Timer timer, Vector<Attribute> formatMap, RtpSocketFactoryImpl rtpSocketFactory) { + public RtpSocketImpl(Vector<Attribute> formatMap, RtpSocketFactoryImpl rtpSocketFactory) {
                this.rtpSocketFactoryImpl = rtpSocketFactory;
-               this.timer = timer;
                this.rtpMap.addAll(formatMap);
-
                this.localAddress = this.rtpSocketFactoryImpl.getBindAddress();
-               this.localPort = this.rtpSocketFactoryImpl.getPort();
+
+
        }

-
-
+ public int init(InetAddress localAddress, int lowPort, int highPort) throws SocketException, IOException {
+               channel = DatagramChannel.open();
+               channel.configureBlocking(false);
+
+               socket = channel.socket();
+
+               boolean bound = false;
+
+               this.localAddress = localAddress.getHostAddress();
+               this.localPort = lowPort;
+
+               // looking for first unused port
+               while (!bound) {
+                       try {
+                               // creating local address and trying to bind 
socket to this
+                               // address
+ InetSocketAddress bindAddress = new InetSocketAddress(localAddress, localPort);
+                               socket.bind(bindAddress);
+                               bound = true;
+                               // if stunHost is assigned then stun ussage is 
supposed
+                               // discovering paublic address
+
+                       } catch (SocketException e) {
+                               // increment port number util upper limit is 
not reached
+                               localPort++;
+                               if (localPort > highPort) {
+                                       throw e;
+                               }
+                       }
+               }
+
+               return localPort;
+       }

        /*
         * (non-Javadoc)
@@ -101,16 +132,37 @@
                this.rtpMap.addAll(this.rtpSocketFactoryImpl.getFormatMap());
        }

-       /*
-        * (non-Javadoc)
+
+       public boolean isChannelOpen() {
+               return (this.channel != null && this.channel.isOpen());
+       }
+       public void release() {
+
+
+               this.resetRtpMap();
+               if (socket != null) {
+                       socket.disconnect();
+                       socket.close();
+               }
+
+               dataSinks.clear();
+               this.rtpSocketFactoryImpl.releaseSocket(this);
+       }
+       /**
+        * Closes socket
         *
-        * @see 
org.mobicents.media.server.testsuite.general.rtp.RtpSocket#close()
         */
        public void close() {
-               
this.rtpSocketFactoryImpl.rtpSockets.remove(remoteInetSocketAddress);
-
-       }
-
+               if (channel != null) {
+                       try {
+                               channel.disconnect();
+
+                       } catch (IOException e) {
+                       }
+               }
+
+
+       }
        /**
         * Assigns remote end.
         *
@@ -123,7 +175,8 @@
                remoteAddress = address.getHostAddress();
                remotePort = port;
remoteInetSocketAddress = new InetSocketAddress(remoteAddress, remotePort);
-               
this.rtpSocketFactoryImpl.rtpSockets.put(remoteInetSocketAddress, this);
+               channel.connect(remoteInetSocketAddress);
+               this.key=rtpSocketFactoryImpl.registerSocket(this);

                if (log.isDebugEnabled()) {
log.debug("Connect RTP socket[" + localAddress + ":" + localPort + " to " + remoteAddress + ":" + remotePort);
@@ -154,13 +207,29 @@
                        l.error(e);
        }

-       /* (non-Javadoc)
- * @see org.mobicents.media.server.testsuite.general.rtp.RtpSocket#receive(org.mobicents.media.server.testsuite.general.rtp.RtpPacket)
+       /*
+        * (non-Javadoc)
+        *
+        * @see
+        * 
org.mobicents.media.server.testsuite.general.rtp.RtpSocket#receive(org
+        * .mobicents.media.server.testsuite.general.rtp.RtpPacket)
         */
        public void receive(RtpPacket rtpPacket) {
                for (RtpSocketListener l : this.dataSinks)
                        l.receive(rtpPacket);
-
+
        }

-}
+       /* (non-Javadoc)
+ * @see org.mobicents.media.server.testsuite.general.rtp.RtpSocket#getSelectionKey()
+        */
+       public SelectionKey getSelectionKey() {
+
+               return this.key;
+       }
+       public DatagramChannel getChannel()
+       {
+               return this.channel;
+       }
+}
+
=======================================
--- /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/Transceiver.java Mon Aug 3 07:46:14 2009 +++ /trunk/servers/media/test-suite/core/src/main/java/org/mobicents/media/server/testsuite/general/rtp/Transceiver.java Sun Aug 9 21:50:54 2009
@@ -20,42 +20,28 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Set;

 /**
  * Start time:14:52:37 2009-08-03<br>
  * Project: mobicents-media-server-test-suite<br>
+ * This class represent similar usability as Transceiver in mms, however it does + * only reiceve and math poper socket, this is due that mms sends data on single
+ * port, thus we can not use the same scheme as mms.
  *
- * @author <a href="mailto:[email protected]";>Bartosz Baranowski
- *         </a>
+ * @author <a href="mailto:[email protected]";>Bartosz Baranowski </a>
  */
 public class Transceiver implements Runnable {
-       private DatagramSocket socket;
-       protected DatagramChannel channel;
-       private Selector readSelector;
-       private InetAddress bindAddress;
-       private int localPort;
+
        private int bufferSize = 8196;
        private ByteBuffer readerBuffer = ByteBuffer.allocate(bufferSize);
        private volatile boolean started = false;
-       private HashMap<SocketAddress, RtpSocket> rtpSockets;
-
- public Transceiver(HashMap<SocketAddress, RtpSocket> rtpSockets, InetAddress bindAddress, int localPort) throws SocketException, IOException {
+       private HashMap<SelectionKey, RtpSocket> rtpSockets;
+       private Selector readSelector;
+
+ public Transceiver(HashMap<SelectionKey, RtpSocket> rtpSockets, Selector readSelector) throws SocketException, IOException {
                this.rtpSockets = rtpSockets;
-               this.bindAddress = bindAddress;
-               this.localPort = localPort;
-
-               channel = DatagramChannel.open();
-               channel.configureBlocking(false);
-
-               socket = channel.socket();
-
-               // creating local address and trying to bind socket to this
-               // address
- InetSocketAddress address = new InetSocketAddress(bindAddress, localPort);
-               socket.bind(address);
-
-               readSelector = SelectorProvider.provider().openSelector();
-               channel.register(readSelector, SelectionKey.OP_READ);
+               this.readSelector = readSelector;
        }

        public void start() {
@@ -66,11 +52,7 @@
        public void stop() {
                started = false;
                try {
-                       channel.disconnect();
-                       channel.close();
-                       socket.disconnect();
-                       socket.close();
-                       readSelector.close();
+
                } catch (Exception e) {
                }
        }
@@ -83,36 +65,53 @@
        }

public void send(byte[] data, int len, SocketAddress remoteAddress) throws IOException {
-               ByteBuffer byteBuffer1 = ByteBuffer.wrap(data);
-               int count = 0;
-
-               while (count < len) {
-                       count += channel.send(byteBuffer1, remoteAddress);
-                       byteBuffer1.compact();
-                       byteBuffer1.flip();
-               }
+               // ByteBuffer byteBuffer1 = ByteBuffer.wrap(data);
+               // int count = 0;
+               //
+               // while (count < len) {
+               // count += channel.send(byteBuffer1, remoteAddress);
+               // byteBuffer1.compact();
+               // byteBuffer1.flip();
+               // }
        }

        public void run() {
                while (started) {
+
                        try {
                                readSelector.select();
-                               SocketAddress remoteAddress = 
channel.receive(readerBuffer);
-
-                               readerBuffer.flip();
-                               RtpSocket rtpSocket = 
rtpSockets.get(remoteAddress);
-
-                               if (rtpSocket != null) {
-                                       RtpPacket rtpPacket = new 
RtpPacket(readerBuffer);
-                                       rtpPacket.setTime(new 
Date(System.currentTimeMillis()));
-                                       rtpSocket.receive(rtpPacket);
-                               }
-
-                               readerBuffer.clear();
-                       } catch (IOException e) {
-                               if (started) {
-                                       notifyError(e);
-                               }
+
+                               long receiveStamp = System.currentTimeMillis();
+                               Set<SelectionKey> keys = 
readSelector.selectedKeys();
+
+                               for (SelectionKey sk : keys) {
+                                       try {
+                                               RtpSocket socket = 
this.rtpSockets.get(sk);
+                                               if (socket == null) {
+                                                       // this can happen, its 
legal
+                                                       continue;
+                                               }
+                                               DatagramChannel channel = 
socket.getChannel();
+                                               if (channel == null || 
!socket.isChannelOpen()) {
+                                                       continue;
+                                               }
+                                               channel.read(readerBuffer);
+                                               readerBuffer.flip();
+
+                                               RtpPacket rtpPacket = new 
RtpPacket(readerBuffer);
+                                               rtpPacket.setTime(new 
Date(receiveStamp));
+                                               socket.receive(rtpPacket);
+
+                                               readerBuffer.clear();
+                                       } catch (IOException e) {
+                                               if (started) {
+                                                       notifyError(e);
+                                               }
+                                       }
+                               }
+                       } catch (IOException e1) {
+                               // TODO Auto-generated catch block
+                               e1.printStackTrace();
                        }
                }
        }

Reply via email to