summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2020-01-07 16:00:46 +0100
committerSimon Rettberg2020-01-07 16:00:46 +0100
commitef84dd1e70d2ea8f6f02ed2e5f025c4181eaf1ab (patch)
tree10b6d83dd085715bb685622b53a080c5d6ca1f93
parentAdd TCP interface (diff)
downloadtaskman-lite-ef84dd1e70d2ea8f6f02ed2e5f025c4181eaf1ab.tar.gz
taskman-lite-ef84dd1e70d2ea8f6f02ed2e5f025c4181eaf1ab.tar.xz
taskman-lite-ef84dd1e70d2ea8f6f02ed2e5f025c4181eaf1ab.zip
Use different compression thresholds for UDP and TCPv3.9
-rw-r--r--daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerTcp.java2
-rw-r--r--daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerUdp.java2
-rw-r--r--daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java24
3 files changed, 13 insertions, 15 deletions
diff --git a/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerTcp.java b/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerTcp.java
index e37b3d7..540ff42 100644
--- a/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerTcp.java
+++ b/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerTcp.java
@@ -122,7 +122,7 @@ public class NetworkHandlerTcp extends NetworkHandlerBase
return;
byte[] reply;
try {
- reply = parser.handle( payload );
+ reply = parser.handle( payload, 8000 );
} catch ( Throwable t ) {
log.error( "Exception in RequestParser", t );
log.error( "Payload was: " + payload );
diff --git a/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerUdp.java b/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerUdp.java
index 4c12960..7a25ed1 100644
--- a/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerUdp.java
+++ b/daemon/src/main/java/org/openslx/taskmanager/network/NetworkHandlerUdp.java
@@ -99,7 +99,7 @@ public class NetworkHandlerUdp extends NetworkHandlerBase
String payload = new String( readBuffer, 0, packet.getLength(), StandardCharsets.UTF_8 );
byte[] reply;
try {
- reply = parser.handle( payload );
+ reply = parser.handle( payload, 1400 );
} catch ( Throwable t ) {
log.error( "Exception in RequestParser: " + t.toString() );
log.error( "Payload was: " + payload );
diff --git a/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java b/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java
index e1f883e..55a44a5 100644
--- a/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java
+++ b/daemon/src/main/java/org/openslx/taskmanager/network/RequestParser.java
@@ -37,8 +37,9 @@ public class RequestParser
* Handle the given unparsed request.
*
* @param payload Packet data received from network, already converted to a string
+ * @param compressThreshold If reply is larger than this (bytes), compress the reply if client supports it
*/
- public byte[] handle( String payload )
+ public byte[] handle( String payload, int compressThreshold )
{
String[] parts = payload.split( ",", 3 );
// Message format is "<message id>, <command>, <command payload/argument>"
@@ -48,7 +49,9 @@ public class RequestParser
}
// Hacked in compression support: If parts[1] is prefixed by at
// least 5 spaces, the sender supports deflated reply payload.
- boolean compress = parts[1].startsWith( " " );
+ if ( !parts[1].startsWith( " " ) ) {
+ compressThreshold = Integer.MAX_VALUE;
+ }
parts[0] = parts[0].trim();
parts[1] = parts[1].trim();
parts[2] = parts[2].trim();
@@ -56,7 +59,7 @@ public class RequestParser
// with the ID given in parts[2]
if ( parts[1].equals( "status" ) ) {
TaskStatus status = taskManager.getTaskStatus( parts[2] );
- return serialize( parts[0], status, compress );
+ return serialize( parts[0], status, compressThreshold );
}
// Now check if parts[1] is "release"
if ( parts[1].equals( "release" ) ) {
@@ -71,25 +74,22 @@ public class RequestParser
// Anything else in parts[0] will be treated as a fresh task invocation, so let's
// pass it on to the task manager.
TaskStatus status = taskManager.submitTask( parts[1], parts[2] );
- return serialize( parts[0], status, compress );
+ return serialize( parts[0], status, compressThreshold );
}
private static final byte[] COMMA = { ',' };
private static final byte[] COMPRESS_PREFIX = { '+', 'z', ':' };
- private byte[] serialize( String messageId, TaskStatus status, boolean compress )
+ private byte[] serialize( String messageId, TaskStatus status, int compressThreshold )
{
byte[] messageIdBytes = messageId.getBytes( StandardCharsets.UTF_8 );
byte[] dataBytes;
+ boolean compress = false;
try {
synchronized ( sendGson ) {
dataBytes = sendGson.toJson( status ).getBytes( StandardCharsets.UTF_8 );
}
- if ( compress && dataBytes.length < 1400 ) {
- // Not worth the effort for small payloads
- compress = false;
- }
- if ( compress ) {
+ if ( dataBytes.length > compressThreshold ) {
byte[] compressed = new byte[ dataBytes.length + 16 ];
int compressedLen;
synchronized ( deflater ) {
@@ -102,9 +102,7 @@ public class RequestParser
log.debug( "Sending compressed reply (" + dataBytes.length + " -> " + compressedLen + ")" );
dataBytes = new byte[ compressedLen ];
System.arraycopy( compressed, 0, dataBytes, 0, compressedLen );
- } else {
- // Failed or actually larger, disable compression
- compress = false;
+ compress = true;
}
}
} catch ( Throwable e ) {