KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > perf > transports > JGroupsClusterTransport


1 package org.jgroups.tests.perf.transports;
2
3 import org.jgroups.Address;
4 import org.jgroups.Message;
5 import org.jgroups.stack.IpAddress;
6 import org.jgroups.tests.perf.Transport;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12
13 /**
14  * Transport with a minimal stack (usually only UDP or TCP transport layer !) and explicit notion of
15  * cluster membership (listed in properties, config.txt)
16  * @author Bela Ban Jan 22
17  * @author 2004
18  * @version $Id: JGroupsClusterTransport.java,v 1.3 2006/12/19 08:51:46 belaban Exp $
19  */

20 public class JGroupsClusterTransport extends JGroupsTransport implements Transport {
21     List JavaDoc members;
22
23     public JGroupsClusterTransport() {
24         super();
25     }
26
27     public void create(Properties JavaDoc properties) throws Exception JavaDoc {
28         super.create(properties);
29         String JavaDoc cluster_def=config.getProperty("cluster");
30         if(cluster_def == null)
31             throw new Exception JavaDoc("property 'cluster' is not defined");
32         members=parseCommaDelimitedList(cluster_def);
33     }
34
35
36     public void send(Object JavaDoc destination, byte[] payload, boolean oob) throws Exception JavaDoc {
37         if(destination != null) {
38             Message msg=new Message((Address)destination, null, payload);
39             if(oob)
40                 msg.setFlag(Message.OOB);
41             channel.send(msg);
42         }
43         else {
44             // we don't know the membership from discovery, so we need to send individually
45
Address mbr;
46             for(int i=0; i < members.size(); i++) {
47                 mbr=(Address)members.get(i);
48                 Message msg=new Message((Address)destination, null, payload);
49                 if(oob)
50                     msg.setFlag(Message.OOB);
51                 msg.setDest(mbr);
52                 channel.send(msg);
53             }
54         }
55     }
56
57
58     private List JavaDoc parseCommaDelimitedList(String JavaDoc s) throws Exception JavaDoc {
59         List JavaDoc retval=new ArrayList JavaDoc();
60         StringTokenizer JavaDoc tok;
61         String JavaDoc hostname, tmp;
62         int port;
63         Address addr;
64         int index;
65
66         if(s == null) return null;
67         tok=new StringTokenizer JavaDoc(s, ",");
68         while(tok.hasMoreTokens()) {
69             tmp=tok.nextToken();
70             index=tmp.indexOf(':');
71             if(index == -1)
72                 throw new Exception JavaDoc("host must be in format <host:port>, was " + tmp);
73             hostname=tmp.substring(0, index);
74             port=Integer.parseInt(tmp.substring(index+1));
75             addr=new IpAddress(hostname, port);
76             retval.add(addr);
77         }
78         return retval;
79     }
80 }
81
Popular Tags