use of com.ociweb.pronghorn.pipe.ChannelReader in project GreenLightning by oci-pronghorn.
the class GreenParserMessageTest method complexStringTest.
@Test
public void complexStringTest() {
NumberFormat formatter = new DecimalFormat("#0.0000");
final GreenReader reader = buildParser().newReader();
ChannelReader testToRead = BlobReaderFactory.generateExtractionDataToTest(new MyConsumer<DataOutputBlobWriter<?>>() {
@Override
public void accept(DataOutputBlobWriter<?> dataOutputBlobWriter) {
complexStreamAppend(dataOutputBlobWriter);
}
});
reader.beginRead(testToRead);
StringBuilder rebuild = new StringBuilder();
while (reader.hasMore()) {
int parsedId = (int) reader.readToken();
if (parsedId == -1) {
reader.skipByte();
} else {
final MsgField msgField = messages[parsedId];
final FieldType fieldType = msgField.type;
final String key = msgField.key;
rebuild.append(key);
switch(fieldType) {
case integer:
{
int value = (int) reader.extractedLong(0);
rebuild.append(value);
break;
}
case int64:
{
long value = reader.extractedLong(0);
rebuild.append(value);
break;
}
case string:
{
StringBuilder value = new StringBuilder();
reader.copyExtractedUTF8ToAppendable(0, value);
rebuild.append("\"");
rebuild.append(value);
rebuild.append("\"");
break;
}
case floatingPoint:
{
double value = reader.extractedDouble(0);
rebuild.append(formatter.format(value));
break;
}
}
}
}
assertEquals(complexData, rebuild.toString());
}
use of com.ociweb.pronghorn.pipe.ChannelReader in project GreenLightning by oci-pronghorn.
the class MQTTApp method declareBehavior.
@Override
public void declareBehavior(final GreenRuntime runtime) {
// optional 2 topics, optional transform lambda
runtime.bridgeSubscription("topic/ingress", mqttConfig);
// optional 2 topics, optional transform lambda
runtime.bridgeTransmission("topic/egress", mqttConfig);
final MsgCommandChannel cmdChnl = runtime.newCommandChannel(DYNAMIC_MESSAGING);
TimeListener timeListener = new TimeListener() {
@Override
public void timeEvent(long time, int iteration) {
Writable writable = new Writable() {
@Override
public void write(ChannelWriter writer) {
Date d = new Date(System.currentTimeMillis());
System.err.println("sent " + d);
writer.writeUTF8Text("egress body " + d);
}
};
cmdChnl.publishTopic("topic/egress", writable);
}
};
runtime.addTimePulseListener(timeListener);
final MsgCommandChannel cmd = runtime.newCommandChannel(DYNAMIC_MESSAGING);
PubSubListener listener = new PubSubListener() {
@Override
public boolean message(CharSequence topic, ChannelReader payload) {
System.out.print("\ningress body: ");
payload.readUTFOfLength(payload.available(), System.out);
System.out.println();
Writable writable = new Writable() {
@Override
public void write(ChannelWriter writer) {
writer.writeUTF("second step test message");
}
};
cmd.publishTopic("localtest", writable);
return true;
}
};
runtime.addPubSubListener(listener).addSubscription("topic/ingress");
PubSubListener localTest = new PubSubListener() {
@Override
public boolean message(CharSequence topic, ChannelReader payload) {
System.out.println("got topic " + topic + " payload " + payload.readUTF());
return true;
}
};
runtime.addPubSubListener(localTest).addSubscription("localtest");
}
use of com.ociweb.pronghorn.pipe.ChannelReader in project GreenLightning by oci-pronghorn.
the class GreenParserTest method simpleReaderTest.
@Test
public void simpleReaderTest() {
GreenReader gr = new GreenTokenMap().add(1, "moe").add(2, "larry").add(3, "curly").add(0, "shemp").newReader();
ChannelReader testToRead = BlobReaderFactory.generateExtractionDataToTest(new MyConsumer<DataOutputBlobWriter<?>>() {
@Override
public void accept(DataOutputBlobWriter<?> dataOutputBlobWriter) {
larryShempStreamAppend(dataOutputBlobWriter);
}
});
// /////
// example consumer code starts here
// /////
boolean foundLarry = false;
boolean foundShemp = false;
gr.beginRead(testToRead);
while (gr.hasMore()) {
long token = gr.readToken();
switch((int) token) {
case // this is a token id
0:
foundShemp = true;
break;
case // this is a token id
2:
foundLarry = true;
break;
case -1:
// unknown
gr.skipByte();
break;
case 1:
case 3:
// ignore
break;
}
}
assertTrue("can not find larry", foundLarry);
assertTrue("can not find shemp", foundShemp);
}
use of com.ociweb.pronghorn.pipe.ChannelReader in project PronghornPipes by oci-pronghorn.
the class JSONParseTest method simpleArrayNullParseTest.
@Test
public void simpleArrayNullParseTest() {
Pipe<RawDataSchema> targetData = parseJSON(simpleArrayNullExample, simpleArrayExtractor);
// confirm data on the pipe is good...
Pipe.takeMsgIdx(targetData);
ChannelReader dataStream = (ChannelReader) Pipe.openInputStream(targetData);
long header = dataStream.readPackedLong();
assertEquals(0, header);
assertEquals(5, dataStream.readPackedInt());
// these are 19 bytes plus 10, 29
assertEquals("one", dataStream.readUTFOfLength(dataStream.readPackedInt()));
assertEquals(null, dataStream.readUTFOfLength(dataStream.readPackedInt()));
assertEquals(null, dataStream.readUTFOfLength(dataStream.readPackedInt()));
assertEquals("four", dataStream.readUTFOfLength(dataStream.readPackedInt()));
assertEquals("five", dataStream.readUTFOfLength(dataStream.readPackedInt()));
assertEquals(5, dataStream.readPackedInt());
assertEquals(1, dataStream.readPackedLong());
assertEquals(0, dataStream.readPackedLong());
assertTrue(dataStream.wasPackedNull());
assertEquals(3, dataStream.readPackedLong());
assertEquals(4, dataStream.readPackedLong());
assertEquals(5, dataStream.readPackedLong());
assertEquals(0, dataStream.available());
}
use of com.ociweb.pronghorn.pipe.ChannelReader in project PronghornPipes by oci-pronghorn.
the class JSONParseTest method simpleNullBTest.
@Test
public void simpleNullBTest() {
try {
Pipe<RawDataSchema> targetData = parseJSON(nullBExample, simpleExtractor);
// confirm data on the pipe is good...
Pipe.takeMsgIdx(targetData);
ChannelReader dataStream = (ChannelReader) Pipe.openInputStream(targetData);
long header = dataStream.readPackedLong();
assertEquals(0, header);
String valueB = dataStream.readUTFOfLength(dataStream.readPackedInt());
assertEquals(null, valueB);
long valueA = dataStream.readPackedLong();
assertEquals(123, valueA);
} catch (Throwable t) {
t.printStackTrace();
throw new AssertionError("rethrow", t);
}
}
Aggregations