Package co.paralleluniverse.galaxy.core

Examples of co.paralleluniverse.galaxy.core.Message


            if (getCluster().getMyAddress() != null && msg.getSrc() != null && getCluster().getMyAddress().equals(msg.getSrc()))
                return; // discard own (cannot set the flag because it screws up th control channel. not much to do about it - annoing up handler in JChannel)
            final byte[] buffer = msg.getRawBuffer();
            if (buffer.length == 0)
                return; // probably just a flush
            final Message message = Message.fromByteArray(buffer);
            final Address source = msg.getSrc();

            if (message.isResponse()) {
                final Deque<Message> pending = pendingReply.get(message.getNode());

                if (pending != null) {
                    boolean res = pending.removeLastOccurrence(message); // relies on Message.equals that matches request/reply
                    if (res)
                        LOG.debug("Message {} is a reply! (removing from pending)", message);
                }
            }

            if (message.isResponse()) {
                final BroadcastEntry entry = pendingBroadcasts.get(message.getMessageId());
                if (entry != null) {
                    if (message.getType() != Message.Type.ACK) {// this is a response - no need to wait for further acks
                        LOG.debug("Message {} is a reply to a broadcast! (discarding pending)", message);
                        pendingBroadcasts.remove(message.getMessageId());
                    } else
                        removeFromPendingBroadcasts(message.getMessageId(), message.getNode());
                }
            }

            final short sourceNode = getNode(source);
            if (sourceNode < 0)
                throw new RuntimeException("Node not found for source address " + source);
            message.setNode(sourceNode);
            receive(message);
        } catch (Exception ex) {
            LOG.error("Error receiving message", ex);
        }
    }
View Full Code Here


            LOG.info("Node peer {} set address to {}", this, nodeAddress);
            this.nodeAddress = nodeAddress;
            lastReceivedBroadcastId = 0;
            if (sentPacket != null) {
                for (Iterator<Message> it = sentPacket.iterator(); it.hasNext();) {
                    final Message message = it.next();
                    if (message.isResponse()) {
                        LOG.debug("Peer {} removing response {} because of node switch.", this, message);
                        it.remove(); // if our peer hasn't requested again then it must have received our response
                    }
                }
            }
