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/package-summary.html">ong> zeus.actors.outtrays;
22
23 import java.io.*;
24 import java.net.*;
25 import java.util.*;
26
27 /***
28 FIPA_2000_HTTP_Accessor is used to carry the information for a http connection
29 from the TransportFactory to the actuall FIPA_2000_HTTP_Transport
30 @author Simon Thompson
31 @since 1.1
32 */
33 public class FIPA_2000_HTTP_Accessor {
34
35
36 private String host = null;
37 private String port = null;
38 private String separator = "/";
39 private String name = null;
40
41
42 public FIPA_2000_HTTP_Accessor (String address) {
43 debug (address);
44 String interesting = address.substring(7);
45 StringTokenizer tokens = new StringTokenizer(interesting,":");
46 host = tokens.nextToken();
47 StringTokenizer nextTokens = new StringTokenizer(tokens.nextToken(),"/");
48 try {
49 port = nextTokens.nextToken();}
50 catch (Exception e) {
51 port = new String ("80");
52 }
53 try {
54 name = nextTokens.nextToken();
55 name = new String ();
56 }
57 catch(Exception e) {
58 }
59 }
60
61
62 /***
63 use this to set the name of the entity to be contacted
64 (usually acc/df/ams) note that if there is a name space then
65 include it here (ie. if the address is http://www.bt.com:80/agents/agentcities/adastral/acc
66 then the parameter entityName should be agents/a
67 */
68 public void setName (String entityName) {
69 this.name = entityName;
70 }
71
72
73 /***
74 guess what this does?
75 */
76 public String getName () {
77 return this.name;
78 }
79
80
81
82 /***
83 set the host that we are contacting : www.bt.com
84 */
85 public void setHost (String hostName) {
86 this.host = hostName;
87 }
88
89
90 /***
91 set the port numnber : 80 for instance
92 */
93 public void setPort (String portNumber) {
94 this.port = portNumber;
95 }
96
97
98 /***
99 get the port number
100 */
101 public String getPort() {
102 return port;
103 }
104
105
106 /***
107 get the host
108 */
109 public String getHost () {
110 return host;
111 }
112
113
114 /***
115 set the port numnber : 80 for instance
116 */
117 public void setPort (int port ) {
118 this.port = String.valueOf(port);
119 }
120
121
122 public String getAddress () {
123 return "http://" + host + ":" + port + separator + name;
124 }
125
126
127
128 /***
129 open a socket to the receiving agent and send it the message
130 */
131 public boolean send (String message) {
132 try {
133 Socket socket = new Socket(host, Integer.parseInt(port));
134
135 PrintWriter out = new PrintWriter(socket.getOutputStream(), true );
136 InputStreamReader ins = new InputStreamReader (socket.getInputStream());
137 BufferedReader in = new BufferedReader(ins);
138 out.println(message);
139 out.flush();
140 String current = new String();
141 while (in.ready()) {
142 current += in.readLine();
143 current += "\n";
144 }
145 out.close();
146 in.close();
147 if (current.startsWith("HTTP:/1.1 200 OK")) {
148 return true;
149 }
150 else
151 {
152 return false;
153 }
154 } catch (Exception e) {
155 e.printStackTrace();
156 System.out.println("Failed to open HTTP connection as expected, message *not* sent");
157 System.out.println("Message was : " + message);
158 System.out.println("Not fatal, attempting to continue...");
159 return false;
160 }
161
162 }
163
164 private void debug(String s){
165 System.out.println("http_accessor >>" + s);
166 }
167
168 }