View Javadoc

1   /*
2    * #%L
3    * Bitrepository Protocol
4    * 
5    * $Id$
6    * $HeadURL$
7    * %%
8    * Copyright (C) 2010 - 2011 The State and University Library, The Royal Library and The State Archives, Denmark
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 2.1 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
23   * #L%
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  /** Models the response state for a given set of components */
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       * Use for identify response bookkeeping.
45       * @param componentsWhichShouldRespond An array of componentIDs specifying which components are expected to respond 
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       * Maintains the bookkeeping regarding which components have responded.
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      /** Returns a list of components where a identify response hasen't been received. */ 
72      public Collection<String> getOutstandComponents() {
73          return Collections.unmodifiableCollection(componentsWithOutstandingResponse);
74      }
75  
76      /**
77       * @return true all components have responded.
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       * @return The set of components which should respond to the identification request.
86       */
87      public final Set<String> getComponentsWhichShouldRespond() {
88          return componentsWhichShouldRespond;
89      }
90  }