⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 Remote Internet Connect简化了从其它运行操作系统的机子上将一个Linux系统连接到Internet的过程。用户可以用一个简单的GUI加快连接、断开连接、或查看谁在线上。
💻 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
import java.util.Vector;
import java.lang.* ;


public class Server {

	private Config config;
		
	private Vector connectionVector;
	private boolean internetState=false;
	private boolean testMode;
	
	
	private ServerExecuter serverExecuter;

	
	private Controller serverController;
    
    private EchoManager echoManager;
  
	public Server(Config config,boolean testMode) {
		this.config=config;		
		this.testMode=testMode;
		
		/* Used for the thread sync */
		/* In short only access to one method a the time */
		
		serverController = new Controller();
		
		/* Create the object that executes the command */
		/* The parameter real means if it should really execute, not fake it */
		serverExecuter = new ServerExecuter(config,testMode);

		/* Create the vector */
		connectionVector = new Vector();
		
		/* Create a instance of the EchoManager. He needs the vector with all the clients connections */
		echoManager = new EchoManager(connectionVector);
	};


	public void run() {
	    
	    
		ServerSocket servSock;
		
		try {
			try {
			    // Create a server socket to accept client connection requests
				servSock = new ServerSocket(config.getServerPort(),0,InetAddress.getByName(config.getServerIP())); 	    
			} 
			catch (UnknownHostException e) {
				System.out.println("Hostname doesn't resolve!");
				return;
			}
		}
		catch (IOException e) {
				System.out.println("Can not bind IP adress... Wrong ip?");
				return; //the end of this thread and the end of all
		}
			
		/* Give the signal go to the EchoManager */ 
	    echoManager.start();
		
		
		try {
		    while (true) { // for ever
		    	Socket clntSock = servSock.accept();     // Get client connection. IDLE POINT
			    
		      	Connection connection = new Connection(clntSock,this);
		      	connectionVector.add(connection); //putting this object in my vector :D
		      	
		      	System.out.println("New user connected. Total of connected users: "+connectionVector.size());	
		      	connection.start();  // it's a thread ;-)
	     	  	
	     	}
		} catch (IOException e) {
			System.out.println("System crash!!!! Server socket not nice :-(");
			e.printStackTrace();
		}  
		
		/* Stopping the echoManager */
		echoManager.interrupt();
	};
	
	public void makeConnection(Connection connection) {
		serverController.askFor();
		int teller;
		if (!internetState) {
			
			if (serverExecuter.make()) {
			    System.out.println("Making a real connection...");
				internetState=true;
				
				// Sending a message to all clients that a connection is made and by who
				for (teller=0;teller<connectionVector.size();teller++) {
						if (((Connection) connectionVector.get(teller)).getUserName() != null)
              	        	((Connection) connectionVector.get(teller)).sendConnectionBy(connection.getUserName());
				}
				connection.setConnected();
			} else { 
			        System.out.println("A real connection could NOT be made!!! Check the dial command");
                    connection.sendConnectFailed();
					// internetState is still false then
            }
		} else {
			// JOIN
			// Sending a message to all other clients that the connection is joined by ....
	
	    	for (teller=0;teller<connectionVector.size();teller++) {
	    		if (((Connection) connectionVector.get(teller)).getUserName() != null)
       	        	((Connection) connectionVector.get(teller)).sendConnectionJoined(connection.getUserName());
			}
			// there is all ready a connection!
			System.out.println("Saving money.. Another one is internet over the same connection");
			connection.setConnected();
		}
		serverController.relinquish();
	};
	
	public void hangupConnection(Connection connection) {
		serverController.askFor();
		int teller;
		boolean overalState=false;
		if (internetState) { // if no real connection do nothing :-)
			for (teller=0;teller<connectionVector.size() && !overalState;teller++) {
				if (connectionVector.get(teller) != connection)      //not the one who whants to disconnect
					if (((Connection) connectionVector.get(teller)).getPersonalState()) {
					//	System.out.println("Naam: " + ((Connection) connectionVector.get(teller)).getUserName() + " is nog online!");	
						overalState=true;  
						
						// It's not neccasary to put the check in of the user has a username
						// if has none he will not be connected.
					}
			}
			if (!overalState) {
				if (serverExecuter.hangup())	{
					System.out.println("The real connection has been disconnected! By:" + connection.getUserName());
					internetState=false;
					
					for (teller=0;teller<connectionVector.size();teller++) 
						if (((Connection) connectionVector.get(teller)).getUserName() != null)
					   		((Connection) connectionVector.get(teller)).sendDisconnectedBy(connection.getUserName());
					 
					connection.setDisconnected();
				} else { 
			     	System.out.println("What tha f*ck!? Can't disconnect... expensive :P");
					connection.sendDisconnectFailed();
			    } 
			} else {
				for (teller=0;teller<connectionVector.size();teller++) 
					if (((Connection) connectionVector.get(teller)).getUserName() != null)
				   		((Connection) connectionVector.get(teller)).sendInternetLeftBy(connection.getUserName());
				System.out.println("User: "+connection.getUserName() + " stopt internetting. Other user are connected! Not hangup the real connection");
				connection.setDisconnected();
			}
		} else System.out.println("Server: Somebody want's to disconnect but we ain't connected!");
		serverController.relinquish();
	};
	
