Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ab93f7ec524c9f4094e09f252d458cf48dbdf12a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
/*******************************************************************************
 * Copyright (c) 2007, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.tcf.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.eclipse.tm.internal.tcf.core.ServiceManager;
import org.eclipse.tm.internal.tcf.core.Token;
import org.eclipse.tm.internal.tcf.core.TransportManager;
import org.eclipse.tm.internal.tcf.services.local.LocatorService;
import org.eclipse.tm.internal.tcf.services.remote.GenericProxy;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IErrorReport;
import org.eclipse.tm.tcf.protocol.IPeer;
import org.eclipse.tm.tcf.protocol.IService;
import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.protocol.JSON;
import org.eclipse.tm.tcf.protocol.Protocol;
import org.eclipse.tm.tcf.services.ILocator;

/**
 * Abstract implementation of IChannel interface.
 *
 * AbstractChannel implements communication link connecting two end points (peers).
 * The channel asynchronously transmits messages: commands, results and events.
 *
 * Clients can subclass AbstractChannel to support particular transport (wire) protocol.
 * Also, see StreamChannel for stream oriented transport protocols.
 */
public abstract class AbstractChannel implements IChannel {

    public interface TraceListener {

        public void onMessageReceived(char type, String token,
                String service, String name, byte[] data);

        public void onMessageSent(char type, String token,
                String service, String name, byte[] data);

        public void onChannelClosed(Throwable error);
    }

    public interface Proxy {

        public void onCommand(IToken token, String service, String name, byte[] data);

        public void onEvent(String service, String name, byte[] data);

        public void onChannelClosed(Throwable error);
    }

    private static class Message {
        final char type;
        Token token;
        String service;
        String name;
        byte[] data;

        boolean is_sent;
        boolean is_canceled;

        Collection<TraceListener> trace;

        Message(char type) {
            this.type = type;
        }

        @Override
        public String toString() {
            try {
                StringBuffer bf = new StringBuffer();
                bf.append('[');;
                bf.append(type);
                if (token != null) {
                    bf.append(' ');
                    bf.append(token.getID());
                }
                if (service != null) {
                    bf.append(' ');
                    bf.append(service);
                }
                if (name != null) {
                    bf.append(' ');
                    bf.append(name);
                }
                if (data != null) {
                    int i = 0;
                    while (i < data.length) {
                        int j = i;
                        while (j < data.length && data[j] != 0) j++;
                        bf.append(' ');
                        bf.append(new String(data, i, j - i, "UTF8"));
                        if (j < data.length && data[j] == 0) j++;
                        i = j;
                    }
                }
                bf.append(']');
                return bf.toString();
            }
            catch (Exception x) {
                return x.toString();
            }
        }
    }

    private static IChannelListener[] listeners_array = new IChannelListener[4];

    private final LinkedList<Map<String,String>> redirect_queue = new LinkedList<Map<String,String>>();
    private final Map<Class<?>,IService> local_service_by_class = new HashMap<Class<?>,IService>();
    private final Map<Class<?>,IService> remote_service_by_class = new HashMap<Class<?>,IService>();
    private final Map<String,IService> local_service_by_name = new HashMap<String,IService>();
    private final Map<String,IService> remote_service_by_name = new HashMap<String,IService>();
    private final LinkedList<Message> out_queue = new LinkedList<Message>();
    private final Collection<IChannelListener> channel_listeners = new ArrayList<IChannelListener>();
    private final Map<String,IChannel.IEventListener[]> event_listeners = new HashMap<String,IChannel.IEventListener[]>();
    private final Map<String,IChannel.ICommandServer> command_servers = new HashMap<String,IChannel.ICommandServer>();
    private final Map<String,Message> out_tokens = new HashMap<String,Message>();
    private final Thread inp_thread;
    private final Thread out_thread;
    private boolean notifying_channel_opened;
    private boolean registered_with_trasport;
    private int state = STATE_OPENING;
    private IToken redirect_command;
    private final IPeer local_peer;
    private IPeer remote_peer;
    private Proxy proxy;
    private boolean zero_copy;

