networkcommunicator.java

来自「Remote Internet Connect简化了从其它运行操作系统的机子上将」· Java 代码 · 共 596 行 · 第 1/2 页

JAVA
596
字号
				} else if (commando.indexOf(RIC_USER_LEFT)!=-1) {
					naam=commando.substring(14,commando.length()-2);  //ripping of the \r\n and the first word
					removeFromRicList(naam);
					mainScreenGUI.addText("User "+naam+" left\n");
				} else if (RIC_PING.equals(commando)) {
					sendRicPong();
				} else if (RIC_BYEBYE.equals(commando)) {
					mainScreenGUI.addText("Server tells us to leave\n");
					doorGaan=false;
				} else {
					System.out.println("Server made a booboo:"+ commando);
			 	}
			 }
		} catch (IOException e)	{
    	
	    	System.out.println("Connection lost!");

        	//make sure that the user see this
        	//and enable the reconnect button
        	mainScreenGUI.addText("Connecting to server LOST!!!\n");
        	mainScreenGUI.setScreenState(1);
        	
        	// clear the user list...
        	ricClients.clear();
			mainScreenGUI.updateUserList(ricClients);
        	
    	
    	    /* Setting the systray to gray */
		    systray.offline();
		    
		    return true;
		}
		return false; // We need to leave

	};
	
	public boolean login() {
        mainScreenGUI.addText("Sending login information...\n");
		try {			
				out.write((USERNAME+"\r\n").getBytes());
				//out.write(("Harry\r\n").getBytes());
				out.write((PASSWORD+"\r\n").getBytes());
				
				String commando=readNetwork(in);
				
				while(!("LOGIN OK\r\n").equals(commando)) {
					mainScreenGUI.addText(commando);
					javax.swing.JOptionPane.showMessageDialog(null,commando);
					commando=readNetwork(in);
				}	
				mainScreenGUI.addText("Login succeeded\n");	
				return true;	
				
		 	}
	 	catch (IOException e) {
      		System.out.println("Connection problem. While authenticating...");
      		mainScreenGUI.addText("Connection problem while authenticating..\n");
      		return false;
        }	
	};
	
	public void setup() {
		
		ricClients = new Vector();
		
		mainScreenGUI = new MainScreenGUI(this);
		
		// Giving MainScreenGUI referentie so that
		// in nows how to place the window next to main
		// The config so it can read/write setting.
		settingsScreen = new SettingsScreen(config,mainScreenGUI);
	
		// systray needs the config for the colors. 
		// A mainScreen to popup. 
		systray = new Systray(mainScreenGUI,config);
		
		mainScreenGUI.setup();
		settingsScreen.setup();
		systray.setup();
	};
	
	public void settingsGUI() {
		settingsScreen.GUI();
	};
	
	public void sendRicPong() {
		try {
			out.write(RIC_PONG.getBytes());
			System.out.println("Sending: PONG! ;-)");
		} catch (IOException e) {
			System.out.println("Can't send pong back to server... not good at all.");	
		}
		
	}
	
	private void verwerkRicList(String commando) {
		String[] aantalVelden; 	
		int overallOnline=0;
		
		/* We got a new list, so we don't want any old information */
		/* Perhaps the server send a new one */
		ricClients.clear();
		
		commando=commando.substring(9,commando.length()-2);  //ripping of RIC_LIST and the \r\n from commando
		aantalVelden = commando.split(";");
		
		
		for(int teller=0;teller<aantalVelden.length;teller++) {
			String[] velden;
			velden = (aantalVelden[teller]).split("=");
			
			/* the older versions <0.91 don't send a version number */
			if (velden.length>2) 
				ricClients.add(new RicUser(velden[0],Double.parseDouble(velden[2]),stringToBool(velden[1])));
			else ricClients.add(new RicUser(velden[0],0,stringToBool(velden[1])));
			
			/* Checking if somebody is online */
			if (stringToBool(velden[1])) 
				overallOnline++;
		}
				
		// RIC list verwerkt. Now update systray and update mainscreen.
		mainScreenGUI.updateUserList(ricClients);
		
		
		/* UPDATE SYSTRAY */ 
		systray.update(false,overallOnline);
	};
	
	private void addUserToRicList(String name,double version) {
		/* very simpel 
		 * when a user comes online its state = false */
		ricClients.add(new RicUser(name,version,false));
		
		// RIC list updated. Now update the mainscreen.
		mainScreenGUI.updateUserList(ricClients);
		systray.update(personalState,aantalOnline);
	}
	
	
	
	private void updateRicList(String name, boolean state) {
		boolean gevonden=false;
		int teller;
		aantalOnline=0;
	
		for (teller=0;teller<ricClients.size();teller++) {
			if ((((RicUser) ricClients.get(teller)).name).equals(name)) {
			 	 	((RicUser) ricClients.get(teller)).state=state;
			 }
			 // Count how many online
			 if ((((RicUser) ricClients.get(teller)).state)) 
			 	aantalOnline++;
		}

		// RIC list updated. Now update the mainscreen.
		mainScreenGUI.updateUserList(ricClients);
		systray.update(personalState,aantalOnline);
	};
	
	private void removeFromRicList(String name) {
		for (int teller=0;teller<ricClients.size();teller++) {
			if ((((RicUser) ricClients.get(teller)).name).equals(name)) {
		 	 	ricClients.remove(teller);
		 	}
		
		}
		
		/* UPDATE mainscreenGUI*/
		/* systray doesn't a update, the user will disconnect first ofcourse */
		mainScreenGUI.updateUserList(ricClients);
	};
	
	private void lostConnection() {
		// when a connection is lost all user will be set offline	
		mainScreenGUI.setStoplicht(false);
					personalState=false;
		for (int teller=0;teller<ricClients.size();teller++) {
		 	((RicUser) ricClients.get(teller)).state=false;
		 }
		 
		/* update the list and the systray, nobody is online */
		systray.update(false,0);
		mainScreenGUI.updateUserList(ricClients);
	}
	
	private void messagesV1(String commando) {
		String[] aantalVelden; 	
		
		aantalVelden = commando.split(";");
		
		/* the parameter means with detail screen open */
		mainScreenGUI.popUp(true);
		mainScreenGUI.addText(aantalVelden[1]+": "+aantalVelden[2]+"\n");
		
		/* version lower than 0.92 do not use this field */
		if (aantalVelden.length>2) {
			String[] command = aantalVelden[3].split("=");
			if (command.length>1) 
				if ("PLAY".equals(command[0])) 
					if (config.getSoundEnabled())
						if (!Sound.play(command[1]))
							mainScreenGUI.addText("Problem while playing audio file\n");
		}
	};
	

	
	private boolean checkVersion() {
		/* This methode is checks the version between server and client 
		 * The server may send some information which we will present on the screen. 
		 * The information from server version 0.92> will contain a boolean value, if it must
		 * be shown on the screen. If its true the user will always see it. 
		 * if it false en the user configured ignore messages it will not be shown */
		 
		boolean ignoreSupported;
		boolean userIgnore=!config.getIgnoreMessages();
		boolean returnWaarde=false;
		try {
			String serverVersion = (readNetwork(in)).substring(6);
			serverVersion=serverVersion.substring(0,(serverVersion.length()-2));
        	System.out.println("Server version is: " +serverVersion);
			mainScreenGUI.addText("Server Version: "+serverVersion+"\n");
			   
    	    versionNumber = Double.parseDouble(serverVersion.substring(5));
	        System.out.println("Het versie number bedraagt: "+versionNumber);
			 
			/* Send our version */
			out.write(("HALLO RIC2-" + RIC.VERSION + "\r\n").getBytes());
			
			/* Does the server support ignore messages? 
			 * also inform the user about the old server version */
		    if (versionNumber<=0.91) {
        	     ignoreSupported=false;
        	     if (userIgnore)  
					javax.swing.JOptionPane.showMessageDialog(null,"Update for your RIC server available! Updated recommended");
        	} else ignoreSupported=true;
			
			/* read the connection until we receive PLEASE LOGIN*/	
			String commando=readNetwork(in);

			while(!("PLEASE LOGIN\r\n").equals(commando)) {
				if (ignoreSupported) { 
					String messages = commando.substring(2);
					mainScreenGUI.addText(messages);
					if (("1".indexOf(commando.substring(0,1))!=-1) || userIgnore)  
							javax.swing.JOptionPane.showMessageDialog(null,messages);
				} else {
					/* for the older versions always show the messages */
					mainScreenGUI.addText(commando);
					javax.swing.JOptionPane.showMessageDialog(null,commando);
				}
				commando=readNetwork(in); /* Read the next messages from the server */
			}	
			returnWaarde=true;
		} catch (IOException e) {
			mainScreenGUI.addText("Version check failed..\n");
			returnWaarde=false;
		}
		return returnWaarde;
	};
	
	private String readNetwork(InputStream in) throws IOException {
		StringBuffer stringBuf = new StringBuffer();
		int byteRead;

		do {	
		 	if ((byteRead = in.read()) == -1) 
				throw new IOException("Connection close prematurely"); 
			else stringBuf.append((char)byteRead);
		} while (byteRead != 10);  // so long as byteRead doesn't match endofline
		
		System.out.println("RECEVIED: "+ stringBuf.toString());
		return  stringBuf.toString();
	};

	private boolean stringToBool(String string) {
		if (string.equals("true"))
			return true;
		else return false;	
	};
	
	private void addNewUserToList(String commando) {
		/* If the server version is higher than 0.91 it will also send the version number 
		 * from the client which came online. */
		 
		String[] stukjes = commando.substring(13,commando.length()-2).split("=");
		double versie=0;
		String naam=stukjes[0];

		if (stukjes.length>1) 
			versie=Double.parseDouble(stukjes[1]);	
		
		addUserToRicList(naam,versie);
		mainScreenGUI.addText("New user:"+naam+"\n");
	}
};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?