Skip to content

Commit

Permalink
GF-159: Points from review
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-cowie-diffusiondata-com committed Mar 25, 2024
1 parent ac059c9 commit b129526
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 45 deletions.
12 changes: 12 additions & 0 deletions human-diffusion-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Human Adapter

The Human Adapter is a Gateway Framework example that treats the user as the external system. Once configured and running The Human Adapter creates a GUI window for interaction. Users can publish a value, and they can set and get the current service state. The example illustrates how to publish values and correctly react to service state changes.

![A running Human Adapter service](screen-shot.png)

## Configuration

Configuration is deliberately simple. A service takes two parameters.

1. `greeting`'` - a string greeting to display in the GUI so the user can differentiate multiple services
2. `topic` - A Diffusion topic path to which values should be published./
5 changes: 0 additions & 5 deletions human-diffusion-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

<artifactId>human-diffusion-adapter</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
Binary file added human-diffusion-adapter/screen-shot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.diffusiondata.example.adapter.human;

import static com.diffusiondata.example.adapter.human.Common.getParam;
import static com.diffusiondata.example.adapter.human.Common.getResourceFileAsString;
import static com.diffusiondata.gateway.framework.DiffusionGatewayFramework.newApplicationDetailsBuilder;
import static com.diffusiondata.gateway.framework.ServiceMode.STREAMING_SOURCE;

Expand All @@ -18,7 +19,9 @@

