📄 serverconnection.java
字号:
package Howdy;/* This file is part of Howdy. Howdy 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. Howdy 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 Foobar; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/import java.io.*;import java.net.*;import java.util.*;public class ServerConnection implements Runnable // Thread that manages individual connections.{ Server server; Socket socket; BufferedReader in; PrintWriter out; String alias; boolean done; String input; String preAlias; String whisperRecipient; Date date; long loginTime; String waitingForInfoAlias; // Person who's requested the info. THIS VAR IS SET FROM ACTIONS BY A DIFFERENT // CLIENT! NOT THIS ONE UNLESS THIS USER REQUESTED HIS OWN INFO! long lagPingStart; public ServerConnection(Server server, Socket socket) { this.server = server; this.socket = socket; alias = "NONAME"; done = false; Date date = new Date(); loginTime = date.getTime(); } public void run() // Deals with input and closing the socket. { try { in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) ); out = new PrintWriter(socket.getOutputStream(), true); out.println("VERSION: " + server.version); //out.println("MSG: {SERVER}==> You've succesfully connected to this Howdy Server. Welcome!"); out.println( "MSG: {SERVER}==> " + server.getUptime() ); while(done == false) { input = in.readLine(); if(input == null) done = true; else { if( input.startsWith("alias: ") ) { setAlias( input.substring(7).toLowerCase() ); } else if(alias == null || alias.equals("NONAME") ) { out.println("MSG: {SERVER}==> Set your alias first!"); date = new Date(); if(date.getTime() - loginTime >= 5000) { out.println("MSG: {SERVER}==> Time's up, slowpoke!"); done = true; } } else if( input.equals("ping") ) pong(); else if( input.equals("PONG") ) server.shootTheTarget(waitingForInfoAlias, alias); else if( input.startsWith("msg: ") ) { if(server.sendMessage( alias, input.substring(5) ) == false) out.println("MSG: {SERVER}==> A server error occured while sending your message. This may or may not be a major problem and people might have recived it."); } else if( input.startsWith("whisper: ") ) whisper( input.substring(9) ); else if( input.startsWith("getinfo: ") ) server.primeTheHitman( alias, input.substring(9) ); else out.println("MSG: {SERVER}==> Unrecognized command: " + input); } } socket.close(); server.connections.remove(this); server.alertLogout(alias); server.distributeChattersList(); } catch(Exception e) {e.printStackTrace();} } void pong() { out.println("PONG"); } void setAlias(String input) { if(input.length() > 20 || input.length() < 1) out.println("MSG: {SERVER}==> Alias is either too long or too short!"); else { preAlias = input; if( server.loginPass(alias, preAlias) == false) out.println("MSG: {SERVER}==> That alias is not available!"); else { server.renamedAlias( alias, preAlias, socket.getInetAddress() ); alias = preAlias; server.distributeChattersList(); } } } void whisper(String input) { if( input.startsWith("recipient: ") ) whisperRecipient = input.substring(11); else if( input.startsWith("msg: ") ) server.sendWhisper( alias, whisperRecipient, input.substring(5) ); }}// Copyright (C) 2003-2004 Serban Giuroiu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -