Search in sources :

Example 6 with FCPublisher

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

the class TestEchoServer method startTestTopic.

public static void startTestTopic(FastCast fc, final long startUpTime) {
    fc.onTransport("default").subscribe("test", new ObjectSubscriber() {

        @Override
        protected void objectReceived(String sender, long sequence, Object msg) {
            System.out.println("received Object:" + msg);
        }
    });
    FCPublisher testPublisher = fc.onTransport("default").publish("test");
    final ObjectPublisher objectPublisher = new ObjectPublisher(testPublisher);
    new Thread("sender") {

        public void run() {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                objectPublisher.sendObject(null, new SampleBroadcast(startUpTime), true);
            }
        }
    }.start();
}
Also used : FCPublisher(org.nustaq.fastcast.api.FCPublisher) ObjectPublisher(org.nustaq.fastcast.api.util.ObjectPublisher) ObjectSubscriber(org.nustaq.fastcast.api.util.ObjectSubscriber)

Example 7 with FCPublisher

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

the class Issue4_Slow method initFC.

public void initFC() {
    if (fc == null) {
        fc = setupFC("t" + (int) (1000 * Math.random()), "stuff/sendreceive.kson");
        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 {
                    Object[] i_time = (Object[]) msg;
                    System.out.println(fc.getNodeId() + " received " + i_time[0] + " delay: " + (System.currentTimeMillis() - (Long) i_time[1]));
                    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);
        FCPublisher rawPublisher = fc.onTransport("default").publish(fc.getPublisherConf("sendreceive"));
        publisher = new ObjectPublisher(rawPublisher);
    }
}
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 8 with FCPublisher

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

the class FCPing method runPingClientSync.

// CO !
public void runPingClientSync() throws InterruptedException {
    final FastCast fc = initFC("pclie", "pingponglat.kson");
    final FCPublisher pingserver = fc.onTransport("ping").publish(fc.getPublisherConf("pingtopic"));
    final Executor ex = Executors.newSingleThreadExecutor();
    final AtomicInteger await = new AtomicInteger(0);
    fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").subscribe(fc.getSubscriberConf("pongtopic"), new FCSubscriber() {

        String pongName;

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            await.decrementAndGet();
        }

        @Override
        public boolean dropped() {
            // reset
            await.set(0);
            System.out.println("Drop and Reset counter !");
            return true;
        }

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

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
            pongName = receivesFrom;
        }
    });
    // just create a byte[] for each struct (*)
    FSTStructAllocator alloc = new FSTStructAllocator(0);
    PingRequest pr = alloc.newStruct(new PingRequest());
    Histogram histo = new Histogram(TimeUnit.SECONDS.toNanos(10), 3);
    // wait for at least one heartbeat
    Thread.sleep(1000);
    System.out.println("starting ping pong " + randomId);
    System.gc();
    pr.setNanoSendTime(randomId);
    Sleeper sl = new Sleeper();
    while (true) {
        await.set(0);
        for (int i = 0; i < NUM_MSG; i++) {
            // sl.sleepMicros(100);
            pingAndAwaitPong(pingserver, await, pr, histo, i);
        }
        histo.outputPercentileDistribution(System.out, 1000.0);
        histo.reset();
        System.gc();
    }
}
Also used : Histogram(org.HdrHistogram.Histogram) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) Sleeper(org.nustaq.fastcast.util.Sleeper) FSTStructAllocator(org.nustaq.offheap.structs.FSTStructAllocator) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber) Executor(java.util.concurrent.Executor) FastCast(org.nustaq.fastcast.api.FastCast) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 9 with FCPublisher

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

the class FCPing method runPongServer.

public void runPongServer() throws InterruptedException {
    FastCast fc = initFC("pserv", "pingponglat.kson");
    final FCPublisher echoresp = fc.onTransport(PING_BACK_ON_SAME_TOPIC ? "ping" : "pong").publish(fc.getPublisherConf("pongtopic"));
    fc.onTransport("ping").subscribe(fc.getSubscriberConf("pingtopic"), new FCSubscriber() {

        @Override
        public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
            // System.out.println("snd:"+sender);
            while (!echoresp.offer(sender, b, off, len, true)) {
                // ensure retrans processing etc.
                echoresp.flush();
            }
        }

        @Override
        public boolean dropped() {
            return true;
        }

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

        @Override
        public void senderBootstrapped(String receivesFrom, long seqNo) {
            System.out.println("bootstrap " + receivesFrom);
        }
    });
    while (true) {
        Thread.sleep(100000);
    }
}
Also used : FastCast(org.nustaq.fastcast.api.FastCast) FCPublisher(org.nustaq.fastcast.api.FCPublisher) Bytez(org.nustaq.offheap.bytez.Bytez) FCSubscriber(org.nustaq.fastcast.api.FCSubscriber)

Example 10 with FCPublisher

use of org.nustaq.fastcast.api.FCPublisher 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)

Aggregations

FCPublisher (org.nustaq.fastcast.api.FCPublisher)11 FCSubscriber (org.nustaq.fastcast.api.FCSubscriber)5 ObjectPublisher (org.nustaq.fastcast.api.util.ObjectPublisher)5 FastCast (org.nustaq.fastcast.api.FastCast)4 RateMeasure (org.nustaq.fastcast.util.RateMeasure)4 FSTStructAllocator (org.nustaq.offheap.structs.FSTStructAllocator)4 Executor (java.util.concurrent.Executor)3 ObjectSubscriber (org.nustaq.fastcast.api.util.ObjectSubscriber)3 PublisherConf (org.nustaq.fastcast.config.PublisherConf)3 Bytez (org.nustaq.offheap.bytez.Bytez)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Histogram (org.HdrHistogram.Histogram)2 Sleeper (org.nustaq.fastcast.util.Sleeper)2 File (java.io.File)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FSTStruct (org.nustaq.offheap.structs.FSTStruct)1