Skip to content

Commit 4651d9f

Browse files
committed
ADAP-156: added ChannelExample.java
1 parent 11b5a64 commit 4651d9f

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package com.openfin.desktop.demo;
2+
3+
import java.util.concurrent.CountDownLatch;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
import com.openfin.desktop.channel.*;
7+
import org.json.JSONObject;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import com.openfin.desktop.Ack;
12+
import com.openfin.desktop.AckListener;
13+
import com.openfin.desktop.AsyncCallback;
14+
import com.openfin.desktop.DesktopConnection;
15+
import com.openfin.desktop.DesktopException;
16+
import com.openfin.desktop.DesktopStateListener;
17+
import com.openfin.desktop.RuntimeConfiguration;
18+
19+
public class ChannelExample implements DesktopStateListener {
20+
21+
private static Logger logger = LoggerFactory.getLogger(ChannelExample.class.getName());
22+
private static CountDownLatch latch = new CountDownLatch(1);
23+
private static String CHANNEL_NAME="ChannelExample";
24+
25+
private DesktopConnection desktopConnection;
26+
27+
public ChannelExample() {
28+
try {
29+
desktopConnection = new DesktopConnection("ChannelExample");
30+
String desktopVersion = java.lang.System.getProperty("com.openfin.demo.runtime.version", "stable");
31+
RuntimeConfiguration configuration = new RuntimeConfiguration();
32+
configuration.setRuntimeVersion(desktopVersion);
33+
desktopConnection.connect(configuration, this, 60);
34+
}
35+
catch (Exception ex) {
36+
logger.error("Error launching Runtime", ex);
37+
}
38+
}
39+
40+
/**
41+
* Create a provider that supports "getValue", "increment" and "incrementBy n" actions
42+
*/
43+
public void createChannelProvider() {
44+
desktopConnection.getChannel().addChannelListener(new ChannelListener() {
45+
@Override
46+
public void onChannelConnect(ConnectionEvent connectionEvent) {
47+
logger.info(String.format("provider receives channel connect event from %s ", connectionEvent.getUuid()));
48+
}
49+
@Override
50+
public void onChannelDisconnect(ConnectionEvent connectionEvent) {
51+
logger.info(String.format("provider receives channel disconnect event from %s ", connectionEvent.getUuid()));
52+
}
53+
});
54+
desktopConnection.getChannel().create(CHANNEL_NAME, new AsyncCallback<ChannelProvider>() {
55+
@Override
56+
public void onSuccess(ChannelProvider provider) {
57+
//provider created, register actions.
58+
AtomicInteger x = new AtomicInteger(0);
59+
60+
provider.register("getValue", new ChannelAction() {
61+
@Override
62+
public JSONObject invoke(String action, JSONObject payload) {
63+
logger.info(String.format("provider processing action %s, payload=%s", action, payload.toString()));
64+
JSONObject obj = new JSONObject();
65+
obj.put("value", x.get());
66+
return obj;
67+
}
68+
});
69+
provider.register("increment", new ChannelAction() {
70+
@Override
71+
public JSONObject invoke(String action, JSONObject payload) {
72+
logger.info(String.format("provider processing action %s, payload=%s", action, payload.toString()));
73+
JSONObject obj = new JSONObject();
74+
obj.put("value", x.incrementAndGet());
75+
return obj;
76+
}
77+
});
78+
provider.register("incrementBy", new ChannelAction() {
79+
@Override
80+
public JSONObject invoke(String action, JSONObject payload) {
81+
logger.info(String.format("provider processing action %s, payload=%s", action, payload.toString()));
82+
int delta = payload.getInt("delta");
83+
JSONObject obj = new JSONObject();
84+
obj.put("value", x.addAndGet(delta));
85+
return obj;
86+
}
87+
});
88+
}
89+
});
90+
}
91+
92+
/**
93+
* Create a channel client that invokes "getValue", "increment" and "incrementBy n" actions
94+
*/
95+
public void createChannelClient() {
96+
desktopConnection.getChannel().connect(CHANNEL_NAME, new AsyncCallback<ChannelClient>() {
97+
@Override
98+
public void onSuccess(ChannelClient client) {
99+
//connected to provider, invoke actions provided by the provider.
100+
//get current value
101+
client.dispatch("getValue", null, new AckListener() {
102+
@Override
103+
public void onSuccess(Ack ack) {
104+
logger.info("current value={}", ack.getJsonObject().getJSONObject("data").getJSONObject("result").getInt("value"));
105+
106+
//got current value, do increment
107+
client.dispatch("increment", null, new AckListener() {
108+
@Override
109+
public void onSuccess(Ack ack) {
110+
logger.info("after invoking increment, value={}", ack.getJsonObject().getJSONObject("data").getJSONObject("result").getInt("value"));
111+
112+
//let's do increatmentBy 10
113+
JSONObject payload = new JSONObject();
114+
payload.put("delta", 10);
115+
client.dispatch("incrementBy", payload, new AckListener() {
116+
@Override
117+
public void onSuccess(Ack ack) {
118+
logger.info("after invoking incrementBy, value={}", ack.getJsonObject().getJSONObject("data").getJSONObject("result").getInt("value"));
119+
120+
try {
121+
desktopConnection.disconnect();
122+
}
123+
catch (DesktopException e) {
124+
e.printStackTrace();
125+
}
126+
}
127+
128+
@Override
129+
public void onError(Ack ack) {
130+
}
131+
});
132+
}
133+
134+
@Override
135+
public void onError(Ack ack) {
136+
}
137+
});
138+
}
139+
140+
@Override
141+
public void onError(Ack ack) {
142+
}
143+
});
144+
}
145+
146+
});
147+
}
148+
149+
@Override
150+
public void onReady() {
151+
createChannelProvider();
152+
createChannelClient();
153+
}
154+
155+
@Override
156+
public void onClose(String error) {
157+
latch.countDown();
158+
}
159+
160+
@Override
161+
public void onError(String reason) {
162+
163+
}
164+
165+
@Override
166+
public void onMessage(String message) {
167+
168+
}
169+
170+
@Override
171+
public void onOutgoingMessage(String message) {
172+
173+
}
174+
175+
public static void main(String[] args) {
176+
177+
try {
178+
new ChannelExample();
179+
latch.await();
180+
}
181+
catch (InterruptedException e) {
182+
// TODO Auto-generated catch block
183+
e.printStackTrace();
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)