|
| 1 | +package com.openfin.desktop.demo; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.concurrent.atomic.AtomicInteger; |
| 5 | +import java.util.concurrent.locks.LockSupport; |
| 6 | + |
| 7 | +import javax.json.Json; |
| 8 | +import javax.json.JsonNumber; |
| 9 | + |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import com.openfin.desktop.FinLauncher; |
| 14 | +import com.openfin.desktop.FinRuntime; |
| 15 | +import com.openfin.desktop.FinRuntimeConnectionListener; |
| 16 | + |
| 17 | +public class ChannelApiDemo { |
| 18 | + |
| 19 | + private final static Logger logger = LoggerFactory.getLogger(ChannelApiDemo.class); |
| 20 | + private final static String CHANNEL_NAME = "ChannelApiDemo"; |
| 21 | + private Thread invokingThread; |
| 22 | + private FinRuntime fin; |
| 23 | + |
| 24 | + public ChannelApiDemo(String type) { |
| 25 | + this.invokingThread = Thread.currentThread(); |
| 26 | + this.initOpenFin(type); |
| 27 | + } |
| 28 | + |
| 29 | + private void initOpenFin(String type) { |
| 30 | + FinLauncher.newLauncherBuilder() |
| 31 | + .connectionListener(new FinRuntimeConnectionListener() { |
| 32 | + @Override |
| 33 | + public void onClose(String reason) { |
| 34 | + LockSupport.unpark(invokingThread); |
| 35 | + } |
| 36 | + }).build().launch().thenAccept(fin->{ |
| 37 | + this.fin = fin; |
| 38 | + if ("client".equals(type)) { |
| 39 | + this.createChannelClient(); |
| 40 | + } |
| 41 | + else { |
| 42 | + this.createChannelProvider(); |
| 43 | + } |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + private void createChannelClient( ) { |
| 48 | + fin.Channel.connect(CHANNEL_NAME).thenAccept(client->{ |
| 49 | + logger.info("channel client connected"); |
| 50 | + client.dispatch("getValue").thenAccept(v->{ |
| 51 | + logger.info("client got value \"{}\" from provider after invoking getValue", ((JsonNumber)v).intValue()); |
| 52 | + }).thenCompose(v->{ |
| 53 | + return client.dispatch("increment").thenAccept(v2->{ |
| 54 | + logger.info("client got value \"{}\" from provider after invoking increment", ((JsonNumber)v2).intValue()); |
| 55 | + }); |
| 56 | + }).thenCompose(v->{ |
| 57 | + return client.dispatch("incrementBy", Json.createValue(13)).thenAccept(v2->{ |
| 58 | + logger.info("client got value \"{}\" from provider after invoking incrementBy", ((JsonNumber)v2).intValue()); |
| 59 | + }); |
| 60 | + }).thenAccept(v->{ |
| 61 | + client.dispatch("quitProvider"); //probably won't get response |
| 62 | + try { |
| 63 | + Thread.sleep(500); |
| 64 | + } |
| 65 | + catch (InterruptedException e) { |
| 66 | + e.printStackTrace(); |
| 67 | + } |
| 68 | + fin.disconnect(); |
| 69 | + }); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private void createChannelProvider() { |
| 74 | + fin.Channel.addChannelDisconnectListener(e->{ |
| 75 | + if (Objects.equals(CHANNEL_NAME, e.getString("channelName"))) { |
| 76 | + logger.info("provider disconnected"); |
| 77 | + fin.disconnect(); |
| 78 | + } |
| 79 | + }); |
| 80 | + fin.Channel.create(CHANNEL_NAME).thenAccept(provider -> { |
| 81 | + logger.info("provider created"); |
| 82 | + // provider created, register actions. |
| 83 | + AtomicInteger x = new AtomicInteger(0); |
| 84 | + |
| 85 | + provider.register("getValue", (payload, senderIdentity) -> { |
| 86 | + logger.info("provider processing action getValue"); |
| 87 | + return Json.createValue(x.get()); |
| 88 | + }); |
| 89 | + |
| 90 | + provider.register("increment", (payload, senderIdentity) -> { |
| 91 | + logger.info("provider processing action increment"); |
| 92 | + return Json.createValue(x.incrementAndGet()); |
| 93 | + }); |
| 94 | + |
| 95 | + provider.register("incrementBy", (payload, senderIdentity) -> { |
| 96 | + logger.info("provider processing action incrementBy, payload={}", payload.toString()); |
| 97 | + int delta = ((JsonNumber) payload).intValue(); |
| 98 | + return Json.createValue(x.addAndGet(delta)); |
| 99 | + }); |
| 100 | + provider.register("quitProvider", (payload, senderIdentity) -> { |
| 101 | + provider.destroy(); |
| 102 | + return null; |
| 103 | + }); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + public static void main(String[] args) { |
| 108 | + new ChannelApiDemo(args.length > 0 ? args[0] : null); |
| 109 | + LockSupport.park(); |
| 110 | + } |
| 111 | +} |
0 commit comments