    private static final int pending_command_limit = 32;
    private int local_congestion_level = -100;
    private int remote_congestion_level = -100;
    private long local_congestion_time;
    private int local_congestion_cnt;
    private Collection<TraceListener> trace_listeners;

    public static final int
        EOS = -1, // End Of Stream
        EOM = -2; // End Of Message

    protected AbstractChannel(IPeer remote_peer) {
        this(LocatorService.getLocalPeer(), remote_peer);
    }

    protected AbstractChannel(IPeer local_peer, IPeer remote_peer) {
        assert Protocol.isDispatchThread();
        this.remote_peer = remote_peer;
        this.local_peer = local_peer;

        inp_thread = new Thread() {

            final byte[] empty_byte_array = new byte[0];
            byte[] buf = new byte[1024];
            byte[] eos;

            private void error() throws IOException {
                throw new IOException("Protocol syntax error");
            }

            private byte[] readBytes(int end) throws IOException {
                int len = 0;
                for (;;) {
                    int n = read();
                    if (n <= 0) {
                        if (n == end) break;
                        if (n == EOM) throw new IOException("Unexpected end of message");
                        if (n < 0) throw new IOException("Communication channel is closed by remote peer");
                    }
                    if (len >= buf.length) {
                        byte[] tmp = new byte[buf.length * 2];
                        System.arraycopy(buf, 0, tmp, 0, len);
                        buf = tmp;
                    }
                    buf[len++] = (byte)n;
                }
                if (len == 0) return empty_byte_array;
                byte[] res = new byte[len];
                System.arraycopy(buf, 0, res, 0, len);
                return res;
            }

            private String readString() throws IOException {
                int len = 0;
                for (;;) {
                    int n = read();
                    if (n <= 0) {
                        if (n == 0) break;
                        if (n == EOM) throw new IOException("Unexpected end of message");
                        if (n < 0) throw new IOException("Communication channel is closed by remote peer");
                    }
                    if (len >= buf.length) {
                        byte[] tmp = new byte[buf.length * 2];
                        System.arraycopy(buf, 0, tmp, 0, len);
                        buf = tmp;
                    }
                    buf[len++] = (byte)n;
                }
                return new String(buf, 0, len, "UTF8");
            }

            @Override
            public void run() {
                try {
                    while (true) {
                        int n = read();
                        if (n == EOM) continue;
                        if (n == EOS) {
                            eos = readBytes(EOM);
                            break;
                        }
                        final Message msg = new Message((char)n);
                        if (read() != 0) error();
                        switch (msg.type) {
                        case 'C':
                            msg.token = new Token(readBytes(0));
                            msg.service = readString();
                            msg.name = readString();
                            msg.data = readBytes(EOM);
                            break;
                        case 'P':
                        case 'R':
                        case 'N':
                            msg.token = new Token(readBytes(0));
                            msg.data = readBytes(EOM);
                            break;
                        case 'E':
                            msg.service = readString();
                            msg.name = readString();
                            msg.data = readBytes(EOM);
                            break;
                        case 'F':
                            msg.data = readBytes(EOM);
                            break;
                        default:
                            error();
                        }
                        Protocol.invokeLater(new Runnable() {
                            public void run() {
                                handleInput(msg);
                            }
                        });
                        int delay = local_congestion_level;
                        if (delay > 0) sleep(delay);
                    }
                    Protocol.invokeLater(new Runnable() {
                        public void run() {
                            if (out_tokens.isEmpty()) {
                                close();
                            }
                            else {
                                IOException x = new IOException("Connection reset by peer");
                                try {
                                    Object[] args = JSON.parseSequence(eos);
                                    if (args.length > 0 && args[0] != null) {
                                        x = new IOException(Command.toErrorString(args[0]));
                                    }
                                }
                                catch (IOException e) {
                                    x = e;
                                }
                                terminate(x);
                            }
                        }
                    });
                }
                catch (final Throwable x) {
                    try {
                        Protocol.invokeLater(new Runnable() {
                            public void run() {
                                terminate(x);
                            }
                        });
                    }
                    catch (IllegalStateException y) {
                        // TCF event dispatcher has shut down
                    }
                }
            }
        };

        out_thread = new Thread() {

            @Override
            public void run() {
                try {
                    while (true) {
                        Message msg = null;
                        boolean last = false;
                        synchronized (out_queue) {
                            while (out_queue.isEmpty()) out_queue.wait();
                            msg = out_queue.removeFirst();
                            if (msg == null) break;
                            last = out_queue.isEmpty();
                            if (msg.is_canceled) {
                                if (last) flush();
                                continue;
                            }
                            msg.is_sent = true;
                        }
                        if (msg.trace != null) {
                            final Message m = msg;
                            Protocol.invokeLater(new Runnable() {
                                public void run() {
                                    for (TraceListener l : m.trace) {
                                        try {
                                            l.onMessageSent(m.type, m.token == null ? null : m.token.getID(),
                                                    m.service, m.name, m.data);
                                        }
                                        catch (Throwable x) {
                                            Protocol.log("Exception in channel listener", x);
                                        }
                                    }
                                }
                            });
                        }
                        write(msg.type);
                        write(0);
                        if (msg.token != null) {
                            write(msg.token.getBytes());
                            write(0);
                        }
                        if (msg.service != null) {
                            write(msg.service.getBytes("UTF8"));
                            write(0);
                        }
                        if (msg.name != null) {
                            write(msg.name.getBytes("UTF8"));
                            write(0);
                        }
                        if (msg.data != null) {
                            write(msg.data);
                        }
                        write(EOM);
                        int delay = 0;
                        int level = remote_congestion_level;
                        if (level > 0) delay = level * 10;
                        if (last || delay > 0) flush();
                        if (delay > 0) sleep(delay);
                        else yield();
                    }
                    write(EOS);
                    write(EOM);
                    flush();
                }
                catch (final Throwable x) {
                    try {
                        Protocol.invokeLater(new Runnable() {
                            public void run() {
                                terminate(x);
                            }
                        });
                    }
                    catch (IllegalStateException y) {
                        // TCF event dispatcher has shut down
                    }
                }
            }
        };
        inp_thread.setName("TCF Channel Receiver");
        out_thread.setName("TCF Channel Transmitter");
    }

