63 lines
1.3 KiB
Java
63 lines
1.3 KiB
Java
package com.flaremicro.crossjeeves;
|
|
|
|
import java.io.IOException;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Properties;
|
|
|
|
import com.flaremicro.crossjeeves.net.NetworkHandler;
|
|
import com.flaremicro.crossjeeves.net.ServerHandler;
|
|
import com.flaremicro.util.Util;
|
|
|
|
import static com.flaremicro.crossjeeves.net.ErrorCodes.*;
|
|
|
|
public class CrossJeevesHost {
|
|
int port;
|
|
boolean running = false;
|
|
|
|
List<NetworkHandler> connections = Collections.synchronizedList(new ArrayList<NetworkHandler>());
|
|
|
|
public CrossJeevesHost(int port) {
|
|
this.port = port;
|
|
}
|
|
|
|
public void endConnections()
|
|
{
|
|
while(connections.size() > 0)
|
|
{
|
|
connections.remove(0).disconnect(AGENT_TERMINATED.id, "This agent has terminated");
|
|
}
|
|
}
|
|
|
|
public void removeConnection(NetworkHandler netHandler)
|
|
{
|
|
connections.remove(netHandler);
|
|
}
|
|
|
|
public void startHosting() throws IOException {
|
|
running = true;
|
|
ServerSocket server = new ServerSocket(port);
|
|
while (running)
|
|
{
|
|
Socket sock = null;
|
|
try
|
|
{
|
|
sock = server.accept();
|
|
//TODO properties!
|
|
ServerHandler handler = new ServerHandler(sock, this, new Properties());
|
|
connections.add(handler);
|
|
handler.init();
|
|
}
|
|
catch (IOException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
Util.cleanClose(sock);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|