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  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  import org.omg.CosNaming.*;
38  import org.omg.CORBA.*;
39  
40  
41  import javax.rmi.*;
42  import java.rmi.*; 
43  import FIPA.*;
44  /***
45      This is an extention of the Server class which is normally used in Zeus
46      Instead of providing a sockets/TCPIP based transport this class provides a 
47      IIOP based transport as per the FIPA 99/2000  specs<p> 
48          
49  */
50  
51  public class FIPA_2000_IIOP_Server extends Server implements InTray {
52          
53      //protected Context initialNamingContext = null; 
54  // private String  connectionPoint   = new String ("iiop://127.0.0.1:900"); 
55  protected AgentContext context = null; 
56  private Queue  msgQ = new Queue("fipa2000In");    
57  private ZeusParser parser = new ZeusParser();
58  private FIPA_2000_Handler handler = null; 
59   String fsep = System.getProperty("file.separator");
60  private String host = null;
61  private String port = null;
62  private String name = null; 
63  private String iorAddr;
64  String iorpath = SystemProps.getProperty("ior.dir");
65  private String orbIOR = null;
66          
67  public FIPA_2000_IIOP_Server(FIPA_2000_Handler handler, String host, String port , String name, String threadId) {
68      iorAddr = iorpath + name; 
69  	Hashtable env = new Hashtable();
70  	this.handler = handler; 
71  	this.host = host;
72  	this.port = port; 
73  	this.name = name; 
74  	env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, 
75  	                "com.sun.jndi.cosnaming.CNCtxFactory"); 
76  	try {
77  	    Properties props = new Properties();
78          props.put("org.omg.CORBA.ORBInitialPort", port);
79          org.omg.CORBA.ORB myORB = org.omg.CORBA.ORB.init(new String[0], props);  
80       
81          
82  	    MTS transport = new FIPA_2000_IIOP_Connection();
83  	    orbIOR = myORB.object_to_string(transport); 
84  	    java.io.File file = new java.io.File (iorAddr);
85  	    java.io.FileWriter write = new java.io.FileWriter (file); 
86              write.write (orbIOR); 
87  	    write.flush(); 
88  	    write.close();
89  
90  	    Thread corba_messageThread = new Thread((FIPA_2000_IIOP_Connection)transport); 
91  	    corba_messageThread.setPriority(Thread.MIN_PRIORITY);
92  	    corba_messageThread.start(); 
93  	    ((FIPA_2000_IIOP_Connection)transport).register(msgQ);
94  	  //  Context initialNamingContext = new InitialContext(env);
95  	    NamingContext context = NamingContextHelper.narrow(myORB.resolve_initial_references("NameService"));
96          NameComponent nc1 = new NameComponent(name, "FIPA.MTS");
97  
98          NameComponent[] nameComp = {nc1};
99          context.rebind(nameComp, transport);
100 	/*    NameComponet nc = new NameComponent
101         initialNamingContext.rebind(name,transport); */
102         } 
103         catch (Exception e) {
104             e.printStackTrace(); 
105         }
106 this.start();
107 this.setName(threadId); 
108 }
109       
110       
111 /***
112     This method should call an agnostic message handler....
113     Right now, it just prints the message to System.out
114          
115     */
116 public void run() {
117     processing = true;
118     System.out.println("Listening for FIPA 2000 IIOP on port " + String.valueOf(port)); 
119     while (processing) {     
120         FIPA.FipaMessage message = this.pop();
121 	            try { 
122 	                handler.handle(message); 
123 	            }
124 	            catch (Exception e) { 
125 	                e.printStackTrace(); 
126 	                }
127     	
128 	yield(); 
129 	}	    
130     }    
131         
132            
133 public FIPA.FipaMessage pop () { 
134     return (FIPA.FipaMessage) msgQ.dequeue(); 
135 }
136 
137 
138 
139  /*** 
140     used to return iiop://host:port/name
141     now returns corbaname://host:port/NameService/name
142     */
143     public String getResponseAddress() { 
144         return new String ("corbaname:iiop:" + host + ":" + port + "/" +"NameService/"+ name); 
145     }
146  
147       
148     public String getIORAddress () { 
149         return new String (orbIOR); 
150     }
151 
152 }