org.cyberlab.local.recognition.extractor.net
Class SocketComm

java.lang.Object
  extended by org.cyberlab.local.recognition.extractor.net.SocketComm

public class SocketComm
extends java.lang.Object

This class implements the socket communication protocols (used by SocketLocalFeaturesExtractor) between the Android device and the socket feature extractor server.
The overall communication protocol follows this pattern:
1. Client sends authentication message.
2a. Server replies with an authentication message.
2b. Server closes connection if authentication fails.
3a. Client sends intent.
3b. Client closes connection if server authentication message fails.
3c. Client closes connection if server closes connection.
4. Server responds to intent.
5. If server understands intent the client announces a file send operation.
6. Server replies with OK if it understands intent.
7. If file send OK from server; client sends file and waits for reply.
8. Server performs operations based on the intent(3a) from the client and announces that it wishes to send a file when it is finished.
9. Client responds with OK if it understands the intent.
10. Server sends file and closes its connection.
11. The client closes its connection.

See the specific methods for protocol details.

Author:
Mats-Gøran Karlsen
See Also:
SocketLocalFeaturesExtractor#getFeatures(android.graphics.Bitmap), authenticate(byte[], String, String), extractIntent(byte[], String, String), sendFile(File, byte[]), receiveFile(String, byte[])

Field Summary
static java.lang.String AUTH_HEADER
           
(package private)  java.io.BufferedInputStream bis
           
(package private)  java.io.BufferedOutputStream bos
           
static int CHUNK_SIZE
           
static java.lang.String CLOSE_HEADER
           
static java.lang.String DELIMITER_HEADER
           
static java.lang.String END_HEADER
           
(package private)  java.lang.String errorMsg
           
static java.lang.String EXTRACT_HEADER
           
static java.lang.String FILECOPY_HEADER
           
static java.lang.String OK_HEADER
           
(package private)  java.net.Socket sock
           
static java.lang.String TAG
           
 
Constructor Summary
SocketComm(java.lang.String ip, int port, int timeout)
           
 
Method Summary
 boolean authenticate(byte[] buffer, java.lang.String authMessage, java.lang.String authReply)
          Description:
Simple two way authenthication mechanism.
 void destroySocket()
          Destroys the socket when it no longer is neeeded.
 boolean extractIntent(byte[] buffer, java.lang.String featureType, java.lang.String descriptorType)
          Description:
Client publish its intent.
 java.lang.String[] parseMessage(byte[] buffer, int messageLength)
          Helper class parsing the message and dividing it into a String array, removing the message delimiter
 java.lang.String receiveFile(java.lang.String path, byte[] buffer)
           
 java.lang.String[] receiveMessage(byte[] buffer)
          Helper method for reading message from the socket
 boolean sendFile(java.io.File file, byte[] buffer)
          Description:
Loads a file into a buffer and sends piece by piece via a socket object until complete.
 boolean sendMessage(java.lang.String message)
          Helper method for sending a message to the socket
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TAG

public static final java.lang.String TAG
See Also:
Constant Field Values

CHUNK_SIZE

public static final int CHUNK_SIZE
See Also:
Constant Field Values

AUTH_HEADER

public static final java.lang.String AUTH_HEADER
See Also:
Constant Field Values

FILECOPY_HEADER

public static final java.lang.String FILECOPY_HEADER
See Also:
Constant Field Values

OK_HEADER

public static final java.lang.String OK_HEADER
See Also:
Constant Field Values

END_HEADER

public static final java.lang.String END_HEADER
See Also:
Constant Field Values

DELIMITER_HEADER

public static final java.lang.String DELIMITER_HEADER
See Also:
Constant Field Values

CLOSE_HEADER

public static final java.lang.String CLOSE_HEADER
See Also:
Constant Field Values

EXTRACT_HEADER

public static final java.lang.String EXTRACT_HEADER
See Also:
Constant Field Values

sock

java.net.Socket sock

bos

java.io.BufferedOutputStream bos

bis

java.io.BufferedInputStream bis

errorMsg

java.lang.String errorMsg
Constructor Detail

SocketComm

public SocketComm(java.lang.String ip,
                  int port,
                  int timeout)
           throws SocketCommException
Throws:
SocketCommException
Method Detail

authenticate

public boolean authenticate(byte[] buffer,
                            java.lang.String authMessage,
                            java.lang.String authReply)
Description:
Simple two way authenthication mechanism. We receive a authentication string from the client and reply by sending a string.

The client must accept the following protocol:
1. Send "AUTH:String\r\n" : We try to authenticate ourselves.
2.a Recv "AUTH:String\r\n" : We get a reply that the authentication was a success and authenticate ourself.
2.b Authentication failure. Send CLOSE\r\n : Authentication failure. Server announces this to client and closes.

Parameters:
buffer - The message buffer
authMessage - The authentication message
authReply - The expected authentication message
Returns:
true if authentication was successfull

destroySocket

public void destroySocket()
Destroys the socket when it no longer is neeeded.


sendMessage

public boolean sendMessage(java.lang.String message)
Helper method for sending a message to the socket

Parameters:
message - The message itself
Returns:
true if the message was sent successfully

receiveMessage

public java.lang.String[] receiveMessage(byte[] buffer)
Helper method for reading message from the socket

Parameters:
buffer - Message buffer
Returns:
An array containing the messages received.

parseMessage

public java.lang.String[] parseMessage(byte[] buffer,
                                       int messageLength)
Helper class parsing the message and dividing it into a String array, removing the message delimiter

Parameters:
buffer - message buffer
messageLength - The length of the message
Returns:
String array with the parsed message

extractIntent

public boolean extractIntent(byte[] buffer,
                             java.lang.String featureType,
                             java.lang.String descriptorType)
Description:
Client publish its intent. The server has only support for one intent: Extract.

The client must accept the following protocol:
1. Send "EXTRACT:String:String\r\n" : This tells the server that we are want to extract features.
2. Send "OK" : The server confirms the intent.

Parameters:
buffer - The message send buffer
featureType - The feature type we want to use detection
descriptorType - The descriptor type we want to use for extraction
Returns:
true if the server understands the intent.

sendFile

public boolean sendFile(java.io.File file,
                        byte[] buffer)
Description:
Loads a file into a buffer and sends piece by piece via a socket object until complete. This process consists of handshaking with the connected server via the socket.

The client must accept the following protocol:
1. Send "FC:String:long\r\n" : We announce that we want to send a file. The client sends the name of the file and the size of the file in bytes.
2. Recv "OK\r\n" : The server understand request.
3. Send bytes : Now we enter a loop sending bytes until file size is received.

Parameters:
file - The file that the client is sending
buffer - The send buffer
Returns:
true on successful file transmission

receiveFile

public java.lang.String receiveFile(java.lang.String path,
                                    byte[] buffer)
Parameters:
path -
buffer -
Returns: