Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/rakiura/mbot/EConnectionThread.java


1   
2   package org.rakiura.mbot;
3   
4   /**/
5   import java.net.*;
6   import java.io.*;
7   import java.util.*;
8   
9   /**
10   * Represents Extended connection over TCP/IP.
11   * <br><br>
12   * ConnectionThread.java<br><br>
13   * Created: Thu Jul 15 12:45:00 1999
14   *
15   *@author Mariusz Nowostawski
16   *@version 0.1 $Revision: 1.1.1.1 $
17   */
18  public class EConnectionThread extends BasicConnection {
19  
20    private Engine engine = null;
21    private String password;
22  
23    /**/
24    public EConnectionThread(Socket sock, String pass, Engine engine) {
25      super();
26      this.socket = sock;
27      this.engine = engine;
28      this.password = pass;
29    }
30  
31    /**/
32    public void run(){
33      try{
34        out = new ObjectOutputStream(socket.getOutputStream());
35        in = new ObjectInputStream(socket.getInputStream());
36        System.out.print("Telnet connection from "+socket.getInetAddress().getHostName()
37                         +" ("+socket.getInetAddress().getHostAddress()+").....");
38        out.writeObject("wait....");
39        out.flush();
40        try{
41          hostNick = (String)in.readObject();
42          hostPort = ((Integer)in.readObject()).intValue();
43          hostName = socket.getInetAddress().getHostName();
44          String pass = (String)in.readObject();
45          if(!pass.equals(password)){
46            out.writeObject("connectionEnd");
47            out.close();
48            in.close();
49            socket.close();
50            return;
51          }
52          System.out.println(" accepted.");
53          out.writeObject("commands:");
54          out.flush();
55  
56          String line = (String)in.readObject();
57          while(line!= null && !line.startsWith(".quit")){    
58            // action notification
59            if(line.startsWith("=action ")){
60              String action = line.substring(8);
61              engine.actionsSet.add(action);
62            }else
63              // userlist request
64              if(line.startsWith(".give_userlist")){
65                engine.writeUsersObject(out);
66              }else
67                if(line.startsWith("=update_userlist")){
68                  synchronized(this){
69                    out.writeObject(".give_userlist");
70                    out.flush();
71                    engine.readUsersObject(in);
72                  }
73                }else
74                  // hostlist request
75                  if(line.startsWith(".give_hostlist")){
76                    synchronized(this){
77                      Enumeration enum = engine.etcpServer.getAllConnectionsExceptMe(this);
78                      if(enum!=null)
79                        while(enum.hasMoreElements()){
80                          BasicConnection b = (BasicConnection)enum.nextElement();
81                          out.writeObject(b.getHostName());
82                          out.writeObject(new Integer(b.getHostPort()));
83                        }
84                      out.writeObject("=endOfHostList");
85                    }
86                  }else
87                    if(line.startsWith("=update_hostlist")){
88                      synchronized(this){
89                        out.writeObject(".give_hostlist");
90                        out.flush();
91                        String o = (String)in.readObject();
92                        while(!o.startsWith("=endOfHostList")){
93                          String ohost = o;
94                          Integer oport = (Integer)in.readObject();
95                          engine.etcpServer.connectTo(ohost, oport);
96                          o = (String)in.readObject();
97                        }
98                        System.out.println("User data uploaded.");
99                      }
100                   }else
101                     // .ex command
102                     if(line.startsWith(".ex ")){
103                       String action = line.substring(4);
104                       engine.writeLine(action);
105                     }
106           //    
107           line = (String)in.readObject();
108         }
109       }catch(Exception e) {
110         e.printStackTrace();
111       }
112       System.out.println("REMOTE connection closed.");
113       engine.etcpServer.removeSlave(this);
114 
115       out.close();
116       in.close();
117       socket.close();
118     } catch (IOException e) {
119       System.out.println("Couldn't get I/O for the connection to server");
120       return;
121     } 
122   }
123     
124 } // EConnectionThread
125 //////////////////// end of file ////////////////////