SDL_Android/SmartDeviceLinkAndroidProxy - added the correct version of the proxy
[profile/ivi/smartdevicelink.git] / SDL_Android / SmartDeviceLinkProxyAndroid / src / com / smartdevicelink / protocol / AbstractProtocol.java
1 package com.smartdevicelink.protocol;\r
2 \r
3 import com.smartdevicelink.protocol.SmartDeviceLinkProtocol.MessageFrameAssembler;\r
4 import com.smartdevicelink.protocol.enums.SessionType;\r
5 import com.smartdevicelink.trace.SmartDeviceLinkTrace;\r
6 import com.smartdevicelink.trace.enums.InterfaceActivityDirection;\r
7 \r
8 public abstract class AbstractProtocol {\r
9         private static final String SMARTDEVICELINK_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66";\r
10         \r
11         private IProtocolListener _protocolListener = null;\r
12         //protected IProtocolListener ProtocolListener() { return _protocolListener; }\r
13         \r
14         // Lock to ensure all frames are sent uninterupted \r
15         private Object _frameLock = new Object();\r
16 \r
17         // Caller must provide a non-null IProtocolListener interface reference.\r
18         public AbstractProtocol(IProtocolListener protocolListener) {\r
19                 if (protocolListener == null) {\r
20                 throw new IllegalArgumentException("Provided protocol listener interface reference is null");\r
21                 } // end-if\r
22                 \r
23                 _protocolListener = protocolListener;\r
24         } // end-ctor\r
25 \r
26         // This method receives raw bytes as they arrive from transport.  Those bytes\r
27         // are then collected by the protocol and assembled into complete messages and\r
28         // handled internally by the protocol or propagated to the protocol listener.\r
29         public abstract void HandleReceivedBytes(byte[] receivedBytes, int length);\r
30 \r
31         // This method receives a protocol message (e.g. RPC, BULK, etc.) and processes\r
32         // it for transmission over the transport.  The results of this processing will\r
33         // be sent to the onProtocolMessageBytesToSend() method on protocol listener\r
34         // interface.  Note that the ProtocolMessage itself contains information\r
35         // about the type of message (e.g. RPC, BULK, etc.) and the protocol session\r
36         // over which to send the message, etc.\r
37         public abstract void SendMessage(ProtocolMessage msg);\r
38 \r
39         // This method starts a protocol session.  A corresponding call to the protocol\r
40         // listener onProtocolSessionStarted() method will be made when the protocol\r
41         // session has been established.\r
42         public abstract void StartProtocolSession(SessionType sessionType);\r
43 \r
44         // This method ends a protocol session.  A corresponding call to the protocol\r
45         // listener onProtocolSessionEnded() method will be made when the protocol\r
46         // session has ended.\r
47         public abstract void EndProtocolSession(SessionType sessionType, byte sessionID);\r
48         \r
49         // This method is called whenever the protocol receives a complete frame\r
50         protected void handleProtocolFrameReceived(ProtocolFrameHeader header, byte[] data, MessageFrameAssembler assembler) {\r
51                 SmartDeviceLinkTrace.logProtocolEvent(InterfaceActivityDirection.Receive, header, data, \r
52                                 0, data.length, SMARTDEVICELINK_LIB_TRACE_KEY);\r
53                 \r
54                 assembler.handleFrame(header, data);\r
55         }\r
56         \r
57         // This method is called whenever a protocol has an entire frame to send\r
58         protected void handleProtocolFrameToSend(ProtocolFrameHeader header, byte[] data, int offset, int length) {\r
59                 SmartDeviceLinkTrace.logProtocolEvent(InterfaceActivityDirection.Transmit, header, data, \r
60                                 offset, length, SMARTDEVICELINK_LIB_TRACE_KEY);\r
61                 \r
62                 synchronized(_frameLock) {\r
63                         byte[] frameHeader = header.assembleHeaderBytes();\r
64                         handleProtocolMessageBytesToSend(frameHeader, 0, frameHeader.length);\r
65                         \r
66                         if (data != null) {\r
67                                 handleProtocolMessageBytesToSend(data, offset, length);\r
68                         } // end-if\r
69                 }\r
70         }\r
71         \r
72         // This method handles protocol message bytes that are ready to send.\r
73         // A callback is sent to the protocol listener.\r
74         protected void handleProtocolMessageBytesToSend(byte[] bytesToSend,\r
75                         int offset, int length) {\r
76                 _protocolListener.onProtocolMessageBytesToSend(bytesToSend, offset, length);\r
77         }\r
78         \r
79         // This method handles received protocol messages. \r
80         protected void handleProtocolMessageReceived(ProtocolMessage message) {\r
81                 _protocolListener.onProtocolMessageReceived(message);\r
82         }\r
83         \r
84         // This method handles the end of a protocol session. A callback is \r
85         // sent to the protocol listener.\r
86         protected void handleProtocolSessionEnded(SessionType sessionType,\r
87                         byte sessionID, String correlationID) {\r
88                 _protocolListener.onProtocolSessionEnded(sessionType, sessionID, correlationID);\r
89         }\r
90         \r
91         // This method handles the startup of a protocol session. A callback is sent\r
92         // to the protocol listener.\r
93         protected void handleProtocolSessionStarted(SessionType sessionType,\r
94                         byte sessionID, byte version, String correlationID) {\r
95                 _protocolListener.onProtocolSessionStarted(sessionType, sessionID, version, correlationID);\r
96         }\r
97         \r
98         // This method handles protocol errors. A callback is sent to the protocol\r
99         // listener.\r
100         protected void handleProtocolError(String string, Exception ex) {\r
101                 _protocolListener.onProtocolError(string, ex);\r
102         }\r
103 } // end-class\r