Page tree
Skip to end of metadata
Go to start of metadata

Warning: This is an early and maybe incomplete version.

Odysseus Client is a simple library to ease the communication with Odysseus. It encapsulates the REST interface and libraries and offers simple methods to create and start queries as well as getting the results. If you want to see an example of how the library can be used within a project, see the "ConditionMonitoring" repository within the Odysseus git.

Getting the jar

To create the jar, checkout the "Odysseus client" repository, open it with eclipse and export the project as a jar. This jar can be used within your project to encapsulate a part of the communication with Odysseus.

Example

Login

String loginIp = "127.0.0.1";
String username = "System";
String password = "manager";
String token = RestService.login(loginIp, username, password);

Full Example

import de.uniol.inf.is.odysseus.client.communication.dto.SocketInfo;
import de.uniol.inf.is.odysseus.client.communication.rest.RestException;
import de.uniol.inf.is.odysseus.client.communication.rest.RestService;
import de.uniol.inf.is.odysseus.client.communication.socket.SocketReceiver;
import de.uniol.inf.is.odysseus.client.data.ConnectionHandler;
import de.uniol.inf.is.odysseus.client.data.DataHandler;
import de.uniol.inf.is.odysseus.client.model.Event;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author Tobias Brandt
 * @since 13.11.2016
 */
public class OdysseusCommunicationTest implements Observer {

    private List<SocketReceiver> receiverList;

    public static void main(String[] args) throws RestException, IOException {

        new Thread() {
            public void run() {
                try {
                    OdysseusCommunicationTest test = new OdysseusCommunicationTest();
                    test.startQuery();
                    test.addObservers();
                } catch (RestException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        // Read line
        System.out.print("Write something to finish: ");
        readLine();
    }

    public OdysseusCommunicationTest() {
        receiverList = new ArrayList<>();
    }

    /**
     * Here we register this class as the observer for all incoming data from Odysseus
     */
    public void addObservers() {
        receiverList.stream().forEach(socketReceiver -> DataHandler.getInstance().addObserverForConnection(socketReceiver, this));
    }

    /**
     * Here we send a query to Odysseus.
     * @throws RestException
     */
    public void startQuery() throws RestException {
        String loginIp = "127.0.0.1";
        String username = "System";
        String password = "manager";
        String token = RestService.login(loginIp, username, password);


        String query = "#PARSER PQL \n #RUNQUERY \n test = MAP({expressions = [['toString(1+1)','test']]}, input)";
        Map<String, Map<Integer, SocketInfo>> socketInfos = RestService.runQuery(loginIp, token, query);

        // Add connection for every info we got
        for (String operatorName : socketInfos.keySet()) {
            Map<Integer, SocketInfo> singleSocketInfos = socketInfos.get(operatorName);
            for (int operatorOutputPort : singleSocketInfos.keySet()) {
                SocketInfo socketInfo = singleSocketInfos.get(operatorOutputPort);
                SocketReceiver receiver = new SocketReceiver(socketInfo);
                receiverList.add(receiver);
                ConnectionHandler.getInstance().addConnection(receiver);
            }
        }
    }

    /**
     * Here we receive the data from Odysseus
     *
     * @param o
     * @param arg
     */
    @Override
    public void update(Observable o, Object arg) {
        System.out.println("-------------------");
        if (arg instanceof Event) {
            Event event = (Event) arg;
            Map<String, String> attriutes = event.getAttributes();
            for (String key : attriutes.keySet()) {
                System.out.println(key + " : " + attriutes.get(key));
            }
        }
    }

    /**
     * Read a line from the console
     * @return
     * @throws IOException
     */
    private static String readLine() throws IOException {
        if (System.console() != null) {
            return System.console().readLine();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                System.in));
        return reader.readLine();
    }
}
  • No labels