/** * 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()); } }