KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > PARTITIONER


1 // $Id: PARTITIONER.java,v 1.4 2004/09/23 16:29:42 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Address;
7 import org.jgroups.Event;
8 import org.jgroups.Header;
9 import org.jgroups.Message;
10 import org.jgroups.stack.Protocol;
11
12 import java.io.IOException JavaDoc;
13 import java.io.ObjectInput JavaDoc;
14 import java.io.ObjectOutput JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.util.Vector JavaDoc;
18
19
20
21 /**
22  * This layer can be put on top of the bottommost layer and is useful to simulate partitions.
23  * It simply adds a header with its partition number and discards Messages with other partition numbers.<br>
24  * If it receives an Event of type Event.SET_PARTITIONS it sends a Header of type COMMAND with the Hashtable
25  * contained in the Event argument to set the partitions of ALL processes (not just processes of the current view but
26  * every process with the same group address that receives the message.
27  */

28
29 public class PARTITIONER extends Protocol {
30     final Vector JavaDoc members=new Vector JavaDoc();
31     Address local_addr=null;
32     int my_partition=1;
33
34     /** All protocol names have to be unique ! */
35     public String JavaDoc getName() {return "PARTITIONER";}
36
37
38     public boolean setProperties(Properties JavaDoc props) {
39         String JavaDoc str;
40
41         super.setProperties(props);
42         if(props.size() > 0) {
43             System.err.println("EXAMPLE.setProperties(): these properties are not recognized:");
44             props.list(System.out);
45             return false;
46         }
47         return true;
48     }
49
50
51     /** Just remove if you don't need to reset any state */
52     public void reset() {}
53
54
55     /**
56      * Discards Messages with the wrong partition number and sets local partition number if
57      * it receives a COMMAND Header
58      */

59
60     public void up(Event evt) {
61         Message msg;
62         Integer JavaDoc num;
63         PartitionerHeader partHead=null;
64
65         switch(evt.getType()) {
66
67         case Event.SET_LOCAL_ADDRESS:
68         local_addr=(Address) evt.getArg();
69          if(log.isInfoEnabled()) log.info("local address is " + local_addr);
70         break;
71
72         case Event.MSG:
73             msg=(Message)evt.getArg();
74             partHead=(PartitionerHeader) msg.removeHeader(getName());
75             if (partHead.type == PartitionerHeader.COMMAND) {
76         num = (Integer JavaDoc) partHead.Destinations.get(local_addr);
77         if (num == null) return;
78          if(log.isInfoEnabled()) log.info("new partition = " + num);
79         my_partition =num.intValue();
80         return;
81             }
82             if (partHead.type == PartitionerHeader.NORMAL && partHead.partition != my_partition ) return;
83             break;
84         }
85
86         passUp(evt); // Pass up to the layer above us
87
}
88
89
90
91
92     /**
93      * Adds to Messages a Header with the local partitin number and if receives a SET_PARTITIONS Event sends
94      * a new Message with a PartitionerHeader set to COMMAND that carries the Hashtable
95      */

96
97     public void down(Event evt) {
98         Message msg;
99         Event newEvent;
100         PartitionerHeader partHeader;
101
102         switch(evt.getType()) {
103
104         case Event.SET_PARTITIONS:
105         //Sends a partitioning message
106
if(log.isInfoEnabled()) log.info("SET_PARTITIONS received, argument " + evt.getArg().toString());
107         msg = new Message(null,null,null);
108         partHeader = new PartitionerHeader(PartitionerHeader.COMMAND);
109         partHeader.Destinations = (Hashtable JavaDoc) evt.getArg();
110         msg.putHeader(getName(), partHeader);
111         passDown(new Event(Event.MSG,msg));
112         break;
113
114         case Event.MSG:
115             msg=(Message)evt.getArg();
116             msg.putHeader(getName(), new PartitionerHeader(PartitionerHeader.NORMAL,my_partition));
117             // Do something with the event, e.g. add a header to the message
118
// Optionally pass down
119
break;
120         }
121
122         passDown(evt); // Pass on to the layer below us
123
}
124
125
126
127
128
129 /**
130  * The Partitioner header normally (type = NORMAL) contains just the partition number that is checked to discard messages
131  * received from other partitions.
132  * If type is COMMAND Destination contains an Hashtable where keys are of type Address and represent process (channel)
133  * addresses and values are Integer representing the partition that shuold be assigned to each Address.
134  */

135
136     public static class PartitionerHeader extends Header {
137     // your variables
138
static final int NORMAL=0; //normal header (do nothing)
139
static final int COMMAND=1; //set partition vector
140
int type=0,partition=1;
141     Hashtable JavaDoc Destinations=null;
142     
143     public PartitionerHeader () {} // used for externalization
144
public PartitionerHeader (int type) { this.type= type; }
145     public PartitionerHeader (int type,int partition) { this.type= type; this.partition = partition; }
146     
147     public String JavaDoc toString() {
148         switch (type) {
149         case NORMAL: return "NORMAL ->partition :" + partition;
150         case COMMAND: return "COMMAND ->hashtable :" + Destinations;
151         default: return "<unknown>";
152         
153         }
154     }
155     
156     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
157         out.writeInt(type);
158         out.writeInt(partition);
159         out.writeObject(Destinations);
160     }
161     
162     
163     
164     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
165         type=in.readInt();
166         partition=in.readInt();
167         Destinations=(Hashtable JavaDoc)in.readObject();
168     }
169 }
170     
171
172
173
174 }
175
Popular Tags