Search in sources :

Example 6 with ObjectPublisher

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

the class Issue4 method initFC.

public void initFC() {
    if (fc == null) {
        fc = setupFC("t" + (int) (1000 * Math.random()), "stuff/sendreceive.kson");
        FCPublisher rawPublisher = fc.onTransport("default").publish(fc.getPublisherConf("sendreceive"));
        publisher = new ObjectPublisher(rawPublisher);
        FCSubscriber sub = new ObjectSubscriber() {

            int count = 0;

            @Override
            protected void objectReceived(String sender, long sequence, Object msg) {
                if (msg instanceof String) {
                    System.out.println(fc.getNodeId() + " received: " + count);
                    count = 0;
                } else {
                    count++;
                }
            }

            @Override
            public boolean dropped() {
                System.out.println("FATAL ERROR. Enlarge send history");
                System.exit(0);
                return false;
            }

            @Override
            public void senderTerminated(String senderNodeId) {
                System.out.println(senderNodeId + " terminated");
            }

            @Override
            public void senderBootstrapped(String receivesFrom, long seqNo) {
                System.out.println("bootstrap " + receivesFrom);
            }
        };
        fc.onTransport("default").subscribe(fc.getSubscriberConf("sendreceive"), sub);
    }
}
Also used : FCPublisher(org.nustaq.fastcast.api.FCPublisher) ObjectPublisher(org.nustaq.fastcast.api.util.ObjectPublisher) ObjectSubscriber(org.nustaq.fastcast.api.util.ObjectSubscriber) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Example 7 with ObjectPublisher

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

the class ProgrammaticConfiguredPublisher method main.

public static void main(String[] arg) {
    // 5 chars MAX !!
    FastCast.getFastCast().setNodeId("PUB");
    configureFastCast();
    FCPublisher pub = FastCast.getFastCast().onTransport("default").publish(// unique-per-transport topic id
    new PublisherConf(1).numPacketHistory(// how long packets are kept for retransmission requests
    40_000).pps(// packets per second rate limit. So max traffic for topic = 5000*2500 = 12.5 MB/second
    5000));
    // could directly send raw on publisher
    // while( ! pub.offer(..) ) { /* spin */ }
    // or use a helper for fast-serialized messages
    ObjectPublisher opub = new ObjectPublisher(pub);
    RateMeasure measure = new RateMeasure("msg/s");
    while (true) {
        measure.count();
        opub.sendObject(// all listeners should receive (by specifying a nodeId, a specific subscriber can be targeted)
        null, // serializable object
        "Hello " + System.currentTimeMillis(), // allow for 'batching' several messages into one (will create slight latency)
        false);
    }
}
Also used : FCPublisher(org.nustaq.fastcast.api.FCPublisher) ObjectPublisher(org.nustaq.fastcast.api.util.ObjectPublisher) RateMeasure(org.nustaq.fastcast.util.RateMeasure) PublisherConf(org.nustaq.fastcast.config.PublisherConf)

Example 8 with ObjectPublisher

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

the class AsyncLatPublisher method initFastCast.

public void initFastCast() throws Exception {
    fastCast = FastCast.getFastCast();
    fastCast.setNodeId("PUB");
    fastCast.loadConfig(CFG_FILE_PATH);
    pub = new ObjectPublisher(fastCast.onTransport("default").publish("stream"), AsyncLatMessage.class);
    fastCast.onTransport("back").subscribe("back", new ObjectSubscriber(false, AsyncLatMessage.class) {

        @Override
        protected void objectReceived(String s, long l, Object o) {
            if ("END".equals(o)) {
                final Histogram oldHi = hi;
                hi = new Histogram(TimeUnit.SECONDS.toNanos(2), 3);
                // no lambdas to stay 1.7 compatible
                // move printing out of the receiving thread
                dumper.execute(new Runnable() {

                    @Override
                    public void run() {
                        oldHi.outputPercentileDistribution(System.out, 1000.0);
                    }
                });
                // hi.reset();
                return;
            }
            final long value = System.nanoTime() - ((AsyncLatMessage) o).getSendTimeStampNanos();
            if (value < 1_000_000_000)
                hi.recordValue(value);
        }

        @Override
        public boolean dropped() {
            System.exit(-2);
            return false;
        }
    });
}
Also used : Histogram(org.HdrHistogram.Histogram) ObjectPublisher(org.nustaq.fastcast.api.util.ObjectPublisher) ObjectSubscriber(org.nustaq.fastcast.api.util.ObjectSubscriber)

Aggregations

ObjectPublisher (org.nustaq.fastcast.api.util.ObjectPublisher)8 FCPublisher (org.nustaq.fastcast.api.FCPublisher)5 ObjectSubscriber (org.nustaq.fastcast.api.util.ObjectSubscriber)5 RateMeasure (org.nustaq.fastcast.util.RateMeasure)3 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)2 File (java.io.File)1 Histogram (org.HdrHistogram.Histogram)1 Test (org.junit.Test)1 FastCast (org.nustaq.fastcast.api.FastCast)1 PublisherConf (org.nustaq.fastcast.config.PublisherConf)1