View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
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 {// extends Thread{
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    // meaningless init  to allow rearch
44    public Connection () {
45    ;}
46    
47  
48  
49    // Initialize the streams and start the thread
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(); //Avoids starving socket buffer
86  	            x = ins.read(buf);
87  	           // System.out.println(x); 
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      //text = null;
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  /* public String decrypt (String text) { 
182         try {
183             String mess = text.substring (9); 
184             String keyId = text.substring (0,10); 
185             byte[] 
186             if (server.getKey(keyId)) != null 
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     //System.out.println("Connection>> " + str);
207   }
208        
209   
210   }
211