/**This class provides a server socket for the monitoring and steering client to connect to. This
*server parses the incoming message, consults the name servers to find out which LP is the
*destination, then send the message to it.
*@author Yin Xiong
*@version 1.0
*
12/2005
*/
import java.net.*;
import java.io.*;
import java.lang.Thread;
import java.lang.*;
import java.util.*;
public class SimSocket extends Thread{
int port;
ServerSocket server_socket;
BufferedReader input;
GlobalNameServer gns;
MessagePool msgPool;
Flag flag;
public SimSocket(GlobalNameServer g, MessagePool mp, Flag f)
{
gns=g;
msgPool=mp;
flag=f;
}
/**Opens a socket and waits for the monitoring and steering client to connect with.
*The socket remains open until the simulation runs to the end or the client requests so.
*/
public void run(){
boolean endOfSim=false;
System.out.println("\n***********port = 1500 (default)**************");
port = 1500;
try {
server_socket = new ServerSocket(port);
System.out.println("Server waiting for client on port " +
server_socket.getLocalPort());
// server infinite loop
while(!endOfSim) {
if(flag.get()<0){
System.out.println("Socket Server gets endOfSim flag; shutting down");
endOfSim=true;
}
else{
Socket socket = server_socket.accept();
System.out.println("New connection accepted " +
socket.getInetAddress() +
":" + socket.getPort());
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// print received data
try {
//while(true) {
while(!endOfSim) {
String message = input.readLine();
if (message==null) break;
// System.out.println(message);
//construct a MS message
Message msg=getMsMessage(message);
if(msg!=null){
System.out.println("\nin SimSocket: ");
System.out.println("MS msg sender app id: " + msg.getSenderAPPid());
System.out.println("MS msg dest LP: " + msg.getReceiverLP());
System.out.println("MS msg dest PE " + msg.getReceiverPE());
System.out.println("MS msg dest APPid " + msg.getReceiverAPPid());
//System.out.println("MS msg content:" + msg.getContent());
msgPool.addMsgToPool(msg);
}//end if
if(flag.get()<0){
System.out.println("Socket Server gets endOfSim flag; shutting down");
endOfSim=true;
}//end if flag is not set
}//end inner while
}//end try
catch (Exception e) {
System.out.println(e);
}
// connection closed by client
try {
socket.close();
System.out.println("Connection closed by client");
}
catch (IOException e) {
System.out.println(e);
}
}//end else flag is not -1
}//end while
}//end try
catch (IOException e) {
System.out.println(e);
}
}//end openSocket
Message getMsMessage(String msg)
{
Message message=null;
String tok;
String[] s=msg.split("\t\t");
//for(int i=0;i