[xSocket-develop] Beginner's problem with simple multiplexed server program
Status: Inactive
Brought to you by:
grro
|
From: Sharath <re...@ya...> - 2010-10-27 22:13:30
|
Hi,
i thought of making a start with xsocket to developing a scalable nio server.
Based on the tutorial on the xsocket web site, I have written the following
code: 2 classes, one is the main startup class, the other a pipeline handler:
public class ServerSocketMain {
static private Logger logger = null;
static private LoggingService loggingService = null;
static private int socketPortNo;
// Buffer size, in bytes, for I/O operations over the socket
static private int buffer_size = 0;
/**
* Private constructor.
*/
private ServerSocketMain () {
logger = Logger.getLogger(this.getClass());
loggingService = new LoggingService();
}
/**
* main(): for starting the program
*
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
ServerSocketMain multiConnServerSocket = new ServerSocketMain ();
multiConnServerSocket.initializeSocketParams();
try {
multiConnServerSocket.setupSocketConnOperations();
}
catch (MarisCommsException e) {
logger.fatal("MAYDAY MAYDAY: Cannot listen for client requests.
Program terminated!");
}
}
/**
* Socket specific initializations performed here e.g. port number.
*/
private void initializeSocketParams() {
ResourceBundle serverSocketResources =
ResourceBundle.getBundle("my.app.ServerSocket", Locale.US);
String socketPortNoStr = (String) serverSocketResources
.getObject("SOCKET_PORT_NO");
socketPortNo = (new Integer(socketPortNoStr)).intValue();
loggingService.debug("PORT NO = " + socketPortNo);
String bufferSizeStr = (String)
marisServerSocketResources.getObject("SOCKET_BUFFER_SIZE");
buffer_size = (new Integer(bufferSizeStr)).intValue();
loggingService.debug("BUFFER SIZE = " + buffer_size);
}
/**
* This is the infinite loop that continually performs the socket
operations: accept, read, write
*
* @throws MyAppException
*/
private void setupSocketConnOperations() throws MyAppException{
loggingService.debug("setupSocketConnOperations()");
IServer server;
try {
InetAddress hostAddress;
hostAddress = InetAddress.getLocalHost();
loggingService.debug("xSocket server address: " +
hostAddress.getHostAddress());
server = new Server(hostAddress, socketPortNo, new
MultiplexedProtocolAdapter(new CommandPipelineHandler()));
try {
ConnectionUtils.start(server);
loggingService.debug("xSocket server started");
}
catch (SocketTimeoutException e) {
loggingService.debug("SocketTimeoutException starting xSocket
Server: " + e.getMessage());
}
}
catch (UnknownHostException e) {
loggingService.debug("UnknownHostException creating xSocket Server:
" + e.getMessage());
}
catch (IOException e) {
loggingService.debug("IOException creating xSocket Server: " +
e.getMessage());
}
}
}
public class CommandPipelineHandler implements IPipelineDataHandler,
IConnectHandler {
static private LoggingService LOGGING_SERVICE = new LoggingService();
/* (non-Javadoc)
* @see
org.xsocket.connection.multiplexed.IPipelineDataHandler#onData(org.xsocket.connection.multiplexed.INonBlockingPipeline)
*/
public boolean onData(INonBlockingPipeline pipeline) throws IOException {
LOGGING_SERVICE.debug("onData()");
String incomingMessage = pipeline.readStringByLength(126);
LOGGING_SERVICE.debug("xSocket incomingMessage: " + incomingMessage);
// Pass off to a worker thread:
return true;
}
/* (non-Javadoc)
* @see
org.xsocket.connection.IConnectHandler#onConnect(org.xsocket.connection.INonBlockingConnection)
*/
@Override
public boolean onConnect(INonBlockingConnection arg0) throws IOException,
BufferUnderflowException, MaxReadSizeExceededException {
LOGGING_SERVICE.debug("onConnect()");
return true;
}
}
Checking through the logs I see that while the server is started, neither of the
callback methods are invoked i.e. onConnect, onData.
Please advise, maybe I am doing something silly here.
regards,
Sharath
|