112 lines
3.6 KiB
Java
112 lines
3.6 KiB
Java
import java.net.*;
|
|
import java.io.*;
|
|
import java.awt.*;
|
|
|
|
class AdviseThread extends Thread
|
|
{
|
|
SocketControl mSocketControl=null;
|
|
List mList;
|
|
public final int AdvisePort=200;
|
|
private boolean mIsCancelled=false;
|
|
private final String mUserTalkDirective=new String("USERTALK(");
|
|
private final String mUserEnterDirective=new String("USERENTER(");
|
|
private final String mUserLeaveDirective=new String("USERLEAVE(");
|
|
private final String mQuitDirective=new String("QUIT");
|
|
public AdviseThread(List list)
|
|
{
|
|
mList=list;
|
|
isCancelled(false);
|
|
mSocketControl=new SocketControl();
|
|
}
|
|
public void run()
|
|
{
|
|
String strLine=null;
|
|
String strAdvise=new String();
|
|
|
|
try{mSocketControl.accept(AdvisePort);}
|
|
catch(IOException exception){return;}
|
|
mSocketControl.writeLine("TESTCONNECT");
|
|
while(true)
|
|
{
|
|
strLine=new String();
|
|
strLine=mSocketControl.readLine();
|
|
if(isCancelled())break;
|
|
if(0==strLine.length())continue;
|
|
if(strLine.regionMatches(0,mUserTalkDirective,0,mUserTalkDirective.length()))strLine=parseTalk(strLine);
|
|
else if(strLine.regionMatches(0,mUserEnterDirective,0,mUserEnterDirective.length()))strLine=parseEnter(strLine);
|
|
else if(strLine.regionMatches(0,mUserLeaveDirective,0,mUserLeaveDirective.length()))strLine=parseLeave(strLine);
|
|
else if(strLine.regionMatches(0,mQuitDirective,0,mQuitDirective.length()))break;
|
|
mList.addItem(strLine);
|
|
mList.select(mList.getItemCount()-1);
|
|
}
|
|
stop();
|
|
}
|
|
public void isCancelled(boolean isCancelled)
|
|
{
|
|
mIsCancelled=isCancelled;
|
|
if(mIsCancelled)
|
|
{
|
|
try{mSocketControl.closeSocket();}
|
|
catch(IOException exception){return;}
|
|
}
|
|
}
|
|
public boolean isCancelled()
|
|
{
|
|
return mIsCancelled;
|
|
}
|
|
|
|
private String parseTalk(String strLine)
|
|
{
|
|
int strLength=strLine.length();
|
|
String strUser=new String();
|
|
String strString=new String();
|
|
String strReturn=new String();
|
|
int index=0;
|
|
|
|
while(index<strLength){if('('==strLine.charAt(index))break;index++;}
|
|
index++;
|
|
while(index<strLength){if(','==strLine.charAt(index))break;strUser+=strLine.charAt(index);index++;}
|
|
index++;
|
|
while(index<strLength){if(')'==strLine.charAt(index))break;strString+=strLine.charAt(index);index++;}
|
|
strReturn+=new String("'")+strUser+new String("' said \"")+strString+new String("\".");
|
|
return strReturn;
|
|
}
|
|
private String parseEnter(String strLine)
|
|
{
|
|
String strUser=new String();
|
|
String strReturn=new String();
|
|
int strLength=strLine.length();
|
|
int index=0;
|
|
|
|
while(index<strLength){if('('==strLine.charAt(index))break;index++;}
|
|
index++;
|
|
while(index<strLength){if(')'==strLine.charAt(index))break;strUser+=strLine.charAt(index);index++;}
|
|
strReturn+=new String("'")+strUser+new String("' just entered.");
|
|
return strReturn;
|
|
}
|
|
private String parseLeave(String strLine)
|
|
{
|
|
String strUser=new String();
|
|
String strReturn=new String();
|
|
int strLength=strLine.length();
|
|
int index=0;
|
|
|
|
while(index<strLength){if('('==strLine.charAt(index))break;index++;}
|
|
index++;
|
|
while(index<strLength){if(')'==strLine.charAt(index))break;strUser+=strLine.charAt(index);index++;}
|
|
strReturn+=new String("'")+strUser+new String("' just left.");
|
|
return strReturn;
|
|
}
|
|
public static String getAdviseAddress()
|
|
{
|
|
String strReturn=new String();
|
|
InetAddress inetAddress=null;
|
|
|
|
try{inetAddress=InetAddress.getLocalHost();}
|
|
catch(UnknownHostException exception){return strReturn;}
|
|
strReturn=new String(inetAddress.getHostAddress());
|
|
return strReturn;
|
|
}
|
|
}
|
|
|