📄 connection.java
字号:
/*
* RIC 2 Server Release. by Harry Otten (c) Copyright 2003 hotten@dds.nl
* Check out http://www.hotten.dds.nl/ric/
*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
import java.net.*; // for Socket, ServerSocket, and InetAddress
import java.io.*; // for IOException and Input/OutputStream
public class Connection extends Thread {
private InputStream in;
private OutputStream out;
private Socket socket;
private Server server;
private boolean personalState;
private boolean clientAuthenticated;
private Controller pingController; // it's not really neccesary but for perfection... it's. :D
// it makes sure that we don't miss any pongs or pings.
private int echoBalans=0; // if we ping we do ++ if we get a pong we do --. Different's now more than 2.
//commando from client
private final String INTERNET_CONNECT = "INTERNET_CONNECT\r\n";
private final String INTERNET_DISCONNECT = "INTERNET_DISCONNECT\r\n";
private final String INTERNET_REAL_STATUS = "INTERNET_REAL_STATUS\r\n";
private final String RIC_PONG = "RIC_PONG\r\n";
private final String RIC_BYEBYE = "RIC_BYEBYE\r\n";
//command to client
private final String INTERNET_REAL_STATUS_DISCONNECTED = "INTERNET_REAL_STATUS_DISCONNECTED\r\n";
private final String INTERNET_REAL_STATUS_CONNECTED = "INTERNET_REAL_STATUS_CONNECTED\r\n";
private final String INTERNET_DISCONNECT_FAILED = "INTERNET_DISCONNECT_FAILED\r\n";
private final String INTERNET_CONNECT_FAILED = "INTERNET_CONNECT_FAILED\r\n";
private final String INTERNET_CONNECTED_BY_ = "INTERNET_CONNECTED_BY_";
private final String INTERNET_JOINED_BY_ = "INTERNET_JOINED_BY_";
private final String INTERNET_DISCONNECTED_BY_ = "INTERNET_DISCONNECTED_BY_";
private final String INTERNET_LEFT_BY_ = "INTERNET_LEFT_BY_";
private final String INTERNET_CONNECTION_LOST = "INTERNET_CONNECTION_LOST\r\n";
private final String RIC_NEW_USER = "RIC_NEW_USER:";
private final String RIC_USER_LEFT = "RIC_USER_LEFT:";
private final String RIC_PING = "RIC_PING\r\n";
private final String RIC_LIST = "RIC_LIST:";
/* I use the null so i now it's not set yet. */
private String userName = null;
private double clientVersion;
public Connection(Socket socket, Server server) {
this.socket=socket;
this.server=server;
pingController = new Controller();
};
public void listen() {
StringBuffer stringBuf = new StringBuffer();
String commando;
int byteRead;
boolean doorGaan=true;
try {
while (doorGaan) {
byteRead = 0;
stringBuf.setLength(0);
while (byteRead != 10) { // so long as byteRead doesn't match endofline
if ((byteRead = in.read()) == -1)
throw new SocketException("Connection close prematurely");
else stringBuf.append((char)byteRead);
}
commando = stringBuf.toString();
if (INTERNET_CONNECT.equals(commando)) {
makeConnection();
} else if (INTERNET_DISCONNECT.equals(commando)) {
hangupConnection();
} else if (INTERNET_REAL_STATUS.equals(commando)){
getRealState();
} else if (RIC_PONG.equals(commando)) {
pong();
} else if (RIC_BYEBYE.equals(commando)) {
if (personalState)
hangupConnection();
sendGoodBye();
doorGaan=false;
} else System.out.println("Client at "+socket.getInetAddress().getHostAddress() + " on port " + socket.getPort() + "made a booboo:"+ commando);
}
} catch (IOException e) {
System.out.println("Client "+socket.getInetAddress().getHostAddress() + " on port " + socket.getPort() + " connection lost!");
if (personalState)
hangupConnection();
}
};
public void run() {
System.out.println("Handling client at " + socket.getInetAddress().getHostAddress() + " on port " + socket.getPort());
try {
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
System.out.println("Client at " + socket.getInetAddress().getHostAddress() + " on port " + socket.getPort() + "Can not get streams");
}
if (checkVersion()) {
// Check the user name and password
if (inloggen()) {
server.sendMeUserList(this);
//listing and obeying the client
listen();
server.sendGoodBye(this); // telling the others that he left.
} else System.out.println("Client at " + socket.getInetAddress().getHostAddress() + " on port " + socket.getPort() + "Authaticatie check failed...disconnecting");
} else System.out.println("Client at " + socket.getInetAddress().getHostAddress() + " on port " + socket.getPort() + "Version check failed...disconnecting");
server.removeClient(this);
//make sure we close it.
try { socket.close();} catch (IOException f) {};
System.out.println("Handeld client at " + socket.getInetAddress().getHostAddress() + " on port " + socket.getPort());
};
private boolean inloggen() {
String tempUserName;
boolean waarde=false;
try {
tempUserName=readNetwork(in);
tempUserName=tempUserName.substring(0,tempUserName.length()-2); //ripping of the \r\n
String password=readNetwork(in);
password=password.substring(0,password.length()-2);
/* DEBUG */
System.out.println("Username is: "+tempUserName+" wachtwoord is: "+password);
/* controle moet hier komen */
if (server.checkUserName(tempUserName)) {
sendString("LOGIN OK\r\n");
waarde=true;
userName = tempUserName; // now other server threads can read this name.
/* Info voor de logbestanden */
System.out.println("Client AUTHENTICATED: " + socket.getInetAddress().getHostAddress() +
" port " + socket.getPort());
/* SEND EVERBODY A MESSAGES THAT A NEW USER occecured */
server.sendNewUser(this);
} else {
sendString("Your username isn't allowed! Login FAILED!\r\n");
socket.close();
System.out.println("Client username NOT ALLOWED closing connection");
}
} catch (IOException e) {
System.out.println("Login problem");
} catch (Exception e) {
System.out.println("Unknown login problem");
}
return waarde;
};
public void sendString(String string) {
try {
out.write(string.getBytes());
}
catch (IOException e)
{ }
};
public boolean getPersonalState() {
return personalState;
};
public void setConnected() {
personalState=true;
// sendString(INTERNET_CONNECT_OK);
};
public void setDisconnected() {
personalState=false;
// sendString(INTERNET_DISCONNECT_OK);
};
public void sendConnectionBy(String name) {
//telling the name who is connecting to the internet
sendString(INTERNET_CONNECTED_BY_+name+"\r\n");
};
public void sendConnectionJoined(String name) {
//telling the name who has joined the internet
sendString(INTERNET_JOINED_BY_+name+"\r\n");
};
public void sendDisconnectedBy(String name) {
//telling the name who has disconnected the internet
sendString(INTERNET_DISCONNECTED_BY_+name+"\r\n");
};
public void sendInternetLeftBy(String name) {
//telling the name who has left the internet
sendString(INTERNET_LEFT_BY_+name+"\r\n");
};
public void sendConnectFailed() {
sendString(INTERNET_CONNECT_FAILED);
};
public void sendDisconnectFailed() {
sendString(INTERNET_DISCONNECT_FAILED);
};
public void sendRealStatusDisconnected() {
sendString(INTERNET_REAL_STATUS_DISCONNECTED);
};
public void sendRealStatusConnected() {
sendString(INTERNET_REAL_STATUS_CONNECTED);
};
public void sendInternetConnectionLost() {
personalState=false;
sendString(INTERNET_CONNECTION_LOST);
};
public void sendMessages(String titel,String messages,String commando) {
sendString("MESSAGESV1;"+titel+messages+commando+"\r\n");
};
public void sendNewUser(String name) {
if (clientVersion<0.91) {
sendString(RIC_NEW_USER+name+"\r\n");
} else {
sendString(RIC_NEW_USER+name+"=1000\r\n");
}
};
public void sendUserLeft(String name) {
sendString(RIC_USER_LEFT+name+"\r\n");
};
public String getUserName() {
return userName;
};
public double getClientVersion() {
return clientVersion;
};
public void sendList(String list) {
sendString(RIC_LIST+list+"\r\n");
}
public void pong() {
pingController.askFor();
if (echoBalans<=0) {
System.out.println("Warning! Client " + socket.getInetAddress().getHostAddress() + " has buggy client!");
} else echoBalans--;
pingController.relinquish();
}
public void sendPing() {
pingController.askFor();
if (echoBalans>=2) {
System.out.println("No ping response from: "+socket.getInetAddress().getHostAddress()+ " dropping connection");
try { socket.close(); } catch (IOException e) { };
} else {
sendString(RIC_PING);
echoBalans++; // if we receive pong we do -- again.
}
pingController.relinquish();
}
private String readNetwork(InputStream in) throws IOException {
StringBuffer stringBuf = new StringBuffer();
int byteRead;
do {
if ((byteRead = in.read()) == -1)
throw new IOException("connection lost");
else stringBuf.append((char)byteRead);
} while (byteRead != 10); // so long as byteRead doesn't match endofline
return stringBuf.toString();
};
private boolean checkVersion() {
double versionNumber;
boolean waarde=false;
try {
// Of we go with the version check
///Fist we tell our version
out.write(("HALLO RIC2-" + RICd.VERSION + "\r\n").getBytes());
// now we read his version.
String version=(readNetwork(in)).substring(6);
clientVersion=Double.parseDouble(version.substring(5,(version.length()-2)));
System.out.println("Client: " +version);
// Sending a messages to the client if the server is in TESTMODE
//Sending some messages to the client if his version is lower than this one.
if (server.testMode()) {
if (clientVersion>0.91)
sendString("1;");
sendString("Warning! The server is in test mode! No real connection will be made!\r\n");
}
if (clientVersion<0.92) {
/* Without the number; Because that version doesn't yet support that */
sendString("Update for your RIC client available! This one contains a bug. Updated recommended. With the newer version you will be able to ignore these startup messages\r\n");
}
// Okay.. now login
out.write(("PLEASE LOGIN\r\n").getBytes());
waarde=true;
} catch (IOException e) {
System.out.println("versie probleem!");
} catch (Exception e) {
System.out.println("unknown error! versie control");
}
return waarde;
};
private void makeConnection() {
server.makeConnection(this);
};
private void hangupConnection() {
server.hangupConnection(this);
};
private void getRealState() {
if (server.getInternetState())
sendRealStatusConnected();
else sendRealStatusDisconnected();
};
private void sendGoodBye() {
System.out.println("Client at "+socket.getInetAddress().getHostAddress() + " on port " + socket.getPort() + " is saying goodbye!");
try {
out.write(RIC_BYEBYE.getBytes());
} catch (IOException e) {
// whatever
}
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -