Last active
June 3, 2020 09:26
-
-
Save chrisgward/d8e6ed0d710c6d5f86a5a5e91c99fbb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.eclipse.paho.client.mqttv3.*; | |
| public class Publisher { | |
| public static MqttAsyncClient makeClient(int qos) throws MqttException { | |
| MqttAsyncClient client = new MqttAsyncClient("tcp://comp3310.ddns.net:1883", "3310-u6053464-fastpub" + qos); | |
| MqttConnectOptions options = new MqttConnectOptions(); | |
| options.setUserName("students"); | |
| options.setPassword("33106331".toCharArray()); | |
| options.setCleanSession(true); | |
| client.setCallback(new MqttCallback() { | |
| @Override | |
| public void connectionLost(Throwable throwable) { | |
| System.out.println("Connection lost " + qos + " " + throwable.getMessage()); | |
| throwable.printStackTrace(); | |
| /*try { | |
| client.reconnect(); | |
| } catch (MqttException e) { | |
| e.printStackTrace(); | |
| }*/ | |
| } | |
| @Override | |
| public void messageArrived(String s, MqttMessage mqttMessage) throws Exception { | |
| System.out.println(s); | |
| } | |
| @Override | |
| public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) { | |
| } | |
| }); | |
| client.connect(options).waitForCompletion(); | |
| return client; | |
| } | |
| public static void main(String[] args) throws MqttException { | |
| int i = 0; | |
| MqttAsyncClient client0 = makeClient(0); | |
| MqttAsyncClient client1 = makeClient(1); | |
| MqttAsyncClient client2 = makeClient(2); | |
| System.out.println("Connected " + client0.isConnected() + client1.isConnected() + client2.isConnected()); | |
| if (!client0.isConnected()) { | |
| return; | |
| } | |
| client0.publish("counter/fast/noteForCourseStaff", "unofficial fast publisher in use, to kill, publish anything to counter/fast/stop. - https://gist.github.com/chrisgward/d8e6ed0d710c6d5f86a5a5e91c99fbb74 - u6053464".getBytes(), 1, true).waitForCompletion(); | |
| client0.publish("counter/fast/important", "If you see this message, use this stream ONLY for testing, and not reporting. The course publisher has been down for some time and will yield different results if it comes back".getBytes(), 1, true).waitForCompletion(); | |
| System.out.println("Published 1"); | |
| client0.subscribe("$SYS/#", 0).waitForCompletion(); | |
| client1.subscribe("$SYS/#", 0).waitForCompletion(); | |
| client2.subscribe("$SYS/#", 0).waitForCompletion(); | |
| System.out.println("Subscribed 1"); | |
| client0.subscribe("counter/fast/stop", 2, (s, mqttMessage) -> { | |
| System.out.println("Stop received"); | |
| System.exit(0); | |
| }).waitForCompletion(); | |
| IMqttToken q0a = null; | |
| IMqttToken q0b = null; | |
| IMqttToken q1a = null; | |
| IMqttToken q1b = null; | |
| IMqttToken q2a = null; | |
| IMqttToken q2b = null; | |
| while (true) { | |
| try { | |
| if (!client0.isConnected()) { | |
| client0.reconnect(); | |
| } | |
| if (!client1.isConnected()) { | |
| client1.reconnect(); | |
| } | |
| if (!client2.isConnected()) { | |
| client2.reconnect(); | |
| } | |
| if (q0a != null) | |
| q0a.waitForCompletion(); | |
| q0a = client0.publish("counter/fast/q0", Integer.toString(i).getBytes(), 0, false); | |
| if (q1a == null || q1b == null || q1a.isComplete() || q1b.isComplete()) { | |
| q1a = client1.publish("counter/fast/q1", Integer.toString(i).getBytes(), 1, false); | |
| } | |
| if (q2a == null || q2b == null || q2a.isComplete() || q2b.isComplete()) { | |
| q2a = client2.publish("counter/fast/q2", Integer.toString(i).getBytes(), 2, false); | |
| } | |
| i++; | |
| if (q0b != null) | |
| q0b.waitForCompletion(); | |
| q0b = client0.publish("counter/fast/q0", Integer.toString(i).getBytes(), 0, false); | |
| if (q1a == null || q1b == null || q1a.isComplete() || q1b.isComplete()) { | |
| q1b = client1.publish("counter/fast/q1", Integer.toString(i).getBytes(), 1, false); | |
| } | |
| if (q2a == null || q2b == null || q2a.isComplete() || q2b.isComplete()) { | |
| q2b = client2.publish("counter/fast/q2", Integer.toString(i).getBytes(), 2, false); | |
| } | |
| i++; | |
| if (i > 10000000) { | |
| i = 0; | |
| } | |
| // Thread.sleep(1); | |
| //q1.waitForCompletion(); | |
| //q2.waitForCompletion(); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment