1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package zeus.actors.outtrays;
22
23 import zeus.actors.intrays.*;
24 import javax.agent.service.*;
25 import javax.agent.*;
26 import zeus.actors.*;
27 import zeus.util.*;
28 import java.net.*;
29 import java.io.*;
30 import java.util.Date;
31
32 import FIPA.*;
33
34 /***
35 * FIPA_2000_HTTP_Transport is an OutTray that is used to send a
36 * message to a FIPA http agent.
37 * @author Simon Thompson
38 * @since 1.1
39 */
40 public class FIPA_2000_HTTP_Transport implements OutTray {
41
42
43 private FIPA_2000_HTTP_Accessor target = null;
44 private FileWriter log = null;
45 private File file;
46
47 protected int BUFFER_SIZE = 1024;
48
49 /***
50 * simple constructor that registers where this OutTray is looking to
51 * send messages to.
52 */
53 public FIPA_2000_HTTP_Transport(FIPA_2000_HTTP_Accessor target,File file) {
54 this.target = target;
55 this.file = file;
56 try {
57 this.log = new FileWriter(file,true); }
58 catch (Exception e) {
59 e.printStackTrace();
60 }
61
62 }
63
64
65 public void send (Object obj) throws UnsuitableMessageException {
66 try {
67 javax.agent.Envelope env = (javax.agent.Envelope) obj;
68 send(env);
69 } catch (ClassCastException cce) {
70 throw new UnsuitableMessageException("Must be javax.agent.envelope to work with this transport");
71 }catch (Exception e) {
72 e.printStackTrace();
73 throw new UnsuitableMessageException("Bad message in send() - unknown problem, Excepiton printed to sout");
74
75 }
76 }
77
78 /***
79 * use this message to send an Envelope containing a FIPA_Performative to
80 * an FIPA 2000 HTTP complient interface..
81 */
82 public void send(javax.agent.Envelope envelope) {
83 try {
84 zeus.concepts.FIPAPerformative fperf = (zeus.concepts.FIPAPerformative) envelope.getObject();
85 String sendString = new String();
86 sendString += "This is not part of the MIME multipart encoded message.";
87 sendString += "\015\012--251D738450A171593A1583EB";
88 sendString += "\015\012Content-Type: application/xml\015\012\015\012";
89
90 sendString += "<?xml version=\"1.0\"?>\n";
91 sendString += "\t<envelope>\n";
92 sendString += "\t\t<params index=\"1\">\n";
93 sendString += "\t\t<to>\n";
94 sendString += fperf.getReceiversXML();
95 sendString += "\t\t</to>\n";
96 sendString += "\t\t<from> \n" ;
97 sendString += fperf.getSenderXML();
98 sendString += "\t\t</from>\n\n";
99 sendString += "\t\t<intended-receiver>\n" + fperf.getReceiversXML() +"\n";
100 sendString += "\t\t</intended-receiver>\n";
101
102 sendString += "<acl-representation>fipa.acl.rep.string.std</acl-representation>\n\n";
103 sendString += "<payload-encoding>US-ASCII</payload-encoding>\n\n";
104 sendString += "<date>" + FIPA_Date.getDate() + "</date>\n\n";
105 sendString += "</params>\n";
106
107 sendString += "</envelope>\n\n";
108 sendString += "\015\012--251D738450A171593A1583EB\015\012";
109 sendString += "Content-Type: application/text\015\012\015\012";
110 sendString += fperf.toFIPAString();
111 sendString += "\015\012--251D738450A171593A1583EB--\n";
112
113
114 sendString += "\r\n\015\012";
115
116
117 try {
118
119 URL url=new URL(target.getAddress());
120 String host = url.getHost();
121 int port = url.getPort();
122 Socket sock = new Socket(host,port);
123 BufferedOutputStream stream = new BufferedOutputStream(sock.getOutputStream(),BUFFER_SIZE);
124 BufferedInputStream is=new BufferedInputStream(sock.getInputStream(),BUFFER_SIZE);
125 String header = new String("POST http://" + host+":"+port+"/ACC HTTP/1.1\nCache-Control: no-cache\nHost: " +host+":"+String.valueOf(port)+"\nMime-Version: 1.0\nContent-type: multipart/mixed; \n\tboundary=\"251D738450A171593A1583EB\""+"\nContent-length: "+String.valueOf(sendString.length())+"\nConnection: close\n\n");
126 String sendMessage = new String(header + sendString);
127
128
129 PrintWriter os=new PrintWriter(stream);
130 os.print(sendMessage);
131 os.flush();
132 System.out.println(" flushed out buffer");
133 Date today = new Date();
134 int month = today.getMonth() + 1;
135 int year = today.getYear();
136 String all = new String("Sent message at " + today.getDate() +"/" + String.valueOf(month) + "/" + String.valueOf(year) + " at " + today.getHours() +":" +today.getMinutes() +":" + today.getSeconds() + "\n");
137 log = new FileWriter(file,true);
138 log.write(all);
139 log.write(sendMessage);
140 log.write("\n\n");
141 log.flush();
142 log.close();
143 file.setLastModified(System.currentTimeMillis());
144
145 debug(sendMessage);
146
147
148
149 ReplyListener rl = new ReplyListener(is,os,file);
150 Thread tr = new Thread(rl);
151 tr.start();
152
153
154
155 }
156 catch (Exception e) {
157 e.printStackTrace();
158 }
159 } catch (Exception e) {
160 e.printStackTrace();
161
162 return;
163 }
164
165
166 }
167
168
169
170
171 /***
172 * debug was used to see what was going on when we built this
173 */
174 private void debug(String message){
175
176 }
177
178 }