Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 1996-2001
   * Logica Mobile Networks Limited
   * All rights reserved.
   *
   * This software is distributed under Logica Open Source License Version 1.0
   * ("Licence Agreement"). You shall use it and distribute only in accordance
   * with the terms of the License Agreement.
   *
  */
 package org.smpp.smscsim;
 
 
This class accepts client connection on given port. When the connection is accepted, the listener creates an instance of SMSCSession, generates new PDUProcessor using object derived from PDUProcessorFactory, passes the processor to the smsc session and starts the session as a standalone thread.

Author(s):
Logica Mobile Networks SMPP Open Source Team
Version:
$Revision: 1.2 $
See also:
SMSCSession
PDUProcessor
PDUProcessorFactory
 
 public class SMSCListenerImpl extends SmppObject implements RunnableSMSCListener {
 	private Connection serverConn = null;
 	private int port;
 	private long acceptTimeout = ...;
 	private boolean keepReceiving = true;
 	private boolean isReceiving = false;
 	private boolean asynchronous = false;

Construct synchronous listener listening on the given port.

Parameters:
port the port to listen on
See also:
SMSCListenerImpl(int,boolean)
 
 	public SMSCListenerImpl(int port) {
 		this. = port;
 	}

Constructor with control if the listener starts as a separate thread. If asynchronous is true, then the listener is started as a separate thread, i.e. the creating thread can continue after calling of method start. If it's false, then the caller blocks while the listener does it's work, i.e. listening.

Parameters:
port the port to listen on
asynchronous if the listening will be performed as separate thread
See also:
start()
 
 	public SMSCListenerImpl(int portboolean asynchronous) {
 		this. = port;
 		this. = asynchronous;
 	}

Starts the listening. If the listener is asynchronous (reccomended), then new thread is created which listens on the port and the start method returns to the caller. Otherwise the caller is blocked in the start method.

See also:
stop()
 
 	public synchronized void start() throws IOException {
 		.write("going to start SMSCListener on port " + );
 		if (!) {
 			 = true;
 			if () {
 				.write("starting listener in separate thread.");
 				Thread serverThread = new Thread(this);
 				serverThread.start();
 				.write("listener started in separate thread.");
 			} else {
 				.write("going to listen in the context of current thread.");
 				run();
 			}
 		} else {
 			.write("already receiving, not starting the listener.");
 		}
 	}

Signals the listener that it should stop listening and wait until the listener stops. Note that based on the timeout settings it can take some time befor this method is finished -- the listener can be blocked on i/o operation and only after exiting i/o it can detect that it should stop.

See also:
start()
	public synchronized void stop() throws IOException {
		.write("going to stop SMSCListener on port " + );
		 = false;
		while () {
			Thread.yield();
		}
		.write("SMSCListener stopped on port " + );
	}

The actual listening code which is run either from the thread (for async listener) or called from start method (for sync listener). The method can be exited by calling of method stop.

See also:
start()
stop()
	public void run() {
		.enter(this"run of SMSCListener on port " + );
		 = true;
		try {
			while () {
				Thread.yield();
			}
finally {
			 = false;
		}
		.exit(this);
	}

The "one" listen attempt called from run method. The listening is atomicised to allow contoled stopping of the listening. The length of the single listen attempt is defined by acceptTimeout. If a connection is accepted, then new session is created on this connection, new PDU processor is generated using PDU processor factory and the new session is started in separate thread.

See also:
run()
com.logica.smpp.Connection
SMSCSession
PDUProcessor
PDUProcessorFactory
	private void listen() {
		.enter(.this"SMSCListener listening on port " + );
		try {
			Connection connection = null;
			connection = .accept();
			if (connection != null) {
				.write("SMSCListener accepted a connection on port " + );
				SMSCSession session = new SMSCSessionImpl(connection);
				PDUProcessor pduProcessor = null;
				if ( != null) {
					pduProcessor = .createPDUProcessor(session);
				}
				session.setPDUProcessor(pduProcessor);
				Thread thread = new Thread(session);
				thread.start();
				.write("SMSCListener launched a session on the accepted connection.");
else {
				.write(."no connection accepted this time.");
			}
catch (InterruptedIOException e) {
			// thrown when the timeout expires => it's ok, we just didn't
			// receive anything
			.write("InterruptedIOException accepting, timeout? -> " + e);
catch (IOException e) {
			// accept can throw this from various reasons
			// and we don't want to continue then (?)
			.write(e"IOException accepting connection");
			 = false;
		}
	}

Sets a PDU processor factory to use for generating PDU processors.

Parameters:
processorFactory the new PDU processor factory
	public void setPDUProcessorFactory(PDUProcessorFactory processorFactory) {
		this. = processorFactory;
	}

Sets new timeout for accepting new connection. The listening blocks the for maximum this time, then it exits regardless the connection was acctepted or not.

Parameters:
value the new value for accept timeout
	public void setAcceptTimeout(int value) {
		 = value;
	}

Returns the current setting of accept timeout.

Returns:
the current accept timeout
See also:
setAcceptTimeout(int)
	public long getAcceptTimeout() {
	}
/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  2003/09/30 09:17:49  sverkera
 * Created an interface for SMSCListener and SMSCSession and implementations of them  so that it is possible to provide other implementations of these classes.
 *
 * Revision 1.1  2003/07/23 00:28:39  sverkera
 * Imported
 *
 * 
 * Old changelog:
 * 23-08-01 ticp@logica.com added yield() to run() to give chance to other
 *						    threads
 * 26-09-01 ticp@logica.com debug now in a group
 */
New to GrepCode? Check out our FAQ X