	public boolean getInternetState() {
		serverController.askFor();
		
		int teller;
		
		if (internetState==true && serverExecuter.check()==false) {
			// connection lost!!! :-(
			// Sending a message to all other conneted clients that a connection is lost
			// and setting there connection status to NONE!
        	for (teller=0;teller<connectionVector.size();teller++) {
            	   if (((Connection) connectionVector.get(teller)).getPersonalState()) 
            	   		if (((Connection) connectionVector.get(teller)).getUserName() != null) {
            	   	    	((Connection) connectionVector.get(teller)).sendInternetConnectionLost();
                    	} else ((Connection) connectionVector.get(teller)).sendRealStatusDisconnected();
            }
            internetState=false; //setting the connection to false	   
            System.out.println("WARNING! Connection lost!! Time-out? to little idle time?");
		} else if (internetState==false && serverExecuter.check()==true) {
				System.out.println("HEY! I should make the connection around here, not somebody else!");
				internetState=true;
		        for (teller=0;teller<connectionVector.size();teller++) 
		        	if (((Connection) connectionVector.get(teller)).getUserName() != null) {
                     ((Connection) connectionVector.get(teller)).sendConnectionBy("UNKNOWN");
				}
				
			}
		boolean tempState; // needed for thread sync. The idea is that every thread who calls this methode have
							// there own tempState. No way that the interState was quickly overriden by a other one.
		tempState = internetState;
		
		serverController.relinquish();
		return tempState;
	};
	
	public void removeClient(Connection connection) {
		serverController.askFor();
		
		connectionVector.remove(connectionVector.indexOf(connection)); //this will make sure the garbage collector will
		 												 // collect lost connetions
		 												 
		serverController.relinquish();
  	};			   
  	
  	public void sendGoodBye(Connection connection) {
  		serverController.askFor();
  		int teller;	
		for (teller=0;teller<connectionVector.size();teller++) {
			if (connectionVector.get(teller) != connection)
				if (((Connection) connectionVector.get(teller)).getUserName() != null)
					((Connection) connectionVector.get(teller)).sendUserLeft(connection.getUserName());
		}	
		serverController.relinquish();
  	};		         				        
  	
	public void sendMessages(String titel,String messages,String commando) {
		serverController.askFor();
		// Perhaps here a filter wich connection wants this messages.
		// Some attributes in the connection would work great I guess.
		
		// for now send it to everybody
		int teller;
		for (teller=0;teller<connectionVector.size();teller++) 
			if (((Connection) connectionVector.get(teller)).getUserName() != null) {
	            ((Connection) connectionVector.get(teller)).sendMessages(titel,messages,commando);
		}
		serverController.relinquish();
	};
	
	public void sendNewUser(Connection connection) {
		serverController.askFor();
		int teller;
		for (teller=0;teller<connectionVector.size();teller++) {
			if (connectionVector.get(teller) != connection)     
				if (((Connection) connectionVector.get(teller)).getUserName() != null)
            		((Connection) connectionVector.get(teller)).sendNewUser(connection.getUserName());
		}
		serverController.relinquish();
	};
	
	public void sendMeUserList(Connection connection) {
		serverController.askFor();
		String RIC_LIST="";
		int teller;
		for (teller=0;teller<connectionVector.size();teller++) 
			if (((Connection) connectionVector.get(teller)).getUserName() != null) { //we also tell him about him self. The client expect us to
	            RIC_LIST+=((Connection) connectionVector.get(teller)).getUserName() + "=" + ((Connection) connectionVector.get(teller)).getPersonalState()+ "="+((Connection) connectionVector.get(teller)).getClientVersion()+ ";";
		}
		connection.sendList(RIC_LIST);
		serverController.relinquish();
	};
	
	public boolean checkUserName(String name) {
		serverController.askFor();
		boolean bestaat=false;
		int teller;
		for (teller=0;teller<connectionVector.size() && !bestaat;teller++) { 
				if (((Connection) connectionVector.get(teller)).getUserName() != null)
					if ((((Connection) connectionVector.get(teller)).getUserName()).equals(name)) 
	           			bestaat=true;
		}
		serverController.relinquish();
		return !bestaat;
	};
	
	public boolean testMode() {
		return testMode;	
	}
};

⌨️ 快捷键说明

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