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  
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      //env.put(Context.PROVIDER_URL,host+":" +port);
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  	    //org.omg.CORBA.Context initialNamingContext = new InitialContext(env);
85  	    
86  	    NamingContext context = NamingContextHelper.narrow(myORB.resolve_initial_references("NameService"));
87          NameComponent nc1 = new NameComponent(name, "FIPA_Agent_97");
88        //  NameComponent nc2 = new NameComponent(name, "agent");
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 }