summaryrefslogtreecommitdiff
path: root/SDL_Android/SmartDeviceLinkProxyAndroid/src/com/smartdevicelink/protocol/AbstractProtocol.java
blob: fde67d636a8168e8948d18e67e966c1bc5b7fa91 (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
package com.smartdevicelink.protocol;

import com.smartdevicelink.protocol.SmartDeviceLinkProtocol.MessageFrameAssembler;
import com.smartdevicelink.protocol.enums.SessionType;
import com.smartdevicelink.trace.SmartDeviceLinkTrace;
import com.smartdevicelink.trace.enums.InterfaceActivityDirection;

public abstract class AbstractProtocol {
	private static final String SMARTDEVICELINK_LIB_TRACE_KEY = "42baba60-eb57-11df-98cf-0800200c9a66";
	
	private IProtocolListener _protocolListener = null;
	//protected IProtocolListener ProtocolListener() { return _protocolListener; }
	
	// Lock to ensure all frames are sent uninterupted 
	private Object _frameLock = new Object();

	// Caller must provide a non-null IProtocolListener interface reference.
	public AbstractProtocol(IProtocolListener protocolListener) {
		if (protocolListener == null) {
    		throw new IllegalArgumentException("Provided protocol listener interface reference is null");
		} // end-if
		
		_protocolListener = protocolListener;
	} // end-ctor

	// This method receives raw bytes as they arrive from transport.  Those bytes
	// are then collected by the protocol and assembled into complete messages and
	// handled internally by the protocol or propagated to the protocol listener.
	public abstract void HandleReceivedBytes(byte[] receivedBytes, int length);

	// This method receives a protocol message (e.g. RPC, BULK, etc.) and processes
	// it for transmission over the transport.  The results of this processing will
	// be sent to the onProtocolMessageBytesToSend() method on protocol listener
	// interface.  Note that the ProtocolMessage itself contains information
	// about the type of message (e.g. RPC, BULK, etc.) and the protocol session
	// over which to send the message, etc.
	public abstract void SendMessage(ProtocolMessage msg);

	// This method starts a protocol session.  A corresponding call to the protocol
	// listener onProtocolSessionStarted() method will be made when the protocol
	// session has been established.
	public abstract void StartProtocolSession(SessionType sessionType);

	// This method ends a protocol session.  A corresponding call to the protocol
	// listener onProtocolSessionEnded() method will be made when the protocol
	// session has ended.
	public abstract void EndProtocolSession(SessionType sessionType, byte sessionID);
	
	// This method is called whenever the protocol receives a complete frame
	protected void handleProtocolFrameReceived(ProtocolFrameHeader header, byte[] data, MessageFrameAssembler assembler) {
		SmartDeviceLinkTrace.logProtocolEvent(InterfaceActivityDirection.Receive, header, data, 
				0, data.length, SMARTDEVICELINK_LIB_TRACE_KEY);
		
		assembler.handleFrame(header, data);
	}
	
	// This method is called whenever a protocol has an entire frame to send
	protected void handleProtocolFrameToSend(ProtocolFrameHeader header, byte[] data, int offset, int length) {
		SmartDeviceLinkTrace.logProtocolEvent(InterfaceActivityDirection.Transmit, header, data, 
				offset, length, SMARTDEVICELINK_LIB_TRACE_KEY);
		
		synchronized(_frameLock) {
			byte[] frameHeader = header.assembleHeaderBytes();
			handleProtocolMessageBytesToSend(frameHeader, 0, frameHeader.length);
			
			if (data != null) {
				handleProtocolMessageBytesToSend(data, offset, length);
			} // end-if
		}
	}
	
	// This method handles protocol message bytes that are ready to send.
	// A callback is sent to the protocol listener.
	protected void handleProtocolMessageBytesToSend(byte[] bytesToSend,
			int offset, int length) {
		_protocolListener.onProtocolMessageBytesToSend(bytesToSend, offset, length);
	}
	
	// This method handles received protocol messages. 
	protected void handleProtocolMessageReceived(ProtocolMessage message) {
		_protocolListener.onProtocolMessageReceived(message);
	}
	
	// This method handles the end of a protocol session. A callback is 
	// sent to the protocol listener.
	protected void handleProtocolSessionEnded(SessionType sessionType,
			byte sessionID, String correlationID) {
		_protocolListener.onProtocolSessionEnded(sessionType, sessionID, correlationID);
	}
	
	// This method handles the startup of a protocol session. A callback is sent
	// to the protocol listener.
	protected void handleProtocolSessionStarted(SessionType sessionType,
			byte sessionID, byte version, String correlationID) {
		_protocolListener.onProtocolSessionStarted(sessionType, sessionID, version, correlationID);
	}
	
	// This method handles protocol errors. A callback is sent to the protocol
	// listener.
	protected void handleProtocolError(String string, Exception ex) {
		_protocolListener.onProtocolError(string, ex);
	}
} // end-class