Initial commit from SVN.
This commit is contained in:
111
Orb/src/com/de22/orb/StServerSocket.java
Normal file
111
Orb/src/com/de22/orb/StServerSocket.java
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (c) 2008,2009 Declarative Engineering LLC.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Declarative Engineering LLC
|
||||
* verson 1 which accompanies this distribution, and is available at
|
||||
* http://declarativeengineering.com/legal/DE_Developer_License_v1.txt
|
||||
*/
|
||||
package com.de22.orb;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
import com.common.debug.Debug;
|
||||
import com.common.event.VoidHandler1;
|
||||
import com.common.event.VoidHandler2;
|
||||
import com.common.thread.ThreadService;
|
||||
|
||||
public class StServerSocket extends AbstractConnectionServer {
|
||||
/** The underlying server socket. */
|
||||
private ServerSocket serverSocket = null;
|
||||
/** The listener thread. */
|
||||
private Thread listener = null;
|
||||
|
||||
/**
|
||||
* The runnable for a server socket to manage and thread the processing of new client sockets.
|
||||
*/
|
||||
private class ServerSocketRunnable implements Runnable {
|
||||
public void run() {
|
||||
while(serverSocket != null) {
|
||||
try {
|
||||
acceptSocket();
|
||||
}//try//
|
||||
catch(java.net.SocketException e) {
|
||||
break;
|
||||
}//catch//
|
||||
catch(Throwable e) {
|
||||
Debug.log(e);
|
||||
Debug.halt();
|
||||
break;
|
||||
}//catch//
|
||||
}//while//
|
||||
}//run()//
|
||||
}//ServerSocketRunnable//
|
||||
/**
|
||||
* StServerSocket constructor.
|
||||
*/
|
||||
public StServerSocket() {
|
||||
}//StServerSocket()//
|
||||
/**
|
||||
* Accepts a socket connection through the server socket.
|
||||
* @return The socket that was accepted.
|
||||
*/
|
||||
public AbstractConnection acceptSocket() throws IOException {
|
||||
StConnection result = null;
|
||||
Socket socket = serverSocket.accept();
|
||||
|
||||
if(socket != null) {
|
||||
result = new StConnection(getNextSessionId(), getServerSocketOptions().getAutoReconnectTimeLimit());
|
||||
registerConnection(result);
|
||||
result.initialize(getOrb(), socket, this, getMessageHandler(), getInitCompleteHandler());
|
||||
Debug.log("Socket listener started on: " + result.getNameAndPort() + " connecting via server socket: " + this.getNameAndPort());
|
||||
}//if//
|
||||
|
||||
return result;
|
||||
}//acceptSocket()//
|
||||
/* (non-Javadoc)
|
||||
* @see com.de22.orb.AbstractServerSocket#close()
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
if(serverSocket != null) {
|
||||
try {
|
||||
serverSocket.close();
|
||||
}//try//
|
||||
catch(Throwable e) {
|
||||
}//catch//
|
||||
|
||||
//Set the server socket reference to null so everyone knows this server socket is not open.//
|
||||
serverSocket = null;
|
||||
|
||||
//If there is a server socket runnable then stop it.//
|
||||
if(listener != null) {
|
||||
//Don't need to do this at this time. Closing the socket will take care of this for us.//
|
||||
//listener.interrupt();
|
||||
}//if//
|
||||
|
||||
super.close();
|
||||
}//if//
|
||||
}//close()//
|
||||
/**
|
||||
* Initializes the server socket for use.
|
||||
* The user may call the acceptSocket() method after this method has successfully returned.
|
||||
* @param orb The orb that created the server socket.
|
||||
* @param options The server socket options used to setup the server socket.
|
||||
* @param address An optional address only used to optimize performance since the caller often will have to get this address before deciding to create a new server socket.
|
||||
* @param classLoader The class loader that the server socket and all accepted sockets will use for loading classes.
|
||||
* @param messageHandler The handler called to process a message after the bytes have been read from the stream.
|
||||
* @param initCompleteHandler The handler called to finish the initialization process (after all initialization of the socket has finished).
|
||||
*/
|
||||
public void initialize(Orb orb, IServerSocketOptions options, InetAddress address, ClassLoader classLoader, VoidHandler2 messageHandler, VoidHandler1 initCompleteHandler) throws IOException {
|
||||
//Setup the address if necessary.//
|
||||
if((address == null) && (options.getAddress().getName() != null)) {
|
||||
address = InetAddress.getByName(options.getAddress().getName());
|
||||
}//if//
|
||||
|
||||
super.initialize(orb, options, address, classLoader, messageHandler, initCompleteHandler);
|
||||
serverSocket = new ServerSocket(options.getAddress().getPort(), (options.getBacklog() != null ? options.getBacklog().intValue() : 50), address);
|
||||
listener = ThreadService.run(new ServerSocketRunnable(), false);
|
||||
}//internalInitialize()//
|
||||
}//StServerSocket//
|
||||
Reference in New Issue
Block a user