Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b6c4af42d5dd7f5f2590ed1868a2022826d7be9d (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
/*******************************************************************************
 * Copyright (c) 2007, 2008 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.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
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.Token;
import org.eclipse.tm.internal.tcf.core.Transport;
import org.eclipse.tm.internal.tcf.services.local.DiagnosticsService;
import org.eclipse.tm.internal.tcf.services.local.LocatorService;
import org.eclipse.tm.internal.tcf.services.remote.GenericProxy;
import org.eclipse.tm.internal.tcf.services.remote.LocatorProxy;
import org.eclipse.tm.tcf.Activator;
import org.eclipse.tm.tcf.protocol.IChannel;
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;


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);
    }

    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;
        }

        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[64];

    private final LinkedList<IPeer> redirect_queue = new LinkedList<IPeer>();
    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> inp_tokens = new HashMap<String,Message>();
    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 shutdown;
    private int state = STATE_OPENNING;
    private IToken redirect_command;
    private IPeer peer;

    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;
    
    private static final MemoryMXBean mem_mbean = ManagementFactory.getMemoryMXBean();
    private static final long mem_limit = 32000000;

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

    protected AbstractChannel(IPeer peer) {
        assert Protocol.isDispatchThread();
        assert Protocol.getLocator().getPeers().get(peer.getID()) == peer;
        this.peer = peer;

        addLocalService(Protocol.getLocator());
        addLocalService(new DiagnosticsService(this));

        inp_thread = new Thread() {

            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 == end) break;
                    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;
                }
                byte[] res = new byte[len];
                System.arraycopy(buf, 0, res, 0, len);
                return res;
            }

            private String readString() throws IOException {
                return new String(readBytes(0), "UTF8");
            }

            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 'R':
                            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);
                                    int error_code = ((Number)args[0]).intValue();
                                    if (error_code != 0) {
                                        x = new IOException(Command.toErrorString(args[1]));
                                    }
                                }
                                catch (IOException e) {
                                    x = e;
                                }
                                terminate(x);
                            }
                        }
                    });
                }
                catch (final Throwable x) {
                    Protocol.invokeLater(new Runnable() {
                        public void run() {
                            terminate(x);
                        }
                    });
                }
            }
        };

        out_thread = new Thread() {

            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) {
                                            Activator.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);
                    flush();
                }
                catch (final Throwable x) {
                    Protocol.invokeLater(new Runnable() {
                        public void run() {
                            terminate(x);
                        }
                    });
                }
            }
        };
        inp_thread.setName("TCF Channel Receiver");
        out_thread.setName("TCF Channel Transmitter");

        try {
            Object[] args = new Object[]{ local_service_by_name.keySet() };  
            sendEvent(Protocol.getLocator(), "Hello", JSON.toJSONSequence(args));
        }
        catch (IOException x) {
            throw new Error(x);
        }
    }

    protected void start() {
        assert Protocol.isDispatchThread();
        inp_thread.start();
        out_thread.start();
        LocatorService.channelStarted(this);
    }

    public void redirect(IPeer peer) {
        assert Protocol.isDispatchThread();
        if (state == STATE_OPENNING) {
            assert redirect_command == null;
            redirect_queue.add(peer);
        }
        else {
            assert state == STATE_OPEN;
            try {
                ILocator l = (ILocator)remote_service_by_class.get(ILocator.class);
                if (l == null) throw new IOException("Peer " + peer.getID() + " has no locator service");
                this.peer = peer;
                redirect_command = l.redirect(peer.getID(), new ILocator.DoneRedirect() {
                    public void doneRedirect(IToken token, Exception x) {
                        assert redirect_command == token;
                        state = STATE_OPENNING;
                        redirect_command = null;
                        remote_congestion_level = 0;
                        if (x != null) terminate(x);
                        // Wait for next "Hello"
                    }
                });
            }
            catch (Throwable x) {
                terminate(x);
            }
        }
    }

    private void onLocatorHello(Collection<String> c) throws IOException {
        if (state != STATE_OPENNING) throw new IOException("Invalid event: Locator.Hello");
        remote_service_by_class.clear();
        String pkg_name = LocatorProxy.class.getPackage().getName();
        for (String service_name : c) {
            try {
                Class<?> cls = Class.forName(pkg_name + "." + service_name + "Proxy");
                IService service = (IService)cls.getConstructor(IChannel.class).newInstance(this);
                for (Class<?> fs : cls.getInterfaces()) {
                    if (fs.equals(IService.class)) continue;
                    if (!IService.class.isAssignableFrom(fs)) continue;
                    remote_service_by_class.put(fs, service);
                }
                assert service_name.equals(service.getName());
                remote_service_by_name.put(service_name, service);
            }
            catch (Exception x) {
                IService service = new GenericProxy(this, service_name);
                remote_service_by_name.put(service_name, service);
            }
        }
        state = STATE_OPEN;
        assert redirect_command == null;
        if (redirect_queue.size() > 0) {
            redirect(redirect_queue.removeFirst());
        }
        else {
            notifying_channel_opened = true;
            Transport.channelOpened(this);
            listeners_array = channel_listeners.toArray(listeners_array);
            for (int i = 0; i < listeners_array.length && listeners_array[i] != null; i++) {
                try {
                    listeners_array[i].onChannelOpened();
                }
                catch (Throwable x) {
                    Activator.log("Exception in channel listener", x);
                }
            }
            notifying_channel_opened = false;
        }
    }

    public final int getState() {
        return state;
    }

    public void addChannelListener(IChannelListener listener) {
        assert Protocol.isDispatchThread();
        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");
        }
    }

    private void sendEndOfStream() {
        if (shutdown) return;
        shutdown = true;
        synchronized (out_queue) {
            out_queue.clear();
            out_queue.add(0, null);
            out_queue.notify();
        }
    }
    
    public void close() {
        assert Protocol.isDispatchThread();
        try {
            sendEndOfStream();
            out_thread.join(10000);
            stop();
            inp_thread.join(10000);
            terminate(null);
        }
        catch (Exception x) {
            terminate(x);
        }
    }

    public void terminate(final Throwable error) {
        assert Protocol.isDispatchThread();
        sendEndOfStream();
        if (state == STATE_CLOSED) return;
        if (error != null) Activator.log("TCF channel terminated", error);
        state = STATE_CLOSED;
        Transport.channelClosed(this, error);
        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()) {
                        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); 
                    }
                    out_tokens.clear();
                }
                listeners_array = channel_listeners.toArray(listeners_array);
                for (int i = 0; i < listeners_array.length && listeners_array[i] != null; i++) {
                    listeners_array[i].onChannelClosed(error);
                }
                if (trace_listeners != null) {
                    for (TraceListener l : trace_listeners) {
                        try {
                            l.onChannelClosed(error);
                        }
                        catch (Throwable x) {
                            Activator.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 LocatorService.getLocalPeer();
    }

    public IPeer getRemotePeer() {
        assert Protocol.isDispatchThread();
        return peer;
    }
    
    private void addLocalService(IService service) {
        for (Class<?> fs : service.getClass().getInterfaces()) {
            if (fs.equals(IService.class)) continue;
            if (!IService.class.isAssignableFrom(fs)) continue;
            local_service_by_class.put(fs, service);
        }
        local_service_by_name.put(service.getName(), service);
    }

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

    public Collection<String> getRemoteServices() {
        return remote_service_by_name.keySet();
    }
    
    @SuppressWarnings("unchecked")
    public <V extends IService> V getLocalService(Class<V> cls) {
        return (V)local_service_by_class.get(cls);
    }

    @SuppressWarnings("unchecked")
    public <V extends IService> V getRemoteService(Class<V> cls) {
        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);
    }
    
    public IService getLocalService(String service_name) {
        return local_service_by_name.get(service_name);
    }

    public IService getRemoteService(String service_name) {
        return remote_service_by_name.get(service_name);
    }
    
    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_OPEN) 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) {
            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 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;
        inp_tokens.remove(((Token)token).getID());
        addToOutQueue(msg);
    }

    public void sendEvent(IService service, String name, byte[] args) {
        assert Protocol.isDispatchThread();
        if (!(state == STATE_OPEN || state == STATE_OPENNING && 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);
    }

    @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) {
                    x.printStackTrace();
                }
            }
        }
        try {
            Token token = null;
            IChannel.IEventListener[] list = null;
            IChannel.ICommandServer cmds = null;
            switch (msg.type) {
            case 'C':
                token = msg.token;
                inp_tokens.put(token.getID(), msg);
                cmds = command_servers.get(msg.service);
                if (cmds != null) {
                    cmds.command(token, msg.name, msg.data);
                }
                else {
                    throw new IOException("Unknown command " + msg.service + "." + msg.name);
                }
                break;
            case 'P':
                token = out_tokens.get(msg.token.getID()).token;
                token.getListener().progress(token, msg.data);
                sendCongestionLevel();
                break;
            case 'R':
                token = out_tokens.remove(msg.token.getID()).token;
                token.getListener().result(token, msg.data);
                sendCongestionLevel();
                break;
            case 'E':
                if (msg.service.equals(ILocator.NAME) && msg.name.equals("Hello")) {
                    onLocatorHello((Collection<String>)JSON.parseSequence(msg.data)[0]);
                }
                else {
                    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':
                remote_congestion_level = Integer.parseInt(new String(msg.data, "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();
        int n = inp_tokens.size() * 100 / pending_command_limit - 100;
        if (n > level) level = n;
        MemoryUsage mem_usage = mem_mbean.getHeapMemoryUsage();
        long mem_free = mem_usage.getMax() - mem_usage.getUsed();
        n = (int)((mem_limit - mem_free) * 100 / mem_limit);
        if (n > 0) mem_mbean.gc();
        if (n > level) level = n;
        n = Protocol.getCongestionLevel();
        if (n > level) level = n;
        if (level > 100) level = 100;
        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]);
    }
}

Back to the top