Search in sources :

Example 11 with FCSubscriber

use of org.nustaq.fastcast.api.FCSubscriber in project fast-cast by RuedigerMoeller.

the class TransportDriver method receiveDatagram.

private boolean receiveDatagram(ByteBuffer p, byte[] wrappedArr) throws IOException {
    if (trans.receive(p)) {
        boolean selfSent = receivedPacket.getSender().equals(nodeId);
        if (!selfSent) {
            // debug
            if (RETRANS_DEBUG) {
                Class debugtype = receivedPacket.getPointedClass();
                if (debugtype == RetransPacket.class) {
                    RetransPacket retransPacket = receivedPacket.cast().detach();
                    System.out.println("retrans received " + retransPacket);
                }
            }
            // 
            int topic = receivedPacket.getTopic();
            if (topic > MAX_NUM_TOPICS || topic < 0) {
                FCLog.get().warn("foreign traffic");
                return true;
            }
            if (receiver[topic] == null && sender[topic] == null) {
                return true;
            }
            Class type = receivedPacket.getPointedClass();
            StructString receivedPacketReceiver = receivedPacket.getReceiver();
            if (type == DataPacket.class) {
                if (receiver[topic] == null)
                    return true;
                dispatchDataPacket(receivedPacket, topic);
            } else if (type == RetransPacket.class) {
                if (sender[topic] == null)
                    return true;
                if (receivedPacketReceiver == null || !receivedPacketReceiver.equals(nodeId)) {
                    return true;
                }
                if (RETRANS_DEBUG)
                    System.out.println("retrans dispatched ");
                dispatchRetransmissionRequest(receivedPacket, topic);
            } else if (type == ControlPacket.class) {
                if (isForeignReceiver(receivedPacketReceiver)) {
                    return true;
                }
                ControlPacket control = receivedPacket.cast();
                if (control.getType() == ControlPacket.DROPPED) {
                    ReceiveBufferDispatcher receiveBufferDispatcher = receiver[topic];
                    if (receiveBufferDispatcher != null) {
                        FCLog.get().warn(nodeId + " has been dropped by " + receivedPacket.getSender() + " on service " + receiveBufferDispatcher.getTopicEntry().getTopicId());
                        FCSubscriber service = receiveBufferDispatcher.getTopicEntry().getSubscriber();
                        if (service != null) {
                            if (service.dropped()) {
                                // retry if returns true
                                FCLog.get().warn("..resyncing..");
                                PacketReceiveBuffer buffer = receiveBufferDispatcher.getBuffer(receivedPacket.getSender());
                                if (buffer != null) {
                                    buffer.resync();
                                } else {
                                    FCLog.get().warn("unexpected null buffer");
                                }
                            } else {
                                // topic is lost forever now ..
                                receiver[topic] = null;
                                receiveBufferDispatcher.cleanupTopic();
                            }
                        }
                    }
                }
            // heartbeats are sent as regular data packets in order to have initialSync to happen correctly
            // else if ( control.getType() == ControlPacket.HEARTBEAT ) {
            // ReceiveBufferDispatcher receiveBufferDispatcher = receiver[topic];
            // if ( receiveBufferDispatcher != null ) {
            // PacketReceiveBuffer buffer = receiveBufferDispatcher.getBuffer(control.getSender());
            // if ( buffer != null ) {
            // buffer.updateHeartBeat(control.getSeqNo(),System.currentTimeMillis());
            // }
            // }
            // }
            }
            return true;
        } else {
            return false;
        }
    }
    return false;
}
Also used : StructString(org.nustaq.offheap.structs.structtypes.StructString) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Aggregations

FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)11 Bytez (org.nustaq.offheap.bytez.Bytez)7 FCPublisher (org.nustaq.fastcast.api.FCPublisher)5 StructString (org.nustaq.offheap.structs.structtypes.StructString)4 Histogram (org.HdrHistogram.Histogram)3 FastCast (org.nustaq.fastcast.api.FastCast)3 RateMeasure (org.nustaq.fastcast.util.RateMeasure)3 FSTStruct (org.nustaq.offheap.structs.FSTStruct)3 Executor (java.util.concurrent.Executor)2 ObjectPublisher (org.nustaq.fastcast.api.util.ObjectPublisher)2 ObjectSubscriber (org.nustaq.fastcast.api.util.ObjectSubscriber)2 SubscriberConf (org.nustaq.fastcast.config.SubscriberConf)2 Sleeper (org.nustaq.fastcast.util.Sleeper)2 FSTStructAllocator (org.nustaq.offheap.structs.FSTStructAllocator)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HeapBytez (org.nustaq.offheap.bytez.onheap.HeapBytez)1