com.canoo.dolphin.server.event.impl.Receiver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dolphin-platform-server Show documentation
Show all versions of dolphin-platform-server Show documentation
The Dolphin Platform is a framework that implements the presentation model pattern and provides amodern way to create enterprise applications. The Platform provides several client implementations that all canbe used in combination with a general sever API.
/*
* Copyright 2015 Canoo Engineering AG.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.canoo.dolphin.server.event.impl;
import com.canoo.dolphin.event.Subscription;
import com.canoo.dolphin.server.event.Message;
import com.canoo.dolphin.server.event.MessageListener;
import groovyx.gpars.dataflow.DataflowQueue;
import org.opendolphin.StringUtil;
import org.opendolphin.core.server.EventBus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* represents one listener to the queue. It need not to be thread safe, because it is related to one dolphin session.
*/
public class Receiver {
DataflowQueue receiverQueue;
Map> listenersPerTopic = new HashMap<>();
public DataflowQueue getReceiverQueue() {
return receiverQueue;
}
public Subscription subscribe(final String topic, final MessageListener handler) {
if(StringUtil.isBlank(topic)) {
throw new IllegalArgumentException("topic mustn't be empty!");
}
if(handler == null) {
throw new IllegalArgumentException("handler mustn't be empty!");
}
List messageListeners = listenersPerTopic.get(topic);
if (messageListeners == null) {
messageListeners = new CopyOnWriteArrayList<>();
listenersPerTopic.put(topic, messageListeners);
}
messageListeners.add(handler);
return new Subscription() {
@Override
public void unsubscribe() {
List messageListeners = listenersPerTopic.get(topic);
if (messageListeners == null) {
return;
}
messageListeners.remove(handler);
if (messageListeners.isEmpty()) {
listenersPerTopic.remove(topic);
}
}
};
}
public boolean handle(Message event) {
List messageListeners = listenersPerTopic.get(event.getTopic());
if (messageListeners == null || messageListeners.isEmpty()) {
return false;
}
// iterate over copy, because the list could be changed in an onMessage method
for (MessageListener messageListener : messageListeners) {
messageListener.onMessage(event);
}
return true;
}
public boolean isListeningToEventBus() {
return receiverQueue != null;
}
public void unregister(EventBus eventBus) {
if(eventBus == null) {
throw new IllegalArgumentException("eventBus mustn't be empty!");
}
if (isListeningToEventBus()) {
eventBus.unSubscribe(receiverQueue);
receiverQueue = null;
}
listenersPerTopic.clear();
}
public void register(EventBus eventBus) {
if(eventBus == null) {
throw new IllegalArgumentException("eventBus mustn't be empty!");
}
if (isListeningToEventBus()) {
return;
}
receiverQueue = new DataflowQueue();
eventBus.subscribe(receiverQueue);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy