Example #1
0
 /**
  * Returns the map of brokerID -> brokerURI of all the existing broker connections.
  *
  * <p>TODO: This method wastes memory because it now maps brokerURI to brokerURI; it got changed
  * on the way; remove this method and use {@link #getBrokerURIList()} instead
  *
  * @return
  */
 public Map<String, String> getBrokerConnections() {
   Map<String, String> idURIMap = new HashMap<String, String>();
   for (BrokerState brokerState : brokerStates.values()) {
     idURIMap.put(
         brokerState.getBrokerAddress().getNodeURI(), brokerState.getBrokerAddress().getNodeURI());
   }
   return idURIMap;
 }
Example #2
0
 /**
  * Returns all the advertisement messages the client has sent in the past. This method is
  * operational only when the store_detail_state option is switched ON.
  *
  * @return
  * @throws ClientException If the client.store_detail_state option is OFF
  */
 public Map<String, AdvertisementMessage> getAdvertisements() throws ClientException {
   if (!clientConfig.detailState)
     throw new ClientException(
         "getAdvertisements() not supported with client.store_detail_state=OFF");
   HashMap<String, AdvertisementMessage> idMsgMap = new HashMap<String, AdvertisementMessage>();
   for (BrokerState brokerState : brokerStates.values()) {
     Set<AdvertisementMessage> advs = brokerState.getAdvMessages();
     for (AdvertisementMessage advMsg : advs) idMsgMap.put(advMsg.getMessageID(), advMsg);
   }
   return idMsgMap;
 }
Example #3
0
 /**
  * Whenever a message is received by the message listener, this method is called. The child
  * classes extended from Client can overwrite this method to implement their own functionalities.
  *
  * @param msg The new message received from the communication interface (from Broker.)
  */
 public void processMessage(Message msg) {
   receivedPubMsg = (PublicationMessage) msg;
   if (clientConfig.detailState) {
     // TODO: the below line works because we now assume the clients connects to only one
     // broker. Modify this so that the received publication is added to the correct
     // broker state.
     BrokerState bState = brokerStates.get(defaultBrokerAddress);
     if (bState != null) bState.addReceivedPub(receivedPubMsg);
   }
   messageLogger.info("Message received at Client " + clientID + ": " + msg);
 }
Example #4
0
 public List<UnadvertisementMessage> unAdvertise(String[] advIDList) throws ClientException {
   ArrayList<UnadvertisementMessage> unAdvMsgs = new ArrayList<UnadvertisementMessage>();
   List<String> foundIDs = new ArrayList<String>(Arrays.asList(advIDList));
   for (BrokerState brokerState : brokerStates.values()) {
     if (clientConfig.detailState) foundIDs = brokerState.containsAdvs(advIDList);
     for (String advID : foundIDs) {
       unAdvMsgs.add(unAdvertise(advID, brokerState));
     }
   }
   return unAdvMsgs;
 }
Example #5
0
 /**
  * Returns all the subscription messages the client has sent in the past. This method is
  * operational only when the store_detail_state option is switched ON.
  *
  * @return
  * @throws ClientException If the client.store_detail_state option is OFF
  */
 public Map<String, SubscriptionMessage> getSubscriptions() throws ClientException {
   if (!clientConfig.detailState)
     throw new ClientException(
         "getSubscriptions() not supported with client.store_detail_state=OFF");
   HashMap<String, SubscriptionMessage> idMsgMap = new HashMap<String, SubscriptionMessage>();
   for (BrokerState brokerState : brokerStates.values()) {
     Set<SubscriptionMessage> subs = brokerState.getSubMessages();
     for (SubscriptionMessage subMsg : subs) idMsgMap.put(subMsg.getMessageID(), subMsg);
   }
   return idMsgMap;
 }
Example #6
0
 public List<UnsubscriptionMessage> unSubscribe(String[] subIDList) throws ClientException {
   List<UnsubscriptionMessage> unSubMsgIDs = new ArrayList<UnsubscriptionMessage>();
   List<String> foundIDs = new ArrayList<String>(Arrays.asList(subIDList));
   for (BrokerState brokerState : brokerStates.values()) {
     if (clientConfig.detailState) foundIDs = brokerState.containsSubs(subIDList);
     for (String advID : foundIDs) {
       unSubMsgIDs.add(unSubscribe(advID, brokerState));
     }
   }
   return unSubMsgIDs;
 }
