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  
23  
24  package zeus.concepts;
25  
26  import java.io.*;
27  import java.util.*;
28  import zeus.util.*;
29  
30  /***
31   * FIPAPerformative extends the performative class so that when getReceivers() is
32   * called it converts the values in the vector into strings from FIPA_AID_Addresses.
33   * This is so that we can store the values and use them to contact FIPA agents,
34   * but for the Zeus internal agency we can use Zeus addressing. <p>
35   * Also there is a method <i> String getFIPASender () </i> which is used to return
36   * an FIPA_AID_Address.
37   * <p>
38   * @author Simon Thompson
39   * @since 1.1
40   */
41  
42  public class FIPAPerformative extends Performative {
43      
44      FIPA_AID_Address sender = null; //overloaded
45      /***
46       * received is used to store any "received" elements: these are
47       * used to hold a list of agents that have previously had this message
48       */
49      Vector received = new Vector();
50      
51      
52      public FIPAPerformative(String type ) {
53          this.type = type;
54      }
55      
56      
57      public FIPAPerformative(Performative perf) {
58          String  token;
59          Hashtable table;
60          Enumeration data;
61          
62          if ( (token = perf.getType()) != null )		setType(token);
63          if ( (token = perf.getSender()) != null )		setSender(token);
64          if ( (data  = perf.getReceivers()) != null )	setReceivers(data);
65          if ( (token = perf.getReplyWith()) != null )	setReplyWith(token);
66          if ( (token = perf.getInReplyTo()) != null )	setInReplyTo(token);
67          if ( (token = perf.getReplyBy()) != null )	setReplyBy(token);
68          if ( (token = perf.getOntology()) != null )	setOntology(token);
69          if ( (token = perf.getLanguage()) != null )	setLanguage(token);
70          if ( (token = perf.getContent()) != null )	setContent(token);
71          if ( (token = perf.getProtocol()) != null )	setProtocol(token);
72          if ( (token = perf.getConversationId()) != null )	setConversationId(token);
73          //if ( (table = perf.getEnvelope()) != null )	setEnvelope(table);
74          // if ( (token = perf.getReplyTo()) != null ) setReplyTo(token);
75          
76      }
77      
78      
79      /***
80       * return the address of the receivers as FIPA_AID_Addresses
81       */
82      public Enumeration getFIPAReceivers() {
83          return receivers.elements();
84      }
85      
86      
87      /***
88       * getReceivers overwrites the getReceivers in Performative to ensure that
89       * the correct result is produced when this performative is handled by Zeus
90       * internal communications. The receivers are set to string equivalents....
91       */
92      public Enumeration getReceivers()	     {
93          Enumeration allReceivers = receivers.elements();
94          Vector tempStore = new Vector();
95          String res = null;
96          Address addr = null;
97          
98          while (allReceivers.hasMoreElements()) {
99              addr = (Address) allReceivers.nextElement();
100             res = addr.getName(); // which should produce a zeus address
101             tempStore.addElement(res);
102             
103         }// end while
104         return (tempStore.elements());
105     }// end method
106     
107     
108     
109     /***
110      * produce a performative that can be handled by native zeus parsers
111      */
112     public Performative performative() {
113         return new Performative(this);
114     }
115     
116     
117     /***
118      * getFIPASender is a bit of a hack, it produces the FIPA_AID_Address in string format which is the sender
119      * field  */
120     public String getSender_As_FIPA_String() {
121         
122         return this.sender.toFIPAString();
123     }
124     
125     
126     public FIPA_AID_Address getSender_As_FIPA_AID() {
127         return this.sender;
128     }
129     
130     
131     /***
132      * use this method to add any "received's" (stamps that agents that
133      * have had the message have put on it) that this
134      * message should have
135      */
136     public void setReceived(FIPA_Received received) {
137         this.received.addElement(received);
138     }
139     
140     
141     /***
142      * get the receiveds as XML: possible compliance issues, what to do
143      * with more than one received? My answer here is to build something that
144      * replicates the example given by FIPA for one, and to get it to produce
145      * a plausable output in the case where there are many
146      *
147      */
148     public String receivedToXML() {
149         if (received.isEmpty()) {
150             FIPA_Received frec = new FIPA_Received();
151             Enumeration allRec = getFIPAReceivers();
152             FIPA_AID_Address addr = (FIPA_AID_Address) allRec.nextElement();
153             String rec = (String) addr.getAddresses().firstElement();
154             frec.setReceivedBy(rec);
155             frec.setReceivedDate(FIPA_Date.getDate());
156             return frec.toXML(); }
157         else {
158             String retVal = new String();
159             Enumeration allRecs = received.elements();
160             while (allRecs.hasMoreElements()) {
161                 retVal+= ((FIPA_Received)allRecs.nextElement()).toXML();
162             }
163             
164             return retVal;
165         }
166     }
167     
168     public void appendContent(String cont) {
169         if (this.content == null) 
170             this.content = new String(); 
171         if (this.content.equals ("null")) { 
172             this.content = new String(); 
173         }
174         if (cont != null && !cont.equals("null")){ 
175         this.content += cont;}
176     }
177     
178     
179     
180     
181     
182     /***
183      * overloaded
184      */
185     public void setSender(FIPA_AID_Address sender) {
186         this.sender = sender;
187     }
188     
189     
190     
191     /***
192      * content brackets may be SL specific
193      */
194     public String toFIPAString() {
195         String str = "(" + type.toUpperCase() + "\n";
196         if ( sender != null )
197             str += " :sender ( " +  getSender_As_FIPA_String() + " )\n";
198         if ( receivers != null && !receivers.isEmpty() ) {
199             str += " :receiver (set ";
200             Enumeration allRec = getFIPAReceivers();
201             while (allRec.hasMoreElements()) {
202                 FIPA_AID_Address addr = (FIPA_AID_Address) allRec.nextElement();
203                 String current ="(" + addr.toFIPAString() +")";
204                 str += current; }
205             str += " )\n";
206         }
207         if ( replyWith != null )
208             str += " :reply-with " + replyWith + "\n";
209         if ( inReplyTo != null )
210             str += " :in-reply-to " + inReplyTo + "\n";
211         if ( replyBy != null )
212             str += " :reply-by " + replyBy + "\n";
213         if ( ontology != null )
214             str += " :ontology " + ontology + "\n";
215         if ( language != null )
216             str += " :language " + language + "\n";
217         if ( content != null )
218             // try no "'s // brackets may be SL specific
219             str += " :content \"" + content  + "\"\n";//" :content \"( "  +Misc.escape(content)  + ")\"\n";
220         if ( protocol != null )
221             str += " :protocol " + protocol + "\n";
222         if ( conversationId != null )
223             str += " :conversation-id " + conversationId + "\n";
224         if ( replyTo != null )
225             str += " :reply-to " + replyTo + "\n";
226     /*
227         if ( envelope != null && !envelope.isEmpty() ) {
228             str += " :envelope (";
229             Enumeration enum = envelope.keys();
230             String key;
231             Object value;
232             while( enum.hasMoreElements() ) {
233                 key = (String)enum.nextElement();
234                 value = envelope.get(key);
235                 str += "(" + key + " \"" + Misc.escape(value.toString()) + "\")";
236             }*/
237         //   str += ")";
238         //}
239         
240         str += ")\n";
241         return str;
242     }
243     
244     
245     /***
246      * receivers are returned as FIPA.AgentID[] so that they can be packed into
247      * a message envelope by the FIPA_99_Transport
248      */
249     public FIPA.AgentID[] getReceiversAgentID() {
250         int numberReceivers = receivers.size();
251         FIPA.AgentID allReceivers[] = new FIPA.AgentID[numberReceivers];
252         Enumeration recs = receivers.elements();
253         int count = 0;
254         while (recs.hasMoreElements()) {
255             allReceivers[count] = ((FIPA_AID_Address) recs.nextElement()).getAgentID();
256             count++;
257         }
258         return (allReceivers);
259     }
260     
261     
262     /***
263      * sender is returned in a FIPA.AgentID[].
264      * In Zeus there is but one sender, whereas the FIPA Spec and the JAS spec
265      * seem to think that there can be multiple senders.. I don't know how this
266      * can be true, and frankly, it frightens me a bit
267      */
268     public FIPA.AgentID[] getSenderAgentID() {
269         FIPA.AgentID allSenders[] = new FIPA.AgentID[1];
270         allSenders[0] = sender.getAgentID();
271         return (allSenders);
272     }
273     
274     
275     /***
276      * encrypted field is added to allow us to add sACL features later
277      */
278     protected String encrypted = "NO";
279     
280     
281     /***
282      * return a String which describes the encryption scheme that is being used
283      */
284     public String getEncryptionScheme() {
285         return encrypted;
286     }
287     
288     
289     /***
290      * set a string that will be used to flag the encryption mechanism we
291      * are using to other agents
292      */
293     public void setEncryptionDescriptor(String descriptor) {
294         encrypted = descriptor;
295     }
296     
297     
298     public FIPA_AID_Address getFIPAReceiver() {
299         Enumeration allRecs = receivers.elements();
300         return (FIPA_AID_Address) allRecs.nextElement();
301     }
302     
303     
304     /***
305      * Build a javax.agent.Envelope from a fipa address and a string
306      */
307     public javax.agent.Envelope jasEnvelope(FIPA_AID_Address addr, String thisTarget ) {
308         /*** System.out.println("target host is : " + addr.toString());
309          * javax.agent.Name recName = new javax.agent.Name (addr.getName(),addr.getHost());
310          *
311          * javax.agent.Name sendName = new javax.agent.Name (this.sender.getName(), this.sender.getHost()); */
312         javax.agent.Identifier ireceiver = null;  //new javax.agent.Identifier(recName,thisTarget);
313         javax.agent.Identifier isender = null; //new javax.agent.Identifier (sendName, this.sender.getHost());
314         javax.agent.Envelope env = new javax.agent.Envelope(ireceiver, isender, this);
315         return env;
316     }
317     
318     
319     /***
320      * spits out the sender as XML
321      */
322     public String getSenderXML() {
323         return sender.toXML();
324     }
325     
326     
327     /***
328      * spits out all receivers as XML strings
329      */
330     public String getReceiversXML() {
331         Enumeration allReceivers = receivers.elements();
332         String retVal = new String();
333         while (allReceivers.hasMoreElements()) {
334             FIPA_AID_Address currentAddr = (FIPA_AID_Address) allReceivers.nextElement();
335             retVal += currentAddr.toXML();
336         }
337         return retVal;
338     }
339     
340     
341     /***
342      * returns appropriate encryption descriptor wrapped in XML tags as per
343      * XC000084
344      */
345     public String get_is_EncryptedXML() {
346         String retVal = new String("<encrypted>");
347         if (getEncryptionScheme().equals("NO")) {
348             retVal += "no encryption"; }
349         else {
350             retVal += getEncryptionScheme(); }
351         retVal += "</encrypted>";
352         return retVal;
353     }// end method
354     
355     
356     
357     public FIPA.FipaMessage FipaMessage() {
358         FIPA.Envelope fenv = new FIPA.Envelope();
359         
360         try {
361             fenv.to = getReceiversAgentID();
362             
363         } catch (NullPointerException npe) { ;}
364         try {
365             fenv.from = getSenderAgentID();
366         } catch (NullPointerException npe) {
367             fenv.from = new FIPA.AgentID[0];}
368         //("null",new String[0], new FIPA.AgentID[0], new FIPA.Property[0]);}
369         try {
370             fenv.comments = new String("Zeus Agent Building Environment v1.1");
371         } catch (NullPointerException npe) { ;}
372         
373         fenv.payloadLength = -1; // indicates that it is up to the ACC to work this out.
374         
375         fenv.payloadEncoding = new String("String");
376         
377         fenv.aclRepresentation = new String("fipa-string-std");
378         
379         fenv.date = new FIPA.DateTime[0];
380         
381         fenv.encrypted = new String [1];
382         
383         fenv.encrypted[0] = getEncryptionScheme();
384         
385         fenv.intendedReceiver = getReceiversAgentID();
386         
387         //  fenv.intendedReceiver[0].getAddress
388         
389         fenv.received = new FIPA.ReceivedObject[0];
390         
391         fenv.transportBehaviour = new FIPA.Property[0][0];
392         
393         fenv.userDefinedProperties = new FIPA.Property[0];
394         
395         FIPA.Envelope envelopes[]  = {fenv};
396         
397         
398         FIPA.FipaMessage message = new FIPA.FipaMessage(envelopes,toFIPAString().getBytes());
399         return message;
400     }
401     
402 }
403 // end class