Skip to content

This is a pure java generic acquisition Wiclax server implementation

Notifications You must be signed in to change notification settings

csutorasa/wiclax4j

Repository files navigation

Wiclax4j Build wiclax4j Maven Central Quality Gate Status

This is a pure java implementation to create a generic acquisition Wiclax server. It has no runtime dependencies and is built with Java 8.

How to use

Check the latest version, and import the library as dependency from here.

Example

package org.example;

import com.github.csutorasa.wiclax.WiclaxClientConnection;
import com.github.csutorasa.wiclax.WiclaxServerSocketTest;
import com.github.csutorasa.wiclax.config.WiclaxProtocolOptions;
import com.github.csutorasa.wiclax.heartbeat.DefaultWiclaxHeartbeatWriterThread;
import com.github.csutorasa.wiclax.message.AcquisitionMessage;
import com.github.csutorasa.wiclax.model.Acquisition;
import com.github.csutorasa.wiclax.reader.DefaultWiclaxClientReaderThread;

import java.io.IOException;
import java.time.Instant;

public class WiclaxExample {
    public static void main(String... args) throws IOException, InterruptedException {
        // Create protocol options
        WiclaxProtocolOptions options = WiclaxProtocolOptions.defaults();
        // Create a server socket with port 12345
        WiclaxServerSocket wiclaxServerSocket = new WiclaxServerSocket(12345, options);
        // Accept a client connection
        WiclaxClientConnection connection = wiclaxServerSocket.accept();
        // Start reading requests from the client
        connection.startReading(new DefaultWiclaxClientReaderThread(WiclaxExample::rewind));
        // Start writing heartbeat messages
        connection.startHeartbeatWriting(new DefaultWiclaxHeartbeatWriterThread());
        // Send an acquisition
        Acquisition acquisition = Acquisition.builder().deviceId("301").chipId("123").detectionTime(Instant.now()).build();
        connection.send(new AcquisitionMessage(acquisition));
        // Do some work
        Thread.sleep(15000);
        // Close connections, preferably in finally clause.
        connection.close();
        wiclaxServerSocket.close();
    }

    public static void rewind(Instant from, Instant to) {
        // Handle rewind requests
    }
}

How it works

First it is recommended to get to know to the protocol specification and custom protocol options and configuration.

Reader

To read from clients you can use any implementation of WiclaxClientReader. There is a default implementation DefaultWiclaxClientReader and a DefaultWiclaxClientReaderThread, but you can customize it by handling the errors correctly. As there are no dependencies, there are no logging is included in the project.

Requests

Requests need to be parsed first. Request parsers are extending the WiclaxRequestParser.

Parsers create requests that extend the WiclaxRequest. These objects store the information about the request.

After the request is parsed request handlers must be added that extend WiclaxRequestHandler. They must describe what requests they can support. It can return a response which will be sent to the client.

Responses

All responses need to implement the WiclaxResponse. To create a new response you need to override the toData() method and return the text to be sent to the client.

Messages

All messages need to implement the WiclaxMessage. To create a new message you need to override the toData(WiclaxProtocolOptions) method and return the text to be sent to the client.

Clock

Wiclax can synchronize the clock by sending a message to the server. The server must accept it as the current time, and show all dates and times according to that time shift. By default, all connections have separate clocks.

About

This is a pure java generic acquisition Wiclax server implementation

Topics

Resources

Stars

Watchers

Forks