Search in sources :

Example 1 with PubSubListener

use of com.ociweb.gl.api.PubSubListener 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();
    final PubSubService pubSubService = cmdChnl.newPubSubService();
    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);
                }
            };
            pubSubService.publishTopic("topic/egress", writable);
        }
    };
    runtime.addTimePulseListener(timeListener);
    final MsgCommandChannel cmd = runtime.newCommandChannel();
    final PubSubService pubSubService2 = cmd.newPubSubService();
    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");
                }
            };
            pubSubService2.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");
}
Also used : TimeListener(com.ociweb.gl.api.TimeListener) MsgCommandChannel(com.ociweb.gl.api.MsgCommandChannel) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) PubSubListener(com.ociweb.gl.api.PubSubListener) PubSubService(com.ociweb.gl.api.PubSubService) Writable(com.ociweb.gl.api.Writable) Date(java.util.Date) ChannelWriter(com.ociweb.pronghorn.pipe.ChannelWriter)

Example 2 with PubSubListener

use of com.ociweb.gl.api.PubSubListener in project GreenLightning by oci-pronghorn.

the class MassiveBehavior method declareBehavior.

@Override
public void declareBehavior(GreenRuntime runtime) {
    // runtime.addTimePulseListener(new stopperBehavior(runtime));
    int i = 7;
    while (--i >= 0) {
        final GreenCommandChannel cmd = runtime.newCommandChannel();
        final PubSubService pubSubServce = cmd.newPubSubService();
        final String topic = "topic" + i;
        final int value = i;
        final Writable writable = new Writable() {

            @Override
            public void write(ChannelWriter writer) {
                writer.writePackedInt(value);
            }
        };
        TimeListener pubs = new TimeListener() {

            @Override
            public void timeEvent(long time, int iteration) {
                if (!pubSubServce.publishTopic(topic, writable)) {
                    System.out.println("overloaded can not publish " + value);
                }
            }
        };
        runtime.addTimePulseListener(pubs);
        PubSubListener subs = new PubSubListener() {

            public boolean message(CharSequence topic, ChannelReader payload) {
                return true;
            }
        };
        runtime.addPubSubListener(subs).addSubscription(topic);
    }
}
Also used : TimeListener(com.ociweb.gl.api.TimeListener) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) PubSubListener(com.ociweb.gl.api.PubSubListener) PubSubService(com.ociweb.gl.api.PubSubService) Writable(com.ociweb.gl.api.Writable) GreenCommandChannel(com.ociweb.gl.api.GreenCommandChannel) ChannelWriter(com.ociweb.pronghorn.pipe.ChannelWriter)

Aggregations

PubSubListener (com.ociweb.gl.api.PubSubListener)2 PubSubService (com.ociweb.gl.api.PubSubService)2 TimeListener (com.ociweb.gl.api.TimeListener)2 Writable (com.ociweb.gl.api.Writable)2 ChannelReader (com.ociweb.pronghorn.pipe.ChannelReader)2 ChannelWriter (com.ociweb.pronghorn.pipe.ChannelWriter)2 GreenCommandChannel (com.ociweb.gl.api.GreenCommandChannel)1 MsgCommandChannel (com.ociweb.gl.api.MsgCommandChannel)1 Date (java.util.Date)1