-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExProtoHandlerDemo.java
182 lines (171 loc) · 6.79 KB
/
ExProtoHandlerDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.math.BigInteger;
import java.util.Arrays;
/**
* EMQ X ExProto java SDK;
* <p>
* Connection java project to EMQ X Broker.
* Note 1:
* Not use "System.in.*" or "System.out.*" They are used to communicate with EMQ X.
* Note 2:
* Invoke "ExProtoSDK.loadExProtoHandler(AbstractExProtoHandler handler)"
* or
* "AbstractExProtoHandler.loadExProtoHandler(AbstractExProtoHandler handler)"
* <p>
* load your AbstractExProtoHandler in the "Nonparametric construction method".
*/
public class ExProtoHandlerDemo extends AbstractExProtoHandler {
private static ExProtoHandlerDemo exProtoHandlerDemo = new ExProtoHandlerDemo(new String[]{"don't use [System.in.*] or [System.out.*]"});
public ExProtoHandlerDemo() {
ExProto.loadExProtoHandler(exProtoHandlerDemo);
}
public ExProtoHandlerDemo(String[] args) {
for (String arg : args) {
System.err.println(arg);
}
}
String help =
"hello --> say hello to AbstractExProtoHandler\r\n" +
"close --> close conn\r\n" +
"reg --> register client \r\n" +
"pub --> publish message\r\n" +
"sub --> subscribe mytopic qos 1\r\n" +
"unsub --> unsubscribe mytopic";
/**
* A connection established.
* <p>
* This function will be scheduled after a TCP connection established to EMQ X
* or receive a new UDP socket.
*
* @param connection The Connection instance
* @param connectionInfo The Connection information
*/
@Override
public void onConnectionEstablished(Connection connection, ConnectionInfo connectionInfo) {
System.err.println("onConnectionEstablished " + connection.getPid() + " " + connectionInfo);
try {
send(connection, help.getBytes());
} catch (Exception e) {
}
}
/**
* A connection received bytes.
* <p>
* This callback will be scheduled when a connection received bytes from TCP/UDP socket.
*
* @param connection The Connection instance
* @param data The bytes array
*/
@Override
public void onConnectionReceived(Connection connection, byte[] data) {
String command = new String(data).trim();
System.err.println(command);
switch (command) {
case "":
break;
case "hello":
try {
send(connection, ("hello my friend " + connection.getPid().id + "\r\n").getBytes());
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
case "close":
try {
send(connection, ("goodbye my friend " + connection.getPid().id + "\r\n").getBytes());
terminate(connection);
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
case "reg":
try {
ClientInfo clientInfo = new ClientInfo("mqtt", "3.1", "testCID", "testUname", "testMP/", 300);
register(connection, clientInfo);
System.err.println(ClientInfo.toErlangDataType(clientInfo));
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
case "pub":
try {
Message message =
new Message("testId", 0, "from", "mytopic", "pubmessage".getBytes(), new BigInteger("" + System.currentTimeMillis()));
publish(connection, message);
send(connection, ("publish " + message.toString()).getBytes());
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
case "sub":
try {
String topic = "mytopic";
subscribe(connection, topic, 1);
System.err.println("subscribe " + topic);
send(connection, ("subscribe " + topic + " qos " + 1).getBytes());
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
case "unsub":
try {
String unSubTop = "mytopic";
unsubscribe(connection, unSubTop);
System.err.println("subscribe " + unSubTop);
send(connection, ("unsubscribe " + unSubTop).getBytes());
} catch (Exception e) {
System.err.println(command + " ERROR");
}
case "help":
try {
send(connection, help.getBytes());
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
default:
try {
send(connection, ("i don't know " + command + "\r\n").getBytes());
} catch (Exception e) {
System.err.println(command + " ERROR");
}
break;
}
}
/**
* A connection terminated.
* <p>
* This function will be scheduled after a connection terminated.
* <p>
* It indicates that the EMQ X process that maintains the TCP/UDP socket
* has been closed. E.g: a TCP connection is closed, or a UDP socket has
* exceeded maintenance hours.
*
* @param connection The Connection instance
* @param reason The Connection terminated reason
*/
@Override
public void onConnectionTerminated(Connection connection, String reason) {
System.err.println("onConnectionTerminated " + connection.getPid() + " Reason " + reason);
}
/**
* A connection received a serial of messages from subscribed topic.
* <p>
* This function will be scheduled when a connection received a Message from EMQ X
* <p>
* When a connection is subscribed to a topic and a message arrives on that topic,
* EMQ X will deliver the message to that connection. At that time, this function
* is triggered.
*
* @param connection The Connection instance
* @param messagesArr The message array
*/
@Override
public void onConnectionDeliver(Connection connection, Message[] messagesArr) {
System.err.println("onConnectionDeliver " + connection.getPid() + " " + Arrays.toString(messagesArr));
try {
send(connection, Arrays.toString(messagesArr).getBytes());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}