    protected void start() {
        assert Protocol.isDispatchThread();
        Protocol.invokeLater(new Runnable() {
            public void run() {
                try {
                    if (proxy != null) return;
                    if (state == STATE_CLOSED) return;
                    ServiceManager.onChannelCreated(AbstractChannel.this, local_service_by_name);
                    makeServiceByClassMap(local_service_by_name, local_service_by_class);
                    Object[] args = new Object[]{ local_service_by_name.keySet() };
                    sendEvent(Protocol.getLocator(), "Hello", JSON.toJSONSequence(args));
                }
                catch (IOException x) {
                    terminate(x);
                }
            }
        });
        inp_thread.start();
        out_thread.start();
    }

    /**
     * Redirect this channel to given peer using this channel remote peer locator service as a proxy.
     * @param peer_id - peer that will become new remote communication endpoint of this channel
     */
    public void redirect(String peer_id) {
        Map<String,String> map = new HashMap<String,String>();
        map.put(IPeer.ATTR_ID, peer_id);
        redirect(map);
    }

    /**
     * Redirect this channel to given peer using this channel remote peer locator service as a proxy.
     * @param peer_attrs - peer that will become new remote communication endpoint of this channel
     */
    public void redirect(final Map<String,String> peer_attrs) {
        assert Protocol.isDispatchThread();
        if (state == STATE_OPENING) {
            redirect_queue.add(peer_attrs);
        }
        else {
            assert state == STATE_OPEN;
            assert redirect_command == null;
            try {
                final ILocator l = (ILocator)remote_service_by_class.get(ILocator.class);
                if (l == null) throw new IOException("Cannot redirect channel: peer " +
                        remote_peer.getID() + " has no locator service");
                final String peer_id = peer_attrs.get(IPeer.ATTR_ID);
                if (peer_id != null && peer_attrs.size() == 1) {
                    final IPeer peer = l.getPeers().get(peer_id);
                    if (peer == null) {
                        // Peer not found, must wait for a while until peer is discovered or time out
                        final boolean[] found = new boolean[1];
                        Protocol.invokeLater(ILocator.DATA_RETENTION_PERIOD / 3, new Runnable() {
                            public void run() {
                                if (found[0]) return;
                                terminate(new Exception("Peer " + peer_id + " not found"));
                            }
                        });
                        l.addListener(new ILocator.LocatorListener() {
                            public void peerAdded(IPeer peer) {
                                if (peer.getID().equals(peer_id)) {
                                    found[0] = true;
                                    state = STATE_OPEN;
                                    l.removeListener(this);
                                    redirect(peer_id);
                                }
                            }
                            public void peerChanged(IPeer peer) {
                            }

                            public void peerHeartBeat(String id) {
                            }

                            public void peerRemoved(String id) {
                            }
                        });
                    }
                    else {
                        redirect_command = l.redirect(peer_id, new ILocator.DoneRedirect() {
                            public void doneRedirect(IToken token, Exception x) {
                                assert redirect_command == token;
                                redirect_command = null;
                                if (state != STATE_OPENING) return;
                                if (x != null) terminate(x);
                                remote_peer = peer;
                                remote_service_by_class.clear();
                                remote_service_by_name.clear();
                                event_listeners.clear();
                            }
                        });
                    }
                }
                else {
                    redirect_command = l.redirect(peer_attrs, new ILocator.DoneRedirect() {
                        public void doneRedirect(IToken token, Exception x) {
                            assert redirect_command == token;
                            redirect_command = null;
                            if (state != STATE_OPENING) return;
                            if (x != null) terminate(x);
                            final IPeer parent = remote_peer;
                            remote_peer = new TransientPeer(peer_attrs) {
                                public IChannel openChannel() {
                                    IChannel c = parent.openChannel();
                                    c.redirect(peer_attrs);
                                    return c;
                                }
                            };
                            remote_service_by_class.clear();
                            remote_service_by_name.clear();
                            event_listeners.clear();
                        }
                    });
                }
                state = STATE_OPENING;
            }
            catch (Throwable x) {
                terminate(x);
            }
        }
    }

