203 lines
5.6 KiB
Java
203 lines
5.6 KiB
Java
import java.net.*;
|
|
import java.io.*;
|
|
|
|
public class IOManager
|
|
{
|
|
public static final int OkResult=220;
|
|
public static final int ErrorResult=500;
|
|
private final int MaxNetworkPacket=1024;
|
|
private final int InvalidPort=-1;
|
|
private SocketControl mTCPSocket=null;
|
|
private boolean mIsLoggedIn=false;
|
|
private StringArray mAckResponseStrings;
|
|
|
|
public IOManager()
|
|
{
|
|
mAckResponseStrings=new StringArray();
|
|
mAckResponseStrings.insert("220");
|
|
}
|
|
public void shutdown()
|
|
{
|
|
if(!mTCPSocket.isConnected())return;
|
|
if(isLoggedIn())quit();
|
|
try{mTCPSocket.closeSocket();}
|
|
catch(IOException exception){return;}
|
|
}
|
|
public boolean isConnected()
|
|
{
|
|
if(mTCPSocket==null||!mTCPSocket.isConnected())return false;
|
|
return true;
|
|
}
|
|
private void isLoggedIn(boolean isLoggedIn)
|
|
{
|
|
mIsLoggedIn=isLoggedIn;
|
|
}
|
|
public boolean isLoggedIn()
|
|
{
|
|
return mIsLoggedIn;
|
|
}
|
|
public boolean connect(String strHost,int port)
|
|
{
|
|
int bytesRead;
|
|
byte streamBytes[];
|
|
int clientPort;
|
|
String strLine=new String();
|
|
|
|
if(isConnected())return false;
|
|
streamBytes=new byte[MaxNetworkPacket];
|
|
InetAddress inetAddress=null;
|
|
SocketControl socket=null;
|
|
if(0!=strHost.length())
|
|
{
|
|
try{inetAddress=InetAddress.getByName(strHost);}
|
|
catch(UnknownHostException exception){return false;}
|
|
try{socket=new SocketControl(inetAddress,port);}
|
|
catch(IOException exception){return false;}
|
|
catch(SecurityException exception){return false;}
|
|
}
|
|
else
|
|
{
|
|
try{socket=new SocketControl("",port);}
|
|
catch(IOException exception){return false;}
|
|
catch(SecurityException exception){return false;}
|
|
}
|
|
if(!socket.isConnected())return false;
|
|
strLine=socket.readLine();
|
|
if(0==strLine.length())return false;
|
|
clientPort=getPort(strLine);
|
|
if(InvalidPort==clientPort)return false;
|
|
socket.writeLine("OK");
|
|
try{socket.closeSocket();}
|
|
catch(IOException exception){return false;}
|
|
if(0!=strHost.length())
|
|
{
|
|
try{inetAddress=InetAddress.getByName(strHost);}
|
|
catch(UnknownHostException exception){return false;}
|
|
try{mTCPSocket=new SocketControl(inetAddress,clientPort);}
|
|
catch(IOException exception){return false;}
|
|
}
|
|
else
|
|
{
|
|
try{mTCPSocket=new SocketControl("",clientPort);}
|
|
catch(IOException exception){return false;}
|
|
}
|
|
strLine=mTCPSocket.readLine();
|
|
return isConnected();
|
|
}
|
|
public boolean advise(String strAddress,int port)
|
|
{
|
|
String strRequest=null;
|
|
if(!isConnected())return false;
|
|
strRequest=new String("ADVISE(")+new String(strAddress)+new String(",")+String.valueOf(port)+new String(")");
|
|
mTCPSocket.writeLine(strRequest);
|
|
strRequest=mTCPSocket.readLine();
|
|
return true;
|
|
}
|
|
private String readLine(InputStream inputStream)
|
|
{
|
|
byte streamByte[];
|
|
String strLine=new String();
|
|
|
|
strLine=new String();
|
|
while(true)
|
|
{
|
|
streamByte=new byte[1];
|
|
try{inputStream.read(streamByte);}
|
|
catch(IOException exception){break;}
|
|
if(13==streamByte[0])continue;
|
|
else if(10==streamByte[0])break;
|
|
strLine+=new String(streamByte);
|
|
}
|
|
return strLine;
|
|
}
|
|
private boolean writeLine(String strLine,OutputStream outputStream)
|
|
{
|
|
if(0==strLine.length())return false;
|
|
strLine+=new String("\r\n");
|
|
byte streamData[]=strLine.getBytes();
|
|
try{outputStream.write(streamData);}
|
|
catch(IOException exception){return false;}
|
|
return true;
|
|
}
|
|
private int getPort(String strLine)
|
|
{
|
|
String strPort;
|
|
int strIndex;
|
|
int port;
|
|
|
|
strIndex=strLine.length()-1;
|
|
port=InvalidPort;
|
|
strPort=new String();
|
|
if(strIndex<0)return -1;
|
|
while(true)
|
|
{
|
|
if(strIndex<0)break;
|
|
if(' '==strLine.charAt(strIndex))break;
|
|
strIndex--;
|
|
}
|
|
strIndex++;
|
|
if(strIndex<0)return port;
|
|
strPort=strLine.substring(strIndex);
|
|
return Integer.parseInt(strPort);
|
|
}
|
|
private boolean isInResponse(String responseString,StringArray responseStrings)
|
|
{
|
|
if(0==responseString.length())return false;
|
|
for(int index=0;index<responseStrings.size();index++)
|
|
{
|
|
if(responseString.equals(responseStrings.getAt(index)))return true;
|
|
}
|
|
return false;
|
|
}
|
|
public boolean user(String strUserName,StringArray receiveStrings)
|
|
{
|
|
receiveStrings.clear();
|
|
if(!isConnected())return false;
|
|
String strLine=new String("USER(")+strUserName+new String(")");
|
|
mTCPSocket.writeLine(strLine);
|
|
if(!mTCPSocket.receive(receiveStrings,mAckResponseStrings))return false;
|
|
isLoggedIn(true);
|
|
return isLoggedIn();
|
|
}
|
|
public boolean who(StringArray userList)
|
|
{
|
|
StringArray responseLines=new StringArray();
|
|
userList.clear();
|
|
if(!isConnected())return false;
|
|
String strLine=new String("WHO");
|
|
mTCPSocket.writeLine(strLine);
|
|
if(!mTCPSocket.receive(responseLines,mAckResponseStrings))return false;
|
|
for(int index=1;index<responseLines.size()-1;index++)
|
|
userList.insert(responseLines.getAt(index).substring(4,responseLines.getAt(index).length()));
|
|
return true;
|
|
}
|
|
public boolean quit()
|
|
{
|
|
if(!isConnected()||!isLoggedIn())return false;
|
|
String strLine=new String("QUIT");
|
|
mTCPSocket.writeLine(strLine);
|
|
strLine=mTCPSocket.readLine();
|
|
return true;
|
|
}
|
|
public boolean talk(String strLine)
|
|
{
|
|
if(!isConnected()||0==strLine.length())return false;
|
|
String strString=new String("TALK(")+strLine+new String(")");
|
|
mTCPSocket.writeLine(strString);
|
|
strLine=mTCPSocket.readLine();
|
|
return true;
|
|
}
|
|
public String version()
|
|
{
|
|
String strVersion=new String();
|
|
String strLine=new String();
|
|
|
|
if(!isConnected())return strVersion;
|
|
mTCPSocket.writeLine("VERSION");
|
|
strLine=mTCPSocket.readLine();
|
|
if(mTCPSocket.OkResult!=mTCPSocket.getResultCode(strLine))return strVersion;
|
|
strVersion=mTCPSocket.getResult(strLine);
|
|
return strVersion;
|
|
}
|
|
|
|
}; |