Search in sources :

Example 1 with TimeListener

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

the class ReactiveListenerStage method initRealStage.

public void initRealStage() {
    if (null == this.realStage) {
        // extract pipes used by listener and use cmdChannelUsageChecker to confirm its not re-used
        // all the outputs are collected in outputPipes together, this can not be done any earler due to auto private topic feature
        // if auto private topics has found them all to be topic then pub sub is disabled since it is not needed.
        // populates outputPipes
        ChildClassScanner.visitUsedByClass(nameId, listener, gatherPipesVisitor, MsgCommandChannel.class);
        if ((!this.builder.isAllPrivateTopics()) && this.builder.isListeningToSubscription(listener)) {
            // do not create this pipe if all the subscriptions have been detected as private subscriptions
            List<PrivateTopic> targetTopics = builder.getPrivateTopicsFromTarget(nameId);
            if (targetTopics.size() != totalSupscriptions) {
                // /this is done late because if we have detected that pub sub router is not required it creates fewer pipes.
                builder.populateListenerIdentityHash(listener);
                inputPipes = PronghornStage.join(inputPipes, MsgRuntime.buildMessageSubscriptionPipe(builder));
            }
        }
        if (null != behaviorName) {
            List<PrivateTopic> sourceTopics = builder.getPrivateTopicsFromSource(nameId);
            int i = sourceTopics.size();
            while (--i >= 0) {
                PrivateTopic privateTopic = sourceTopics.get(i);
                outputPipes = PronghornStage.join(outputPipes, privateTopic.getPipe(parallelInstance));
            }
            List<PrivateTopic> targetTopics = builder.getPrivateTopicsFromTarget(nameId);
            int j = targetTopics.size();
            while (--j >= 0) {
                PrivateTopic privateTopic = targetTopics.get(j);
                inputPipes = PronghornStage.join(inputPipes, privateTopic.getPipe(parallelInstance));
            }
            List<PrivateTopic> privateTopicList = builder.getPrivateTopicsFromTarget(nameId);
            logger.trace("setting stage name: {}", this.behaviorName);
            // only lookup topics if the builder knows of some
            if (!privateTopicList.isEmpty()) {
                i = inputPipes.length;
                this.receivePrivateTopics = new PrivateTopic[inputPipes.length];
                while (--i >= 0) {
                    if (Pipe.isForSchema(inputPipes[i], MessagePrivate.instance)) {
                        j = privateTopicList.size();
                        while (--j >= 0) {
                            PrivateTopic privateTopic = privateTopicList.get(j);
                            if (privateTopic.getPipe(parallelInstance) == (inputPipes[i])) {
                                this.receivePrivateTopics[i] = privateTopic;
                                // done
                                break;
                            }
                        }
                        assert (j >= 0) : "error: did not find matching pipe for private topic";
                    }
                }
            } else {
                this.receivePrivateTopics = null;
            }
            // //////////////
            List<PrivateTopic> listIn = builder.getPrivateTopicsFromSource(nameId);
            if (!listIn.isEmpty()) {
                TrieParser privateTopicsPublishTrie;
                TrieParserReader privateTopicsTrieReader;
                i = listIn.size();
                // a topic is case-sensitive
                privateTopicsPublishTrie = new TrieParser(i, 1, false, false, false);
                Pipe<MessagePrivate>[] privateTopicPublishPipes = new Pipe[i];
                String[] topics = new String[i];
                while (--i >= 0) {
                    PrivateTopic topic = listIn.get(i);
                    // logger.info("set private topic for use {} {}",i,topic.topic);
                    privateTopicPublishPipes[i] = topic.getPipe(parallelInstance);
                    topics[i] = topic.topic;
                    // to matching pipe index
                    privateTopicsPublishTrie.setUTF8Value(topic.topic, i);
                }
                privateTopicsTrieReader = new TrieParserReader(true);
                this.publishPrivateTopics = new PublishPrivateTopics(privateTopicsPublishTrie, privateTopicPublishPipes, privateTopicsTrieReader, topics);
            } else {
                this.publishPrivateTopics = null;
            }
        }
        this.realStage = new ReactiveProxyStage(this, graphManager, consumerJoin(inputPipes, consumers.iterator()), outputPipes);
        if (listener instanceof ShutdownListener) {
            toStringDetails = toStringDetails + "ShutdownListener\n";
            int shudownListenrCount = builder.liveShutdownListeners.incrementAndGet();
            assert (shudownListenrCount >= 0);
        }
        GraphManager.addNota(graphManager, GraphManager.STAGE_NAME, this.behaviorName, this.realStage);
        if (listener instanceof TimeListener) {
            toStringDetails = toStringDetails + "TimeListener\n";
            timeListener = (TimeListener) listener;
            // time listeners are producers by definition
            GraphManager.addNota(graphManager, GraphManager.PRODUCER, GraphManager.PRODUCER, this.realStage);
        } else {
            timeListener = null;
        }
        if (listener instanceof StartupListener) {
            toStringDetails = toStringDetails + "StartupListener\n";
        }
        GraphManager.addNota(graphManager, GraphManager.DOT_BACKGROUND, "burlywood2", this.realStage);
        if (listener instanceof RestListenerBase) {
            GraphManager.addNota(graphManager, GraphManager.DOT_RANK_NAME, "ModuleStage", this.realStage);
        } else if (listener instanceof IngressMQTTBehavior) {
            GraphManager.addNota(graphManager, GraphManager.DOT_RANK_NAME, "SocketReader", this.realStage);
            GraphManager.addNota(graphManager, GraphManager.DOT_BACKGROUND, MQTTClientGraphBuilder.BACKGROUND_COLOR, this.realStage);
        } else if (listener instanceof EgressMQTTBehavior) {
            GraphManager.addNota(graphManager, GraphManager.DOT_RANK_NAME, "SocketWriter", this.realStage);
            GraphManager.addNota(graphManager, GraphManager.DOT_BACKGROUND, MQTTClientGraphBuilder.BACKGROUND_COLOR, this.realStage);
        }
        if (isolate) {
            GraphManager.addNota(graphManager, GraphManager.ISOLATE, GraphManager.ISOLATE, this.realStage.stageId);
        }
        if (producer) {
            GraphManager.addNota(graphManager, GraphManager.PRODUCER, GraphManager.PRODUCER, this.realStage.stageId);
        }
        if (slaLatency >= 0) {
            GraphManager.addNota(graphManager, GraphManager.SLA_LATENCY, slaLatency, this.realStage);
        }
        configureStageRate();
        // finds all the command channels which make use of private topics.
        regPrivateTopics();
    }
}
Also used : RestListenerBase(com.ociweb.gl.impl.RestListenerBase) EgressMQTTBehavior(com.ociweb.gl.impl.mqtt.EgressMQTTBehavior) IngressMQTTBehavior(com.ociweb.gl.impl.mqtt.IngressMQTTBehavior) TrieParser(com.ociweb.pronghorn.util.TrieParser) Pipe(com.ociweb.pronghorn.pipe.Pipe) StartupListener(com.ociweb.gl.api.StartupListener) ShutdownListener(com.ociweb.gl.api.ShutdownListener) TimeListener(com.ociweb.gl.api.TimeListener) PrivateTopic(com.ociweb.gl.impl.PrivateTopic) TrieParserReader(com.ociweb.pronghorn.util.TrieParserReader)

Example 2 with TimeListener

use of com.ociweb.gl.api.TimeListener 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 3 with TimeListener

use of com.ociweb.gl.api.TimeListener 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

TimeListener (com.ociweb.gl.api.TimeListener)3 PubSubListener (com.ociweb.gl.api.PubSubListener)2 PubSubService (com.ociweb.gl.api.PubSubService)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 ShutdownListener (com.ociweb.gl.api.ShutdownListener)1 StartupListener (com.ociweb.gl.api.StartupListener)1 PrivateTopic (com.ociweb.gl.impl.PrivateTopic)1 RestListenerBase (com.ociweb.gl.impl.RestListenerBase)1 EgressMQTTBehavior (com.ociweb.gl.impl.mqtt.EgressMQTTBehavior)1 IngressMQTTBehavior (com.ociweb.gl.impl.mqtt.IngressMQTTBehavior)1 Pipe (com.ociweb.pronghorn.pipe.Pipe)1 TrieParser (com.ociweb.pronghorn.util.TrieParser)1 TrieParserReader (com.ociweb.pronghorn.util.TrieParserReader)1 Date (java.util.Date)1