01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.object.handler;
06:
07: import com.tc.async.api.AbstractEventHandler;
08: import com.tc.async.api.ConfigurationContext;
09: 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)
28: throws EventHandlerException {
29: // this instanceof stuff is yucky, but these are very low volume events
30:
31: if (context instanceof ClusterMembershipMessage) {
32: handleClusterMembershipMessage((ClusterMembershipMessage) context);
33: } else if (context instanceof ClientHandshakeAckMessage) {
34: handleClientHandshakeAckMessage((ClientHandshakeAckMessage) context);
35: } else if (context instanceof PauseContext) {
36: handlePauseContext((PauseContext) context);
37: } else {
38: throw new AssertionError("unknown event type: "
39: + context.getClass().getName());
40: }
41: }
42:
43: private void handlePauseContext(PauseContext ctxt) {
44: if (ctxt.getIsPause()) {
45: handshakeManager.pause();
46: } else {
47: handshakeManager.unpause();
48: }
49: }
50:
51: private void handleClientHandshakeAckMessage(
52: ClientHandshakeAckMessage handshakeAck) {
53: handshakeManager.acknowledgeHandshake(handshakeAck);
54: }
55:
56: private void handleClusterMembershipMessage(
57: ClusterMembershipMessage cmm) throws EventHandlerException {
58: if (cmm.isNodeConnectedEvent()) {
59: cluster.nodeConnected(cmm.getNodeId());
60: } else if (cmm.isNodeDisconnectedEvent()) {
61: cluster.nodeDisconnected(cmm.getNodeId());
62: } else {
63: throw new EventHandlerException("Unknown event type: "
64: + cmm);
65: }
66: }
67:
68: public synchronized void initialize(ConfigurationContext context) {
69: super .initialize(context);
70: this .handshakeManager = ((ClientConfigurationContext) context)
71: .getClientHandshakeManager();
72: }
73:
74: }
|