Revision: 6533a433be0f
Author:   Sergey Vetyutnev <[email protected]>
Date:     Tue Aug 14 07:21:04 2012
Log:      anonymous connection support 14082012
http://code.google.com/p/sctp/source/detail?r=6533a433be0f

Added:
 /sctp-api/src/main/java/org/mobicents/protocols/api/ServerListener.java
Modified:
 /sctp-api/src/main/java/org/mobicents/protocols/api/AssociationType.java
 /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java
 /sctp-api/src/main/java/org/mobicents/protocols/api/Server.java
 /sctp-impl/src/main/java/org/mobicents/protocols/sctp/AssociationImpl.java
 /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java
 /sctp-impl/src/main/java/org/mobicents/protocols/sctp/SelectorThread.java
 /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java

=======================================
--- /dev/null
+++ /sctp-api/src/main/java/org/mobicents/protocols/api/ServerListener.java Tue Aug 14 07:21:04 2012
@@ -0,0 +1,34 @@
+/*
+ * TeleStax, Open Source Cloud Communications  Copyright 2012.
+ * and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.mobicents.protocols.api;
+
+/**
+ *
+ * @author sergey vetyutnev
+ *
+ */
+public interface ServerListener {
+
+ public void onNewRemoteConnection(Server server, Association association, String peerAddress, int peerPort);
+
+}
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/AssociationType.java Wed Feb 22 09:02:17 2012 +++ /sctp-api/src/main/java/org/mobicents/protocols/api/AssociationType.java Tue Aug 14 07:21:04 2012
@@ -26,7 +26,7 @@
  *
  */
 public enum AssociationType {
-       CLIENT("CLIENT"), SERVER("SERVER");
+       CLIENT("CLIENT"), SERVER("SERVER"), 
ANONYMOUS_SERVER("ANONYMOUS_SERVER");

        private final String type;

@@ -48,6 +48,8 @@
                        return CLIENT;
                } else if (type.equalsIgnoreCase(SERVER.getType())) {
                        return SERVER;
+               } else if (type.equalsIgnoreCase(ANONYMOUS_SERVER.getType())) {
+                       return ANONYMOUS_SERVER;
                } else {
                        return null;
                }
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java Thu Jul 5 03:32:52 2012 +++ /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java Tue Aug 14 07:21:04 2012
@@ -123,6 +123,10 @@
         *            port that this server will bind to
         * @param ipChannelType
         *            IP channel type: SCTP or TCP
+        * @param acceptAnonymousConnections
+        *            true: this Server accepts Anonymous connections, false: no
+        * @param maxConcurrentConnectionsCount
+ * A count of concurrent connections that can accept a Server. 0 means an unlimited count.
         * @param extraHostAddresses
* When SCTP multi-homing configuration extra IP addresses can be put here
         *            If multi-homing absence this parameter can be null
@@ -131,7 +135,8 @@
         *             Exception if management not started or server name 
already
         *             taken or some other server already has same ip:port
         */
- public Server addServer(String serverName, String hostAddress, int port, IpChannelType ipChannelType, String[] extraHostAddresses) throws Exception; + public Server addServer(String serverName, String hostAddress, int port, IpChannelType ipChannelType, boolean acceptAnonymousConnections, + int maxConcurrentConnectionsCount, String[] extraHostAddresses) throws Exception;

        /**
         * Add new {@link Server}. IP channel type is SCTP.
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/Server.java Mon Mar 5 00:44:41 2012 +++ /sctp-api/src/main/java/org/mobicents/protocols/api/Server.java Tue Aug 14 07:21:04 2012
@@ -37,7 +37,42 @@
         * @return
         */
        public IpChannelType getIpChannelType();
