001: /*
002: * The contents of this file are subject to the Sapient Public License
003: * Version 1.0 (the "License"); you may not use this file except in compliance
004: * with the License. You may obtain a copy of the License at
005: * http://carbon.sf.net/License.html.
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is The Carbon Component Framework.
012: *
013: * The Initial Developer of the Original Code is Sapient Corporation
014: *
015: * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016: */
017:
018: package org.sape.carbon.services.clustering;
019:
020: import java.util.Enumeration;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Set;
024: import java.util.Vector;
025:
026: import org.sape.carbon.core.component.lifecycle.Configurable;
027: import org.sape.carbon.core.component.lifecycle.Destroyable;
028: import org.sape.carbon.core.component.lifecycle.Initializable;
029: import org.sape.carbon.core.component.lifecycle.Startable;
030:
031: import org.javagroups.Address;
032: import org.javagroups.ChannelListener;
033: import org.javagroups.JChannel;
034: import org.javagroups.JChannelFactory;
035: import org.javagroups.View;
036: import org.javagroups.blocks.DistributedHashtable;
037: import org.javagroups.debug.Debugger;
038: import org.javagroups.log.Trace;
039:
040: /**
041: * <p>This is the template for Classes.</p>
042: *
043: *
044: * @since carbon 1.0
045: * @author Greg Hinkle, January 2002
046: * @version $Revision: 1.4 $($Author: dvoet $ / $Date: 2003/05/05 21:21:08 $)
047: * @copyright 2002 Sapient
048: */
049: public class CarbonClusterImpl implements CarbonCluster, Initializable,
050: Startable, Configurable, Destroyable, ChannelListener,
051: DistributedHashtable.Notification {
052:
053: private Debugger debugger;
054: private DistributedHashtable table;
055:
056: private Integer key = new Integer(System
057: .identityHashCode(Runtime.class));
058:
059: public void printMembership() {
060: try {
061: System.out.println("Printing Memberships:");
062: Set set = this .table.entrySet();
063: Iterator iter = set.iterator();
064: while (iter.hasNext()) {
065: Map.Entry entry = (Map.Entry) iter.next();
066: System.out.println(entry.getValue());
067: }
068: } catch (Exception e) {
069: e.printStackTrace();
070: }
071: }
072:
073: public void showAll(JChannel channel) {
074: try {
075: Vector members = channel.getView().getMembers();
076: if (members != null) {
077: Enumeration enum = members.elements();
078: while (enum.hasMoreElements()) {
079: Address address = (Address) enum.nextElement();
080:
081: System.out.println(address);
082: }
083: }
084: } catch (Exception e) { e.printStackTrace(); }
085: }
086:
087: public Vector getMembers() {
088: JChannel channel = (JChannel) this .table.getChannel();
089: if (channel == null)
090: return null;
091: View view = channel.getView();
092: if (view == null)
093: return null;
094: return view.getMembers();
095: }
096:
097: public void initialize() throws java.lang.Exception {
098: //Trace.init();
099:
100: String props = "UDP(mcast_addr=224.0.0.36;mcast_port=55566;"
101: + "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):"
102: + "PING(timeout=2000;num_initial_members=3):"
103: + "MERGE2(min_interval=5000;max_interval=10000):"
104: + "FD_SOCK:"
105: + "VERIFY_SUSPECT(timeout=1500):"
106: + "pbcast.STABLE(desired_avg_gossip=20000):"
107: + "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):"
108: + "UNICAST(timeout=5000;min_wait_time=2000):"
109: + "FRAG(frag_size=16000;down_thread=false;up_thread=false):"
110: + "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;"
111: + "shun=false;print_local_addr=true):"
112: + "pbcast.STATE_TRANSFER";
113: //"PERF(details=true)";
114:
115: /*
116: String props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
117: "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +
118: "PING(timeout=2000;num_initial_members=3):" +
119: "MERGE2(min_interval=5000;max_interval=10000):" +
120: "FD_SOCK:" +
121: "VERIFY_SUSPECT(timeout=1500):" +
122: "pbcast.STABLE(desired_avg_gossip=20000):" +
123: "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
124: "UNICAST(timeout=5000;min_wait_time=2000):" +
125: "FRAG(frag_size=8096;down_thread=false;up_thread=false):" +
126: // "CAUSAL:" +
127: "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
128: "shun=false;print_local_addr=true)";
129: */
130: //channel=new JChannel(props);
131: Trace.init();
132: this .table = new DistributedHashtable("Carbon",
133: new JChannelFactory(), props, 10000);
134: //this.channel = (JChannel)this.table.getChannel();
135: //System.out.println("Channel is " + channel);
136:
137: //debugger=new Debugger((JChannel)channel, true);
138: //debugger.start();
139:
140: //channel.setChannelListener(this);
141: this .table.addNotifier(this );
142:
143: this .table.put(this .key, new CarbonUserMessage());
144: }
145:
146: public void start() throws java.lang.Exception {
147: System.out.println("Starting Cluster...");
148: //this.printMembership();
149: }
150:
151: public void stop() throws java.lang.Exception {
152: //donoting
153: }
154:
155: public void destroy() throws java.lang.Exception {
156: //this.table.getChannel().close();
157: }
158:
159: public void channelClosed(org.javagroups.Channel channel) {
160: System.out.println("Channel closed");
161: showAll((JChannel) channel);
162: }
163:
164: public void channelConnected(org.javagroups.Channel channel) {
165: System.out.println("Channel connected");
166: showAll((JChannel) channel);
167: }
168:
169: public void channelDisconnected(org.javagroups.Channel channel) {
170: System.out.println("Channel disconnected");
171: showAll((JChannel) channel);
172: }
173:
174: public void channelReconnected(org.javagroups.Address address) {
175: System.out.println("Channel reconnected " + address);
176: //showAll(channel);
177: }
178:
179: public void channelShunned() {
180: System.out.println("Channel shunned");
181: }
182:
183: public void entryRemoved(Object obj) {
184: CarbonUserMessage msg = (CarbonUserMessage) obj;
185:
186: System.out.println("Someone left the cluster: " + msg);
187: }
188:
189: public void entrySet(Object key, Object value) {
190: System.out.println("Someone joined the cluster at key [" + key
191: + "]: " + value);
192: }
193:
194: public void viewChange(java.util.Vector newMembers,
195: java.util.Vector oldMembers) {
196: System.out.println("NewMembers: " + newMembers
197: + " OldMembers: " + oldMembers);
198: }
199:
200: public void configure(
201: org.sape.carbon.core.component.ComponentConfiguration componentConfiguration)
202: throws java.lang.Exception {
203: }
204:
205: }
|