    private void makeServiceByClassMap(Map<String,IService> by_name, Map<Class<?>,IService> by_class) {
        for (IService service : by_name.values()) {
            for (Class<?> fs : service.getClass().getInterfaces()) {
                if (fs.equals(IService.class)) continue;
                if (!IService.class.isAssignableFrom(fs)) continue;
                by_class.put(fs, service);
            }
        }
    }

    public final int getState() {
        return state;
    }

    public void addChannelListener(IChannelListener listener) {
        assert Protocol.isDispatchThread();
        assert listener != null;
        channel_listeners.add(listener);
    }

    public void removeChannelListener(IChannelListener listener) {
        assert Protocol.isDispatchThread();
        channel_listeners.remove(listener);
    }

    public void addTraceListener(TraceListener listener) {
        if (trace_listeners == null) {
            trace_listeners = new ArrayList<TraceListener>();
        }
        else {
            trace_listeners = new ArrayList<TraceListener>(trace_listeners);
        }
        trace_listeners.add(listener);
    }

    public void removeTraceListener(TraceListener listener) {
        trace_listeners = new ArrayList<TraceListener>(trace_listeners);
        trace_listeners.remove(listener);
        if (trace_listeners.isEmpty()) trace_listeners = null;
    }

    public void addEventListener(IService service, IChannel.IEventListener listener) {
        assert Protocol.isDispatchThread();
        IChannel.IEventListener[] list = event_listeners.get(service.getName());
        IChannel.IEventListener[] next = new IChannel.IEventListener[list == null ? 1 : list.length + 1];
        if (list != null) System.arraycopy(list, 0, next, 0, list.length);
        next[next.length - 1] = listener;
        event_listeners.put(service.getName(), next);
    }