-
+
+       /**
+        * Return if this Server accepts Anonymous connections
+        *
+        * @return
+        */
+       public boolean isAcceptAnonymousConnections();
+
+       /**
+ * Return the count of concurrent connections that can accept a Server. 0 means an unlimited count.
+        *
+        * @return
+        */
+       public int getMaxConcurrentConnectionsCount();
+
+       /**
+ * Set the count of concurrent connections that can accept a Server. 0 means an unlimited count.
+        *
+        * @return
+        */
+       public void setMaxConcurrentConnectionsCount(int val);
+
+       /**
+       * The AssociationListener set for this Association
+       *
+       * @return
+       */
+       public ServerListener getServerListener();
+
+       /**
+        * The {@link AssociationListener} to be registered for this Association
+        *
+        * @param associationListener
+        */
+       public void setServerListener(ServerListener serverListener);
+
        /**
         * Get name of this Server. Should be unique in a management instance
         *
@@ -75,6 +110,7 @@

        /**
         * {@link Association} configured for this Server
+        * Anonymous associations are not present in this list
         *
         * @return
         */
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/AssociationImpl.java Mon Apr 23 13:19:02 2012 +++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/AssociationImpl.java Tue Aug 14 07:21:04 2012
@@ -133,20 +133,22 @@
        }

        /**
-        *
+        * Creating a CLIENT Association
         *
         * @param hostAddress
-        * @param hostport
+        * @param hostPort
         * @param peerAddress
         * @param peerPort
         * @param assocName
+        * @param ipChannelType
+        * @param extraHostAddresses
         * @throws IOException
         */
