1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.actors;
25
26 import java.net.*;
27 import java.io.*;
28 import java.util.*;
29 import zeus.util.*;
30 import zeus.concepts.ZeusParser;
31 import zeus.concepts.Performative;
32 import zeus.concepts.PerformativeParser;
33
34
35 class Connection {
36 protected static int BUF_SIZ = 1000;
37 private static Integer token;
38
39 protected Socket client;
40 protected Server server;
41 protected BufferedInputStream ins;
42 String id = null;
43
44 public Connection () {
45 ;}
46
47
48
49
50 public Connection(Socket client, Server server, String id ) {
51 this.id = id;
52 if (token == null) token = new Integer(0);
53 this.client = client;
54 this.server = server;
55
56 try {
57 ins = new BufferedInputStream (client.getInputStream());
58 }
59 catch (IOException e) {
60 System.err.println("Exception while getting socket streams: " + e);
61 e.printStackTrace();
62 try {
63 client.close();
64 client = null;
65 }
66 catch (IOException e2) {
67 System.err.println("Exception while closing client: " + e2);
68 e2.printStackTrace(); }
69 return;
70 }
71 this.run();
72 }
73
74
75 public void run() {
76 boolean done = false;
77
78 while(!done) {
79 int x = 0;
80 int count = 1;
81 byte buf[] = new byte[BUF_SIZ];
82 String text = new String("");
83 while ( !done ) {
84 try {
85 Thread.yield();
86 x = ins.read(buf);
87
88 }
89 catch (IOException e) {
90 Core.DEBUG(1,"IOException: " + e);
91 e.printStackTrace();
92 server.updateCount(-1);
93 try {
94 ins.close();
95 ins = null;
96 client.close();
97 client = null;
98 }
99 catch (IOException e1) {
100 System.err.println(e1);
101 e1.printStackTrace();
102 }
103 return;
104 }
105
106 if ( x == -1 ) {
107 done = true;
108 debug ("x was -1 (done)");
109 }
110 else if (x <BUF_SIZ) {
111 done = true;
112 text += new String(buf,0,x);
113 debug (x + " bytes read (less than BUF_SIZ), " +
114 "assuming end of buffer.");
115 }
116 else {
117 text += new String(buf,0,x);
118 debug (x + " bytes read (BUF_SIZ), continuing.");
119 }
120 }
121 buf = null;
122 if ( text.equals("")) {
123 System.err.println("No data read from stream");
124 return;
125 }
126
127 try {
128 PerformativeParser parser = new PerformativeParser(new ByteArrayInputStream(text.getBytes()));
129
130 Performative msg = parser.Message();
131 debug ("trying test");
132 if ( !msg.isValid() ) {
133 debug ("not valid");
134 return;
135 }
136 debug("Sending message to mhandler");
137 server.newMsg(msg);
138 }
139 catch (Exception e) {
140 debug ("in exception handler");
141 return;
142 }
143 catch (Error e) {
144 e.printStackTrace();
145 debug ("in error handler");
146 return;
147 }
148 debug ("after try catch block");
149 }
150 try {
151 server = null;
152 if (ins != null) {
153 ins.close();
154 ins=null; }
155 if (client != null ) {
156 client.close();
157 client = null;
158 }
159 id = null;
160 }
161 catch (Exception e) {
162 e.printStackTrace();
163 }
164
165 }
166
167
168 /***
169 isEncrypted returns true if this message is encrypted.
170 */
171 public boolean isEncrypted (String text) {
172 if (text.startsWith("encrypted"))
173 return (true);
174 else
175 return false;
176 }
177
178 /***
179 decrypt returns the decrypted string which can then be converted into a performative and
180 passed to the inbox*/
181
182
183
184
185
186
187
188
189
190
191 protected void finalize() {
192 try {
193 if ( ins != null ) ins.close();
194 if ( client != null ) client.close();
195 }
196 catch (IOException e) {
197 }
198 }
199
200 public String toString() {
201 return " connected to: " + client.getInetAddress().getHostName() + ":" + client.getPort();
202 }
203
204
205 private void debug (String str) {
206
207 }
208
209
210 }
211