import java.net.*;
import java.io.*;
import java.util.*;
//this class is used by the Sever
public class MServerSocket extends vMSocket{
public String myName;
public static HashMap<String ,MServerSocket> msocDB=
new HashMap<String ,MServerSocket> ();
public MServerSocket(Socket soc){
super(soc);
if(soc != null){
String r=super.receive(); //comfirm the login of the user
String[] lst=r.split(":",2);
if( lst.length==2){
String name= lst[0];//user name
String pswd= lst[1];//userpswd
User u=User.userDB.get(name);
//if the user is not online and the pswd is correct
//then CONFORM him to login
if(u.getStat()!=User.Stat.ONLINE &&
lst[1].equals(u.getPswd())){
myName=lst[0];
msend("CONFORM");
u.setStat(User.Stat.ONLINE);
msocDB.put(lst[0],this);
start(); //start a new Thread to receive msg
} else{ msend("NOT CONFORM"); }
}
}
}
public void run(){
keepreceive();
msocDB.remove(myName);
}
public void keepreceive(){
while(!exitNow){
if(super.soc.isClosed())
break;
String str=super.receive();
int i=str.indexOf(":");
if(i != -1){
String yourName=str.substring(0,i);
String msg= str.substring(i,str.length());
MServerSocket soc=msocDB.get(yourName);
if(soc !=null){
System.out.println(myName+" send msg\""+ msg +"\" to "+yourName);
soc.msend(this.myName + msg);
try{ sleep(1000);
}catch(InterruptedException e){ }
}
}
}
}
}