- public AssociationImpl(String hostAddress, int hostport, String peerAddress, int peerPort, String assocName, IpChannelType ipChannelType, + public AssociationImpl(String hostAddress, int hostPort, String peerAddress, int peerPort, String assocName, IpChannelType ipChannelType,
                        String[] extraHostAddresses) throws IOException {
                this();
                this.hostAddress = hostAddress;
-               this.hostPort = hostport;
+               this.hostPort = hostPort;
                this.peerAddress = peerAddress;
                this.peerPort = peerPort;
                this.name = assocName;
@@ -158,10 +160,13 @@
        }

        /**
+        * Creating a SERVER Association
+        *
         * @param peerAddress
         * @param peerPort
         * @param serverName
         * @param assocName
+        * @param ipChannelType
         */
public AssociationImpl(String peerAddress, int peerPort, String serverName, String assocName, IpChannelType ipChannelType) {
                this();
@@ -174,6 +179,30 @@
                this.type = AssociationType.SERVER;

        }
+
+       /**
+        * Creating an ANONYMOUS_SERVER Association
+        *
+        * @param hostAddress
+        * @param hostPort
+        * @param peerAddress
+        * @param peerPort
+        * @param serverName
+        * @param assocName
+        * @param ipChannelType
+        */
+ protected AssociationImpl(String hostAddress, int hostPort, String peerAddress, int peerPort, String serverName, IpChannelType ipChannelType) {
+               this();
+               this.hostAddress = hostAddress;
+               this.hostPort = hostPort;
+               this.peerAddress = peerAddress;
+               this.peerPort = peerPort;
+               this.serverName = serverName;
+               this.ipChannelType = ipChannelType;
+
+               this.type = AssociationType.ANONYMOUS_SERVER;
+
+       }

        protected void start() throws Exception {
                if (this.associationListener == null) {
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java Tue Jul 10 02:30:45 2012 +++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java Tue Aug 14 07:21:04 2012
@@ -74,7 +74,7 @@

        protected String persistDir = null;

-       private FastList<Server> servers = new FastList<Server>();
+       protected FastList<Server> servers = new FastList<Server>();
protected AssociationMap<String, Association> associations = new AssociationMap<String, Association>();

private FastList<ChangeRequest> pendingChanges = new FastList<ChangeRequest>();
@@ -426,11 +426,11 @@
        }

public ServerImpl addServer(String serverName, String hostAddress, int port) throws Exception { - return addServer(serverName, hostAddress, port, IpChannelType.SCTP, null); + return addServer(serverName, hostAddress, port, IpChannelType.SCTP, false, 0, null);
        }

- public ServerImpl addServer(String serverName, String hostAddress, int port, IpChannelType ipChannelType,
-                       String[] extraHostAddresses) throws Exception {
+ public ServerImpl addServer(String serverName, String hostAddress, int port, IpChannelType ipChannelType, boolean acceptAnonymousConnections, + int maxConcurrentConnectionsCount, String[] extraHostAddresses) throws Exception {

                if (!this.started) {
throw new Exception(String.format("Management=%s not started", this.name));
@@ -461,7 +461,8 @@
                                }
                        }

- ServerImpl server = new ServerImpl(serverName, hostAddress, port, ipChannelType, extraHostAddresses); + ServerImpl server = new ServerImpl(serverName, hostAddress, port, ipChannelType, acceptAnonymousConnections, maxConcurrentConnectionsCount,
+                                       extraHostAddresses);
                        server.setManagement(this);

                        FastList<Server> newServers = new FastList<Server>();
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/SelectorThread.java Wed Feb 22 09:02:17 2012 +++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/SelectorThread.java Tue Aug 14 07:21:04 2012
@@ -42,6 +42,7 @@
 import org.apache.log4j.Logger;
 import org.mobicents.protocols.api.Association;
 import org.mobicents.protocols.api.IpChannelType;
+import org.mobicents.protocols.api.Server;

 import com.sun.nio.sctp.AssociationChangeNotification;
 import com.sun.nio.sctp.AssociationChangeNotification.AssocChangeEvent;
@@ -184,9 +185,10 @@
                // Accept the connection and make it non-blocking
                SctpChannel socketChannel = serverSocketChannel.accept();

-               Set<SocketAddress> socAddresses = 
socketChannel.getRemoteAddresses();
+               Set<SocketAddress> peerAddresses = 
socketChannel.getRemoteAddresses();
+ Set<SocketAddress> localAddresses = serverSocketChannel.getAllLocalAddresses();

-               this.doAccept(socketChannel, socAddresses);
+               this.doAccept(socketChannel, peerAddresses, localAddresses);
        }

        private void acceptTcp(SelectionKey key) throws IOException {
@@ -198,20 +200,43 @@
                // Accept the connection and make it non-blocking
                SocketChannel socketChannel = serverSocketChannel.accept();

-               SocketAddress sockAdd = socketChannel.getRemoteAddress();
-               Set<SocketAddress> socAddresses = new HashSet<SocketAddress>();
-               socAddresses.add(sockAdd);
+               Set<SocketAddress> peerAddresses = new HashSet<SocketAddress>();
+               peerAddresses.add(socketChannel.getRemoteAddress());
+               Set<SocketAddress> localAddresses = new 
HashSet<SocketAddress>();
+               localAddresses.add(serverSocketChannel.getLocalAddress());

-               this.doAccept(socketChannel, socAddresses);
+               this.doAccept(socketChannel, peerAddresses, localAddresses);
        }

- private void doAccept(AbstractSelectableChannel socketChannel, Set<SocketAddress> socAddresses) throws IOException, ClosedChannelException { + private void doAccept(AbstractSelectableChannel socketChannel, Set<SocketAddress> peerAddresses, Set<SocketAddress> localAddresses) throws IOException, ClosedChannelException {

                boolean provisioned = false;
                int port = 0;
                InetAddress inetAddress = null;

-               for (SocketAddress sockAdd : socAddresses) {
+               // server selection
+               for(Server srv : this.management.servers) {
+                       for (SocketAddress localAdd : localAddresses) {
+                               inetAddress = ((InetSocketAddress) 
localAdd).getAddress();
+                               port = ((InetSocketAddress) localAdd).getPort();
+
+                               boolean srvFound = false;
+ if ((port == srv.getHostport()) && (inetAddress.getHostAddress().equals(srv.getHostAddress()))) {
+                                       srvFound = true;
+                               } else {
+                                       // ........................
+                               }
+
+                               // .......................
+                       }
+               }
+
+
+
+
+               // ............................
+
+               for (SocketAddress sockAdd : peerAddresses) {

                        inetAddress = ((InetSocketAddress) 
sockAdd).getAddress();
                        port = ((InetSocketAddress) sockAdd).getPort();
@@ -223,6 +248,9 @@

for (FastMap.Entry<String, Association> n = associations.head(), end = associations.tail(); (n = n.getNext()) != end && !provisioned;) {
                                AssociationImpl association = 
(AssociationImpl)n.getValue();
+
+                               // ..................................
+
                                // compare port and ip of remote with 
provisioned
if ((port == association.getPeerPort()) && (inetAddress.getHostAddress().equals(association.getPeerAddress()))) {
                                        provisioned = true;
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java Thu Jul 5 03:32:52 2012 +++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java Tue Aug 14 07:21:04 2012
@@ -38,6 +38,7 @@
 import org.mobicents.protocols.api.Association;
 import org.mobicents.protocols.api.IpChannelType;
 import org.mobicents.protocols.api.Server;
+import org.mobicents.protocols.api.ServerListener;

 import com.sun.nio.sctp.SctpServerChannel;

@@ -68,16 +69,21 @@
        private int hostport;
        private volatile boolean started = false;
        private IpChannelType ipChannelType;
+       private boolean acceptAnonymousConnections;
+       private int maxConcurrentConnectionsCount;
        private String[] extraHostAddresses;

        private ManagementImpl management = null;

        protected FastList<String> associations = new FastList<String>();
+//     protected FastList<String> anonymAssociations = new FastList<String>();

        // The channel on which we'll accept connections
        private SctpServerChannel serverChannelSctp;
        private ServerSocketChannel serverChannelTcp;

+       private ServerListener serverListener = null;
+
        /**
         *
         */
@@ -91,13 +97,15 @@
         * @param port
         * @throws IOException
         */
- public ServerImpl(String name, String hostAddress, int hostport, IpChannelType ipChannelType,
-                       String[] extraHostAddresses) throws IOException {
+ public ServerImpl(String name, String hostAddress, int hostport, IpChannelType ipChannelType, boolean acceptAnonymousConnections, + int maxConcurrentConnectionsCount, String[] extraHostAddresses) throws IOException {
                super();
                this.name = name;
                this.hostAddress = hostAddress;
                this.hostport = hostport;
                this.ipChannelType = ipChannelType;
+               this.acceptAnonymousConnections = acceptAnonymousConnections;
+               this.maxConcurrentConnectionsCount = 
maxConcurrentConnectionsCount;
                this.extraHostAddresses = extraHostAddresses;
        }

@@ -121,6 +129,8 @@
                        }
                }

+               // TODO: stop all anonymous associations?
+
                if (this.getIpChannel() != null) {
                        try {
                                this.getIpChannel().close();
@@ -195,6 +205,18 @@
        public IpChannelType getIpChannelType() {
                return this.ipChannelType;
        }
+
+       public boolean isAcceptAnonymousConnections() {
+               return acceptAnonymousConnections;
+       }
+
+       public int getMaxConcurrentConnectionsCount() {
+               return maxConcurrentConnectionsCount;
+       }
+
+       public void setMaxConcurrentConnectionsCount(int val) {
+               maxConcurrentConnectionsCount = val;
+       }

        protected AbstractSelectableChannel getIpChannel() {
                if (this.ipChannelType == IpChannelType.SCTP)
@@ -202,6 +224,14 @@
                else
                        return this.serverChannelTcp;
        }
+
+       public ServerListener getServerListener() {
+               return serverListener;
+       }
+
+       public void setServerListener(ServerListener serverListener) {
+               this.serverListener = serverListener;
+       }

        /**
         * @return the name
@@ -258,7 +288,7 @@

sb.append("Server [name=").append(this.name).append(", started=").append(this.started).append(", hostAddress=") .append(this.hostAddress).append(", hostPort=").append(hostport).append(", ipChannelType=")
-                               .append(ipChannelType).append(", 
associations=[");
+ .append(ipChannelType).append(", associations(anonymous does not included)=[");

for (FastList.Node<String> n = this.associations.head(), end = this.associations.tail(); (n = n.getNext()) != end;) {
                        sb.append(n.getValue());

Reply via email to