Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:CSEMike/OneSwarm

  • Loading branch information...
commit 7fdf5e3e3ad98b19c297bf115a7504b1130d5931 2 parents 2d49959 + 2140298
@willscott willscott authored
View
34 oneswarm_f2f/src/edu/washington/cs/oneswarm/f2f/messaging/OSF2FTextSearch.java
@@ -18,25 +18,36 @@
private String description;
private DirectByteBuffer buffer;
- private final static int BASE_MESSAGE_LENGTH = 5;
+ private final static int BASE_MESSAGE_LENGTH = 5; // 1 byte type, 4 byte id
private final static int MAX_MESSAGE_LENGTH = 109;
- private int messageLength;
+ private static final int MAX_SEARCH_STRING_LENGTH = MAX_MESSAGE_LENGTH - BASE_MESSAGE_LENGTH;
+
+ private final int messageLength;
public OSF2FTextSearch(byte version, byte type, int searchID, String searchString) {
super(version, searchID);
this.searchString = searchString;
+ byte[] bytes;
try {
- this.searchStringBytes = searchString.getBytes("UTF-8");
+ bytes = searchString.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
- this.searchStringBytes = searchString.getBytes();
+ bytes = searchString.getBytes();
+ }
+ if (bytes.length <= MAX_SEARCH_STRING_LENGTH) {
+ this.searchStringBytes = bytes;
+ } else {
+ // Search string too large, crop it.
+ this.searchStringBytes = new byte[MAX_SEARCH_STRING_LENGTH];
+ System.arraycopy(bytes, 0, this.searchStringBytes, 0, MAX_SEARCH_STRING_LENGTH);
+ Debug.out("Search '" + searchString + "' too long, cropping.");
}
this.type = type;
- this.messageLength = Math.min(MAX_MESSAGE_LENGTH, BASE_MESSAGE_LENGTH
- + searchStringBytes.length);
+ this.messageLength = BASE_MESSAGE_LENGTH + searchStringBytes.length;
}
+ @Override
public OSF2FTextSearch clone() {
return new OSF2FTextSearch(this.getVersion(), type, this.getSearchID(),
this.getSearchString());
@@ -46,26 +57,32 @@ public String getSearchString() {
return searchString;
}
+ @Override
public String getID() {
return OSF2FMessage.ID_OS_TEXT_SEARCH;
}
+ @Override
public byte[] getIDBytes() {
return OSF2FMessage.ID_OS_TEXT_SEARCH_BYTES;
}
+ @Override
public String getFeatureID() {
return OSF2FMessage.OS_FEATURE_ID;
}
+ @Override
public int getFeatureSubID() {
return OSF2FMessage.SUBID_OS_TEXT_SEARCH;
}
+ @Override
public int getType() {
return Message.TYPE_PROTOCOL_PAYLOAD;
}
+ @Override
public String getDescription() {
if (description == null) {
description = OSF2FMessage.ID_OS_TEXT_SEARCH + "\tsearchID="
@@ -75,6 +92,7 @@ public String getDescription() {
return description;
}
+ @Override
public DirectByteBuffer[] getData() {
if (buffer == null) {
buffer = DirectByteBufferPool.getBuffer(DirectByteBuffer.AL_MSG, messageLength);
@@ -87,6 +105,7 @@ public String getDescription() {
return new DirectByteBuffer[] { buffer };
}
+ @Override
public Message deserialize(DirectByteBuffer data, byte version) throws MessageException {
if (data == null) {
throw new MessageException("[" + getID() + "] decode error: data == null");
@@ -111,6 +130,7 @@ public Message deserialize(DirectByteBuffer data, byte version) throws MessageEx
}
}
+ @Override
public void destroy() {
if (buffer != null)
buffer.returnToPool();
@@ -120,10 +140,12 @@ public byte getRequestType() {
return type;
}
+ @Override
public int getMessageSize() {
return messageLength;
}
+ @Override
public int getValueID() {
return searchString.hashCode();
}
Please sign in to comment.
Something went wrong with that request. Please try again.