    public void removeEventListener(IService service, IChannel.IEventListener listener) {
        assert Protocol.isDispatchThread();
        IChannel.IEventListener[] list = event_listeners.get(service.getName());
        for (int i = 0; i < list.length; i++) {
            if (list[i] == listener) {
                if (list.length == 1) {
                    event_listeners.remove(service.getName());
                }
                else {
                    IChannel.IEventListener[] next = new IChannel.IEventListener[list.length - 1];
                    System.arraycopy(list, 0, next, 0, i);
                    System.arraycopy(list, i + 1, next, i, next.length - i);
                    event_listeners.put(service.getName(), next);
                }
                return;
            }
        }
    }

    public void addCommandServer(IService service, IChannel.ICommandServer listener) {
        assert Protocol.isDispatchThread();
        if (command_servers.put(service.getName(), listener) != null) {
            throw new Error("Only one command server per service is allowed");
        }
    }

    public void removeCommandServer(IService service, IChannel.ICommandServer listener) {
        assert Protocol.isDispatchThread();
        if (command_servers.remove(service.getName()) != listener) {
            throw new Error("Invalid command server");
        }
    }

    public void close() {
        assert Protocol.isDispatchThread();
        if (state == STATE_CLOSED) return;
        try {
            sendEndOfStream(10000);
            close(null);
        }
        catch (Exception x) {
            close(x);
        }
    }

    public void terminate(Throwable error) {
        assert Protocol.isDispatchThread();
        if (state == STATE_CLOSED) return;
        try {
            sendEndOfStream(500);
            close(error);
        }
        catch (Exception x) {
            if (error == null) error = x;
            close(error);
        }
    }

    private void sendEndOfStream(long timeout) throws Exception {
        synchronized (out_queue) {
            out_queue.clear();
            out_queue.add(null);
            out_queue.notify();
        }
        out_thread.join(timeout);
    }

