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-2001. All Rights Reserved.
18       *
19       * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20       */
21  
22  package zeus.actors.intrays;
23  
24  import java.net.*;
25  import java.io.*;
26  import java.util.*;
27  import zeus.util.*;
28  import zeus.concepts.*;
29  import zeus.actors.*;
30  import zeus.agents.*;
31  import javax.naming.*;
32  import java.net.*;
33  import java.io.*;
34  import java.util.*;
35  import zeus.util.*;
36  import zeus.concepts.*;
37  
38  import javax.rmi.*;
39  import java.rmi.*;
40  import FIPA.*;
41  /***
42   * This is an extention of the Server class which provides an "InTray" service for HTTP
43   * transports. It will read messages from a http connection and will then call a the handle
44   * method in the FIPA_2000_Handler object that was used to init it.
45   * @author Simon Thompson
46   * @since 1.1
47   *
48   */
49  
50  public class FIPA_2000_HTTP_Server extends Server implements InTray {
51      
52      protected AgentContext context = null;
53      private Queue  msgQ = new Queue("fipaHTTP2000In");
54      private ZeusParser parser = new ZeusParser();
55      private FIPA_2000_Handler handler = null;
56      
57      private String host = null;
58      private String port = null;
59      private String name = null;
60      private File file = null;
61      private FileWriter log;
62      
63      public FIPA_2000_HTTP_Server(FIPA_2000_Handler handler, String host, String port,  String name, String threadId) {
64          this.handler = handler;
65          this.host = host;
66          this.port = port;
67          this.name = name;
68          try {
69              
70              
71              FIPA_2000_HTTP_Connection transport = new FIPA_2000_HTTP_Connection(host,port,name);
72              Thread http_messageThread = new Thread(transport);
73              //   http_messageThread.setPriority(Thread.MIN_PRIORITY);
74              http_messageThread.start();
75              transport.register(msgQ);
76          }
77          catch (Exception e) {
78              e.printStackTrace();
79          }
80          this.start();
81          this.setName(threadId);
82      }
83      
84      
85      /***
86       *   this is the method that gets stuff off the q and sends it to the handler for
87       *processing
88       */
89      public void run() {
90          processing = true;
91          System.out.println("Listening for FIPA 2000 HTTP on port " + String.valueOf(port));
92          
93          try {
94              file =  new File(SystemProps.getProperty("http_root") + SystemProps.getProperty("in_log"));
95              log = new FileWriter(file,true);
96              
97          }
98          catch (Exception e) {
99              e.printStackTrace();
100         }
101         while (processing) {
102             FIPAPerformative message = this.pop();
103             file.setLastModified(java.lang.System.currentTimeMillis());
104             try {
105                 debug(message.getContent());
106                 FIPA.FipaMessage fmess = message.FipaMessage();
107                 handler.handle(fmess);
108                 
109             }
110             catch (Exception e) {
111                 e.printStackTrace();
112             }
113             
114             yield();
115         }
116     }
117     
118     
119     public FIPAPerformative pop() {
120         FIPAPerformative perf =  (FIPAPerformative) msgQ.dequeue();
121         Date today = new Date();
122         int year = today.getYear();
123         int month = today.getMonth() +1;
124         try {
125             file.setLastModified(java.lang.System.currentTimeMillis());
126             log = new FileWriter(file,true); 
127             log.write("Message recieved at : " + today.getDate() +"/" + String.valueOf(month) +"/" + String.valueOf(year) + " at " + today.getHours() +":" +today.getMinutes() +":" + today.getSeconds()+"\n\n\n");
128             System.out.println("UPDATING FILE TIMESTAMP!!!!!!!");
129             log.write(perf.toFIPAString());
130             log.write("\n\n");
131             log.flush(); 
132             file.setLastModified(java.lang.System.currentTimeMillis());
133         }
134         catch (Exception e) {
135             e.printStackTrace();
136         }
137         return(perf);
138     }
139     
140     /***
141      * returns http://host:port/name
142      */
143     public String getResponseAddress() {
144         return new String("http://" + host + ":" + port + "/" + name);
145     }
146     
147     
148     /***
149      * main method for testing only - not for user applications
150      */
151     public static void main(String argv[]) {
152         FIPA_2000_HTTP_Server Server = new FIPA_2000_HTTP_Server(null,"127.0.0.1","8002","acc","test");
153     }
154     
155     
156     public void debug(String str) {
157         System.out.println("HTTP_Server>>" + str);
158     }
159     
160     
161 }