View Full Code Here

            boolean oobMulticast = false;
            if (receivedPacket.isMulticast()) { // multicast messages may overlap with unicast ones if the original broadcast was sent as a unicast, say if the peers sentPacket wasn't empty
                long maxIdInPacket = -1;
                for (Iterator<Message> it = receivedPacket.iterator(); it.hasNext();) {
                    final Message message = it.next();
//                    if (message.getMessageId() < lastReceivedBroadcastId) {
//                        LOG.trace("Peer {} received a multicast message {} which has already been seen.", this, message);
//                        it.remove();
//                    }
                    maxIdInPacket = Math.max(maxIdInPacket, message.getMessageId());
                }
                if (maxIdInPacket < lastReceivedBroadcastId) {
                    LOG.debug("Peer {} received an out-of-band multicast packet {} which has already been seen.", this, receivedPacket);
                    oobMulticast = true;
                }
            }
            if (receivedPacket.isEmpty())
                return;

            if (!oobMulticast && sentPacket != null) {
                for (Iterator<Message> it = sentPacket.iterator(); it.hasNext();) {
                    final Message message = it.next();
                    // here we rely on Message.equals() to match request/response
                    if (message.isResponse() && !receivedPacket.contains(message)) {
                        LOG.debug("Peer {} removing response {} from sent packet because it was no longer asked for.", this, message);
                        it.remove(); // if our peer hasn't requested again then it must have received our response
                    }
                }
            }
            for (Message message : receivedPacket) {
                message.setTimestamp(receivedPacket.getTimestamp());
                if (message.isBroadcast()) {
                    if (message.getMessageId() > lastReceivedBroadcastId)
                        lastReceivedBroadcastId = message.getMessageId();
                }
                // here we rely on Message.equals() to match request/response
                if (message.isResponse()) {
                    final Message request = (sentPacket != null ? sentPacket.getMessage(message) : null);
                    if (request == null && !(isTimeout(message) || (broadcast && broadcastPeer.isTimeout(message)))) {
                        LOG.debug("Peer {} ignoring repeat response {}", this, message);
                        continue; // we may be re-receiving the response, so the request may be gone. in this case we don't need to pass the message again to the receiver
                    }
                    if (LOG.isDebugEnabled())
                        LOG.debug("Peer {} received response {} for request ({})", new Object[]{this, message, request != null ? request : "TIMEOUT"});
                    if (request != null) {
                        if (request.isBroadcast())
                            broadcastResponses.add(message);

//                        if(message.getType() == Message.Type.CHNGD_OWNR && ((Message.CHNGD_OWNR)message).getNewOwner() == message.getNode()) {
//                            // this is a quickReplyToBroadcast
//                            // TODO
View Full Code Here

            if (broadcast || sentPacket == null || sentPacket.isEmpty())
                return;

            final long timeoutNanos = NANOSECONDS.convert(getTimeout(), MILLISECONDS);
            for (Iterator<Message> it = sentPacket.reverseIterator(); it.hasNext();) {
                final Message message = it.next();
                if (message.getType() != Message.Type.INV && now - message.getTimestamp() > timeoutNanos) {
                    if (message.isResponse() || message.isBroadcast())
                        continue;
                    if (message instanceof LineMessage) {
                        LOG.debug("Timeout on message {}", message);
                        received.add(Message.TIMEOUT((LineMessage) message).setIncoming());
                    }
View Full Code Here

            // the packet, only it can't b/c its sentPacket is also full - we got a deadlock.
            // as I see it, the only way to truly resolve it is to have multi-part packets, but we don't want to do that.
            // what we do is that we don't allow a packet with requests only to be full - we always leave room for a response.

            // assumes hasRequests and requestsOnly are up to date.
            Message next = overflow;
            overflow = null;
            if (next == null)
                next = queue.poll();
            for (;;) {
                LOG.trace("handleQueue loop");
                if (next == null) {
                    LOG.trace("handleQueue loop: next == null");
                    break;
                }
                overflow = next; // we put the next message into overflow. if we _don't_ break out of the loop and use the message, we'll null overflow

                final boolean unicastBroadcast = next.isBroadcast() && unicastBroadcasts.remove(next);

                if (broadcast && (!next.isBroadcast() || unicastBroadcast)) {
                    LOG.trace("Node peer {} not taking non-broadcast message {} during broadcast", this, next);
                    break; // we're not taking any non-broadcast messages during broadcast
                }

                if (!broadcast && next.isBroadcast() && !unicastBroadcast) {
                    if (sentPacket == null || sentPacket.isEmpty()) {
                        LOG.debug("Node peer {} going into broadcast mode for message {}.", this, next);
                        broadcast = true;
                    }
                    // else, we add message to packet, and continue transmitting.
                    // if the packet had responses only, the new broadcast request would force a re-send and expedite matters
                    // if a response for the broadcast is received before we get a chance to multicast, that's ok because we simply remove the node
                    // from the BroadcastEntry
                }

                if (next.size() > maxPacketSize) {
                    LOG.error("Message {} is larger than the maximum packet size {}", next, maxPacketSize);
                    throw new RuntimeException("Message is larger than maxPacketSize");
                }

                if (next.size() + sentPacketSizeInBytes() > maxPacketSize) {
                    if (next.isResponse() && requestsOnly)
                        LOG.warn("IMPORTANT: Response message {} does not fit in packet {} which contains only requests. THIS MAY CAUSE A DEADLOCK!", next, sentPacket);
                    LOG.debug("Message {} cannot be added to packet now; packet full.");
                    break;
                }

                if (!next.isResponse()) {
                    if (requestsOnly && next.size() + sentPacketSizeInBytes() > maxRequestOnlyPacketSize && sentPacketSizeInBytes() > 0) {
                        // check if packet consists of requestOnly message unless it is only one message.
                        LOG.debug("NOT Sending requests only {}. can't add to packet {} bytes long.", next, sentPacketSizeInBytes());
                        break;
                    }
                    hasRequests = true;
                } else
                    requestsOnly = false;

                if (next.isResponse())
                    pendingRequests.remove(next.getMessageId());

                LOG.debug("Adding message {} to sent-packet", next);
                if (sentPacket == null)
                    sentPacket = new MessagePacket();
                sentPacket.addMessage(next);
View Full Code Here

            LOG.trace("BroadcastPeer CALL DONE");
            return null;
        }

        private void handleQueue(long start) throws InterruptedException {
            Message next = overflow;
            overflow = null;
            if (next == null)
                next = queue.poll();
            loop:
            for (;;) {
                if (next == null)
                    break;

                overflow = next; // we put the next message into overflow. if we _don't_ break out of the loop and use the message, we'll null overflow

                if (next.size() > maxPacketSize) {
                    LOG.error("Message {} is larger than the maximum packet size {}", next, maxPacketSize);
                    throw new RuntimeException("Message is larger than maxPacketSize");
                }

                if (sentPacket != null && next.size() + sentPacket.sizeInBytes() > maxPacketSize)
                    break;

                LOG.debug("Waiting for peers to enter broadcast mode for message {}", next);
                BroadcastEntry entry = broadcasts.get(next.getMessageId());

                if (entry != null) {
                    if (entry.nodes.isEmpty()) {
                        broadcasts.remove(next.getMessageId());
                        if (next instanceof LineMessage) {
                            LOG.debug("No other nodes in cluster. Responding with NOT_FOUND to message {}", next);
                            receive(Message.NOT_FOUND((LineMessage) next).setIncoming());
                        }
                        entry = null;
                    }
                }

                if (entry != null) {
                    for (TShortIterator it = entry.nodes.iterator(); it.hasNext();) {
                        final short node = it.next();
                        final NodePeer peer = peers.get(node);
                        synchronized (peer) {
                            if (!(peer.isBroadcast() && peer.sentPacket.contains(next.getMessageId()))) {
                                LOG.trace("Waiting for peer {}.", peer);
                                break loop;
                            }
                            LOG.trace("Peer {} ok (broadcast {})", peer, next);
                        }
View Full Code Here

                return;
            final long timeoutNanos = NANOSECONDS.convert(getTimeout(), MILLISECONDS);

            for (Iterator<BroadcastEntry> it = broadcasts.values().iterator(); it.hasNext();) {
                final BroadcastEntry entry = it.next();
                final Message message = entry.message;
                if (message.getType() != Message.Type.INV && now - message.getTimestamp() > timeoutNanos) {
                    if (message instanceof LineMessage) {
                        LOG.debug("Timeout on message {}", message);
                        received.add(Message.TIMEOUT((LineMessage) message).setIncoming());
                    }
                    it.remove();
                    releasePeers(entry, (short) -1);
                    addTimeout(message);
                    if (sentPacket != null)
                        sentPacket.removeMessage(message.getMessageId());
                }
            }
            if (sentPacket != null && sentPacket.isEmpty())
                sentPacket = null;
View Full Code Here

                    sentPacket = null;
            }
        }

        private void releasePeers(BroadcastEntry entry, short node) {
            final Message message = entry.message;
            for (TShortIterator it = entry.nodes.iterator(); it.hasNext();) {
                final NodePeer peer = peers.get(it.next());
                if (peer.isBroadcast()) {
                    LOG.debug("Broadcast releasing peer {} for message {}", peer, message);
                    if (peer.node != node) {
View Full Code Here

            LOG.info("Node peer {} set address to {}", this, nodeAddress);
            this.nodeAddress = nodeAddress;
            lastReceivedBroadcastId = 0;
            if (sentPacket != null) {
                for (Iterator<Message> it = sentPacket.iterator(); it.hasNext();) {
                    final Message message = it.next();
                    if (message.isResponse()) {
                        LOG.debug("Peer {} removing response {} because of node switch.", this, message);
                        it.remove(); // if our peer hasn't requested again then it must have received our response
                    }
                }
            }
View Full Code Here

            LOG.debug("Peer {} has received packet {}", this, receivedPacket);

            boolean oobMulticast = false;
            if (receivedPacket.isMulticast()) { // multicast messages may overlap with unicast ones if the original broadcast was sent as a unicast, say if the peers sentPacket wasn't empty
                for (Iterator<Message> it = receivedPacket.iterator(); it.hasNext();) {
                    final Message message = it.next();
                    if (message.getMessageId() < lastReceivedBroadcastId) {
                        LOG.debug("Peer {} received an out-of-band multicast message {} which has already been seen.", this, message);
                        oobMulticast = true;
                        it.remove();
                    }
                }
            }
            if (receivedPacket.isEmpty())
                return;

            if (!oobMulticast && sentPacket != null) {
                for (Iterator<Message> it = sentPacket.iterator(); it.hasNext();) {
                    final Message message = it.next();
                    // here we rely on Message.equals() to match request/response
                    if (message.isResponse() && !receivedPacket.contains(message)) {
                        LOG.debug("Peer {} removing response {} from sent packet because it was no longer asked for.", this, message);
                        it.remove(); // if our peer hasn't requested again then it must have received our response
                    }
                }
            }
            for (Message message : receivedPacket) {
                message.setTimestamp(receivedPacket.getTimestamp());
                if (message.isBroadcast()) {
                    if (message.getMessageId() > lastReceivedBroadcastId)
                        lastReceivedBroadcastId = message.getMessageId();
                }
                // here we rely on Message.equals() to match request/response
                if (message.isResponse()) {
                    final Message request = (sentPacket != null ? sentPacket.getMessage(message) : null);
                    if (request == null && !(isTimeout(message) || (broadcast && broadcastPeer.isTimeout(message)))) {
                        LOG.debug("Peer {} ignoring repeat response {}", this, message);
                        continue; // we may be re-receiving the response, so the request may be gone. in this case we don't need to pass the message again to the receiver
                    }
                    if (LOG.isDebugEnabled())
                        LOG.debug("Peer {} received response {} for request ({})", new Object[]{this, message, request != null ? request : "TIMEOUT"});
                    if (request != null) {
                        if (request.isBroadcast())
                            broadcastResponses.add(message);
                        sentPacket.removeMessage(message);
                    }
                } else {
                    if (sentPacket != null && sentPacket.contains(message)) {
View Full Code Here

TOP

Related Classes of co.paralleluniverse.galaxy.core.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.