/** * Handles a message. Knows type "collectiveUpdate-"+name only. It is the responsibility of the * owner to propagate messages of this type using this method. */ public boolean handleMessage(Message m, Object o) { if (!shouldLive || !m.getType().equals("collectiveUpdate-" + name)) return false; final String logSender = observer + "#collectiveUpdate"; Logger.debug(logSender, "Update from " + m.getSender()); /**/ if (!m.getRecipient().name.equals(contributor.getName())) Logger.warning( logSender, "Recipient and my contributor are not the same:\n" + "Recipient: " + m.getRecipient().name + "\n" + "Contributor: " + contributor.getName(), null); /**/ Collective c = (Collective) o; // --- reset array representations cacheCollection = null; commandCollection = null; // --- remove possible garbage cache.remove(m.getRecipient().name); c.cache.remove(m.getRecipient().name); cache.remove(m.getSender().name); c.cache.remove(m.getSender().name); // --- sending our contributions if (contributor == null) Logger.warning(logSender, "Non-contributor observer is known by " + m.getSender(), null); updateLocalInfo(); m.setReply(this); // --- update containers repairSenderAddress(c, m.getSender()); merge(c); observer.collectiveUpdated((ContributionBox) cache.get(m.getSender().name)); return true; }
/** * The format looks as follows: * * <p>32bit p2p version - 32bit id - 4bit message type - 4bit message name - 160bit sender id - * 16bit tcp port - 16bit udp port - 160bit recipient id - 16bit (4x4)content type - 8bit network * address information. It total, the header is of size 56 bytes. * * @param buffer The Netty buffer to fill * @param message The message with the header that will be serialized * @return The buffer passed as an argument */ public static ChannelBuffer encodeHeader(final ChannelBuffer buffer, final Message message) { buffer.writeInt(message.getVersion()); // 4 buffer.writeInt(message.getMessageId()); // 8 buffer.writeByte((message.getType().ordinal() << 4) | message.getCommand().ordinal()); // 9 buffer.writeBytes(message.getSender().getID().toByteArray()); // 29 buffer.writeShort((short) message.getSender().portTCP()); // 31 buffer.writeShort((short) message.getSender().portUDP()); // 33 buffer.writeBytes(message.getRecipient().getID().toByteArray()); // 53 final int content = ((message.getContentType4().ordinal() << 12) | (message.getContentType3().ordinal() << 8) | (message.getContentType2().ordinal() << 4) | message.getContentType1().ordinal()); buffer.writeShort((short) content); // 55 // options int options = (message.getSender().getOptions() << 4) | message.getOptions(); buffer.writeByte(options); // 56 return buffer; }
/** * Creates a message Id. If the message is a request, the peer address is the sender, otherwise * its the recipient. This is due to the fact that depending on the direction, peer address may * change, but its still considered the same message. * * @param message The message */ public MessageID(final Message message) { this( message.getMessageId(), message.isRequest() ? message.getSender() : message.getRecipient()); }