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-2001. All Rights Reserved.
18  *
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
20  */
21  
22  package zeus.actors.outtrays;
23  
24  // import rmi classes
25  import javax.naming.*;
26  import java.util.*;
27  import zeus.util.*;
28  //import java.rmi.RemoteException;
29  //import java.rmi.RMISecurityManager;
30  //import java.rmi.server.UnicastRemoteObject;
31  import javax.rmi.*;
32  import zeus.concepts.*;
33  import zeus.actors.*;
34  import java.io.*;
35  import fipa97.FIPA_Agent_97;
36  import fipa97.FIPA_Agent_97Helper;
37  import org.omg.CORBA.*;
38  import org.omg.CORBA.portable.*;
39  import org.omg.CosNaming.*;
40  import zeus.actors.factories.*;
41  import javax.agent.service.*;
42  import javax.agent.*;
43  
44  /***
45      This class is a FIPA complient PostMan that can be used to build an
46      ACC agent which when working with the NameServer provides a link between
47      the Zeus platform and other FIPA complient platforms
48      */
49  public class FIPA_PostMan extends zeus.actors.PostMan {
50  
51    protected  javax.naming.Context  initialNamingContext = null;
52    private FileWriter log;
53    private File file = null; 
54    public FIPA_PostMan(MailBox mbox, Queue outMail, Queue ccMail, Address myAddress) {
55     //super (mbox,outMail,ccMail,myAddress);
56  
57      this.mbox      = mbox;
58      this.outMail   = outMail;
59      this.ccMail    = ccMail;
60      this.myAddress = myAddress;
61  
62      if ( ccMail == null )
63         this.setPriority(Thread.NORM_PRIORITY);
64  
65      try {
66           initialNamingContext = new InitialContext();}
67           catch (Exception e) {
68              e.printStackTrace(); }
69      this.setName ("FIPA_PostMan");
70      this.start();
71  
72    }
73  
74  
75    public void run() {
76       FIPAPerformative msg;
77       String       receiver;
78       boolean done = false;
79       dispatching = true;
80       
81       try {
82        file = new File  (SystemProps.getProperty("http_root")+ SystemProps.getProperty("out_log")); 
83        log = new FileWriter (file,true);    
84           
85       }
86       catch (Exception e) {
87           e.printStackTrace();
88       }
89       while( dispatching ) {
90          try {
91              msg = (FIPAPerformative) outMail.dequeue();
92             /*        
93              Enumeration test = msg.getFIPAReceivers();
94              while (test.hasMoreElements()){
95               FIPA_AID_Address addr = (FIPA_AID_Address) test.nextElement();
96              }*/
97              done = false;
98              Enumeration recs = msg.getFIPAReceivers();
99              while (recs.hasMoreElements () && !done) {
100             try {
101                 FIPA_AID_Address addr = (FIPA_AID_Address) recs.nextElement();
102                 System.out.println(msg.toString());
103                 done = postFIPAMsg (msg, addr);
104                 file.setLastModified(java.lang.System.currentTimeMillis());
105                 }
106                 catch (Exception e) {
107                         System.out.println("exception in address, probably proprietary/ unhandled transport");
108                         System.out.println("zeus is trying next address");
109               		 e.printStackTrace(); }
110                	catch (Error er) {
111 	                System.out.println("error in address, probably proprietary/ unhandled transport");
112                         System.out.println("zeus is trying next address");
113                 	er.printStackTrace();
114                 }
115                 System.out.println("recs = " + recs.toString());
116                 if (done) System.out.println("done = true");
117                 else System.out.println("done = false");
118                 }
119               System.out.println("iterating");
120             } catch (Exception e) {
121                e.printStackTrace(); }
122                catch (Error er) {
123                 er.printStackTrace();
124                 }
125        // System.out.println("\n Message Transport error \n Zeus is recovering");
126 //     	 yield();
127 
128      }
129   }
130 
131 
132   /***
133     posts the message to the appropriate transport, will set the return address to
134     */
135    public boolean postFIPAMsg( FIPAPerformative msg, FIPA_AID_Address addr ) {
136    try {
137     boolean isOk = false;
138     int nTry = 0;
139     //FIPA_AID_Address sender = new FIPA_AID_Address("agent-identifier\n:name " + myAddress.getName() + "@" + myAddress.getHost() + "\n:addresses (sequence iiop://" + myAddress.getHost() + "/ACC)");
140   //  msg.setSender(sender);
141   //  msg.setAddress(myAddress);
142     Iterator addressIter = addr.iterator();
143     String thisAddress = new String();
144     while( addressIter.hasNext()) {
145 
146             thisAddress = (String) addressIter.next();
147 	    debug ("trying : " + thisAddress);
148             TransportFactoryMethod tfm = new TransportFactoryMethod();
149             TransportFactory tFactory = tfm.getTransportFactory();
150             tFactory.setLog(file); 
151             try {
152                 OutTray trans = tFactory.getTransport(thisAddress);
153                 javax.agent.Envelope env = msg.jasEnvelope(addr,thisAddress);
154                 trans.send (env);
155                 return (true);
156                 }
157                 catch (TransportUnsupportedException tue) {
158                     tue.printStackTrace();
159                    }
160                 catch (UnsuitableMessageException ume) {
161                     ume.printStackTrace();
162                 }
163                 catch (Exception e) {
164                         e.printStackTrace();
165                         // continue
166                         }
167                 catch (Error er) {
168                         // continue
169                         }
170     }
171     return false;    }
172     catch (Exception e){
173         e.printStackTrace();
174         return false;
175         }
176   }
177 
178   private void debug(String str) {
179    //     System.out.println("FIPA_PostMan>> " + str);
180         }
181 
182 
183 
184 
185 
186 }