Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 14f4a92fa14a346693ded216b0c5a9b9303c2f9e (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
/*******************************************************************************
 * Copyright (c) 2008, 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.internal.tcf.services.local;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.tm.internal.tcf.core.ChannelLoop;
import org.eclipse.tm.tcf.core.AbstractChannel;
import org.eclipse.tm.tcf.protocol.IChannel;
import org.eclipse.tm.tcf.protocol.IService;
import org.eclipse.tm.tcf.protocol.IToken;

/**
 * ChannelProxy implements forwarding of TCF messages between two channels.
 * The class is used to implement Locator service "redirect" command.
 */
class ChannelProxy {

    private final AbstractChannel ch_x;
    private final AbstractChannel ch_y;

    private boolean closed_x;
    private boolean closed_y;

    private final Map<IToken,IToken> tokens_x = new HashMap<IToken,IToken>();
    private final Map<IToken,IToken> tokens_y = new HashMap<IToken,IToken>();

    private final AbstractChannel.Proxy proxy_x = new AbstractChannel.Proxy() {

        public void onChannelClosed(Throwable error) {
            closed_x = true;
            if (closed_y) return;
            if (error == null) ch_y.close();
            else ch_y.terminate(error);
        }

        public void onCommand(IToken token, String service, String name, byte[] data) {
            if (closed_y) return;
            assert ch_y.getState() == IChannel.STATE_OPEN;
            IService s = ch_y.getRemoteService(service);
            if (s == null) ch_x.terminate(new IOException("Invalid service name"));
            else tokens_x.put(ch_y.sendCommand(s, name, data, cmd_listener_x), token);
        }

        public void onEvent(String service, String name, byte[] data) {
            IService s = ch_x.getRemoteService(service);
            if (s == null) ch_x.terminate(new IOException("Invalid service name"));
            else if (!closed_y) ch_y.sendEvent(s, name, data);
        }
    };

    private final AbstractChannel.Proxy proxy_y = new AbstractChannel.Proxy() {

        public void onChannelClosed(Throwable error) {
            closed_y = true;
            if (closed_x) return;
            if (error == null) ch_x.close();
            else ch_x.terminate(error);
        }

        public void onCommand(IToken token, String service, String name, byte[] data) {
            if (closed_x) return;
            assert ch_x.getState() == IChannel.STATE_OPEN;
            IService s = ch_x.getRemoteService(service);
            if (s == null) ch_y.terminate(new IOException("Invalid service name"));
            else tokens_y.put(ch_x.sendCommand(s, name, data, cmd_listener_y), token);
        }

        public void onEvent(String service, String name, byte[] data) {
            IService s = ch_y.getRemoteService(service);
            if (s == null) ch_y.terminate(new IOException("Invalid service name"));
            else if (!closed_x) ch_x.sendEvent(s, name, data);
        }
    };

    private final IChannel.ICommandListener cmd_listener_x = new IChannel.ICommandListener() {

        public void progress(IToken token, byte[] data) {
            ch_x.sendProgress(tokens_x.get(token), data);
        }

        public void result(IToken token, byte[] data) {
            ch_x.sendResult(tokens_x.remove(token), data);
        }

        public void terminated(IToken token, Exception error) {
            ch_x.rejectCommand(tokens_x.remove(token));
        }
    };

    private final IChannel.ICommandListener cmd_listener_y = new IChannel.ICommandListener() {

        public void progress(IToken token, byte[] data) {
            ch_y.sendProgress(tokens_y.get(token), data);
        }

        public void result(IToken token, byte[] data) {
            ch_y.sendResult(tokens_y.remove(token), data);
        }

        public void terminated(IToken token, Exception error) {
            ch_y.rejectCommand(tokens_y.remove(token));
        }
    };

    ChannelProxy(IChannel x, IChannel y) {
        assert !(x instanceof ChannelLoop);
        assert !(y instanceof ChannelLoop);
        ch_x = (AbstractChannel)x;
        ch_y = (AbstractChannel)y;
        assert ch_x.getState() == IChannel.STATE_OPEN;
        assert ch_y.getState() == IChannel.STATE_OPENNING;
        try {
            ch_y.setProxy(proxy_y, ch_x.getRemoteServices());
            ch_y.addChannelListener(new IChannel.IChannelListener() {

                public void congestionLevel(int level) {
                }

                public void onChannelClosed(Throwable error) {
                    ch_y.removeChannelListener(this);
                    if (error == null) error = new Exception("Channel closed");
                }

                public void onChannelOpened() {
                    ch_y.removeChannelListener(this);
                    try {
                        ch_x.setProxy(proxy_x, ch_y.getRemoteServices());
                    }
                    catch (IOException e) {
                        ch_x.terminate(e);
                        ch_y.terminate(e);
                    }
                }
            });
        }
        catch (IOException e) {
            ch_x.terminate(e);
            ch_y.terminate(e);
        }
    }
}

Back to the top