networkcommunicator.java
来自「Remote Internet Connect简化了从其它运行操作系统的机子上将」· Java 代码 · 共 596 行 · 第 1/2 页
JAVA
596 行
/*
* RIC 2 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
import java.io.*; // for IOException and Input/OutputStream
import java.util.Vector;
import sun.audio.*; //import the sun.audio package
public class NetworkCommunicator implements Runnable {
private MainScreenGUI mainScreenGUI;
private Config config;
private SettingsScreen settingsScreen;
private Systray systray;
private boolean personalState;
private double versionNumber;
private int aantalOnline = 0; // Used to determine the color for the systray.
// this varaible is created in updateRicList;
private Socket socket;
private InputStream in;
private OutputStream out;
private Vector ricClients; // vector with clients name and status
//commando wich we sent
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_REAL_DISCONNECT = "RIC_REAL_DISCONNECT\r\n";
private final String RIC_PONG = "RIC_PONG\r\n";
private final String RIC_BYEBYE = "RIC_BYEBYE\r\n";
private final String RIC_CHAT_GET_IP_ = "RIC_CHAT_GET_IP_";
//command from server
private final String INTERNET_REAL_STATUS_CONNECTED = "INTERNET_REAL_STATUS_CONNECTED\r\n";
private final String INTERNET_REAL_STATUS_DISCONNECTED = "INTERNET_REAL_STATUS_DISCONNECTED\r\n";
private final String INTERNET_DISCONNECT_FAILED = "INTERNET_DISCONNECT_FAILED\r\n";
private final String INTERNET_DISCONNECT_OK = "INTERNET_DISCONNECT_OK\r\n";
private final String INTERNET_CONNECT_FAILED = "INTERNET_CONNECT_FAILED\r\n";
private final String INTERNET_CONNECT_OK = "INTERNET_CONNECT_OK\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 MESSAGESV1 = "MESSAGESV1";
private final String RIC_LIST = "RIC_LIST:";
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_CHAT_TELL_IP_AND_NAME = "RIC_CHAT_TELL_IP_AND_NAME:";
private final String RIC_CHAT_GIVE_IP_AND_NAME = "RIC_CHAT_GIVE_IP_AND_NAME:";
private String HOST;
private int HOST_PORT;
private String USERNAME;
private String PASSWORD;
public NetworkCommunicator(Config config) {
this.config = config;
};
public void connect() {
/* The connect methode will connect the client to the server.
* It will call a version check methode
* After that it will check authentication (a methode)
* While doing so it will control the mainScreenGUI
* It wil also start a thread which will listen to the connection
*/
boolean retry=false;
mainScreenGUI.setScreenState(2);
//set the button disable, (refresh/connect)
//and the stoplicht button
//(if the connection gets lost after a while, this methode will be runed again
// thats why we always need to do this)
if (config.getState()) {
// Getting the setting from the config object
HOST=config.getHost();
HOST_PORT=config.getHostPort();
USERNAME=config.getUserName();
PASSWORD=config.getPassWord();
do {
mainScreenGUI.addText("Trying to connect to " + HOST +":" + HOST_PORT+"\n");
try {
socket = new Socket(HOST,HOST_PORT);
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e){
mainScreenGUI.addText("Failed! no tcp connection!\n");
mainScreenGUI.setScreenState(1);
if (!retry) mainScreenGUI.popUp(true);
socket=null;
}
if (socket!=null) {
mainScreenGUI.addText("Connected...\n");
mainScreenGUI.addText("Version check...\n");
//in this methode the version is being checked ;-)
//perhaps in the future it can disable surtain things.
if (checkVersion()) {
try {
//Now whe login on the machine
if (login()) {
mainScreenGUI.setScreenState(0);
// Now read the connection until it stops
// It gives back false when the connection ends normal
// true if it is lost
retry=listen();
} else {
socket.close();
mainScreenGUI.setScreenState(1);
}
} catch (IOException e) {
System.out.println("Server not nice. Between version check and login :-(");
retry=false;
}
} else {
mainScreenGUI.addText("Version not okay\n");
mainScreenGUI.setScreenState(1);
mainScreenGUI.popUp(true);
retry=false;
}
}
/* If connection is lost we will reconnect with a 5 secondes interval */
if (retry)
try {
mainScreenGUI.setScreenState(2);
mainScreenGUI.addText("Trying to reconnect in 5 seconds...\n");
Thread.sleep(5000);
} catch (InterruptedException e) {
/* not important */
}
}while(retry);
} else {
mainScreenGUI.addText("Please set the settings first!\n");
mainScreenGUI.setScreenState(1);
mainScreenGUI.popUp(true);
return;
}
};
public void disconnect() {
try {
out.write(RIC_BYEBYE.getBytes()); // saying goodbye to the server
systray.stop();
} catch (IOException e) {
System.out.println("Problem disconnected... ;-) Whatever!");
}
};
public void sendStatusCheck() {
try {
out.write(INTERNET_REAL_STATUS.getBytes());
System.out.println("sending Status query");
}
catch (IOException e) {
System.out.println("Connection problem while sending Status query");
}
};
public void sendConnect() {
try {
out.write(INTERNET_CONNECT.getBytes());
System.out.println("sending connect query");
} catch (IOException e){
System.out.println("Connection problem while sending Connect");
}
};
public void sendDisconnect() {
try {
out.write(INTERNET_DISCONNECT.getBytes());
System.out.println("sending disconnect query");
} catch (IOException e) {
System.out.println("Connection problem while sending DISCONNECT query");
}
};
public void run() {
//System.out.println("Thread Started. Listening to connection");
connect();
}
public boolean listen() {
String commando;
String naam;
boolean doorGaan=true;
try {
while (doorGaan) { // never ending lus
commando = readNetwork(in);
if (commando.indexOf(MESSAGESV1)!=-1) {
messagesV1(commando);
} else if (INTERNET_CONNECT_FAILED.equals(commando)) {
mainScreenGUI.setStoplicht(false);
mainScreenGUI.addText("Failed to connect to internet\n");
} else if (commando.indexOf(INTERNET_CONNECTED_BY_)!=-1) {
naam=commando.substring(22,commando.length()-2); //ripping of the \r\n
if (naam.equals(USERNAME)) {
mainScreenGUI.setStoplicht(true);
mainScreenGUI.addText("You connected the Internet\n");
personalState=true;
} else {
(new WindowPopup(mainScreenGUI,"Internet available")).start();
mainScreenGUI.addText("Made by "+naam+"\n");
mainScreenGUI.addText("Internet available\n");
if (config.getSoundEnabled())
Sound.play("online.wav");
}
updateRicList(naam,true);
} else if (commando.indexOf(INTERNET_DISCONNECTED_BY_)!=-1) {
naam=commando.substring(25,commando.length()-2); //ripping of the \r\n
if (naam.equals(USERNAME)) {
mainScreenGUI.addText("You disconnected the internet\n");
personalState=false;
} else {
mainScreenGUI.addText("by "+naam+"\n");
mainScreenGUI.addText("Internet disconnected\n");
}
updateRicList(naam,false);
mainScreenGUI.setStoplicht(false); // Normaly you always need to disconnect your self
// but in the future perhaps a admin can
} else if (commando.indexOf(INTERNET_JOINED_BY_)!=-1) {
naam=commando.substring(19,commando.length()-2); //ripping of the \r\n
if (naam.equals(USERNAME)) {
mainScreenGUI.setStoplicht(true);
mainScreenGUI.addText("You joined the Internet\n");
personalState=true;
} else {
mainScreenGUI.addText(naam+" joins internetting\n");
}
updateRicList(naam,true);
} else if (commando.indexOf(INTERNET_LEFT_BY_)!=-1) {
naam=commando.substring(17,commando.length()-2); //ripping of the \r\n
if (naam.equals(USERNAME)) {
mainScreenGUI.setStoplicht(false);
mainScreenGUI.addText("You stopt internetting\n");
personalState=false;
} else {
mainScreenGUI.addText(naam+" stopt internetting\n");
}
updateRicList(naam,false);
} else if (INTERNET_CONNECTION_LOST.equals(commando)) {
mainScreenGUI.addText("Internet Connection lost!\n");
(new WindowPopup(mainScreenGUI,"Connection Lost!")).start();
lostConnection();
} else if (INTERNET_DISCONNECT_FAILED.equals(commando)) {
mainScreenGUI.setStoplicht(true);
mainScreenGUI.addText("Failed to disconnect. ??? :-(\n");
} else if (INTERNET_REAL_STATUS_CONNECTED.equals(commando)) {
mainScreenGUI.addText("Internet is connected\n");
} else if (INTERNET_REAL_STATUS_DISCONNECTED.equals(commando)) {
mainScreenGUI.addText("No internet connection\n");
} else if (commando.indexOf(RIC_LIST)!=-1) {
verwerkRicList(commando);
mainScreenGUI.addText("Received a user list\n");
} else if (commando.indexOf(RIC_NEW_USER)!=-1) {
addNewUserToList(commando);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?