1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.bitrepository.client.conversation.selector;
26
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Set;
31 import org.bitrepository.bitrepositorymessages.MessageResponse;
32 import org.bitrepository.client.exceptions.UnexpectedResponseException;
33 import org.bitrepository.protocol.utils.MessageUtils;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38 public class ContributorResponseStatus {
39 private final Logger log = LoggerFactory.getLogger(this.getClass());
40 private final Set<String> componentsWhichShouldRespond;
41 private final Set<String> componentsWithOutstandingResponse;
42
43
44
45
46
47 public ContributorResponseStatus(Collection<String> componentsWhichShouldRespond) {
48 this.componentsWhichShouldRespond = new HashSet<String>(componentsWhichShouldRespond);
49 this.componentsWithOutstandingResponse = new HashSet<String>(componentsWhichShouldRespond);
50 }
51
52
53
54
55
56 public final void responseReceived(MessageResponse response) throws UnexpectedResponseException {
57 if (MessageUtils.isEndMessageForPrimitive(response)) {
58 String componentID = response.getFrom();
59 log.debug("Received response from: " + componentID);
60 if (componentsWithOutstandingResponse.contains(componentID)) {
61 componentsWithOutstandingResponse.remove(componentID);
62 } else if (!componentsWithOutstandingResponse.contains(componentID) &&
63 componentsWhichShouldRespond.contains(componentID)) {
64 log.debug("Received more than one response from component " + componentID);
65 } else {
66 log.debug("Received response from irrelevant component " + componentID);
67 }
68 }
69 }
70
71
72 public Collection<String> getOutstandComponents() {
73 return Collections.unmodifiableCollection(componentsWithOutstandingResponse);
74 }
75
76
77
78
79 public final boolean haveAllComponentsResponded() {
80 log.debug("Expected contributors: " + componentsWhichShouldRespond + ", components that have not answered: " + componentsWithOutstandingResponse);
81 return componentsWithOutstandingResponse.isEmpty();
82 }
83
84
85
86
87 public final Set<String> getComponentsWhichShouldRespond() {
88 return componentsWhichShouldRespond;
89 }
90 }