77
88import java .io .IOException ;
99import java .net .URISyntaxException ;
10- import java .util .* ;
10+ import java .util .Scanner ;
1111
1212/**
1313 * Handles messages from an IoT Hub. Default protocol is to use
@@ -107,133 +107,6 @@ public void execute(IotHubStatusCode status, Object context){
107107 }
108108 }
109109
110- private static final Set <String > KEY_SET ;
111-
112- static {
113- KEY_SET = Collections .unmodifiableSet (new HashSet <>(Arrays .asList (
114- "HostName=MokaFeatherM0Suite.azure-devices.net;DeviceId=JavaDevice1;SharedAccessKey=2tRTqmjechvTyFe9SzP4qRGXzkZLSyjwdpXdJaWdWGY=" ,
115- "HostName=MokaFeatherM0Suite.azure-devices.net;DeviceId=JavaDevice2;SharedAccessKey=nLjD7ziGAy8RDrmZrXHsUITWHvRIkOI0ogiNnpHMveI=" ,
116- "HostName=MokaFeatherM0Suite.azure-devices.net;DeviceId=JavaDevice3;SharedAccessKey=tzoCq4sEU6zoNmGWZ01t/8kSgY5MYfQttkqikbYY0lE=" ,
117- "HostName=MokaFeatherM0Suite.azure-devices.net;DeviceId=JavaDevice4;SharedAccessKey=gkRTfe0FU1laPEJfC4+IjPrGiXbQ1n4Ga0mzchwgshM=" ,
118- "HostName=MokaFeatherM0Suite.azure-devices.net;DeviceId=JavaDevice5;SharedAccessKey=pphgSkq0wAj0m0IT0rYUbkS5dPRVGwMqugRhsJQidK4="
119- )));
120- }
121-
122- protected static class TestDevice implements Runnable
123- {
124- private DeviceClient client ;
125- private String deviceid ;
126- private String connString ;
127- private IotHubClientProtocol protocol ;
128- private String pathToCertificate ;
129- private int numRequests ;
130- private int numKeys ;
131-
132- @ Override
133- public void run ()
134- {
135- for (int i = 0 ; i < 100 ; i ++) {
136-
137- try {
138- this .OpenConnection ();
139- } catch (Exception e ) {
140- System .out .println ("Open throws " + e );
141- }
142-
143- this .SendAndRecieve_n ();
144-
145- try {
146- this .CloseConnection ();
147- } catch (Exception e ) {
148- System .out .println ("close throws " + e );
149- }
150- }
151- }
152-
153- public TestDevice (String connString , IotHubClientProtocol protocol , String pathToCertificate , int numRequests , int numKeys )
154- {
155- this .connString = connString ;
156- this .protocol = protocol ;
157- this .pathToCertificate = pathToCertificate ;
158- this .numRequests = numRequests ;
159- this .numKeys = numKeys ;
160- deviceid = connString .split (";" )[1 ];
161- }
162-
163- private void OpenConnection () throws URISyntaxException , IOException
164- {
165- System .out .println ();
166- System .out .println ("--------------------------------------------------------------------------------------------------------------------------------" );
167- System .out .println ("START TEST FOR: " + deviceid );
168- System .out .format ("Using communication protocol %s.\n " , protocol .name ());
169- System .out .format ("Using path to certificate %s.\n " , pathToCertificate );
170-
171- client = new DeviceClient (connString , protocol );
172-
173- if (pathToCertificate != null ) {
174- client .setOption ("SetCertificatePath" , pathToCertificate );
175- }
176-
177- System .out .println ("Successfully created an IoT Hub client." );
178-
179- if (protocol == IotHubClientProtocol .MQTT ) {
180- MessageCallbackMqtt callback = new MessageCallbackMqtt ();
181- Counter counter = new Counter (0 );
182- client .setMessageCallback (callback , counter );
183- } else {
184- MessageCallback callback = new MessageCallback ();
185- Counter counter = new Counter (0 );
186- client .setMessageCallback (callback , counter );
187- }
188-
189- System .out .println ("Successfully set message callback." );
190-
191- // Set your token expiry time limit here
192- long time = 2400 ;
193- client .setOption ("SetSASTokenExpiryTime" , time );
194-
195- client .open ();
196-
197- System .out .println ("Opened connection to IoT Hub." );
198-
199- System .out .println ("Beginning to receive messages..." );
200-
201- System .out .println ("Sending the following event messages: " );
202-
203- System .out .println ("Updated token expiry time to " + time );
204-
205- }
206-
207- public void SendAndRecieve_n () {
208- for (int i = 0 ; i < numRequests ; ++i ) {
209- String msgStr = "Event Message " + Integer .toString (i ) + " to " + deviceid ;
210- try {
211- Message msg = new Message (msgStr );
212- msg .setProperty ("messageCount" , Integer .toString (i ));
213- for (int j = 0 ; j < numKeys ; j ++) {
214- msg .setProperty ("key" +j , "value" +j );
215- }
216- msg .setExpiryTime (5000 );
217- System .out .println (msgStr );
218- EventCallback eventCallback = new EventCallback ();
219- client .sendEventAsync (msg , eventCallback , i );
220- } catch (Exception e ) {
221- }
222- try {
223- Thread .sleep (10 );
224- } catch (InterruptedException e ) {
225- e .printStackTrace ();
226- }
227- }
228- }
229-
230- public void CloseConnection () throws IOException
231- {
232- client .close ();
233- System .out .println ("CLOSE CONNECTION." );
234- }
235- }
236-
237110 /**
238111 * Receives requests from an IoT Hub. Default protocol is to use
239112 * use MQTT transport.
@@ -244,6 +117,7 @@ public void CloseConnection() throws IOException
244117 * args[2] = protocol (optional, one of 'mqtt' or 'amqps' or 'https' or 'amqps_ws')
245118 * args[3] = path to certificate to enable one-way authentication over ssl for amqps (optional, default shall be used if unspecified).
246119 */
120+
247121 public static void main (String [] args )
248122 throws IOException , URISyntaxException
249123 {
@@ -325,16 +199,78 @@ else if (protocolStr.equals("amqps_ws"))
325199 }
326200
327201 System .out .println ("Successfully read input parameters." );
202+ System .out .format ("Using communication protocol %s.\n " ,
203+ protocol .name ());
204+
205+ DeviceClient client = new DeviceClient (connString , protocol );
206+ if (pathToCertificate != null )
207+ {
208+ client .setOption ("SetCertificatePath" , pathToCertificate );
209+ }
210+
211+ System .out .println ("Successfully created an IoT Hub client." );
212+
213+ if (protocol == IotHubClientProtocol .MQTT )
214+ {
215+ MessageCallbackMqtt callback = new MessageCallbackMqtt ();
216+ Counter counter = new Counter (0 );
217+ client .setMessageCallback (callback , counter );
218+ }
219+ else
220+ {
221+ MessageCallback callback = new MessageCallback ();
222+ Counter counter = new Counter (0 );
223+ client .setMessageCallback (callback , counter );
224+ }
225+
226+ System .out .println ("Successfully set message callback." );
227+
228+ // Set your token expiry time limit here
229+ long time = 2400 ;
230+ client .setOption ("SetSASTokenExpiryTime" , time );
231+
232+ client .open ();
328233
329- Thread thread = new Thread (new TestDevice (connString , protocol , pathToCertificate , 10 , 1000 ));
330- thread .start ();
234+ System .out .println ("Opened connection to IoT Hub." );
331235
332- for (String connectionString : KEY_SET )
236+ System .out .println ("Beginning to receive messages..." );
237+
238+ System .out .println ("Sending the following event messages: " );
239+
240+ System .out .println ("Updated token expiry time to " + time );
241+
242+ for (int i = 0 ; i < numRequests ; ++i )
333243 {
334- thread = new Thread (new TestDevice (connectionString , protocol , pathToCertificate , 10 , 1000 ));
335- thread .start ();
244+ String msgStr = "Event Message " + Integer .toString (i );
245+ try
246+ {
247+ Message msg = new Message (msgStr );
248+ msg .setProperty ("messageCount" , Integer .toString (i ));
249+ msg .setProperty ("key1" , "value1" );
250+ msg .setProperty ("key2" , "value2" );
251+ msg .setExpiryTime (5000 );
252+ System .out .println (msgStr );
253+ EventCallback eventCallback = new EventCallback ();
254+ client .sendEventAsync (msg , eventCallback , i );
255+ } catch (Exception e )
256+ {
257+ }
258+ try
259+ {
260+ Thread .sleep (200 );
261+ } catch (InterruptedException e )
262+ {
263+ e .printStackTrace ();
264+ }
336265 }
337266
267+ System .out .println ("Press any key to exit..." );
268+
269+ Scanner scanner = new Scanner (System .in );
270+ scanner .nextLine ();
271+
272+ client .close ();
273+
338274 System .out .println ("Shutting down..." );
339275 }
340276}
0 commit comments