Revision: c3d6868de0ad
Author: Sergey Vetyutnev <[email protected]>
Date: Wed Aug 15 06:32:03 2012
Log: AnonimousConnections 15082012
http://code.google.com/p/sctp/source/detail?r=c3d6868de0ad
Modified:
/sctp-api/src/main/java/org/mobicents/protocols/api/Association.java
/sctp-api/src/main/java/org/mobicents/protocols/api/Management.java
/sctp-api/src/main/java/org/mobicents/protocols/api/Server.java
/sctp-api/src/main/java/org/mobicents/protocols/api/ServerListener.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
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/Association.java
Thu Apr 19 03:19:51 2012
+++ /sctp-api/src/main/java/org/mobicents/protocols/api/Association.java
Wed Aug 15 06:32:03 2012
@@ -141,4 +141,17 @@
*/
public void send(PayloadData payloadData) throws Exception;
+ public void acceptAnonymousAssociation(AssociationListener
associationListener) throws Exception;
+
+ public void rejectAnonymousAssociation();
+
+ /**
+ * Stop the anonymous association. The connection will be closed and we
will not reuse this association
+ * This can be applied only for anonymous association, other associations
must be stopped by
+ * Management.stopAssociation(String assocName)
+ *
+ * @throws Exception
+ */
+ public void stopAnonymousAssociation() throws Exception;
+
}
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java Tue
Aug 14 07:21:04 2012
+++ /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java Wed
Aug 15 06:32:03 2012
@@ -71,6 +71,20 @@
*/
public void setPersistDir(String persistDir);
+ /**
+ * 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);
+
/**
* Adding ManagementEventListener.
* This listener is notified when adding/removing servers and
associations
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/Server.java Tue Aug
14 07:21:04 2012
+++ /sctp-api/src/main/java/org/mobicents/protocols/api/Server.java Wed Aug
15 06:32:03 2012
@@ -59,20 +59,6 @@
*/
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
*
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/ServerListener.java
Tue Aug 14 07:21:04 2012
+++ /sctp-api/src/main/java/org/mobicents/protocols/api/ServerListener.java
Wed Aug 15 06:32:03 2012
@@ -29,6 +29,6 @@
*/
public interface ServerListener {
- public void onNewRemoteConnection(Server server, Association association,
String peerAddress, int peerPort);
+ public void onNewRemoteConnection(Server server, Association
association);
}
=======================================
---
/sctp-impl/src/main/java/org/mobicents/protocols/sctp/AssociationImpl.java
Tue Aug 14 07:21:04 2012
+++
/sctp-impl/src/main/java/org/mobicents/protocols/sctp/AssociationImpl.java
Wed Aug 15 06:32:03 2012
@@ -191,10 +191,8 @@
* @param assocName
* @param ipChannelType
*/
- protected AssociationImpl(String hostAddress, int hostPort, String
peerAddress, int peerPort, String serverName, IpChannelType ipChannelType) {
+ protected AssociationImpl(String peerAddress, int peerPort, String
serverName, IpChannelType ipChannelType) {
this();
- this.hostAddress = hostAddress;
- this.hostPort = hostPort;
this.peerAddress = peerAddress;
this.peerPort = peerPort;
this.serverName = serverName;
@@ -239,6 +237,28 @@
this.management.getSocketSelector().wakeup();
}
}
+
+ public void acceptAnonymousAssociation(AssociationListener
associationListener) throws Exception {
+ this.associationListener = associationListener;
+
+ if (this.getAssociationType() !=
AssociationType.ANONYMOUS_SERVER) {
+ throw new
UnsupportedOperationException("Association.acceptAnonymousAssociation() can
be applied only for anonymous associations");
+ }
+
+ this.start();
+ }
+
+ public void rejectAnonymousAssociation() {
+ }
+
+ public void stopAnonymousAssociation() throws Exception {
+
+ if (this.getAssociationType() !=
AssociationType.ANONYMOUS_SERVER) {
+ throw new
UnsupportedOperationException("Association.stopAnonymousAssociation() can
be applied only for anonymous associations");
+ }
+
+ this.stop();
+ }
public IpChannelType getIpChannelType() {
return this.ipChannelType;
=======================================
---
/sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java
Tue Aug 14 07:21:04 2012
+++
/sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java
Wed Aug 15 06:32:03 2012
@@ -48,6 +48,7 @@
import org.mobicents.protocols.api.Management;
import org.mobicents.protocols.api.ManagementEventListener;
import org.mobicents.protocols.api.Server;
+import org.mobicents.protocols.api.ServerListener;
/**
* @author amit bhayani
@@ -101,6 +102,8 @@
private ExecutorService[] executorServices = null;
private FastList<ManagementEventListener> managementEventListeners = new
FastList<ManagementEventListener>();
+
+ private ServerListener serverListener = null;
private volatile boolean started = false;
@@ -194,6 +197,14 @@
public void setSingleThread(boolean singleThread) {
this.singleThread = singleThread;
}
+
+ public ServerListener getServerListener() {
+ return serverListener;
+ }
+
+ public void setServerListener(ServerListener serverListener) {
+ this.serverListener = serverListener;
+ }
public void addManagementEventListener(ManagementEventListener
listener) {
synchronized (this) {
=======================================
---
/sctp-impl/src/main/java/org/mobicents/protocols/sctp/SelectorThread.java
Tue Aug 14 07:21:04 2012
+++
/sctp-impl/src/main/java/org/mobicents/protocols/sctp/SelectorThread.java
Wed Aug 15 06:32:03 2012
@@ -186,9 +186,8 @@
SctpChannel socketChannel = serverSocketChannel.accept();
Set<SocketAddress> peerAddresses =
socketChannel.getRemoteAddresses();
- Set<SocketAddress> localAddresses =
serverSocketChannel.getAllLocalAddresses();
- this.doAccept(socketChannel, peerAddresses, localAddresses);
+ this.doAccept(serverSocketChannel, socketChannel,
peerAddresses);
}
private void acceptTcp(SelectionKey key) throws IOException {
@@ -202,91 +201,134 @@
Set<SocketAddress> peerAddresses = new HashSet<SocketAddress>();
peerAddresses.add(socketChannel.getRemoteAddress());
- Set<SocketAddress> localAddresses = new
HashSet<SocketAddress>();
- localAddresses.add(serverSocketChannel.getLocalAddress());
- this.doAccept(socketChannel, peerAddresses, localAddresses);
+ this.doAccept(serverSocketChannel, socketChannel,
peerAddresses);
}
- private void doAccept(AbstractSelectableChannel socketChannel,
Set<SocketAddress> peerAddresses, Set<SocketAddress> localAddresses) throws
IOException, ClosedChannelException {
+ private void doAccept(AbstractSelectableChannel serverSocketChannel,
AbstractSelectableChannel socketChannel, Set<SocketAddress> peerAddresses)
+ throws IOException, ClosedChannelException {
boolean provisioned = false;
int port = 0;
InetAddress inetAddress = null;
+ int firstPort = 0;
+ InetAddress firstInetAddress = null;
// server selection
- for(Server srv : this.management.servers) {
- for (SocketAddress localAdd : localAddresses) {
- inetAddress = ((InetSocketAddress)
localAdd).getAddress();
- port = ((InetSocketAddress) localAdd).getPort();
+ for (Server srv : this.management.servers) {
+ ServerImpl srvv = (ServerImpl) srv;
+ if (srvv.getIpChannel() == serverSocketChannel) { // we have found a
server
+ for (SocketAddress sockAdd : peerAddresses) {
- 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();
+ if (firstInetAddress == null) {
+ firstInetAddress = inetAddress;
+ firstPort = port;
+ }
- inetAddress = ((InetSocketAddress)
sockAdd).getAddress();
- port = ((InetSocketAddress) sockAdd).getPort();
+ // Iterate through all corresponding
associate to
+ // check if incoming connection request
matches with any provisioned
+ // ip:port
+ FastMap<String, Association> associations =
this.management.associations;
- // Iterate through all the servers and corresponding
associate to
- // check if incoming connection request matches with
any provisioned
- // ip:port
- FastMap<String, Association> associations =
this.management.associations;
+ for (FastMap.Entry<String, Association> n = associations.head(), end
= associations.tail(); (n = n.getNext()) != end && !provisioned;) {
+ AssociationImpl association =
(AssociationImpl)n.getValue();
+
+ // check if an association
binds to the found server
+ if
(srv.getName().equals(association.getServerName())) {
- 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;
- // ..................................
+ if
(!association.isStarted()) {
+ logger.error(String.format("Received connect request for
Association=%s but not started yet. Droping the connection! ",
+
association.getName()));
+
socketChannel.close();
+ break;
+ }
- // compare port and ip of remote with
provisioned
- if ((port == association.getPeerPort()) &&
(inetAddress.getHostAddress().equals(association.getPeerAddress()))) {
- provisioned = true;
+
((AssociationImpl) association).setSocketChannel(socketChannel);
- if (!association.isStarted()) {
- logger.error(String.format("Received connect request for
Association=%s but not started yet. Droping the connection! ",
-
association.getName()));
- socketChannel.close();
- break;
+ // Accept the
connection and make it non-blocking
+
socketChannel.configureBlocking(false);
+
+ // Register the
new SocketChannel with our Selector,
+ // indicating
we'd like to be notified when there's data
+ // waiting to
be read
+ SelectionKey key1 = socketChannel.register(this.selector,
SelectionKey.OP_READ);
+
key1.attach(association);
+
+ if
(logger.isInfoEnabled()) {
+
logger.info(String.format("Connected %s", association));
+ }
+
+ if
(association.getIpChannelType() == IpChannelType.TCP) {
+
AssocChangeEvent ace = AssocChangeEvent.COMM_UP;
+ AssociationChangeNotification2 acn = new
AssociationChangeNotification2(ace);
+ association.associationHandler.handleNotification(acn,
association);
+ }
+
+ break;
+ }
+ }
}
- ((AssociationImpl)
association).setSocketChannel(socketChannel);
+ if (provisioned)
+ break;
+ }// for (SocketAddress sockAdd : socAddresses)
- // Accept the connection and make it
non-blocking
+ if (!provisioned && srv.isAcceptAnonymousConnections() &&
this.management.getServerListener() != null) {
+ // the server accepts anonymous
connections
+
+ provisioned = true;
+
+ AssociationImpl anonymAssociation = new
AssociationImpl(firstInetAddress.getHostAddress(), firstPort,
srv.getName(), srv.getIpChannelType());
+
anonymAssociation.setSocketChannel(socketChannel);
+
+ // Accept the connection and make it
+ // non-blocking
socketChannel.configureBlocking(false);
+ try {
+ this.management.getServerListener().onNewRemoteConnection(srv,
anonymAssociation);
+ } catch (Throwable e) {
+ logger.warn(String.format("Exception when invoking
ServerListener.onNewRemoteConnection() Ass=%s", anonymAssociation), e);
+ try {
+ socketChannel.close();
+ } catch (Exception ee) {
+ }
+ return;
+ }
+
+ if (!anonymAssociation.isStarted()) {
+ logger.info(String.format("Rejected anonymous %s",
anonymAssociation));
+ try {
+ socketChannel.close();
+ } catch (Exception ee) {
+ }
+ return;
+ }
+
// Register the new SocketChannel with
our Selector,
// indicating we'd like to be notified
when there's data
// waiting to be read
SelectionKey key1 = socketChannel.register(this.selector,
SelectionKey.OP_READ);
- key1.attach(association);
+ key1.attach(anonymAssociation);
if (logger.isInfoEnabled()) {
- logger.info(String.format("Connected
%s", association));
+ logger.info(String.format("Connected anonymous %s",
anonymAssociation));
}
- if (association.getIpChannelType() ==
IpChannelType.TCP) {
+ if
(anonymAssociation.getIpChannelType() == IpChannelType.TCP) {
AssocChangeEvent ace =
AssocChangeEvent.COMM_UP;
AssociationChangeNotification2 acn = new
AssociationChangeNotification2(ace);
-
association.associationHandler.handleNotification(acn, association);
+ anonymAssociation.associationHandler.handleNotification(acn,
anonymAssociation);
}
-
- break;
}
}
- }// for (SocketAddress sockAdd : socAddresses)
+ }
if (!provisioned) {
// There is no corresponding Associate provisioned.
Lets close the
@@ -303,6 +345,9 @@
this.finishConnectionSctp(key);
else
this.finishConnectionTcp(key);
+
+ // TODO: may be add anonymAssociation to the
anonymAssociationList;
+ // ...........................
}
private void finishConnectionSctp(SelectionKey key) throws IOException {
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java
Tue Aug 14 07:21:04 2012
+++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java
Wed Aug 15 06:32:03 2012
@@ -82,8 +82,6 @@
private SctpServerChannel serverChannelSctp;
private ServerSocketChannel serverChannelTcp;
- private ServerListener serverListener = null;
-
/**
*
*/
@@ -224,14 +222,6 @@
else
return this.serverChannelTcp;
}
-
- public ServerListener getServerListener() {
- return serverListener;
- }
-
- public void setServerListener(ServerListener serverListener) {
- this.serverListener = serverListener;
- }
/**
* @return the name