    private void close(final Throwable error) {
        assert state != STATE_CLOSED;
        state = STATE_CLOSED;
        // Closing channel underlying streams can block for a long time,
        // so it needs to be done by a background thread.
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    AbstractChannel.this.stop();
                }
                catch (Exception x) {
                    Protocol.log("Cannot close channel streams", x);
                }
            }
        };
        thread.setName("TCF Channel Cleanup");
        thread.setDaemon(true);
        thread.start();
        if (error != null && remote_peer instanceof AbstractPeer) {
            ((AbstractPeer)remote_peer).onChannelTerminated();
        }
        if (registered_with_trasport) {
            registered_with_trasport = false;
            TransportManager.channelClosed(this, error);
        }
        if (proxy != null) {
            try {
                proxy.onChannelClosed(error);
            }
            catch (Throwable x) {
                Protocol.log("Exception in channel listener", x);
            }
        }
        Protocol.invokeLater(new Runnable() {
            public void run() {
                if (!out_tokens.isEmpty()) {
                    Exception x = null;
                    if (error instanceof Exception) x = (Exception)error;
                    else if (error != null) x = new Exception(error);
                    else x = new IOException("Channel is closed");
                    for (Message msg : out_tokens.values()) {
                        try {
                            String s = msg.toString();
                            if (s.length() > 72) s = s.substring(0, 72) + "...]";
                            IOException y = new IOException("Command " + s + " aborted");
                            y.initCause(x);
                            msg.token.getListener().terminated(msg.token, y);
                        }
                        catch (Throwable e) {
                            Protocol.log("Exception in command listener", e);
                        }
                    }
                    out_tokens.clear();
                }
                if (!channel_listeners.isEmpty()) {
                    listeners_array = channel_listeners.toArray(listeners_array);
                    for (IChannelListener l : listeners_array) {
                        if (l == null) break;
                        try {
                            l.onChannelClosed(error);
                        }
                        catch (Throwable x) {
                            Protocol.log("Exception in channel listener", x);
                        }
                    }
                }
                else if (error != null) {
                    Protocol.log("TCF channel terminated", error);
                }
                if (trace_listeners != null) {
                    for (TraceListener l : trace_listeners) {
                        try {
                            l.onChannelClosed(error);
                        }
                        catch (Throwable x) {
                            Protocol.log("Exception in channel listener", x);
                        }
                    }
                }
            }
        });
    }

    public int getCongestion() {
        assert Protocol.isDispatchThread();
        int level = out_tokens.size() * 100 / pending_command_limit - 100;
        if (remote_congestion_level > level) level = remote_congestion_level;
        if (level > 100) level = 100;
        return level;
    }

    public IPeer getLocalPeer() {
        assert Protocol.isDispatchThread();
        return local_peer;
    }

    public IPeer getRemotePeer() {
        assert Protocol.isDispatchThread();
        return remote_peer;
    }

    public Collection<String> getLocalServices() {
        assert Protocol.isDispatchThread();
        assert state != STATE_OPENING;
        return local_service_by_name.keySet();
    }

    public Collection<String> getRemoteServices() {
        assert Protocol.isDispatchThread();
        assert state != STATE_OPENING;
        return remote_service_by_name.keySet();
    }

    @SuppressWarnings("unchecked")
    public <V extends IService> V getLocalService(Class<V> cls) {
        assert Protocol.isDispatchThread();
        assert state != STATE_OPENING;
        return (V)local_service_by_class.get(cls);
    }

    @SuppressWarnings("unchecked")
    public <V extends IService> V getRemoteService(Class<V> cls) {
        assert Protocol.isDispatchThread();
        assert state != STATE_OPENING;
        return (V)remote_service_by_class.get(cls);
    }

    public <V extends IService> void setServiceProxy(Class<V> service_interface, IService service_proxy) {
        if (!notifying_channel_opened) new Error("setServiceProxe() can be called only from channel open call-back");
        if (!(remote_service_by_name.get(service_proxy.getName()) instanceof GenericProxy)) throw new Error("Proxy already set");
        if (remote_service_by_class.get(service_interface) != null) throw new Error("Proxy already set");
        remote_service_by_class.put(service_interface, service_proxy);
        remote_service_by_name.put(service_proxy.getName(), service_proxy);
    }

    public IService getLocalService(String service_name) {
        assert Protocol.isDispatchThread();
        assert state != STATE_OPENING;
        return local_service_by_name.get(service_name);
    }

    public IService getRemoteService(String service_name) {
        assert Protocol.isDispatchThread();
        assert state != STATE_OPENING;
        return remote_service_by_name.get(service_name);
    }

    public void setProxy(Proxy proxy, Collection<String> services) throws IOException {
        this.proxy = proxy;
        sendEvent(Protocol.getLocator(), "Hello", JSON.toJSONSequence(new Object[]{ services }));
        local_service_by_class.clear();
        local_service_by_name.clear();
    }

    private void addToOutQueue(Message msg) {
        msg.trace = trace_listeners;
        synchronized (out_queue) {
            out_queue.add(msg);
            out_queue.notify();
        }
    }

    public IToken sendCommand(IService service, String name, byte[] args, ICommandListener listener) {
        assert Protocol.isDispatchThread();
        if (state == STATE_OPENING) throw new Error("Channel is waiting for Hello message");
        if (state == STATE_CLOSED) throw new Error("Channel is closed");
        final Message msg = new Message('C');
        msg.service = service.getName();
        msg.name = name;
        msg.data = args;
        Token token = new Token(listener) {
            @Override
            public boolean cancel() {
                assert Protocol.isDispatchThread();
                if (state != STATE_OPEN) return false;
                synchronized (out_queue) {
                    if (msg.is_sent) return false;
                    msg.is_canceled = true;
                }
                out_tokens.remove(msg.token.getID());
                return true;
            }
        };
        msg.token = token;
        out_tokens.put(token.getID(), msg);
        addToOutQueue(msg);
        return token;
    }

    public void sendProgress(IToken token, byte[] results) {
        assert Protocol.isDispatchThread();
        if (state != STATE_OPEN) throw new Error("Channel is closed");
        Message msg = new Message('P');
        msg.data = results;
        msg.token = (Token)token;
        addToOutQueue(msg);
    }

    public void sendResult(IToken token, byte[] results) {
        assert Protocol.isDispatchThread();
        if (state != STATE_OPEN) throw new Error("Channel is closed");
        Message msg = new Message('R');
        msg.data = results;
        msg.token = (Token)token;
        addToOutQueue(msg);
    }

    public void rejectCommand(IToken token) {
        assert Protocol.isDispatchThread();
        if (state != STATE_OPEN) throw new Error("Channel is closed");
        Message msg = new Message('N');
        msg.token = (Token)token;
        addToOutQueue(msg);
    }

    public void sendEvent(IService service, String name, byte[] args) {
        assert Protocol.isDispatchThread();
        if (!(state == STATE_OPEN || state == STATE_OPENING && service instanceof ILocator)) {
            throw new Error("Channel is closed");
        }
        Message msg = new Message('E');
        msg.service = service.getName();
        msg.name = name;
        msg.data = args;
        addToOutQueue(msg);
    }

    public boolean isZeroCopySupported() {
        return zero_copy;
    }

    @SuppressWarnings("unchecked")
    private void handleInput(Message msg) {
        assert Protocol.isDispatchThread();
        if (state == STATE_CLOSED) return;
        if (trace_listeners != null) {
            for (TraceListener l : trace_listeners) {
                try {
                    l.onMessageReceived(msg.type,
                            msg.token != null ? msg.token.getID() : null,
                            msg.service, msg.name, msg.data);
                }
                catch (Throwable x) {
                    Protocol.log("Exception in trace listener", x);
                }
            }
        }
        try {
            Token token = null;
            switch (msg.type) {
            case 'P':
            case 'R':
            case 'N':
                String token_id = msg.token.getID();
                Message cmd = msg.type == 'P' ? out_tokens.get(token_id) : out_tokens.remove(token_id);
                if (cmd == null) throw new Exception("Invalid token received: " + token_id);
                token = cmd.token;
                break;
            }
            switch (msg.type) {
            case 'C':
                if (state == STATE_OPENING) {
                    throw new IOException("Received command " + msg.service + "." + msg.name + " before Hello message");
                }
                if (proxy != null) {
                    proxy.onCommand(msg.token, msg.service, msg.name, msg.data);
                }
                else {
                    token = msg.token;
                    IChannel.ICommandServer cmds = command_servers.get(msg.service);
                    if (cmds != null) {
                        cmds.command(token, msg.name, msg.data);
                    }
                    else {
                        rejectCommand(token);
                    }
                }
                break;
            case 'P':
                token.getListener().progress(token, msg.data);
                sendCongestionLevel();
                break;
            case 'R':
                token.getListener().result(token, msg.data);
                sendCongestionLevel();
                break;
            case 'N':
                token.getListener().terminated(token, new ErrorReport(
                        "Command is not recognized", IErrorReport.TCF_ERROR_INV_COMMAND));
                break;
            case 'E':
                boolean hello = msg.service.equals(ILocator.NAME) && msg.name.equals("Hello");
                if (hello) {
                    remote_service_by_name.clear();
                    remote_service_by_class.clear();
                    ServiceManager.onChannelOpened(this, (Collection<String>)JSON.parseSequence(msg.data)[0], remote_service_by_name);
                    makeServiceByClassMap(remote_service_by_name, remote_service_by_class);
                    zero_copy = remote_service_by_name.containsKey("ZeroCopy");
                }
                if (proxy != null && state == STATE_OPEN) {
                    proxy.onEvent(msg.service, msg.name, msg.data);
                }
                else if (hello) {
                    assert state == STATE_OPENING;
                    state = STATE_OPEN;
                    assert redirect_command == null;
                    if (redirect_queue.size() > 0) {
                        redirect(redirect_queue.removeFirst());
                    }
                    else {
                        notifying_channel_opened = true;
                        if (!registered_with_trasport) {
                            TransportManager.channelOpened(this);
                            registered_with_trasport = true;
                        }
                        listeners_array = channel_listeners.toArray(listeners_array);
                        for (IChannelListener l : listeners_array) {
                            if (l == null) break;
                            try {
                                l.onChannelOpened();
                            }
                            catch (Throwable x) {
                                Protocol.log("Exception in channel listener", x);
                            }
                        }
                        notifying_channel_opened = false;
                    }
                }
                else {
                    IChannel.IEventListener[] list = event_listeners.get(msg.service);
                    if (list != null) {
                        for (int i = 0; i < list.length; i++) {
                            list[i].event(msg.name, msg.data);
                        }
                    }
                    sendCongestionLevel();
                }
                break;
            case 'F':
                int len = msg.data.length;
                if (len > 0 && msg.data[len - 1] == 0) len--;
                remote_congestion_level = Integer.parseInt(new String(msg.data, 0, len, "ASCII"));
                break;
            default:
                assert false;
                break;
            }
        }
        catch (Throwable x) {
            terminate(x);
        }
    }

    private void sendCongestionLevel() throws IOException {
        if (++local_congestion_cnt < 8) return;
        local_congestion_cnt = 0;
        if (state != STATE_OPEN) return;
        long time = System.currentTimeMillis();
        if (time - local_congestion_time < 500) return;
        assert Protocol.isDispatchThread();
        int level = Protocol.getCongestionLevel();
        if (level == local_congestion_level) return;
        int i = (level - local_congestion_level) / 8;
        if (i != 0) level = local_congestion_level + i;
        local_congestion_time = time;
        synchronized (out_queue) {
            Message msg = out_queue.isEmpty() ? null : out_queue.get(0);
            if (msg == null || msg.type != 'F') {
                msg = new Message('F');
                out_queue.add(0, msg);
                out_queue.notify();
            }
            StringBuilder buffer = new StringBuilder();
            buffer.append(local_congestion_level);
            buffer.append((char)0); // 0 terminate
            msg.data = buffer.toString().getBytes("ASCII");
            msg.trace = trace_listeners;
            local_congestion_level = level;
        }
    }

    /**
     * Read one byte from the channel input stream.
     * @return next data byte or EOS (-1) if end of stream is reached,
     * or EOM (-2) if end of message is reached.
     * @throws IOException
     */
    protected abstract int read() throws IOException;

    /**
     * Write one byte into the channel output stream.
     * The method argument can be one of two special values:
     *   EOS (-1) end of stream marker;
     *   EOM (-2) end of message marker.
     * The stream can put the byte into a buffer instead of transmitting it right away.
     * @param n - the data byte.
     * @throws IOException
     */
    protected abstract void write(int n) throws IOException;

    /**
     * Flush the channel output stream.
     * All buffered data should be transmitted immediately.
     * @throws IOException
     */
    protected abstract void flush() throws IOException;

    /**
     * Stop (close) channel underlying streams.
     * If a thread is blocked by read() or write(), it should be
     * resumed (or interrupted).
     * @throws IOException
     */
    protected abstract void stop() throws IOException;

    /**
     * Write array of bytes into the channel output stream.
     * The stream can put bytes into a buffer instead of transmitting it right away.
     * @param buf
     * @throws IOException
     */
    protected void write(byte[] buf) throws IOException {
        assert Thread.currentThread() == out_thread;
        for (int i = 0; i < buf.length; i++) write(buf[i] & 0xff);
    }
}

Back to the top