summaryrefslogtreecommitdiff
path: root/javaSE/javaSE/src/main/java/com/smartdevicelink/transport/TransportManager.java
blob: c8a07a1ccaffff18b70d7ebece952d22324a5e10 (plain)
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
/*
 * Copyright (c) 2018 Livio, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Livio Inc. nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.smartdevicelink.transport;


import com.smartdevicelink.protocol.SdlPacket;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.transport.utl.TransportRecord;
import com.smartdevicelink.util.DebugTool;

import java.util.Collections;
import java.util.List;

@SuppressWarnings("unused")
public class TransportManager extends TransportManagerBase{
    private static final String TAG = "TransportManager";

    TransportInterface transport;

    /**
     * Managing transports
     * List for status of all transports
     * If transport is not connected. Request Router service connect to it. Get connected message
     */

    public TransportManager(BaseTransportConfig config, TransportEventListener listener){
        super(config, listener);

        //Start the new transport
        switch (config.getTransportType()){
            case WEB_SOCKET_SERVER:
                transport = new WebSocketServer((WebSocketServerConfig)config, new SingleTransportCallbackImpl(new TransportRecord(TransportType.WEB_SOCKET_SERVER,"127.0.0.1:"+((WebSocketServerConfig)config).port)));
                break;
            case CUSTOM:
                transport = ((CustomTransportConfig) config).getTransportInterface();
                transport.setCallback(new SingleTransportCallbackImpl(transport.getTransportRecord()));
                break;
        }

    }

    @Override
    public void start(){
        if(transport != null){
            transport.start();
        }else{
            System.out.print("Unable to start transport.");
        }
    }

    @Override
    public void close(long sessionId){
        if(transport != null) {
            transport.stop();
        }
    }

    @Deprecated
    @Override
    public void resetSession(){

    }

    /**
     * Check to see if a transport is connected.
     * @param transportType the transport to have its connection status returned. If `null` is
     *                      passed in, all transports will be checked and if any are connected a
     *                      true value will be returned.
     * @param address the address associated with the transport type. If null, the first transport
     *                of supplied type will be used to return if connected.
     * @return if a transport is connected based on included variables
     */
    @Override
    public boolean isConnected(TransportType transportType, String address){
        synchronized (TRANSPORT_STATUS_LOCK) {
            if (transportType == null) {
                return !transportStatus.isEmpty();
            }
            for (TransportRecord record : transportStatus) {
                if (record.getType().equals(transportType)) {
                    if (address != null) {
                        if (address.equals(record.getAddress())) {
                            return true;
                        } // Address doesn't match, move forward
                    } else {
                        //If no address is included, assume any transport of correct type is acceptable
                        return true;
                    }
                }
            }
            return false;
        }
    }
    /**
     * Retrieve a transport record with the supplied params
     * @param transportType the transport to have its connection status returned.
     * @param address the address associated with the transport type. If null, the first transport
     *                of supplied type will be returned.
     * @return the transport record for the transport type and address if supplied
     */
    @Override
    public TransportRecord getTransportRecord(TransportType transportType, String address){
        synchronized (TRANSPORT_STATUS_LOCK) {
            if (transportType == null) {
                return null;
            }
            for (TransportRecord record : transportStatus) {
                if (record.getType().equals(transportType)) {
                    if (address != null) {
                        if (address.equals(record.getAddress())) {
                            return record;
                        } // Address doesn't match, move forward
                    } else {
                        //If no address is included, assume any transport of correct type is acceptable
                        return record;
                    }
                }
            }
            return null;
        }
    }


    @Override
    public void sendPacket(SdlPacket packet){
        if(transport !=null){
            transport.write(packet);
        }else {

        }
    }

    class  SingleTransportCallbackImpl implements TransportCallback {

        final List<TransportRecord> finalList;
        final TransportRecord record;
        protected SingleTransportCallbackImpl(TransportRecord transportRecord){
             record = transportRecord;
             finalList = Collections.singletonList(record);
        }

        @Override
        public void onConnectionEstablished() {
            synchronized (TRANSPORT_STATUS_LOCK){
                transportStatus.clear();
                transportStatus.addAll(finalList);
            }
            transportListener.onTransportConnected(finalList);
        }

        @Override
        public void onError() {
            DebugTool.logError(TAG, "Error in the transport manager from the web socket server");
            if(transportListener != null){
                transportListener.onError("");
            }
        }

        @Override
        public void onConnectionTerminated(String reason) {
            if(record != null){
                DebugTool.logInfo(TAG, "Transport disconnected - " + record);
            }else{
                DebugTool.logInfo(TAG, "Transport disconnected");

            }

            synchronized (TRANSPORT_STATUS_LOCK){
                TransportManager.this.transportStatus.remove(record);
                //Might check connectedTransports vs transportStatus to ensure they are equal
            }
            //Inform the transport listener that a transport has disconnected
            transportListener.onTransportDisconnected(reason, record, Collections.EMPTY_LIST);
        }

        @Override
        public void onPacketReceived(SdlPacket packet) {
            if(packet!=null){
                transportListener.onPacketReceived(packet);
            }
        }
    }

}