@@ -47,55 +47,111 @@ public ChannelExample() {
4747 * Create a provider that supports "getValue", "increment" and "incrementBy n" actions
4848 */
4949 public void createChannelProvider () {
50+
51+ // Create the channel provider.
5052 desktopConnection .getChannel (CHANNEL_NAME ).createAsync ().thenAccept (provider -> {
5153 provider .addProviderListener (new ChannelProviderListener () {
54+
55+ // Create the onChannelConnect event handler.
5256 @ Override
5357 public void onClientConnect (ChannelClientConnectEvent connectionEvent ) throws Exception {
58+
59+ // Add a line to the log file to identify the UUID of the caller.
5460 logger .info (String .format ("provider receives client connect event from %s " , connectionEvent .getUuid ()));
61+
62+ // Extract the JSON payload.
5563 JSONObject payload = (JSONObject ) connectionEvent .getPayload ();
64+
65+ // If the "name" element of the payload says the client is invalid, reject the request.
5666 if (payload != null ) {
5767 String name = payload .optString ("name" );
58- if ("badguy" .equals (name )) {
59- // throw exception here to reject the connection
60- throw new Exception ("stay out" );
68+ if ("Invalid Client" .equals (name )) {
69+ throw new Exception ("request rejected" );
6170 }
6271 }
6372 }
73+
74+ // Create the onChannelDisconnect event handler.
6475 @ Override
6576 public void onClientDisconnect (ChannelClientConnectEvent connectionEvent ) {
77+
78+ // Add a line to the log file identifying the UUID of the caller.
6679 logger .info (String .format ("provider receives channel disconnect event from %s " , connectionEvent .getUuid ()));
6780 }
6881 });
6982
70- //provider created, register actions.
71- AtomicInteger x = new AtomicInteger ( 0 );
83+ // The provider was created. Now to register the actions.
84+ // ------------------------------------------------------
7285
86+ // This variable is used as the "value" element for the getValue, increment, and incrementBy actions.
87+ AtomicInteger localInteger = new AtomicInteger (0 );
88+
89+ // Register the "getValue" action.
90+ // This action will return the value of the localInteger variable.
7391 provider .register ("getValue" , new ChannelAction () {
92+
93+ // This is the logic for the "getValue" action.
7494 @ Override
7595 public JSONObject invoke (String action , Object payload , JSONObject senderIdentity ) {
96+
97+ // Write a string to the logfile that shows the requested action and payload.
7698 logger .info (String .format ("provider processing action %s, payload=%s" , action , payload .toString ()));
99+
100+ // Create a JSON object to return to the channel client.
77101 JSONObject obj = new JSONObject ();
78- obj .put ("value" , x .get ());
102+
103+ // Set the "value" JSON element to the value of the localInteger variable.
104+ obj .put ("value" , localInteger .get ());
105+
106+ // Return the JSON object to the channel client.
79107 return obj ;
80108 }
81109 });
110+
111+ // Register the "increment" action.
112+ // This action will increment the value of the localInteger variable by one.
82113 provider .register ("increment" , new ChannelAction () {
114+
115+ // This is the logic for the "increment" action.
83116 @ Override
84117 public JSONObject invoke (String action , Object payload , JSONObject senderIdentity ) {
118+
119+ // Write a string to the logfile that identifies the action and payload.
85120 logger .info (String .format ("provider processing action %s, payload=%s" , action , payload .toString ()));
121+
122+ // Create a JSON object to return to the channel client.
86123 JSONObject obj = new JSONObject ();
87- obj .put ("value" , x .incrementAndGet ());
124+
125+ // Increment localInteger and set the "value" JSON element to the new value of localInteger.
126+ obj .put ("value" , localInteger .incrementAndGet ());
88127 provider .publish ("event" , obj , null );
128+
129+ // Return the JSON object to the channel client.
89130 return obj ;
90131 }
91132 });
133+
134+ // Register the "incrementBy" action.
135+ // This action will increment the value of the localInteger variable by a specified amount.
92136 provider .register ("incrementBy" , new ChannelAction () {
137+
138+ // This is the logic for the "incrementBy" action.
93139 @ Override
94140 public JSONObject invoke (String action , Object payload , JSONObject senderIdentity ) {
141+
142+ // Write a string to the logfile that identifies the action and payload.
95143 logger .info (String .format ("provider processing action %s, payload=%s" , action , payload .toString ()));
144+
145+ // Extract the increment amount (delta) from the payload JSON object.
96146 int delta = ((JSONObject )payload ).getInt ("delta" );
147+
148+ // Create a new JSON object to return to the channel client.
97149 JSONObject obj = new JSONObject ();
98- obj .put ("value" , x .addAndGet (delta ));
150+
151+ // Increase localInteger by the delta amount and set the "value" JSON element to the new value of localInteger.
152+ obj .put ("value" , localInteger .addAndGet (delta ));
153+
154+ // Return the new JSON object to the channel client.
99155 return obj ;
100156 }
101157 });
@@ -217,4 +273,4 @@ public static void main(String[] args) {
217273
218274 java .lang .System .exit (0 );
219275 }
220- }
276+ }
0 commit comments