public class Application implements GatewayApplication {

private static final String GUI_SERVICE_SCHEMA = Common.getResourceFileAsString(Application.class, "human.schema.json");
private static final String GUI_SERVICE_SCHEMA = getResourceFileAsString(
Application.class,
"human.schema.json");

@Override
public ApplicationDetails getApplicationDetails() throws ApplicationConfigurationException {
Expand All @@ -33,8 +36,11 @@ public ApplicationDetails getApplicationDetails() throws ApplicationConfiguratio
}

@Override
public StreamingSourceHandler addStreamingSource(ServiceDefinition serviceDefinition, Publisher publisher, StateHandler stateHandler) throws InvalidConfigurationException {

public StreamingSourceHandler addStreamingSource(
ServiceDefinition serviceDefinition,
Publisher publisher,
StateHandler stateHandler
) throws InvalidConfigurationException {
final Map<String, Object> params = serviceDefinition.getParameters();
final String greeting = getParam(params, "greeting", String.class, "Hello Human!");
final String topicPath = getParam(params, "topic", String.class, "human/says");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
import java.util.Map;
import java.util.stream.Collectors;

public class Common {
final class Common {

private Common() {
/* this is a namespace */
}

/**
* Fetch a text resource relative to a given class for initialising a constant.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class Constraints {

GridBagConstraints result = new GridBagConstraints();
private final GridBagConstraints result = new GridBagConstraints();

private Constraints(int x, int y, int width, int height) {
this.result.gridx = x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.diffusiondata.example.adapter.human;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class EventHandlers<T> extends ArrayList<Consumer<T>> {
public EventHandlers(int initialCapacity) {
super(initialCapacity);
}
public class EventHandlers<T> {
private List<Consumer<T>> consumers = new ArrayList<>();

public EventHandlers() {
this(1);
void dispatch(T event) {
consumers.forEach(l -> l.accept(event));
}

void dispatch(T event) {
this.forEach(l -> l.accept(event));
public void add(Consumer<T> handler) {
consumers.add(handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
Expand Down Expand Up @@ -51,7 +50,6 @@
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand All @@ -66,13 +64,17 @@ public class HumanGui {
private final JFrame frame;
private final JTextArea messageTextArea;
private final JButton sendButton;
private final Supplier<ServiceState> serviceStateSupplier;
private final Function<SetStateEvent, ServiceState> setStateListener;
private JLabel serviceStateText;

// Mutable state
private Supplier<ServiceState> serviceStateSupplier = null;
private Function<SetStateEvent, ServiceState> setStateListeners = null;

public HumanGui(String greeting) {
public HumanGui(
String greeting,
Supplier<ServiceState> serviceStateSupplier,
Function<SetStateEvent, ServiceState> setStateListener
) {
this.frame = new JFrame(greeting);
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
frame.setSize(400, 300);
Expand Down Expand Up @@ -112,6 +114,9 @@ public void actionPerformed(ActionEvent e) {

frame.setLocationRelativeTo(null); // Center on screen
frame.add(splitPane);

this.serviceStateSupplier = serviceStateSupplier;
this.setStateListener = setStateListener;
}

private JComponent buildStatusPanel() {
Expand Down Expand Up @@ -201,8 +206,8 @@ private JComponent buildStatusPanel() {
result.add(formPanel, CENTER);
final JButton setStatusButton = new JButton("Set Status");
setStatusButton.addActionListener((ev) -> {
if (this.setStateListeners != null) {
final ServiceState serviceState = this.setStateListeners.apply(new SetStateEvent(
if (this.setStateListener != null) {
final ServiceState serviceState = this.setStateListener.apply(new SetStateEvent(
(StateHandler.Status)statusCombo.getSelectedItem(),
titleText.getText(),
descriptionText.getText())
Expand All @@ -225,7 +230,12 @@ public void actionPerformed(ActionEvent e) {
return result;
}

private static void bindKeyStrokeToAction(JComponent component, KeyStroke keyStroke, String actionKey, Action action) {
private static void bindKeyStrokeToAction(
JComponent component,
KeyStroke keyStroke,
String actionKey,
Action action
) {
final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = component.getActionMap();

Expand All @@ -246,32 +256,24 @@ public void addSendEventHandler(Consumer<SendMessageEvent> handler) {
this.sendMessageListeners.add(handler);
}

public void addSetStateEventHandler(Function<SetStateEvent, ServiceState> handler) {
this.setStateListeners = handler;
}

public void setServiceStateProducer(java.util.function.Supplier<ServiceState> supplier) {
this.serviceStateSupplier = supplier;
}

public static void main(String[] args) {
invokeLater(() -> {
try {
setLookAndFeel(getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
catch (UnsupportedLookAndFeelException | ClassNotFoundException |
InstantiationException | IllegalAccessException ex) {
LOG.warn("Cannot set look and feel", ex);
}

final HumanGui gui = new HumanGui("Simple test");
final HumanGui gui = new HumanGui("Simple test",
() -> ServiceState.INITIAL,
ev ->ServiceState.INITIAL);
gui.setVisible(true);
gui.addSendEventHandler(ev -> LOG.info("Sending: {}", ev));
gui.addSetStateEventHandler(ev ->{LOG.info("Setting status: {}", ev); return ServiceState.INITIAL;});
gui.setServiceStateProducer(() -> ServiceState.INITIAL);
});
}


}

class SendMessageEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ public class HumanStreamingSourceHandler implements StreamingSourceHandler {
private final StateHandler stateHandler;
private final HumanGui gui;

public HumanStreamingSourceHandler(Publisher publisher, StateHandler stateHandler, String greeting, String topicPath) {
public HumanStreamingSourceHandler(
Publisher publisher,
StateHandler stateHandler,
String greeting,
String topicPath
) {
this.stateHandler = stateHandler;
this.gui = new HumanGui(greeting);
this.gui = new HumanGui(
greeting,
this.stateHandler::getState,
ev -> this.stateHandler.reportStatus(ev.getStatus(), ev.getTitle(), ev.getDescription())
);
this.gui.addSendEventHandler((ev) -> {
try {
publisher.publish(topicPath, ev.getMessage()).get(1, TimeUnit.SECONDS);
Expand All @@ -33,10 +42,6 @@ public HumanStreamingSourceHandler(Publisher publisher, StateHandler stateHandle
LOG.error("Cannot publish '{}' to {}", ev.getMessage(), topicPath, ex);
}
});
this.gui.addSetStateEventHandler(ev ->
this.stateHandler.reportStatus(ev.getStatus(), ev.getTitle(), ev.getDescription())
);
this.gui.setServiceStateProducer(this.stateHandler::getState);
}

@Override
Expand Down

0 comments on commit b129526

Please sign in to comment.