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