Example #7
0
 /**
  * Disconnects from all the connected brokers. The client first unsubscribe/unadvertise all the
  * subscriptions/advertisements before disconnecting from brokers.
  *
  * @return A String message that describes the success/failure of the operation.
  */
 public String disconnectAll() {
   String outStr = "";
   for (BrokerState brokerState : new HashMap<NodeAddress, BrokerState>(brokerStates).values()) {
     try {
       disconnect(brokerState);
       outStr += "disconnected from " + brokerState.getBrokerAddress() + "\n";
     } catch (ClientException e) {
       outStr += e.getMessage() + "\n";
     }
   }
   return outStr;
 }
Example #8
0
 public UnadvertisementMessage unAdvertise(String advID) throws ClientException {
   UnadvertisementMessage resultMsg = null;
   boolean sendRequest = true;
   for (BrokerState brokerState : brokerStates.values()) {
     sendRequest = true;
     if (clientConfig.detailState) sendRequest = brokerState.containsAdv(advID);
     if (sendRequest) {
       resultMsg = unAdvertise(advID, brokerState);
     }
   }
   if (!sendRequest) throw new ClientException("Advertisement not found");
   return resultMsg;
 }
Example #9
0
 public UncompositesubscriptionMessage unSubscribeCS(String csID) throws ClientException {
   UncompositesubscriptionMessage resultMsg = null;
   boolean sendRequest = true;
   for (BrokerState brokerState : brokerStates.values()) {
     sendRequest = true;
     if (clientConfig.detailState) sendRequest = brokerState.containsCS(csID);
     if (sendRequest) {
       resultMsg = unSubscribeCS(csID, brokerState);
     }
   }
   if (!sendRequest) throw new ClientException("Composite subscription not found");
   return resultMsg;
 }
Example #10
0
  /**
   * Send a subscription to a broker with the given URI. In case the brokerURI is null, the
   * subscription will be sent to the default broker.
   *
   * @param sub The subscription to be sent.
   * @param brokerURI The broker to which the subscription is to be sent.
   * @return The SubscriptionMessage containing the given subscription.
   * @throws ClientException Upon the following situations:
   *     <ul>
   *       <li>The client is not connected to the specified broker.
   *       <li>Given brokerURI is badly formated.
   *       <li>There is an error in sending the subscription message.
   *     </ul>
   */
  public SubscriptionMessage subscribe(Subscription sub, String brokerURI) throws ClientException {
    if (!isConnected()) throw new ClientException("Not connected to any broker");
    try {
      if (brokerURI == null || brokerURI.equals("")) brokerURI = defaultBrokerAddress.getNodeURI();
      BrokerState brokerState = getBrokerState(brokerURI);
      if (brokerState == null) {
        throw new ClientException("Not connected to broker " + brokerURI);
      }
      MessageDestination clientDest =
          MessageDestination.formatClientDestination(
              clientID, brokerState.getBrokerAddress().getNodeURI());
      SubscriptionMessage subMsg =
          new SubscriptionMessage(
              sub, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);

      // TODO: fix this hack for historic queries
      Map<String, Predicate> predMap = subMsg.getSubscription().getPredicateMap();
      if (predMap.get("_start_time") != null) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        try {
          Date startTime = timeFormat.parse((String) (predMap.get("_start_time")).getValue());
          predMap.remove("_start_time");
          subMsg.setStartTime(startTime);
        } catch (java.text.ParseException e) {
          exceptionLogger.error("Fail to convert Date format : " + e);
        }
      }
      if (predMap.get("_end_time") != null) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        try {
          Date endTime = timeFormat.parse((String) (predMap.get("_end_time")).getValue());
          predMap.remove("_end_time");
          subMsg.setEndTime(endTime);
        } catch (java.text.ParseException e) {
          exceptionLogger.error("Fail to convert Date format : " + e);
        }
      }

      String msgID = brokerState.getMsgSender().send(subMsg, HostType.CLIENT);
      subMsg.setMessageID(msgID);
      if (clientConfig.detailState) brokerState.addSubMsg(subMsg);
      return subMsg;
    } catch (CommunicationException e) {
      throw new ClientException(e.getMessage());
    }
  }
