Revision: 0dd73cd5e551
Author:   Sergey Vetyutnev <[email protected]>
Date:     Thu Jul  5 03:32:52 2012
Log: Adding ManagementEventListener and multi-threading support for SCTP
management collections
http://code.google.com/p/sctp/source/detail?r=0dd73cd5e551

Added:
/sctp-api/src/main/java/org/mobicents/protocols/api/ManagementEventListener.java
Modified:
 /docs/sources/src/main/resources/en-US/Chapter-Management.xml
 /docs/sources/src/main/resources/en-US/Revision_History.xml
 /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java
 /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java
 /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java
 /sctp-impl/src/test/java/org/mobicents/protocols/sctp/ManagementTest.java

=======================================
--- /dev/null
+++ /sctp-api/src/main/java/org/mobicents/protocols/api/ManagementEventListener.java Thu Jul 5 03:32:52 2012
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2011, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors as indicated by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a full listing
+ * of individual contributors.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License, v. 2.0.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License,
+ * v. 2.0 along with this distribution; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+package org.mobicents.protocols.api;
+
+/**
+ *
+ * @author sergey vetyutnev
+ *
+ */
+public interface ManagementEventListener {
+
+       public void onServerAdded(Server server);
+
+       public void onServerRemoved(Server serverName);
+
+       public void onAssociationAdded(Association association);
+
+       public void onAssociationRemoved(Association association);
+
+       public void onRemoveAllResourses();
+
+}
=======================================
--- /docs/sources/src/main/resources/en-US/Chapter-Management.xml Wed Mar 21 03:27:04 2012 +++ /docs/sources/src/main/resources/en-US/Chapter-Management.xml Thu Jul 5 03:32:52 2012
@@ -76,6 +76,20 @@
         */
        public void setPersistDir(String persistDir);

+       /**
+        * Adding ManagementEventListener.
+        * This listener is notified when adding/removing servers and 
associations
+        * @param listener
+        */
+       public void addManagementEventListener(ManagementEventListener 
listener);
+
+       /**
+        * Removing ManagementEventListener.
+        * This listener is notified when adding/removing servers and 
associations
+        * @param listener
+        */
+ public void removeManagementEventListener(ManagementEventListener listener);
+
        /**
         * Start the management. No management operation can be executed unless
* {@link Management} is started. If {@link Server} and {@link Association}
@@ -351,6 +365,28 @@
                <section>
                        <title>API's to manage resources</title>
                        <variablelist>
+                               <varlistentry>
+                                       <term>
+ <literal>public void addManagementEventListener(ManagementEventListener listener)</literal>
+                                       </term>
+                                       <listitem>
+                                               <para>
+ Adding a listener for management events (adding/removing servers and associations).
+                                               </para>
+                                       </listitem>
+                               </varlistentry>
+
+                               <varlistentry>
+                                       <term>
+ <literal>public void removeManagementEventListener(ManagementEventListener listener)</literal>
+                                       </term>
+                                       <listitem>
+                                               <para>
+ Removing a listener for management events (adding/removing servers and associations).
+                                               </para>
+                                       </listitem>
+                               </varlistentry>
+
                                <varlistentry>
                                        <term>
<literal>public Association addAssociation(String hostAddress, int hostPort, String peerAddress, int peerPort, String assocName, IpChannelType ipChannelType,
@@ -539,7 +575,7 @@
                                        </term>
                                        <listitem>
                                                <para>
-                                                       Returns the 
unmodifiable lis of servers.
+                                                       Returns the 
unmodifiable list of servers.
                                                </para>
                                        </listitem>
                                </varlistentry>
=======================================
--- /docs/sources/src/main/resources/en-US/Revision_History.xml Sun Nov 27 03:05:49 2011 +++ /docs/sources/src/main/resources/en-US/Revision_History.xml Thu Jul 5 03:32:52 2012
@@ -21,6 +21,21 @@
                                        </simplelist>
                                </revdescription>
                        </revision>
+
+                       <revision>
+                               <revnumber>1.1</revnumber>
+                               <date>Thu Jul 05 2012</date>
+                               <author>
+                                       <firstname>Serg</firstname>
+                                       <surname>Vetyutnev</surname>
+                               </author>
+                               <revdescription>
+                                       <simplelist>
+                                               <member>Adding 
ManagementEventListener.</member>
+                                       </simplelist>
+                               </revdescription>
+                       </revision>
+
                </revhistory>
        </simpara>
 </appendix>
=======================================
--- /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java Wed Mar 21 03:27:04 2012 +++ /sctp-api/src/main/java/org/mobicents/protocols/api/Management.java Thu Jul 5 03:32:52 2012
@@ -19,6 +19,7 @@
  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA 02110-1301, USA.
  */
