JBoss.orgCommunity Documentation

Chapter 6. Example

6.1. Before Getting Started
6.2. Initiating Management
6.3. Adding Server and server Association
6.4. Adding Association

This chapter tours around the core constructs of Mobicents SCTP Library with simple examples to let you get started quickly. You will be able to write a client and a server on top of Mobicents SCTP Library right away when you are at the end of this chapter.

The minimum requirements to run the examples which are introduced in this chapter are only two; the latest version of Mobicents SCTP Library and JDK 1.7 or above with SCTP support. At time of writing this guide linux kernel has native support for SCTP (lksctp) and also Solaris includes SCTP.

The primitive step in uisng Mobicents SCTP Library is to create instance of Management class and set appropriate parameters.

    
		private static final String SERVER_NAME = "testserver";
		private static final String SERVER_HOST = "127.0.0.1";
		private static final int SERVER_PORT = 2345;
	
		private static final String SERVER_ASSOCIATION_NAME = "serverAsscoiation";
		private static final String CLIENT_ASSOCIATION_NAME = "clientAsscoiation";
	
		private static final String CLIENT_HOST = "127.0.0.1";
		private static final int CLIENT_PORT = 2346;
		...
		....
		....    	
    
		Management management = new ManagementImpl("SCTPTest");1
		management.setConnectDelay(10000);// Try connecting every 10 secs2
		management.setSingleThread(true);3
		management.start();    
    
	

1

Crate new instance of ManagementImpl and setting the management name. The management will search for SCTPTest_SCTP.xml file to load the previously configured Serever's or Association's. If file is not found it will create one.

2

connectDelay is only useful for Associations acting as client side trying to connect to peer. The value specified is time in milliseconds the unedrlying socket will try to connect to peer if the existing connection is broken or even if its fresh connection attempt.

3

Setting management to single thread. All the callback's (irrespective of streamNumber of packet) to the listener will be via single thread. However there is one dedicated thread only for I/O.

Once the Managment is setup, application can create Server and add server Association

    	Server server = this.management.addServer(SERVER_NAME, SERVER_HOST, SERVER_PORT);
		Association serverAssociation = this.management.addServerAssociation(CLIENT_HOST, CLIENT_PORT, SERVER_NAME, SERVER_ASSOCIATION_NAME);1
				
		serverAssociation.setAssociationListener(new ServerAssociationListener());2
				
		this.management.startAssociation(SERVER_ASSOCIATION_NAME);
		this.management.startServer(SERVER_NAME);
		
	

1

Add the server and server association. Multiple server's can be added to each management and each server can have multiple server association's

2

The instance of AssociationListener should be registered with newly created Associaton before starting it. There is no dependency on order of starting server and server association.

Below is example of class implementing AssociationListener

	class ServerAssociationListener implements AssociationListener {
		
		private final byte[] SERVER_MESSAGE = "Server says Hi".getBytes();
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.mobicents.protocols.sctp.AssociationListener#onCommunicationUp
		 * (org.mobicents.protocols.sctp.Association)
		 */
		@Override
		public void onCommunicationUp(Association association) {
			System.out.println(this + " onCommunicationUp");

			serverAssocUp = true;

			PayloadData payloadData = new PayloadData(SERVER_MESSAGE.length, SERVER_MESSAGE, true, false, 3, 1);

			try {
				association.send(payloadData);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.mobicents.protocols.sctp.AssociationListener#onCommunicationShutdown
		 * (org.mobicents.protocols.sctp.Association)
		 */
		@Override
		public void onCommunicationShutdown(Association association) {
			System.out.println(this + " onCommunicationShutdown");
			serverAssocDown = true;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.mobicents.protocols.sctp.AssociationListener#onCommunicationLost
		 * (org.mobicents.protocols.sctp.Association)
		 */
		@Override
		public void onCommunicationLost(Association association) {
			System.out.println(this + " onCommunicationLost");
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.mobicents.protocols.sctp.AssociationListener#onCommunicationRestart
		 * (org.mobicents.protocols.sctp.Association)
		 */
		@Override
		public void onCommunicationRestart(Association association) {
			System.out.println(this + " onCommunicationRestart");
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.mobicents.protocols.sctp.AssociationListener#onPayload(org.mobicents
		 * .protocols.sctp.Association,
		 * org.mobicents.protocols.sctp.PayloadData)
		 */
		@Override
		public void onPayload(Association association, PayloadData payloadData) {
			System.out.println(this + " onPayload");

			serverMessage = new byte[payloadData.getDataLength()];
			System.arraycopy(payloadData.getData(), 0, serverMessage, 0, payloadData.getDataLength());

			System.out.println(this + "received " + new String(serverMessage));
		}

	}
	

Once the Managment is setup, application can create client side Association.

		Association clientAssociation = this.management.addAssociation(CLIENT_HOST, CLIENT_PORT, SERVER_HOST, SERVER_PORT, CLIENT_ASSOCIATION_NAME);
		clientAssociation.setAssociationListener(new ClientAssociationListenerImpl());
		this.management.startAssociation(CLIENT_ASSOCIATION_NAME);