Example #11
0
 protected UnadvertisementMessage unAdvertise(String advID, BrokerState brokerState)
     throws ClientException {
   try {
     Unadvertisement unAdv = new Unadvertisement(advID);
     MessageDestination clientDest =
         MessageDestination.formatClientDestination(
             clientID, brokerState.getBrokerAddress().getNodeURI());
     UnadvertisementMessage unAdvMsg =
         new UnadvertisementMessage(
             unAdv, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     String msgID = brokerState.getMsgSender().send(unAdvMsg, HostType.CLIENT);
     unAdvMsg.setMessageID(msgID);
     if (clientConfig.detailState) brokerState.removeAdvMsg(advID);
     return unAdvMsg;
   } catch (CommunicationException e) {
     throw new ClientException(e.getMessage());
   }
 }
Example #12
0
 protected UncompositesubscriptionMessage unSubscribeCS(String csID, BrokerState brokerState)
     throws ClientException {
   try {
     Uncompositesubscription unCS = new Uncompositesubscription(csID);
     MessageDestination clientDest =
         MessageDestination.formatClientDestination(
             clientID, brokerState.getBrokerAddress().getNodeURI());
     UncompositesubscriptionMessage unCSMsg =
         new UncompositesubscriptionMessage(
             unCS, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     String msgID = brokerState.getMsgSender().send(unCSMsg, HostType.CLIENT);
     unCSMsg.setMessageID(msgID);
     if (clientConfig.detailState) brokerState.removeCSMsg(csID);
     return unCSMsg;
   } catch (CommunicationException e) {
     throw new ClientException(e.getMessage());
   }
 }
Example #13
0
 protected void unAdvertiseAll(BrokerState brokerState) throws ClientException {
   if (!clientConfig.detailState)
     throw new ClientException("unAdertiseAll() not supported with client.store_detail_state=OFF");
   MessageDestination clientDest =
       MessageDestination.formatClientDestination(
           clientID, brokerState.getBrokerAddress().getNodeURI());
   MessageSender msgSender = brokerState.getMsgSender();
   if (msgSender == null)
     throw new ClientException(
         "Connection not found for broker " + brokerState.getBrokerAddress());
   AdvertisementMessage[] advMsgArray =
       brokerState.getAdvMessages().toArray(new AdvertisementMessage[0]);
   for (AdvertisementMessage advMsg : advMsgArray) {
     Unadvertisement unAdv = new Unadvertisement(advMsg.getMessageID());
     UnadvertisementMessage unAdvMsg =
         new UnadvertisementMessage(
             unAdv, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     try {
       msgSender.send(unAdvMsg, HostType.CLIENT);
       brokerState.removeAdvMsg(advMsg);
     } catch (CommunicationException e) {
       throw new ClientException(e.getMessage());
     }
   }
 }
Example #14
0
 protected void unsubscribeCSAll(BrokerState brokerState) throws ClientException {
   if (!clientConfig.detailState)
     throw new ClientException(
         "unsubscribeCSAll() not supported with client.store_detail_state=OFF");
   MessageDestination clientDest =
       MessageDestination.formatClientDestination(
           clientID, brokerState.getBrokerAddress().getNodeURI());
   MessageSender msgSender = brokerState.getMsgSender();
   if (msgSender == null)
     throw new ClientException(
         "Connection not found for broker " + brokerState.getBrokerAddress());
   CompositeSubscriptionMessage[] csMsgArray =
       brokerState.getCSMessages().toArray(new CompositeSubscriptionMessage[0]);
   for (CompositeSubscriptionMessage csMsg : csMsgArray) {
     Uncompositesubscription unCS = new Uncompositesubscription(csMsg.getMessageID());
     UncompositesubscriptionMessage unCSMsg =
         new UncompositesubscriptionMessage(
             unCS, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     try {
       msgSender.send(unCSMsg, HostType.CLIENT);
       brokerState.removeCSMsg(csMsg);
     } catch (CommunicationException e) {
       throw new ClientException(e.getMessage());
     }
   }
 }
Example #15
0
 public PublicationMessage publish(Publication pub, String brokerURI) throws ClientException {
   if (!isConnected()) throw new ClientException("Not connected to any broker");
   try {
     if (brokerURI == null || brokerURI.trim().length() == 0)
       brokerURI = defaultBrokerAddress.getNodeURI();
     BrokerState brokerState = getBrokerState(brokerURI);
     if (brokerState == null) {
       throw new ClientException("Not connected to broker " + brokerURI);
     }
     MessageDestination clientDest =
         MessageDestination.formatClientDestination(
             clientID, brokerState.getBrokerAddress().getNodeURI());
     PublicationMessage pubMsg =
         new PublicationMessage(
             pub, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     pubCount++;
     String msgID = brokerState.getMsgSender().send(pubMsg, HostType.CLIENT);
     pubMsg.setMessageID(msgID);
     return pubMsg;
   } catch (CommunicationException e) {
     throw new ClientException(e.getMessage());
   }
 }
Example #16
0
 /**
  * Sends an advertisement to a given broker.
  *
  * @param adv The advertisement to be sent. It is an {@link Advertisement} object.
  * @param brokerURI The URI of the broker to where the advertisement is sent.
  * @return The {@link AdvertisementMessage} produced by the given advertisement. The message ID of
  *     the message is returned by the broker.
  * @throws ClientException If the given URI is malformatted, the client is not connected to the
  *     broker, or there is a communication error while sending the advertisement.
  */
 public AdvertisementMessage advertise(Advertisement adv, String brokerURI)
     throws ClientException {
   if (!isConnected()) throw new ClientException("Not connected to any broker");
   try {
     if (brokerURI == null || brokerURI.equals("")) brokerURI = defaultBrokerAddress.getNodeURI();
     BrokerState brokerState = getBrokerState(brokerURI);
     if (brokerState == null) {
       throw new ClientException("Not connected to broker " + brokerURI);
     }
     MessageDestination clientDest =
         MessageDestination.formatClientDestination(
             clientID, brokerState.getBrokerAddress().getNodeURI());
     AdvertisementMessage advMsg =
         new AdvertisementMessage(
             adv, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     String msgID = brokerState.getMsgSender().send(advMsg, HostType.CLIENT);
     advMsg.setMessageID(msgID);
     if (clientConfig.detailState) brokerState.addAdvMsg(advMsg);
     return advMsg;
   } catch (CommunicationException e) {
     throw new ClientException(e.getMessage());
   }
 }
Example #17
0
 /**
  * Disconnects from a broker with a specified BrokerState. The client first
  * unsubscribe/unadvertise all the subscriptions/advertisements before disconnecting from brokers.
  *
  * @param brokerState the BrokerState of the broker to disconnect from
  * @return The given broker state.
  * @throws ClientException Some exeception is unsubscribing/unadvertising or some other
  *     communication error.
  */
 protected BrokerState disconnect(BrokerState brokerState) throws ClientException {
   try {
     // Withdraw all the messages
     if (clientConfig.detailState) {
       unsubscribeAll(brokerState);
       unsubscribeCSAll(brokerState);
       unAdvertiseAll(brokerState);
     }
     // disconnect
     MessageSender msgSender = brokerState.getMsgSender();
     msgSender.disconnect(
         MessageDestination.formatClientDestination(
             clientID, brokerState.getBrokerAddress().getNodeURI()));
     // remove the broker state
     brokerStates.remove(brokerState.getBrokerAddress());
   } catch (CommunicationException e) {
     throw new ClientException(
         String.format(
             "Problem disconnecting from Broker %s: %s",
             brokerState.getBrokerAddress(), e.getMessage()));
   }
   return brokerState;
 }
Example #18
0
 public CompositeSubscriptionMessage subscribeCS(CompositeSubscription cs, String brokerURI)
     throws ClientException {
   if (!isConnected()) throw new ClientException("Not connected to any broker");
   try {
     if (brokerURI == null || brokerURI.trim().equals(""))
       brokerURI = defaultBrokerAddress.getNodeURI();
     BrokerState brokerState = getBrokerState(brokerURI);
     if (brokerState == null) {
       throw new ClientException("Not connected to broker " + brokerURI);
     }
     MessageDestination clientDest =
         MessageDestination.formatClientDestination(
             clientID, brokerState.getBrokerAddress().getNodeURI());
     CompositeSubscriptionMessage newCSMsg =
         new CompositeSubscriptionMessage(
             cs, getNextMessageID(brokerState.getBrokerAddress().getNodeURI()), clientDest);
     String msgID = brokerState.getMsgSender().send(newCSMsg, HostType.CLIENT);
     newCSMsg.setMessageID(msgID);
     if (clientConfig.detailState) brokerState.addCSSubMsg(newCSMsg);
     return newCSMsg;
   } catch (CommunicationException e) {
     throw new ClientException(e.getMessage());
   }
 }
Example #19
0
 /**
  * Create a new broker state for the given broker URI and add it to the list managed by this
  * client. If the a non-null message sender is pass on to this method, it will be associated with
  * the broker state being added
  *
  * @param brokerAddress
  * @param msgSender
  * @return
  */
 protected BrokerState addBrokerState(NodeAddress brokerAddress, MessageSender msgSender) {
   BrokerState newBrokerState = createBrokerState(brokerAddress);
   if (msgSender != null) newBrokerState.setMsgSender(msgSender);
   brokerStates.put(brokerAddress, newBrokerState);
   return newBrokerState;
 }