KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > handler > ClientCoordinationHandler


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.object.handler;
6
7 import com.tc.async.api.AbstractEventHandler;
8 import com.tc.async.api.ConfigurationContext;
9 import com.tc.async.api.EventContext;
10 import com.tc.async.api.EventHandlerException;
11 import com.tc.cluster.Cluster;
12 import com.tc.object.ClientConfigurationContext;
13 import com.tc.object.context.PauseContext;
14 import com.tc.object.handshakemanager.ClientHandshakeManager;
15 import com.tc.object.msg.ClientHandshakeAckMessage;
16 import com.tc.object.msg.ClusterMembershipMessage;
17
18 public class ClientCoordinationHandler extends AbstractEventHandler {
19
20   private ClientHandshakeManager handshakeManager;
21   private final Cluster cluster;
22
23   public ClientCoordinationHandler(Cluster cluster) {
24     this.cluster = cluster;
25   }
26
27   public void handleEvent(EventContext context) throws EventHandlerException {
28     // this instanceof stuff is yucky, but these are very low volume events
29

30     if (context instanceof ClusterMembershipMessage) {
31       handleClusterMembershipMessage((ClusterMembershipMessage) context);
32     } else if (context instanceof ClientHandshakeAckMessage) {
33       handleClientHandshakeAckMessage((ClientHandshakeAckMessage) context);
34     } else if (context instanceof PauseContext) {
35       handlePauseContext((PauseContext) context);
36     } else {
37       throw new AssertionError JavaDoc("unknown event type: " + context.getClass().getName());
38     }
39   }
40
41   private void handlePauseContext(PauseContext ctxt) {
42     if (ctxt.getIsPause()) {
43       handshakeManager.pause();
44     } else {
45       handshakeManager.unpause();
46     }
47   }
48
49   private void handleClientHandshakeAckMessage(ClientHandshakeAckMessage handshakeAck) {
50     handshakeManager.acknowledgeHandshake(handshakeAck.getObjectIDSequenceStart(), handshakeAck
51         .getObjectIDSequenceEnd(), handshakeAck.getPersistentServer(), handshakeAck.getThisNodeId(), handshakeAck
52         .getAllNodes());
53   }
54
55   private void handleClusterMembershipMessage(ClusterMembershipMessage cmm) throws EventHandlerException {
56     if (cmm.isNodeConnectedEvent()) {
57       cluster.nodeConnected(cmm.getNodeId());
58     } else if (cmm.isNodeDisconnectedEvent()) {
59       cluster.nodeDisconnected(cmm.getNodeId());
60     } else {
61       throw new EventHandlerException("Unknown event type: " + cmm);
62     }
63   }
64
65   public synchronized void initialize(ConfigurationContext context) {
66     super.initialize(context);
67     this.handshakeManager = ((ClientConfigurationContext) context).getClientHandshakeManager();
68   }
69
70 }
71
Popular Tags