use of org.nustaq.fastcast.api.FCSubscriber 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);
}
}
use of org.nustaq.fastcast.api.FCSubscriber in project fast-cast by RuedigerMoeller.
the class AsyncPubStruct method initFastCast.
public void initFastCast() throws Exception {
fastCast = FastCast.getFastCast();
fastCast.setNodeId("PUB");
fastCast.loadConfig(CFG_FILE_PATH);
pub = fastCast.onTransport("default").publish("stream");
// pointer to message
final FSTStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class);
fastCast.onTransport("back").subscribe("back", new FCSubscriber() {
@Override
public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
// as structs decode literally in zero time, we can decode inside receiver thread
msg.baseOn(b, (int) off);
Class type = msg.getPointedClass();
if (type == StructString.class) {
// sequence end, print histogram
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);
}
});
} else {
// a regular message, record latency
AsyncLatMessageStruct mdata = msg.cast();
long value = System.nanoTime() - mdata.getSendTimeStampNanos();
if (value < 1_000_000_000) {
// avoid AIOB during init + JITTING
hi.recordValue(value);
}
// a real app would need to copy the message (recycle byte arrays/objects !) and
// run msg processing in an executor to get out of the receiving thread.
// mdata gets invalid on finish of this method
}
}
@Override
public boolean dropped() {
System.out.println("DROPPED");
System.exit(-1);
return false;
}
@Override
public void senderTerminated(String senderNodeId) {
}
@Override
public void senderBootstrapped(String receivesFrom, long seqNo) {
}
});
}
use of org.nustaq.fastcast.api.FCSubscriber in project fast-cast by RuedigerMoeller.
the class AsyncSubStruct method initFastCast.
public void initFastCast() throws Exception {
fastCast = FastCast.getFastCast();
fastCast.setNodeId("SUB");
fastCast.loadConfig(AsyncLatPublisher.CFG_FILE_PATH);
backPub = fastCast.onTransport("back").publish("back");
// pointer to message
final FSTStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class);
final RateMeasure measure = new RateMeasure("receive rate");
fastCast.onTransport("default").subscribe("stream", new FCSubscriber() {
int count = 0;
@Override
public void messageReceived(String sender, long sequence, Bytez b, long off, final int len) {
measure.count();
if (++count == 10 || len < 30) /*"END" marker*/
{
count = 0;
final byte[] copy = pool.getBA();
if (// prevent segfault :) !
len < copy.length) {
b.getArr(off, copy, 0, len);
count = 0;
// do bounce back in different thread, else blocking on send will pressure back to
// sender resulting in whacky behaviour+throughput
bounceBackExec.execute(new Runnable() {
@Override
public void run() {
while (!backPub.offer(null, copy, 0, len, true)) {
// spin
}
// give back to pool
pool.returnBA(copy);
}
});
} else {
throw new RuntimeException("was soll das ?");
}
}
}
@Override
public boolean dropped() {
// fatal, exit
System.out.println("process dropped ");
System.exit(-1);
return false;
}
@Override
public void senderTerminated(String senderNodeId) {
}
@Override
public void senderBootstrapped(String receivesFrom, long seqNo) {
}
});
}
use of org.nustaq.fastcast.api.FCSubscriber in project fast-cast by RuedigerMoeller.
the class MPSubscriber method main.
public static void main(String[] arg) {
MultipleProtocol.initStructFactory();
// 5 chars MAX !!
FastCast.getFastCast().setNodeId("MSUB");
MPPublisher.configureFastCast();
final RateMeasure rateMeasure = new RateMeasure("receive rate");
FastCast.getFastCast().onTransport("default").subscribe(new SubscriberConf(1).receiveBufferPackets(33_000), new FCSubscriber() {
FSTStruct msg = FSTStructFactory.getInstance().createEmptyStructPointer(FSTStruct.class);
int count = 0;
@Override
public void messageReceived(String sender, long sequence, Bytez b, long off, int len) {
rateMeasure.count();
msg.baseOn(b, (int) off);
Class type = msg.getPointedClass();
if (type == AMessage.class) {
AMessage am = msg.cast();
// am is valid until another pointer is used. use "am = am.detach" in order to get a non-cached pointer
// note msg data is valid only inside this method (reused). You need to copy data in case processing should
// be done in a different thread (usually do basic filtering in-thread, then do processing on a dedicated thread)
} else if (type == OtherMessage.class) {
OtherMessage om = msg.cast();
// ..
} else if (type == ComposedMessage.class) {
ComposedMessage cm = msg.cast();
if (count++ == 500_000) {
System.out.println("Other" + cm);
count = 0;
}
}
}
@Override
public boolean dropped() {
System.out.println("fatal, could not keep up. exiting");
System.exit(0);
return false;
}
@Override
public void senderTerminated(String senderNodeId) {
System.out.println("sender died " + senderNodeId);
}
@Override
public void senderBootstrapped(String receivesFrom, long seqNo) {
System.out.println("bootstrap " + receivesFrom);
}
});
}
use of org.nustaq.fastcast.api.FCSubscriber in project fast-cast by RuedigerMoeller.
the class ReceiveBufferDispatcher method cleanup.
/**
* if a sender stops sending, remove from map to free memory
* @param senderName
*/
public void cleanup(String senderName) {
StructString struct = new StructString(senderName);
PacketReceiveBuffer packetReceiveBuffer = bufferMap.get(struct);
bufferMap.remove(struct);
if (packetReceiveBuffer != null) {
FCSubscriber subscriber = packetReceiveBuffer.getTopicEntry().getSubscriber();
if (subscriber != null) {
subscriber.senderTerminated(senderName);
} else {
FCLog.get().severe("no subscriber for stopped sender", null);
}
packetReceiveBuffer.terminate();
} else {
FCLog.get().warn("cannot find packetReceiver to terminate");
}
}
Aggregations