mirror of
https://github.com/EDCD/EDDN.git
synced 2025-06-15 23:02:05 +03:00
commit
22261d1b9d
24
examples/Java/build.gradle
Normal file
24
examples/Java/build.gradle
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'java'
|
||||||
|
apply plugin: 'idea'
|
||||||
|
|
||||||
|
jar {
|
||||||
|
baseName = 'eddn-java-example'
|
||||||
|
version = '0.0.1'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceCompatibility = 1.8
|
||||||
|
targetCompatibility = 1.8
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
compile group: 'org.zeromq', name: 'jeromq', version: '0.4.0'
|
||||||
|
}
|
72
examples/Java/src/main/java/org/eddn/examples/EddnPump.java
Normal file
72
examples/Java/src/main/java/org/eddn/examples/EddnPump.java
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package org.eddn.examples;
|
||||||
|
import org.zeromq.ZContext;
|
||||||
|
import org.zeromq.ZMQ;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.zip.DataFormatException;
|
||||||
|
import java.util.zip.Inflater;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to zmq relay from EDDN
|
||||||
|
*/
|
||||||
|
public class EddnPump extends Thread {
|
||||||
|
|
||||||
|
public static final String SCHEMA_KEY = "$schemaRef";
|
||||||
|
public static final String RELAY = "tcp://eddn-relay.elite-markets.net:9500";
|
||||||
|
|
||||||
|
public void msg(String msg) {
|
||||||
|
System.out.println(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
pump();
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void pump() {
|
||||||
|
ZContext ctx = new ZContext();
|
||||||
|
ZMQ.Socket client = ctx.createSocket(ZMQ.SUB);
|
||||||
|
client.subscribe("".getBytes());
|
||||||
|
client.setReceiveTimeOut(30000);
|
||||||
|
|
||||||
|
client.connect(RELAY);
|
||||||
|
msg("EDDN Relay connected");
|
||||||
|
ZMQ.Poller poller = ctx.createPoller(2);
|
||||||
|
poller.register(client, ZMQ.Poller.POLLIN);
|
||||||
|
byte[] output = new byte[256 * 1024];
|
||||||
|
while (true) {
|
||||||
|
int poll = poller.poll(10);
|
||||||
|
if (poll == ZMQ.Poller.POLLIN) {
|
||||||
|
ZMQ.PollItem item = poller.getItem(poll);
|
||||||
|
|
||||||
|
if (poller.pollin(0)) {
|
||||||
|
byte[] recv = client.recv(ZMQ.NOBLOCK);
|
||||||
|
if (recv.length > 0) {
|
||||||
|
// decompress
|
||||||
|
Inflater inflater = new Inflater();
|
||||||
|
inflater.setInput(recv);
|
||||||
|
try {
|
||||||
|
int outlen = inflater.inflate(output);
|
||||||
|
String outputString = new String(output, 0, outlen, "UTF-8");
|
||||||
|
// outputString contains a json message
|
||||||
|
|
||||||
|
if (outputString.contains(SCHEMA_KEY)) {
|
||||||
|
msg(outputString);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (DataFormatException | IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
new EddnPump().pump();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user