Skip to content

Commit

Permalink
Added message headers and the handling thereof
Browse files Browse the repository at this point in the history
  • Loading branch information
zmjjmz committed Mar 27, 2013
1 parent c75df51 commit dc6df2d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
23 changes: 23 additions & 0 deletions BlueMesh/src/blue/mesh/AndroidBluetoothConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ public int read(byte[] b) throws IOException {
}
return bytes;
}
@Override
public int read(byte[] b, int offset, int len) throws IOException {
int bytes;
try {
bytes = input.read(incommingBuffer, offset, len);
} catch (IOException e) {
Log.e(TAG, "Error reading", e);
throw new IOException();
}
if (bytes != len) {
IOException e = new IOException();
Log.e(TAG, "Did not read enough bytes", e);
throw e;
}

for (int i = 0; i < bytes; i++) {
b[i] = incommingBuffer[i];
}
return bytes;

}



public String getID(){
return type + '@' + socket.getRemoteDevice().toString();
Expand Down
1 change: 1 addition & 0 deletions BlueMesh/src/blue/mesh/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public abstract class Connection {
public abstract void close();
public abstract void write( byte[] b );
public abstract int read( byte[] b ) throws IOException;
public abstract int read( byte[] b, int offset, int len) throws IOException;
public abstract String getID();
}
67 changes: 56 additions & 11 deletions BlueMesh/src/blue/mesh/ReadWriteThread.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package blue.mesh;

import java.io.IOException;
import java.nio.ByteBuffer;

import android.util.Log;

Expand All @@ -16,16 +17,61 @@ protected ReadWriteThread(RouterObject a_router, Connection a_connection){
router = a_router;
connection = a_connection;
}


private byte[] readData() throws IOException {
// Initially allocate 4 byte buffer to read the size of the message
int header_size = Constants.HEADER_SIZE;
byte[] header = new byte[header_size];

try {
connection.read(header, 0, header_size);
} catch (IOException e) {
Log.e(TAG, "Header read failed", e);
throw e;
}
ByteBuffer bb = ByteBuffer.wrap(header);
int msg_size = bb.getInt();
// Hopefully we now have the size

byte[] message = new byte[msg_size];
try {
connection.read(message, 0, msg_size);
} catch (IOException e) {
Log.e(TAG, "Message read failed", e);
throw e;
}
return message;
}

private int writeData( byte[] buffer ) {
// Format message as follows:
// Header: [4 bytes: num chunks|2 bytes size of last chunk|2 bytes message id]
// Message: [2 bytes message id|1024 bytes data]
// End: [2 bytes message id|16 byte checksum]
// No wait
// Message: [4 bytes message size|message]
int header_size = Constants.HEADER_SIZE;
int size = buffer.length;
byte[] header = ByteBuffer.allocate(header_size).putInt(size).array();
byte[] message = new byte[header_size + size];
System.arraycopy(header, 0, message, 0, header_size);
System.arraycopy(buffer, 0, message, header_size, size);

connection.write(message);

return Constants.SUCCESS;
}

public void run() {
byte[] buffer = new byte[Constants.MAX_MESSAGE_LEN];
byte[] buffer = null;


// Keep listening to the InputStream while connected
while (!this.isInterrupted()) {

int bytes = 0;
try{
bytes = connection.read( buffer );
buffer = readData();
}
catch( IOException e ){
if(Constants.DEBUG) Log.e(TAG, "read failed", e);
Expand All @@ -34,17 +80,16 @@ public void run() {
}


if( bytes > 0 ){
if(Constants.DEBUG) Log.d(TAG, "Got something");
}
if( bytes > 0 && buffer != null ){

if( buffer != null ){
if(Constants.DEBUG) Log.d(TAG, buffer.toString());
byte[] returnBuffer = new byte[bytes];
for( int i = 0; i < bytes; i++ ){
byte[] returnBuffer = new byte[buffer.length];
for( int i = 0; i < buffer.length; i++ ){
returnBuffer[i] = buffer[i];
}
router.route(returnBuffer, Constants.SRC_OTHER);
}

}

return;
Expand All @@ -61,8 +106,8 @@ protected int disconnect(){

protected int write(byte[] buffer) {
if(Constants.DEBUG) Log.d(TAG, "Writing bytes: " + buffer.toString() );
connection.write( buffer );
return Constants.SUCCESS;
int stat = writeData( buffer );
return stat;
}

}

0 comments on commit dc6df2d

Please sign in to comment.