Download Jar. Transpiled version (2771L) is out of date.
1 | !7 |
2 | |
3 | import java.nio.*; |
4 | import java.nio.channels.*; |
5 | |
6 | static long timeout = 30000+10000; // idle time + latency |
7 | static ConnectedInstances cI; |
8 | static ReliableSingleThread rst = new(r update); |
9 | static int theNumber; |
10 | static int port = 6000; |
11 | static Set<AsynchronousSocketChannel> channels = synchroSet(); |
12 | |
13 | sclass State { |
14 | S computerID; |
15 | int countSeen = -1; |
16 | } |
17 | |
18 | p { |
19 | cI = new ConnectedInstances; |
20 | cI.connected2.onChange(rst); |
21 | |
22 | InetSocketAddress sockAddr = new InetSocketAddress(port); |
23 | AsynchronousServerSocketChannel serverSock = AsynchronousServerSocketChannel.open().bind(sockAddr); |
24 | |
25 | //start to accept the connection from client |
26 | serverSock.accept(serverSock, new CompletionHandler<AsynchronousSocketChannel,AsynchronousServerSocketChannel>() { |
27 | public void completed(AsynchronousSocketChannel sockChannel, AsynchronousServerSocketChannel serverSock) { |
28 | //a connection is accepted, start to accept next connection |
29 | serverSock.accept(serverSock, this); |
30 | |
31 | channels.add(channel); |
32 | |
33 | //start to read message from the client |
34 | startRead( sockChannel ); |
35 | } |
36 | |
37 | public void failed(Throwable exc, AsynchronousServerSocketChannel serverSock) { |
38 | print("fail to accept a connection"); |
39 | } |
40 | }); |
41 | } |
42 | |
43 | static void startRead( AsynchronousSocketChannel sockChannel) { |
44 | final ByteBuffer buf = ByteBuffer.allocate(2048); |
45 | |
46 | //read message from client |
47 | sockChannel.read(buf, sockChannel, new CompletionHandler<Int, AsynchronousSocketChannel>() { |
48 | @Override |
49 | public void completed(Int result, AsynchronousSocketChannel channel) { |
50 | buf.flip(); |
51 | |
52 | // echo the message |
53 | startWrite(channel, buf); |
54 | |
55 | // start to read next message again |
56 | startRead(channel); |
57 | } |
58 | |
59 | @Override |
60 | public void failed(Throwable exc, AsynchronousSocketChannel channel) { |
61 | print( "fail to read message from client"); |
62 | } |
63 | }); |
64 | } |
65 | |
66 | static void startWrite(AsynchronousSocketChannel sockChannel, final ByteBuffer buf) { |
67 | sockChannel.write(buf, sockChannel, new CompletionHandler<Integer, AsynchronousSocketChannel >() { |
68 | @Override |
69 | public void completed(Int result, AsynchronousSocketChannel channel) { |
70 | // finish to write message to client, nothing to do |
71 | // (close socket?) |
72 | } |
73 | |
74 | @Override |
75 | public void failed(Throwable exc, AsynchronousSocketChannel channel) { |
76 | print("Fail to write message to client"); |
77 | } |
78 | }); |
79 | } |
80 | |
81 | html { ret "Use port " + port; } |
82 | |
83 | svoid sendNumber(O ws) { |
84 | State s = wsToState.get(ws); |
85 | if (s != null && theNumber != s.countSeen) { |
86 | s.countSeen = theNumber; |
87 | print("Sending number to " + s.computerID + ": " + theNumber); |
88 | call(ws, 'send, str(theNumber)); |
89 | } |
90 | } |
91 | |
92 | svoid update { |
93 | print("OS Instances update"); |
94 | if (webSocketManager.isEmpty()) ret; |
95 | |
96 | int value = cI.value(); |
97 | //if (value == theNumber) ret; |
98 | theNumber = value; |
99 | for (O ws : webSocketManager.webSockets()) pcall { |
100 | sendNumber(ws); |
101 | } |
102 | print("OS Instances update done"); |
103 | } |
Began life as a copy of #1016453
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1016571 |
Snippet name: | OS Instances Connector [Async Socket, Web Bot, dev.] |
Eternal ID of this version: | #1016571/6 |
Text MD5: | 2dfe289545dcc95a29c57ee622d5b8e6 |
Author: | stefan |
Category: | javax / a.i. / web |
Type: | JavaX module |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-06-23 22:45:01 |
Source code size: | 3136 bytes / 103 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 442 / 968 |
Version history: | 5 change(s) |
Referenced in: | [show references] |