Source code: org/rakiura/mbot/EPassConnectionThread.java
1
2 package org.rakiura.mbot;
3
4 /**/
5 import java.net.*;
6 import java.io.*;
7 import java.util.*;
8
9 /**
10 * EPassConnectionThread.java<br><br>
11 * Created: Thu Jul 15 12:38:56 1999
12 *
13 *@author Mariusz Nowostawski
14 *@version 0.3 $Revision: 1.1.1.1 $
15 */
16 public class EPassConnectionThread extends BasicConnection {
17
18 private boolean listening;
19 private Engine engine;
20 private boolean secondaryMaster;
21
22 /**/
23 public EPassConnectionThread(Engine engine) {
24 super();
25 hostPort = engine.MasterPort;
26 hostName = engine.MasterHost;
27 this.engine = engine;
28 listening = true;
29 secondaryMaster = false;
30 }
31 /**/
32 public EPassConnectionThread(String host, Integer port, Engine engine) {
33 super();
34 this.hostPort = port.intValue();
35 this.hostName = host;
36 this.engine = engine;
37 listening = true;
38 secondaryMaster = false;
39 }
40
41 /**/
42 public void setSecondaryMaster(){ secondaryMaster = true; }
43
44 /**/
45 public void run() {
46 try {
47 socket = new Socket(hostName, hostPort);
48 out = new ObjectOutputStream(socket.getOutputStream());
49 in = new ObjectInputStream(socket.getInputStream());
50 System.out.print("Connecting to master "+hostName+":"+hostPort+" ...... ");
51
52 /// loging into the IRC server
53 String line = (String)in.readObject();//read welcome
54 out.writeObject(engine.getNick());//write nick
55 out.writeObject(new Integer(engine.EDCCport));//write port
56 out.writeObject(engine.EDCCpass);//write pass
57 out.flush();
58 line = (String)in.readObject();//read confirmation
59
60 System.out.println(" ok!");
61
62 if(!secondaryMaster){
63 synchronized(this){
64 out.writeObject(".give_userlist");
65 out.flush();
66 engine.readUsersObject(in);
67 out.writeObject(".give_hostlist");
68 out.flush();
69 String o = (String)in.readObject();
70 while(!o.startsWith("=endOfHostList")){
71 String ohost = o;
72 Integer oport = (Integer)in.readObject();
73 engine.etcpServer.connectTo(ohost, oport);
74 o = (String)in.readObject();
75 }
76 System.out.println("Bots data uploaded.");
77 }
78 }
79 while(line != null){
80 line = (String)in.readObject();//read request
81
82 // userlist request
83 if(line.startsWith(".give_userlist")){
84 engine.writeUsersObject(out);
85 }else
86 // userlist update needed
87 if(line.startsWith("=update_userlist")){
88 synchronized(this){
89 out.writeObject(".give_userlist");
90 out.flush();
91 engine.readUsersObject(in);
92 }
93 }else
94 //
95 if(line.startsWith("=action ")){
96 engine.actionsSet.add(line.substring(8));
97 }else
98 // hostlist request
99 if(line.startsWith(".give_hostlist")){
100 synchronized(this){
101 Enumeration enum = engine.etcpServer.getAllConnectionsExceptMe(this);
102 if(enum!=null)
103 while(enum.hasMoreElements()){
104 BasicConnection b = (BasicConnection)enum.nextElement();
105 out.writeObject(b.getHostName());
106 out.writeObject(new Integer(b.getHostPort()));
107 }
108 out.writeObject("=endOfHostList");
109 }
110
111 System.out.println("Bot hosts/ports uploaded.");
112 }else
113 if(line.startsWith("=update_hostlist")){
114 synchronized(this){
115 out.writeObject(".give_hostlist");
116 out.flush();
117 String o = (String)in.readObject();
118 while(!o.startsWith("=endOfHostList")){
119 String ohost = o;
120 Integer oport = (Integer)in.readObject();
121 engine.etcpServer.connectTo(ohost, oport);
122 o = (String)in.readObject();
123 }
124 System.out.println("Bots data uploaded.");
125 }
126 }else
127 // .ex command
128 if(line.startsWith(".ex ")){
129 String action = line.substring(4);
130 engine.writeLine(action);
131 }
132 }
133
134 engine.etcpServer.removeMaster(this);
135 engine.switchToMode(1);
136 System.out.println("Switching to mode 1... closed connection to master.");
137 } catch (UnknownHostException e) {
138 System.out.println("Don't know about hostname of another eMbot.. check configuration file.");
139 e.printStackTrace();
140 engine.etcpServer.removeMaster(this);
141 engine.switchToMode(1);
142 return;
143 } catch (IOException e) {
144 System.out.println("I/O problem during the connection to another eMbot.... connection closed.\nThe other eMbot is going down, lost TCP handle.\nI am switching to Master Mode.");
145 // e.printStackTrace();
146 System.out.println(e.getMessage());
147 engine.etcpServer.removeMaster(this);
148 engine.switchToMode(1);
149 return;
150 } catch (Exception e) {
151 System.out.println("Couldn't connect for some reasons to another eMbot...");
152 engine.etcpServer.removeMaster(this);
153 engine.switchToMode(1);
154 return;
155 }
156 }
157
158 public void setListening(boolean b){ listening = b; }
159
160 } // EPassConnectionThread
161 //////////////////// end of file ////////////////////