Carrying on in the context of Oil & Gas, in this post I will share a very simple Java application to stream random Wits data, provided through a TCP Socket Server to a single client. If you are new to Wits protocol, please visit my previous post, Wits Simplified.
For this project, I’m using:
- Java JDK 8u73;
- Maven 3.3.9.
There are also other libraries being used (JUnit, Mockito, JaCoCo, etc), however, Maven will automatically download them.
This code is also available on my GitHub page. Feel free to comment if you have any doubt or suggestion. 😄
Licensing
This software is licensed under the MIT license, which basically means that you can use it for any purpose without any warranty. ⚠️
Architecture
This application creates a thread to provide a TCP server, generates Wits data and publishes the generated data into the socket of the connected client. Its design is quite simple, and the overall responsibility and behavior of the classes are as described below:
- WitsServer.java: orchestrates the start/stop of the resources and is responsible for writing the data into the socket;
- TcpServer.java: is started as other thread, to provide socket connection for the client to connect to;
- WitsGenerator.java: generates Wits blocks of data, by assembling Wits lines;
- WitsLineGenerator.java: generates each Wits line of data.
The class WitsServer.java depends on the other classes, TcpServer.java, WitsGenerator.java and WitsLineGenerator.java (transitively), respecting all the different responsibilities of these classes.
The class Main.java is only used to interface the user’s call and the application itself, thus it will not be covered in this document. However, it’s interesting to look into the class to better understand how the command-line is being interpreted and instantiating the main classes.
Compilation
There are no runtime dependencies of the application. However, some plugins and test libraries are being used to produce coverage reports as well as to make sure the expected behaviors are produced from the classes. Thus Maven will be responsible for downloading them.
To compile the project, run mvn clean install
. After executing, it will result in a tinny JAR file under the ./target
folder, which is the compiled application.
Execution
The compiled application can be easily executed from the command-line, as the following help describes: {% highlight text %} Usage: java -jar wits-generator-1.1.jar [port] [frequency] [records] [items] Creates a socket server and transmits random Wits data from time to time. Only allows ONE single client connected.
Mandatory arguments port The port to run the socket Server frequency The frequency, in seconds, of the data generation records The number of records to be transmitted items The number of items to be transmitted in each record {% endhighlight %}
When executed, it will start listening for the client at the port port
and produce records x items
lines of data in each frequency
milliseconds.
All the configuration parameters may be changed in the command-line call, making it really adaptable to provide as many items and records as necessary in the wanted frequency. Make sure to provide reasonable input parameters, so the client can consume the data in less time than the frequency it’s created.
It means that, if you want to consume the generated data from a slow or unstable TCP connection (e.g. through a low bandwidth Internet connection, a VPN connection, or an old 10Mbps Ethernet network adapter), you must adapt the frequency and the amount of data being produced to meet your connection limitations. As Wits runs as plain text over a socket connection and does not provide historical replay by default, the TCP protocol may spend some effort trying to resend lost packages. That way, publishing too much data may cause the client not to be able to retrieve it all.
Code
I’m not attaching here the Main.class. If necessary, visit my GitHub page.