13 * 4 byte: msg length
\r
16 public class TCPW implements Runnable{
\r
17 private TCP $parent = null;
\r
19 private Socket $sock = null;
\r
20 private InputStream $in = null;
\r
21 private OutputStream $out = null;
\r
22 private String[] $usrInfo = null;
\r
25 public TCPW(Socket $s, TCP $t)throws Exception{
\r
29 $in = $sock.getInputStream();
\r
30 $out = $sock.getOutputStream();
\r
31 new Thread(this).start();
\r
36 while($sock != null){
\r
39 }catch(Exception $e){$e.printStackTrace();}
\r
40 $parent.delConn(this);
\r
43 public void send(String $msg){
\r
45 byte[] $len = Helper.int2ba($msg.length());
\r
48 $out.write($msg.getBytes());
\r
50 }catch(Exception $e){$e.printStackTrace();}
\r
53 public void receive()throws Exception{
\r
54 int $len = Helper.ba2int(Helper.readBA($in, 4));
\r
55 String $msg = new String(Helper.readBA($in, $len));
\r
56 String $data[] = $msg.trim().split("\\s+"); //regex: whitespace character once and more
\r
58 if($data[0].equalsIgnoreCase("!login")){
\r
59 String[] $udata = Main.$usr.get($data[1]);
\r
60 if($udata == null || $data.length <2){
\r
61 send("Supplied user is invalid!");
\r
64 TCPW $tmp = $parent.getConnByName($data[1]);
\r
66 send("User allready logged in!");
\r
67 $tmp.send($sock.getRemoteSocketAddress()+" tried to login as you!");
\r
70 if($data.length > 2 && $udata[1].equals($data[2])){
\r
72 java.util.Date now = new Date(System.currentTimeMillis());
\r
73 java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm");
\r
74 String $date = formatter.format(now);
\r
75 $usrInfo[2] = "Online since:"+$date;
\r
79 send("Wrong password?");
\r
82 send("Not logged in! Log in now! (!login user pass)");
\r
85 if($data[0].equalsIgnoreCase("!logout")){
\r
86 java.util.Date now = new Date(System.currentTimeMillis());
\r
87 java.text.DateFormat formatter = new java.text.SimpleDateFormat("dd.MM.yyyy HH:mm");
\r
88 String $date = formatter.format(now);
\r
89 $usrInfo[2] = "Offline since:"+$date;
\r
91 send("logged out!");
\r
93 if($data[0].equalsIgnoreCase("!send")){
\r
94 $parent.sendAll($usrInfo[0]+" said: "+$msg.substring(5).trim());
\r
96 if($data[0].equalsIgnoreCase("!msg")){
\r
97 String[] $udata = Main.$usr.get($data[1]);
\r
98 if($udata == null || $data.length <2){
\r
99 send("Supplied user is invalid!");
\r
102 TCPW $tmp = $parent.getConnByName($data[1]);
\r
104 send("User offline");
\r
107 if($tmp != null && $tmp.isLoggedIn())$tmp.send($usrInfo[0]+" whispers: "+$msg.substring(5+$data[1].length()).trim());
\r
108 send("privmsg sent!");
\r
112 public boolean isLoggedIn(){
\r
113 if($usrInfo != null) return true;
\r
116 public String getName(){
\r
117 if($usrInfo != null)return $usrInfo[0];
\r
121 public void shutdown(){
\r
123 send("SERVER shutting down...");
\r
124 send("SERVER please shut down your client by yourself!");
\r
129 }catch(Exception $e){$e.printStackTrace();}
\r