/** {@inheritDoc} */ public boolean send(Message message) throws IOException { if (closed) { throw new IOException("Pipe closed"); } Message msg = message.clone(); WireHeader header = new WireHeader(); header.setPipeID(getPipeID()); header.setSrcPeer(group.getPeerID()); header.setTTL(1); header.setMsgId(WirePipe.createMsgId()); XMLDocument asDoc = (XMLDocument) header.getDocument(MimeMediaType.XMLUTF8); MessageElement elem = new TextDocumentMessageElement(WirePipeImpl.WIRE_HEADER_ELEMENT_NAME, asDoc, null); msg.replaceMessageElement(WirePipeImpl.WIRE_HEADER_ELEMENT_NAMESPACE, elem); checkMessenger(); try { destMessenger.sendMessageB(msg, null, null); } catch (IOException io) { checkMessenger(); destMessenger.sendMessageB(msg, null, null); } return true; }
/** * Send a message to the peer which is represented by the current PeerViewElement. * * @param msg the message to send * @param serviceName the service name on the destination peer to which the message will be * demultiplexed * @param serviceParam the service param on the destination peer to which the message will be * demultiplexed * @return true if the message was successfully handed off to the endpoint for delivery, false * otherwise */ public boolean sendMessage(Message msg, String serviceName, String serviceParam) { if (throttling) { if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Declining to send -- throttling on " + this); } return false; } Messenger sendVia = getCachedMessenger(); if (null == sendVia) { // There is nothing really we can do. if (LOG.isEnabledFor(Level.WARN)) { LOG.warn("Could not get messenger for " + getDestAddress()); } OutgoingMessageEvent event = new OutgoingMessageEvent( msg, new IOException("Couldn't get messenger for " + getDestAddress())); messageSendFailed(event); return false; } sendVia.sendMessage(msg, serviceName, serviceParam, this); return true; }
/** * Check the messenger to the remote peer and attempt to re-create the messenger if necessary. * * @return {@code true} if the current messenger is adequate. * @throws IOException is thrown for errors in creating a new messenger. */ private synchronized boolean checkMessenger() throws IOException { if ((destMessenger != null) && ((destMessenger.getState() & Messenger.USABLE) != 0)) { // Everything fine! return true; } // Try making a direct messenger first. if (route != null) { destMessenger = endpoint.getDirectMessenger(destination, route, true); } // Try making a regular messenger if that didn't work. if ((destMessenger == null) || ((destMessenger.getState() & Messenger.TERMINAL) != 0)) { destMessenger = null; destMessenger = endpoint.getMessenger(destination, route); } // Dismal failure if ((destMessenger == null) || ((destMessenger.getState() & Messenger.TERMINAL) != 0)) { destMessenger = null; throw new IOException("Unable to create a messenger to " + destination.toString()); } return true; }
/** * Return a messenger suitable for sending to this peer. * * @return a messenger to this PVE peer or if <code>null</code> if peer is unreachable. */ private Messenger getCachedMessenger() { boolean updateAlive = false; synchronized (this) { if ((null == cachedMessenger) || cachedMessenger.isClosed()) { cachedMessenger = null; if (LOG.isEnabledFor(Level.DEBUG)) { LOG.debug("Getting cached Messenger for " + radv.getName()); } updateAlive = true; cachedMessenger = endpoint.getMessenger(getDestAddress(), radv.getRouteAdv()); } } if (updateAlive) { setAlive(null != cachedMessenger); } return cachedMessenger; }