1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package zeus.actors.intrays;
27
28 import java.net.*;
29 import java.io.*;
30 import java.util.*;
31 import zeus.util.*;
32 import zeus.concepts.*;
33 import zeus.actors.*;
34 import zeus.agents.*;
35 import javax.naming.*;
36 import java.net.*;
37 import java.io.*;
38 import java.util.*;
39 import zeus.util.*;
40 import zeus.concepts.*;
41 import org.omg.CosNaming.*;
42 import org.omg.CORBA.*;
43
44 import javax.rmi.*;
45 import java.rmi.*;
46 import fipa97.FIPA_Agent_97;
47 /***
48 This is an extention of the Server class which is normally used in Zeus
49 Instead of providing a sockets/TCPIP based transport this class provides a
50 IIOP based transport as per the FIPA specs<p>
51 Perhaps it should be called IIOP_Server?
52
53 */
54
55 public class FIPA_97_Server extends Server implements InTray {
56
57 protected AgentContext context = null;
58 private Queue msgQ = new Queue("fipa97In");
59 private ZeusParser parser = new ZeusParser();
60 private String host = null;
61 private String port = null;
62 private String name = null;
63
64 private FIPA_97_Handler handler = null;
65
66
67 public FIPA_97_Server(FIPA_97_Handler handler, String host, String port, String name, String threadId) {
68 Hashtable env = new Hashtable();
69 this.handler = handler;
70 this.host = host;
71 this.port = port;
72 this.name = name;
73 env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
74
75 try {
76 Properties props = new Properties();
77 props.put("org.omg.CORBA.ORBInitialPort", port);
78 org.omg.CORBA.ORB myORB = org.omg.CORBA.ORB.init(new String[0], props);
79 FIPA_Agent_97 fipa = new FIPA_97_Connection();
80 Thread corba_messageThread = new Thread((FIPA_97_Connection)fipa);
81 corba_messageThread.setPriority(Thread.MIN_PRIORITY);
82 corba_messageThread.start();
83 ((FIPA_97_Connection)fipa).register(msgQ);
84
85
86 NamingContext context = NamingContextHelper.narrow(myORB.resolve_initial_references("NameService"));
87 NameComponent nc1 = new NameComponent(name, "FIPA_Agent_97");
88
89 NameComponent[] nameComp = {nc1};
90 context.rebind(nameComp, fipa);
91
92
93 }
94 catch (Exception e) {
95 e.printStackTrace();
96 }
97 this.start();
98 this.setName(threadId);
99 }
100
101
102 /***
103 run loop that pops a message off the Q (it will wait () if there is no message)
104 and then sends it to the handler that was used to construct this object.
105 */
106 public void run() {
107 processing = true;
108 System.out.println("Listening for FIPA 97 IIOP on port " + String.valueOf(port));
109 while (processing) {
110 String text = this.pop();
111 try {
112 System.out.println("text in f97 = " + text);
113 handler.handle(text);
114 }
115 catch (Exception e) {
116 e.printStackTrace();
117 }
118
119 yield();
120 }
121 }
122
123
124 public String pop () {
125 return (String) msgQ.dequeue();
126 }
127
128 /***
129 returns iiop://host:port/name
130 */
131 public String getResponseAddress() {
132 return (new String ("iiop://" +host + ":" +port + "/" + name));
133 }
134
135 }