01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.object.idprovider.impl;
05:
06: import com.tc.async.api.AbstractEventHandler;
07: import com.tc.async.api.EventContext;
08: import com.tc.object.msg.ObjectIDBatchRequestMessage;
09: import com.tc.object.msg.ObjectIDBatchRequestMessageFactory;
10: import com.tc.object.msg.ObjectIDBatchRequestResponseMessage;
11: import com.tc.util.Assert;
12: import com.tc.util.sequence.BatchSequenceProvider;
13: import com.tc.util.sequence.BatchSequenceReceiver;
14:
15: import java.util.HashMap;
16: import java.util.Map;
17:
18: /**
19: * @author steve manages requests to servers
20: */
21: public class RemoteObjectIDBatchSequenceProvider extends
22: AbstractEventHandler implements BatchSequenceProvider {
23: private long requestID;
24: private Map requests = new HashMap();
25: private final ObjectIDBatchRequestMessageFactory mf;
26:
27: public RemoteObjectIDBatchSequenceProvider(
28: ObjectIDBatchRequestMessageFactory mf) {
29: this .mf = mf;
30: }
31:
32: public synchronized void requestBatch(
33: BatchSequenceReceiver receiver, int size) {
34: requests.put(new Long(requestID), new Request(receiver, size,
35: requestID));
36: ObjectIDBatchRequestMessage m = mf
37: .newObjectIDBatchRequestMessage();
38: m.initialize(requestID++, size);
39: m.send();
40: }
41:
42: private static class Request {
43: private BatchSequenceReceiver receiver;
44:
45: private Request(BatchSequenceReceiver receiver, int batchSize,
46: long requestID) {
47: this .receiver = receiver;
48: }
49:
50: public BatchSequenceReceiver getReceiver() {
51: return receiver;
52: }
53: }
54:
55: public void handleEvent(EventContext context) {
56: ObjectIDBatchRequestResponseMessage m = (ObjectIDBatchRequestResponseMessage) context;
57: Long reqID = new Long(m.getRequestID());
58: Request r = (Request) requests.get(reqID);
59: Assert.eval(r != null);
60: requests.remove(reqID);
61: r.getReceiver()
62: .setNextBatch(m.getBatchStart(), m.getBatchEnd());
63: }
64: }
|