1 package com.smartdevicelink.protocol;
\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
8 public abstract class AbstractProtocol {
\r
9 private static final String SMARTDEVICELINK_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66";
\r
11 private IProtocolListener _protocolListener = null;
\r
12 //protected IProtocolListener ProtocolListener() { return _protocolListener; }
\r
14 // Lock to ensure all frames are sent uninterupted
\r
15 private Object _frameLock = new Object();
\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
23 _protocolListener = protocolListener;
\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
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
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
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
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
54 assembler.handleFrame(header, data);
\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
62 synchronized(_frameLock) {
\r
63 byte[] frameHeader = header.assembleHeaderBytes();
\r
64 handleProtocolMessageBytesToSend(frameHeader, 0, frameHeader.length);
\r
67 handleProtocolMessageBytesToSend(data, offset, length);
\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
79 // This method handles received protocol messages.
\r
80 protected void handleProtocolMessageReceived(ProtocolMessage message) {
\r
81 _protocolListener.onProtocolMessageReceived(message);
\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
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
98 // This method handles protocol errors. A callback is sent to the protocol
\r
100 protected void handleProtocolError(String string, Exception ex) {
\r
101 _protocolListener.onProtocolError(string, ex);
\r