+
 package org.mobicents.protocols.api;

 import java.util.List;
@@ -70,6 +71,20 @@
         */
        public void setPersistDir(String persistDir);

+       /**
+        * Adding ManagementEventListener.
+        * This listener is notified when adding/removing servers and 
associations
+        * @param listener
+        */
+       public void addManagementEventListener(ManagementEventListener 
listener);
+
+       /**
+        * Removing ManagementEventListener.
+        * This listener is notified when adding/removing servers and 
associations
+        * @param listener
+        */
+ public void removeManagementEventListener(ManagementEventListener listener);
+
        /**
         * Start the management. No management operation can be executed unless
* {@link Management} is started. If {@link Server} and {@link Association}
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java Sat May 12 00:33:30 2012 +++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ManagementImpl.java Thu Jul 5 03:32:52 2012
@@ -35,19 +35,18 @@
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
-
 import javolution.text.TextBuilder;
 import javolution.util.FastList;
 import javolution.util.FastMap;
 import javolution.xml.XMLObjectReader;
 import javolution.xml.XMLObjectWriter;
 import javolution.xml.stream.XMLStreamException;
-
 import org.apache.log4j.Logger;
 import org.mobicents.protocols.api.Association;
 import org.mobicents.protocols.api.AssociationType;
 import org.mobicents.protocols.api.IpChannelType;
 import org.mobicents.protocols.api.Management;
+import org.mobicents.protocols.api.ManagementEventListener;
 import org.mobicents.protocols.api.Server;

 /**
@@ -101,6 +100,8 @@

        private ExecutorService[] executorServices = null;

+ private FastList<ManagementEventListener> managementEventListeners = new FastList<ManagementEventListener>();
+
        private volatile boolean started = false;

        public ManagementImpl(String name) throws IOException {
@@ -194,42 +195,68 @@
                this.singleThread = singleThread;
        }

-       public void start() throws Exception {
-               this.persistFile.clear();
-
-               if (persistDir != null) {
- this.persistFile.append(persistDir).append(File.separator).append(this.name).append("_")
-                                       .append(PERSIST_FILE_NAME);
-               } else {
- persistFile.append(System.getProperty(SCTP_PERSIST_DIR_KEY, System.getProperty(USER_DIR_KEY))) - .append(File.separator).append(this.name).append("_").append(PERSIST_FILE_NAME);
-               }
-
- logger.info(String.format("SCTP configuration file path %s", persistFile.toString()));
-
-               try {
-                       this.load();
-               } catch (FileNotFoundException e) {
- logger.warn(String.format("Failed to load the SCTP configuration file. \n%s", e.getMessage()));
-               }
-
-               if (!this.singleThread) {
-                       // If not single thread model we create worker threads
-                       this.executorServices = new 
ExecutorService[this.workerThreads];
-                       for (int i = 0; i < this.workerThreads; i++) {
-                               this.executorServices[i] = 
Executors.newSingleThreadExecutor();
-                       }
-               }
-               this.selectorThread = new SelectorThread(this.socketSelector, 
this);
-               this.selectorThread.setStarted(true);
-
-               (new Thread(this.selectorThread)).start();
-
-               this.started = true;
-
-               if (logger.isInfoEnabled()) {
- logger.info(String.format("Started SCTP Management=%s WorkerThreads=%d SingleThread=%s", this.name,
-                                       (this.singleThread ? 0 : 
this.workerThreads), this.singleThread));
+       public void addManagementEventListener(ManagementEventListener 
listener) {
+               synchronized (this) {
+                       if (this.managementEventListeners.contains(listener))
+                               return;
+
+ FastList<ManagementEventListener> newManagementEventListeners = new FastList<ManagementEventListener>();
+                       
newManagementEventListeners.addAll(this.managementEventListeners);
+                       newManagementEventListeners.add(listener);
+                       this.managementEventListeners = 
newManagementEventListeners;
+               }
+       }
+
+ public void removeManagementEventListener(ManagementEventListener listener) {
+               synchronized (this) {
+                       if (!this.managementEventListeners.contains(listener))
+                               return;
+
+ FastList<ManagementEventListener> newManagementEventListeners = new FastList<ManagementEventListener>();
+                       
newManagementEventListeners.addAll(this.managementEventListeners);
+                       newManagementEventListeners.remove(listener);
+                       this.managementEventListeners = 
newManagementEventListeners;
+               }
+       }
+
+       public void start() throws Exception {
+
+               synchronized (this) {
+                       this.persistFile.clear();
+
+                       if (persistDir != null) {
+ this.persistFile.append(persistDir).append(File.separator).append(this.name).append("_").append(PERSIST_FILE_NAME);
+                       } else {
+ persistFile.append(System.getProperty(SCTP_PERSIST_DIR_KEY, System.getProperty(USER_DIR_KEY))).append(File.separator).append(this.name)
+                                               
.append("_").append(PERSIST_FILE_NAME);
+                       }
+
+ logger.info(String.format("SCTP configuration file path %s", persistFile.toString()));
+
+                       try {
+                               this.load();
+                       } catch (FileNotFoundException e) {
+ logger.warn(String.format("Failed to load the SCTP configuration file. \n%s", e.getMessage()));
+                       }
+
+                       if (!this.singleThread) {
+                               // If not single thread model we create worker 
threads
+                               this.executorServices = new 
ExecutorService[this.workerThreads];
+                               for (int i = 0; i < this.workerThreads; i++) {
+                                       this.executorServices[i] = 
Executors.newSingleThreadExecutor();
+                               }
+                       }
+                       this.selectorThread = new 
SelectorThread(this.socketSelector, this);
+                       this.selectorThread.setStarted(true);
+
+                       (new Thread(this.selectorThread)).start();
+
+                       this.started = true;
+
+                       if (logger.isInfoEnabled()) {
+ logger.info(String.format("Started SCTP Management=%s WorkerThreads=%d SingleThread=%s", this.name,
+                                               (this.singleThread ? 0 : 
this.workerThreads), this.singleThread));
+                       }
                }
        }

@@ -239,7 +266,8 @@
                this.store();

                // Stop all associations
- for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n
+               FastMap<String, Association> associationsTemp = 
this.associations;
+ for (FastMap.Entry<String, Association> n = associationsTemp.head(), end = associationsTemp.tail(); (n = n
                                .getNext()) != end;) {
                        Association associationTemp = n.getValue();
                        if (associationTemp.isStarted()) {
@@ -247,7 +275,8 @@
                        }
                }

- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+               FastList<Server> tempServers = servers;
+ for (FastList.Node<Server> n = tempServers.head(), end = tempServers.tail(); (n = n.getNext()) != end;) {
                        Server serverTemp = n.getValue();
                        if (serverTemp.isStarted()) {
                                try {
@@ -352,42 +381,48 @@

        public void removeAllResourses() throws Exception {

-               if (!this.started) {
- throw new Exception(String.format("Management=%s not started", this.name));
-               }
-
-               if (this.associations.size() == 0 && this.servers.size() == 0)
-                       // no resources allocated - nothing to do
-                       return;
-
-               if (logger.isInfoEnabled()) {
- logger.info(String.format("Removing allocated resources: Servers=%d, Associations=%d", this.servers.size(),
-                                       this.associations.size()));
-               }
-
-               // Remove all associations
-               ArrayList<String> lst = new ArrayList<String>();
- for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n
-                               .getNext()) != end;) {
-                       lst.add(n.getKey());
-               }
-               for (String n : lst) {
-                       this.stopAssociation(n);
-                       this.removeAssociation(n);
-               }
-
-               // Remove all servers
-               lst.clear();
- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
-                       lst.add(n.getValue().getName());
-               }
-               for (String n : lst) {
-                       this.stopServer(n);
-                       this.removeServer(n);
-               }
-
-               // We store the cleared state
-               this.store();
+               synchronized (this) {
+                       if (!this.started) {
+ throw new Exception(String.format("Management=%s not started", this.name));
+                       }
+
+                       if (this.associations.size() == 0 && 
this.servers.size() == 0)
+                               // no resources allocated - nothing to do
+                               return;
+
+                       if (logger.isInfoEnabled()) {
+ logger.info(String.format("Removing allocated resources: Servers=%d, Associations=%d", this.servers.size(), this.associations.size()));
+                       }
+
+                       synchronized (this) {
+                               // Remove all associations
+                               ArrayList<String> lst = new ArrayList<String>();
+ for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n.getNext()) != end;) {
+                                       lst.add(n.getKey());
+                               }
+                               for (String n : lst) {
+                                       this.stopAssociation(n);
+                                       this.removeAssociation(n);
+                               }
+
+                               // Remove all servers
+                               lst.clear();
+ for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+                                       lst.add(n.getValue().getName());
+                               }
+                               for (String n : lst) {
+                                       this.stopServer(n);
+                                       this.removeServer(n);
+                               }
+
+                               // We store the cleared state
+                               this.store();
+                       }
+
+                       for (ManagementEventListener lstr : 
managementEventListeners) {
+                               lstr.onRemoveAllResourses();
+                       }
+               }
        }

public ServerImpl addServer(String serverName, String hostAddress, int port) throws Exception {
@@ -413,29 +448,40 @@
                        throw new Exception("Server host port cannot be less than 
1");
                }

- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
-                       Server serverTemp = n.getValue();
-                       if (serverName.equals(serverTemp.getName())) {
- throw new Exception(String.format("Server name=%s already exist", serverName));
-                       }
-
- if (hostAddress.equals(serverTemp.getHostAddress()) && port == serverTemp.getHostport()) { - throw new Exception(String.format("Server name=%s is already bound to %s:%d", serverTemp.getName(),
-                                               serverTemp.getHostAddress(), 
serverTemp.getHostport()));
-                       }
-               }
-
- ServerImpl server = new ServerImpl(serverName, hostAddress, port, ipChannelType, extraHostAddresses);
-               server.setManagement(this);
-
-               this.servers.add(server);
-
-               this.store();
-               if (logger.isInfoEnabled()) {
-                       logger.info(String.format("Created Server=%s", 
server.getName()));
-               }
-
-               return server;
+               synchronized (this) {
+ for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+                               Server serverTemp = n.getValue();
+                               if (serverName.equals(serverTemp.getName())) {
+ throw new Exception(String.format("Server name=%s already exist", serverName));
+                               }
+
+ if (hostAddress.equals(serverTemp.getHostAddress()) && port == serverTemp.getHostport()) { + throw new Exception(String.format("Server name=%s is already bound to %s:%d", serverTemp.getName(), serverTemp.getHostAddress(),
+                                                       
serverTemp.getHostport()));
+                               }
+                       }
+
+ ServerImpl server = new ServerImpl(serverName, hostAddress, port, ipChannelType, extraHostAddresses);
+                       server.setManagement(this);
+
+                       FastList<Server> newServers = new FastList<Server>();
+                       newServers.addAll(this.servers);
+                       newServers.add(server);
+                       this.servers = newServers;
+//                     this.servers.add(server);
+
+                       this.store();
+
+                       for (ManagementEventListener lstr : 
managementEventListeners) {
+                               lstr.onServerAdded(server);
+                       }
+
+                       if (logger.isInfoEnabled()) {
+                               logger.info(String.format("Created Server=%s", 
server.getName()));
+                       }
+
+                       return server;
+               }
        }

        public void removeServer(String serverName) throws Exception {
@@ -448,26 +494,36 @@
                        throw new Exception("Server name cannot be null");
                }

-               Server removeServer = null;
- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
-                       Server serverTemp = n.getValue();
-
-                       if (serverName.equals(serverTemp.getName())) {
-                               if (serverTemp.isStarted()) {
- throw new Exception(String.format("Server=%s is started. Stop the server before removing",
-                                                       serverName));
-                               }
-                               removeServer = serverTemp;
-                               break;
-                       }
-               }
-
-               if (removeServer == null) {
- throw new Exception(String.format("No Server found with name=%s", serverName));
-               }
-
-               this.servers.remove(removeServer);
-               this.store();
+               synchronized (this) {
+                       Server removeServer = null;
+ for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+                               Server serverTemp = n.getValue();
+
+                               if (serverName.equals(serverTemp.getName())) {
+                                       if (serverTemp.isStarted()) {
+ throw new Exception(String.format("Server=%s is started. Stop the server before removing", serverName));
+                                       }
+                                       removeServer = serverTemp;
+                                       break;
+                               }
+                       }
+
+                       if (removeServer == null) {
+ throw new Exception(String.format("No Server found with name=%s", serverName));
+                       }
+
+                       FastList<Server> newServers = new FastList<Server>();
+                       newServers.addAll(this.servers);
+                       newServers.remove(removeServer);
+                       this.servers = newServers;
+                       //this.servers.remove(removeServer);
+
+                       this.store();
+
+                       for (ManagementEventListener lstr : 
managementEventListeners) {
+                               lstr.onServerRemoved(removeServer);
+                       }
+               }
        }

        public void startServer(String serverName) throws Exception {
@@ -480,7 +536,8 @@
                        throw new Exception("Server name cannot be null");
                }

- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+               FastList<Server> tempServers = servers;
+ for (FastList.Node<Server> n = tempServers.head(), end = tempServers.tail(); (n = n.getNext()) != end;) {
                        Server serverTemp = n.getValue();

                        if (serverName.equals(serverTemp.getName())) {
@@ -506,7 +563,8 @@
                        throw new Exception("Server name cannot be null");
                }

- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+               FastList<Server> tempServers = servers;
+ for (FastList.Node<Server> n = tempServers.head(), end = tempServers.tail(); (n = n.getNext()) != end;) {
                        Server serverTemp = n.getValue();

                        if (serverName.equals(serverTemp.getName())) {
@@ -547,49 +605,63 @@
                        throw new Exception("Association name cannot be null");
                }

-               if (this.associations.get(assocName) != null) {
- throw new Exception(String.format("Already has association=%s", assocName));
-               }
-
-               Server server = null;
-
- for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
-                       Server serverTemp = n.getValue();
-                       if (serverTemp.getName().equals(serverName)) {
-                               server = serverTemp;
-                       }
-               }
-
-               if (server == null) {
- throw new Exception(String.format("No Server found for name=%s", serverName));
-               }
-
- for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n
-                               .getNext()) != end;) {
-                       Association associationTemp = n.getValue();
-
- if (peerAddress.equals(associationTemp.getPeerAddress()) && associationTemp.getPeerPort() == peerPort) { - throw new Exception(String.format("Already has association=%s with same peer address=%s and port=%d",
-                                               associationTemp.getName(), 
peerAddress, peerPort));
-                       }
-               }
-
-               if (server.getIpChannelType() != ipChannelType)
- throw new Exception(String.format("Server and Accociation has different IP channel type"));
-
- AssociationImpl association = new AssociationImpl(peerAddress, peerPort, serverName, assocName, ipChannelType);
-               association.setManagement(this);
-               this.associations.put(assocName, association);
-               ((ServerImpl) server).associations.add(assocName);
-
-               this.store();
-
-               if (logger.isInfoEnabled()) {
- logger.info(String.format("Added Associoation=%s of type=%s", association.getName(),
-                                       association.getAssociationType()));
-               }
-
-               return association;
+               synchronized (this) {
+                       if (this.associations.get(assocName) != null) {
+ throw new Exception(String.format("Already has association=%s", assocName));
+                       }
+
+                       Server server = null;
+
+ for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+                               Server serverTemp = n.getValue();
+                               if (serverTemp.getName().equals(serverName)) {
+                                       server = serverTemp;
+                               }
+                       }
+
+                       if (server == null) {
+ throw new Exception(String.format("No Server found for name=%s", serverName));
+                       }
+
+ for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n.getNext()) != end;) {
+                               Association associationTemp = n.getValue();
+
+ if (peerAddress.equals(associationTemp.getPeerAddress()) && associationTemp.getPeerPort() == peerPort) { + throw new Exception(String.format("Already has association=%s with same peer address=%s and port=%d", associationTemp.getName(),
+                                                       peerAddress, peerPort));
+                               }
+                       }
+
+                       if (server.getIpChannelType() != ipChannelType)
+ throw new Exception(String.format("Server and Accociation has different IP channel type"));
+
+ AssociationImpl association = new AssociationImpl(peerAddress, peerPort, serverName, assocName, ipChannelType);
+                       association.setManagement(this);
+
+ AssociationMap<String, Association> newAssociations = new AssociationMap<String, Association>();
+                       newAssociations.putAll(this.associations);
+                       newAssociations.put(assocName, association);
+                       this.associations = newAssociations;
+//                     this.associations.put(assocName, association);
+
+                       FastList<String> newAssociations2 = new 
FastList<String>();
+                       newAssociations2.addAll(((ServerImpl) 
server).associations);
+                       newAssociations2.add(assocName);
+                       ((ServerImpl) server).associations = newAssociations2;
+                       //((ServerImpl) server).associations.add(assocName);
+
+                       this.store();
+
+                       for (ManagementEventListener lstr : 
managementEventListeners) {
+                               lstr.onAssociationAdded(association);
+                       }
+
+                       if (logger.isInfoEnabled()) {
+ logger.info(String.format("Added Associoation=%s of type=%s", association.getName(), association.getAssociationType()));
+                       }
+
+                       return association;
+               }
        }

public AssociationImpl addAssociation(String hostAddress, int hostPort, String peerAddress, int peerPort,
@@ -624,39 +696,47 @@
                        throw new Exception("Association name cannot be null");
                }

- for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n
-                               .getNext()) != end;) {
-                       Association associationTemp = n.getValue();
-
-                       if (assocName.equals(associationTemp.getName())) {
- throw new Exception(String.format("Already has association=%s", associationTemp.getName()));
-                       }
-
- if (peerAddress.equals(associationTemp.getPeerAddress()) && associationTemp.getPeerPort() == peerPort) { - throw new Exception(String.format("Already has association=%s with same peer address=%s and port=%d",
-                                               associationTemp.getName(), 
peerAddress, peerPort));
-                       }
-
- if (hostAddress.equals(associationTemp.getHostAddress()) && associationTemp.getHostPort() == hostPort) { - throw new Exception(String.format("Already has association=%s with same host address=%s and port=%d",
-                                               associationTemp.getName(), 
hostAddress, hostPort));
+               synchronized (this) {
+ for (FastMap.Entry<String, Association> n = this.associations.head(), end = this.associations.tail(); (n = n.getNext()) != end;) {
+                               Association associationTemp = n.getValue();
+
+                               if 
(assocName.equals(associationTemp.getName())) {
+ throw new Exception(String.format("Already has association=%s", associationTemp.getName()));
+                               }
+
+ if (peerAddress.equals(associationTemp.getPeerAddress()) && associationTemp.getPeerPort() == peerPort) { + throw new Exception(String.format("Already has association=%s with same peer address=%s and port=%d", associationTemp.getName(),
+                                                       peerAddress, peerPort));
+                               }
+
+ if (hostAddress.equals(associationTemp.getHostAddress()) && associationTemp.getHostPort() == hostPort) { + throw new Exception(String.format("Already has association=%s with same host address=%s and port=%d", associationTemp.getName(),
+                                                       hostAddress, hostPort));
+                               }
+
                        }

-               }
-
- AssociationImpl association = new AssociationImpl(hostAddress, hostPort, peerAddress, peerPort, assocName,
-                               ipChannelType, extraHostAddresses);
-               association.setManagement(this);
-               associations.put(assocName, association);
-
-               this.store();
-
-               if (logger.isInfoEnabled()) {
- logger.info(String.format("Added Associoation=%s of type=%s", association.getName(),
-                                       association.getAssociationType()));
-               }
-
-               return association;
+ AssociationImpl association = new AssociationImpl(hostAddress, hostPort, peerAddress, peerPort, assocName, ipChannelType, extraHostAddresses);
+                       association.setManagement(this);
+
+ AssociationMap<String, Association> newAssociations = new AssociationMap<String, Association>();
+                       newAssociations.putAll(this.associations);
+                       newAssociations.put(assocName, association);
+                       this.associations = newAssociations;
+//                     associations.put(assocName, association);
+
+                       this.store();
+
+                       for (ManagementEventListener lstr : 
managementEventListeners) {
+                               lstr.onAssociationAdded(association);
+                       }
+
+                       if (logger.isInfoEnabled()) {
+ logger.info(String.format("Added Associoation=%s of type=%s", association.getName(), association.getAssociationType()));
+                       }
+
+                       return association;
+               }
        }

        public Association getAssociation(String assocName) throws Exception {
@@ -729,29 +809,44 @@
                        throw new Exception("Association name cannot be null");
                }

-               Association association = this.associations.get(assocName);
-
-               if (association == null) {
- throw new Exception(String.format("No Association found for name=%s", assocName));
-               }
-
-               if (association.isStarted()) {
- throw new Exception(String.format("Association name=%s is started. Stop before removing", assocName));
-               }
-
-               this.associations.remove(assocName);
-
- if (((AssociationImpl) association).getAssociationType() == AssociationType.SERVER) { - for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
-                               Server serverTemp = n.getValue();
-                               if 
(serverTemp.getName().equals(association.getServerName())) {
-                                       ((ServerImpl) 
serverTemp).associations.remove(assocName);
-                                       break;
+               synchronized (this) {
+                       Association association = 
this.associations.get(assocName);
+
+                       if (association == null) {
+ throw new Exception(String.format("No Association found for name=%s", assocName));
+                       }
+
+                       if (association.isStarted()) {
+ throw new Exception(String.format("Association name=%s is started. Stop before removing", assocName));
+                       }
+
+ AssociationMap<String, Association> newAssociations = new AssociationMap<String, Association>();
+                       newAssociations.putAll(this.associations);
+                       newAssociations.remove(assocName);
+                       this.associations = newAssociations;
+//                     this.associations.remove(assocName);
+
+ if (((AssociationImpl) association).getAssociationType() == AssociationType.SERVER) { + for (FastList.Node<Server> n = this.servers.head(), end = this.servers.tail(); (n = n.getNext()) != end;) {
+                                       Server serverTemp = n.getValue();
+                                       if 
(serverTemp.getName().equals(association.getServerName())) {
+                                               FastList<String> newAssociations2 = 
new FastList<String>();
+                                               
newAssociations2.addAll(((ServerImpl) serverTemp).associations);
+                                               
newAssociations2.remove(assocName);
+                                               ((ServerImpl) 
serverTemp).associations = newAssociations2;
+//                                             ((ServerImpl) 
serverTemp).associations.remove(assocName);
+
+                                               break;
+                                       }
                                }
                        }
-               }
-
-               this.store();
+
+                       this.store();
+
+                       for (ManagementEventListener lstr : 
managementEventListeners) {
+                               lstr.onAssociationRemoved(association);
+                       }
+               }
        }

        /**
=======================================
--- /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java Mon Mar 5 00:44:41 2012 +++ /sctp-impl/src/main/java/org/mobicents/protocols/sctp/ServerImpl.java Thu Jul 5 03:32:52 2012
@@ -111,7 +111,8 @@
        }

        protected void stop() throws Exception {
- for (FastList.Node<String> n = this.associations.head(), end = this.associations.tail(); (n = n.getNext()) != end;) {
+               FastList<String> tempAssociations = associations;
+ for (FastList.Node<String> n = tempAssociations.head(), end = tempAssociations.tail(); (n = n.getNext()) != end;) {
                        String assocName = n.getValue();
                        Association associationTemp = 
this.management.getAssociation(assocName);
                        if (associationTemp.isStarted()) {
=======================================
--- /sctp-impl/src/test/java/org/mobicents/protocols/sctp/ManagementTest.java Sat May 12 00:33:30 2012 +++ /sctp-impl/src/test/java/org/mobicents/protocols/sctp/ManagementTest.java Thu Jul 5 03:32:52 2012
@@ -30,7 +30,6 @@
 import java.util.List;
 import java.util.Map;

-import org.apache.log4j.Logger;
 import org.mobicents.protocols.api.Association;
 import org.mobicents.protocols.api.AssociationListener;
 import org.mobicents.protocols.api.IpChannelType;
@@ -103,6 +102,7 @@

                String[] arr = new String[]{"127.0.0.2", "127.0.0.3"};
Server server = management.addServer(SERVER_NAME, SERVER_HOST, SERVER_PORT, ipChannelType, arr);
+
                management.startServer(SERVER_NAME);

                assertTrue(server.isStarted());

Reply via email to