1 package com.smartdevicelink.transport;
\r
3 import com.smartdevicelink.exception.SmartDeviceLinkException;
\r
4 import com.smartdevicelink.trace.SmartDeviceLinkTrace;
\r
5 import com.smartdevicelink.trace.enums.InterfaceActivityDirection;
\r
6 import com.smartdevicelink.util.DebugTool;
\r
8 public abstract class SmartDeviceLinkTransport {
\r
9 private static final String SMARTDEVICELINK_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66";
\r
11 private final static String FailurePropagating_Msg = "Failure propagating ";
\r
12 private Boolean isConnected = false;
\r
14 private String _sendLockObj = "lock";
\r
16 // Get status of transport connection
\r
17 public Boolean getIsConnected() {
\r
21 //protected SmartDeviceLinkTransport(String endpointName, String param2, ITransportListener transportListener)
\r
22 protected SmartDeviceLinkTransport(ITransportListener transportListener) {
\r
23 if (transportListener == null) {
\r
24 throw new IllegalArgumentException("Provided transport listener interface reference is null");
\r
26 _transportListener = transportListener;
\r
29 // This method is called by the subclass to indicate that data has arrived from
\r
31 protected void handleReceivedBytes(byte[] receivedBytes, int receivedBytesLength) {
\r
33 // Trace received data
\r
34 if (receivedBytesLength > 0) {
\r
35 // Send transport data to the siphon server
\r
36 SiphonServer.sendBytesFromSMARTDEVICELINK(receivedBytes, 0, receivedBytesLength);
\r
37 SmartDeviceLinkTrace.logTransportEvent("", null, InterfaceActivityDirection.Receive, receivedBytes, receivedBytesLength, SMARTDEVICELINK_LIB_TRACE_KEY);
\r
39 _transportListener.onTransportBytesReceived(receivedBytes, receivedBytesLength);
\r
41 } catch (Exception excp) {
\r
42 DebugTool.logError(FailurePropagating_Msg + "handleBytesFromTransport: " + excp.toString(), excp);
\r
43 handleTransportError(FailurePropagating_Msg, excp);
\r
47 // This method must be implemented by transport subclass, and is called by this
\r
48 // base class to actually send an array of bytes out over the transport. This
\r
49 // method is meant to only be callable within the class hierarchy.
\r
50 protected abstract boolean sendBytesOverTransport(byte[] msgBytes, int offset, int length);
\r
52 // This method is called by whomever has reference to transport to have bytes
\r
53 // sent out over transport.
\r
54 public boolean sendBytes(byte[] message) {
\r
55 return sendBytes(message, 0, message.length);
\r
58 // This method is called by whomever has reference to transport to have bytes
\r
59 // sent out over transport.
\r
60 public boolean sendBytes(byte[] message, int offset, int length) {
\r
61 boolean bytesWereSent = false;
\r
62 synchronized (_sendLockObj) {
\r
63 bytesWereSent = sendBytesOverTransport(message, offset, length);
\r
65 // Send transport data to the siphon server
\r
66 SiphonServer.sendBytesFromAPP(message, offset, length);
\r
68 SmartDeviceLinkTrace.logTransportEvent("", null, InterfaceActivityDirection.Transmit, message, offset, length, SMARTDEVICELINK_LIB_TRACE_KEY);
\r
69 return bytesWereSent;
\r
72 private ITransportListener _transportListener = null;
\r
74 // This method is called by the subclass to indicate that transport connection
\r
75 // has been established.
\r
76 protected void handleTransportConnected() {
\r
79 SmartDeviceLinkTrace.logTransportEvent("Transport.connected", null, InterfaceActivityDirection.Receive, null, 0, SMARTDEVICELINK_LIB_TRACE_KEY);
\r
80 _transportListener.onTransportConnected();
\r
81 } catch (Exception excp) {
\r
82 DebugTool.logError(FailurePropagating_Msg + "onTransportConnected: " + excp.toString(), excp);
\r
83 handleTransportError(FailurePropagating_Msg + "onTransportConnected", excp);
\r
87 // This method is called by the subclass to indicate that transport disconnection
\r
89 protected void handleTransportDisconnected(final String info) {
\r
90 isConnected = false;
\r
93 SmartDeviceLinkTrace.logTransportEvent("Transport.disconnect: " + info, null, InterfaceActivityDirection.Transmit, null, 0, SMARTDEVICELINK_LIB_TRACE_KEY);
\r
94 _transportListener.onTransportDisconnected(info);
\r
95 } catch (Exception excp) {
\r
96 DebugTool.logError(FailurePropagating_Msg + "onTransportDisconnected: " + excp.toString(), excp);
\r
100 // This method is called by the subclass to indicate a transport error has occurred.
\r
101 protected void handleTransportError(final String message, final Exception ex) {
\r
102 isConnected = false;
\r
103 _transportListener.onTransportError(message, ex);
\r
106 public abstract void openConnection() throws SmartDeviceLinkException;
\r
107 public abstract void disconnect();
\r
110 * Abstract method which should be implemented by subclasses in order to return actual type of the transport.
\r
112 * @return One of {@link TransportType} enumeration values.
\r
114 * @see TransportType
\r
116 public abstract TransportType getTransportType();
\r