Commit 9912dc88 authored by Vít Kabele's avatar Vít Kabele

ServerMessage and ClientMessage wrappers added.

parent abbc288d
......@@ -246,7 +246,7 @@ __instrument_class (
// Check if error occurred on the server.
// The control field of the response contains the error message.
//
if (response->result == INSTRUMENT_CLASS_RESULT__ERROR) {
if (response->result == INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__ERROR) {
fprintf (
stderr, "%sinstrumentation server error:\n%s\n",
ERROR_PREFIX, response->errormessage
......@@ -260,7 +260,7 @@ __instrument_class (
// modified if non-empty class code has been returned. Otherwise,
// signal that the class has not been modified.
//
if (response->result == INSTRUMENT_CLASS_RESULT__CLASS_MODIFIED) {
if (response->result == INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__CLASS_MODIFIED) {
class_def->class_byte_count = response->classbytes.len;
class_def->class_bytes = response->classbytes.data;
......
This diff is collapsed.
......@@ -10,7 +10,7 @@ PROTOBUF_C__BEGIN_DECLS
#if PROTOBUF_C_VERSION_NUMBER < 1003000
# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers.
#elif 1003000 < PROTOBUF_C_MIN_COMPILER_VERSION
#elif 1003001 < PROTOBUF_C_MIN_COMPILER_VERSION
# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c.
#endif
......@@ -22,16 +22,18 @@ typedef struct _InstrumentationDelivery InstrumentationDelivery;
typedef struct _InstrumentationAcceptConfirmation InstrumentationAcceptConfirmation;
typedef struct _InstrumentClassRequest InstrumentClassRequest;
typedef struct _InstrumentClassResponse InstrumentClassResponse;
typedef struct _ClientMessage ClientMessage;
typedef struct _ServerMessage ServerMessage;
/* --- enums --- */
typedef enum _InstrumentClassResult {
INSTRUMENT_CLASS_RESULT__CLASS_UNMODIFIED = 0,
INSTRUMENT_CLASS_RESULT__CLASS_MODIFIED = 1,
INSTRUMENT_CLASS_RESULT__ERROR = 3
PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(INSTRUMENT_CLASS_RESULT)
} InstrumentClassResult;
typedef enum _InstrumentClassResponse__InstrumentClassResult {
INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__CLASS_UNMODIFIED = 0,
INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__CLASS_MODIFIED = 1,
INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__ERROR = 3
PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT)
} InstrumentClassResponse__InstrumentClassResult;
/* --- messages --- */
......@@ -57,10 +59,11 @@ struct _Error
struct _SessionInitRequest
{
ProtobufCMessage base;
int32_t code;
};
#define SESSION_INIT_REQUEST__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&session_init_request__descriptor) \
}
, 0 }
/*
......@@ -113,7 +116,7 @@ struct _InstrumentationAcceptConfirmation
/*
* This is the response to the instrumentaion server to instrument attached class.
* This is the request to the instrumentaion server to instrument attached class.
*/
struct _InstrumentClassRequest
{
......@@ -128,16 +131,71 @@ struct _InstrumentClassRequest
, 0, 0, (char *)protobuf_c_empty_string, {0,NULL} }
/*
* Server response containing either the instrumented classbytes or error message
*/
struct _InstrumentClassResponse
{
ProtobufCMessage base;
InstrumentClassResult result;
InstrumentClassResponse__InstrumentClassResult result;
char *errormessage;
ProtobufCBinaryData classbytes;
};
#define INSTRUMENT_CLASS_RESPONSE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&instrument_class_response__descriptor) \
, INSTRUMENT_CLASS_RESULT__CLASS_UNMODIFIED, (char *)protobuf_c_empty_string, {0,NULL} }
, INSTRUMENT_CLASS_RESPONSE__INSTRUMENT_CLASS_RESULT__CLASS_UNMODIFIED, (char *)protobuf_c_empty_string, {0,NULL} }
typedef enum {
CLIENT_MESSAGE__REQUEST__NOT_SET = 0,
CLIENT_MESSAGE__REQUEST_SESSION_INIT_REQUEST = 1,
CLIENT_MESSAGE__REQUEST_INSTRUMENTATION_DELIVERY = 2,
CLIENT_MESSAGE__REQUEST_INSTRUMENT_CLASS_REQUEST = 3
PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(CLIENT_MESSAGE__REQUEST)
} ClientMessage__RequestCase;
/*
* Pack together messages from client to simply distinguish them at the server side.
*/
struct _ClientMessage
{
ProtobufCMessage base;
ClientMessage__RequestCase request_case;
union {
SessionInitRequest *session_init_request;
InstrumentationDelivery *instrumentation_delivery;
InstrumentClassRequest *instrument_class_request;
};
};
#define CLIENT_MESSAGE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&client_message__descriptor) \
, CLIENT_MESSAGE__REQUEST__NOT_SET, {0} }
typedef enum {
SERVER_MESSAGE__RESPONSE__NOT_SET = 0,
SERVER_MESSAGE__RESPONSE_SESSION_INIT_RESPONSE = 1,
SERVER_MESSAGE__RESPONSE_INSTRUMENTATION_ACCEPT_CONFIRMATION = 2,
SERVER_MESSAGE__RESPONSE_INSTRUMENT_CLASS_RESPONSE = 3
PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(SERVER_MESSAGE__RESPONSE)
} ServerMessage__ResponseCase;
/*
* Pack together messages from server to simply distinguish them at the client side.
*/
struct _ServerMessage
{
ProtobufCMessage base;
ServerMessage__ResponseCase response_case;
union {
SessionInitResponse *session_init_response;
InstrumentationAcceptConfirmation *instrumentation_accept_confirmation;
InstrumentClassResponse *instrument_class_response;
};
};
#define SERVER_MESSAGE__INIT \
{ PROTOBUF_C_MESSAGE_INIT (&server_message__descriptor) \
, SERVER_MESSAGE__RESPONSE__NOT_SET, {0} }
/* Error methods */
......@@ -273,6 +331,44 @@ InstrumentClassResponse *
void instrument_class_response__free_unpacked
(InstrumentClassResponse *message,
ProtobufCAllocator *allocator);
/* ClientMessage methods */
void client_message__init
(ClientMessage *message);
size_t client_message__get_packed_size
(const ClientMessage *message);
size_t client_message__pack
(const ClientMessage *message,
uint8_t *out);
size_t client_message__pack_to_buffer
(const ClientMessage *message,
ProtobufCBuffer *buffer);
ClientMessage *
client_message__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void client_message__free_unpacked
(ClientMessage *message,
ProtobufCAllocator *allocator);
/* ServerMessage methods */
void server_message__init
(ServerMessage *message);
size_t server_message__get_packed_size
(const ServerMessage *message);
size_t server_message__pack
(const ServerMessage *message,
uint8_t *out);
size_t server_message__pack_to_buffer
(const ServerMessage *message,
ProtobufCBuffer *buffer);
ServerMessage *
server_message__unpack
(ProtobufCAllocator *allocator,
size_t len,
const uint8_t *data);
void server_message__free_unpacked
(ServerMessage *message,
ProtobufCAllocator *allocator);
/* --- per-message closures --- */
typedef void (*Error_Closure)
......@@ -296,13 +392,18 @@ typedef void (*InstrumentClassRequest_Closure)
typedef void (*InstrumentClassResponse_Closure)
(const InstrumentClassResponse *message,
void *closure_data);
typedef void (*ClientMessage_Closure)
(const ClientMessage *message,
void *closure_data);
typedef void (*ServerMessage_Closure)
(const ServerMessage *message,
void *closure_data);
/* --- services --- */
/* --- descriptors --- */
extern const ProtobufCEnumDescriptor instrument_class_result__descriptor;
extern const ProtobufCMessageDescriptor error__descriptor;
extern const ProtobufCMessageDescriptor session_init_request__descriptor;
extern const ProtobufCMessageDescriptor session_init_response__descriptor;
......@@ -310,6 +411,9 @@ extern const ProtobufCMessageDescriptor instrumentation_delivery__descriptor;
extern const ProtobufCMessageDescriptor instrumentation_accept_confirmation__descriptor;
extern const ProtobufCMessageDescriptor instrument_class_request__descriptor;
extern const ProtobufCMessageDescriptor instrument_class_response__descriptor;
extern const ProtobufCEnumDescriptor instrument_class_response__instrument_class_result__descriptor;
extern const ProtobufCMessageDescriptor client_message__descriptor;
extern const ProtobufCMessageDescriptor server_message__descriptor;
PROTOBUF_C__END_DECLS
......
......@@ -34,12 +34,12 @@ message InstrumentationDelivery {
}
// This is server response to InstrumentationDelivery message
message InstrumentationAcceptConfirmation{
message InstrumentationAcceptConfirmation {
bool instrumentation_accepted = 1;
repeated Error errors = 2;
}
// This is the response to the instrumentaion server to instrument attached class.
// This is the request to the instrumentaion server to instrument attached class.
message InstrumentClassRequest {
int32 session_id = 1;
int32 flags = 2;
......@@ -47,14 +47,34 @@ message InstrumentClassRequest {
bytes classBytes = 4;
}
enum InstrumentClassResult {
CLASS_UNMODIFIED = 0;
CLASS_MODIFIED = 1;
ERROR = 3;
// Server response containing either the instrumented classbytes or error message
message InstrumentClassResponse {
enum InstrumentClassResult {
CLASS_UNMODIFIED = 0;
CLASS_MODIFIED = 1;
ERROR = 3;
}
InstrumentClassResult result = 1;
string errorMessage = 2;
bytes classBytes = 3;
}
message InstrumentClassResponse {
InstrumentClassResult result = 1;
string errorMessage = 2;
bytes classBytes = 3;
// Pack together messages from client to simply distinguish them at the server side.
message ClientMessage {
oneof request {
SessionInitRequest session_init_request = 1;
InstrumentationDelivery instrumentation_delivery = 2;
InstrumentClassRequest instrument_class_request = 3;
}
}
// Pack together messages from server to simply distinguish them at the client side.
message ServerMessage {
oneof response {
SessionInitResponse session_init_response = 1;
InstrumentationAcceptConfirmation instrumentation_accept_confirmation =2;
InstrumentClassResponse instrument_class_response = 3;
}
}
......@@ -22,7 +22,6 @@ import com.google.protobuf.CodedOutputStream;
import ch.usi.dag.dislserver.Protocol.InstrumentClassRequest;
import ch.usi.dag.dislserver.Protocol.InstrumentClassResponse;
import ch.usi.dag.dislserver.Protocol.InstrumentClassResult;
import ch.usi.dag.util.logging.Logger;
......@@ -33,18 +32,22 @@ public final class DiSLServer {
//
private static final String PROP_PORT = "dislserver.port";
private static final int DEFAULT_PORT = 11217;
private static final String PROP_CONT = "dislserver.continuous";
private static final boolean continuous = Boolean.getBoolean(PROP_CONT);
private static final boolean continuous = Boolean.getBoolean (PROP_CONT);
//
private static final String __PID_FILE__ = "server.pid.file";
private static final String __STATUS_FILE__ = "server.status.file";
//
private enum ElapsedTime {
RECEIVE, UNPACK, PROCESS, PACK, TRANSMIT;
}
......@@ -52,18 +55,24 @@ public final class DiSLServer {
//
private final AtomicInteger __workerCount = new AtomicInteger ();
private final CounterSet <ElapsedTime> __globalStats = new CounterSet <ElapsedTime> (ElapsedTime.class);
private final CounterSet<ElapsedTime> __globalStats = new CounterSet<ElapsedTime> (
ElapsedTime.class);
//
final class ConnectionHandler implements Runnable {
private final SocketChannel __clientSocket;
private final RequestProcessor __requestProcessor;
private final Thread __serverThread;
//
ConnectionHandler (
final SocketChannel clientSocket,
final RequestProcessor requestProcessor,
......@@ -74,12 +83,14 @@ public final class DiSLServer {
__serverThread = serverThread;
}
@Override
public void run () {
__workerCount.incrementAndGet ();
final CounterSet <ElapsedTime> stats = new CounterSet <ElapsedTime> (ElapsedTime.class);
final IntervalTimer <ElapsedTime> timer = new IntervalTimer <ElapsedTime> (ElapsedTime.class);
final CounterSet<ElapsedTime> stats = new CounterSet<ElapsedTime> (ElapsedTime.class);
final IntervalTimer<ElapsedTime> timer = new IntervalTimer<ElapsedTime> (
ElapsedTime.class);
try {
//
......@@ -90,7 +101,8 @@ public final class DiSLServer {
ByteBuffer recvBuffer = __allocDirect (4096);
ByteBuffer sendBuffer = __allocDirect (4096);
REQUEST_LOOP: while (true) {
REQUEST_LOOP:
while (true) {
timer.reset ();
//
......@@ -126,7 +138,8 @@ public final class DiSLServer {
recvBuffer.flip ();
final CodedInputStream recvStream = CodedInputStream.newInstance (recvBuffer);
final InstrumentClassRequest request = InstrumentClassRequest.parseFrom (recvStream);
final InstrumentClassRequest request = InstrumentClassRequest.parseFrom (
recvStream);
timer.mark (ElapsedTime.UNPACK);
//
......@@ -159,7 +172,8 @@ public final class DiSLServer {
}
if (responseLength > 0) {
final CodedOutputStream sendStream = CodedOutputStream.newInstance (sendBuffer);
final CodedOutputStream sendStream = CodedOutputStream.newInstance (
sendBuffer);
response.writeTo (sendStream);
sendStream.flush ();
}
......@@ -179,7 +193,7 @@ public final class DiSLServer {
stats.update (timer);
if (response.getResult () == InstrumentClassResult.ERROR) {
if (response.getResult () == InstrumentClassResponse.InstrumentClassResult.ERROR) {
//
// Error during instrumentation. Report it to the client
// and stop receiving requests from this connection.
......@@ -195,7 +209,6 @@ public final class DiSLServer {
//
__globalStats.update (stats);
} catch (final IOException ioe) {
//
// Communication error -- just log a message here.
......@@ -222,6 +235,7 @@ public final class DiSLServer {
//
private void __bufferSendTo (
final ByteBuffer buffer, final SocketChannel sc
) throws IOException {
......@@ -272,6 +286,7 @@ public final class DiSLServer {
//
void run (
final ServerSocketChannel serverSocket,
final ExecutorService executor,
......@@ -315,6 +330,7 @@ public final class DiSLServer {
__log.debug ("transmitting data took %d ms", __stats (ElapsedTime.TRANSMIT));
}
private long __stats (final ElapsedTime et) {
return TimeUnit.MILLISECONDS.convert (
__globalStats.get (et), TimeUnit.NANOSECONDS
......@@ -323,7 +339,8 @@ public final class DiSLServer {
//
public static void main (final String [] args) {
public static void main (final String[] args) {
__log.debug ("server starting");
__log.debug ("java.home: %s", System.getProperty ("java.home", ""));
__log.debug ("java.class.path: %s", System.getProperty ("java.class.path", ""));
......@@ -359,6 +376,7 @@ public final class DiSLServer {
//
private static void __serverStarting () {
final File file = __getFileProperty (__PID_FILE__);
if (file != null) {
......
......@@ -81,13 +81,13 @@ final class RequestProcessor {
}
return InstrumentClassResponse.newBuilder ()
.setResult (Protocol.InstrumentClassResult.CLASS_MODIFIED)
.setResult (Protocol.InstrumentClassResponse.InstrumentClassResult.CLASS_MODIFIED)
.setClassBytes (ByteString.copyFrom (newClassBytes))
.build ();
} else {
return InstrumentClassResponse.newBuilder ()
.setResult (Protocol.InstrumentClassResult.CLASS_UNMODIFIED)
.setResult (Protocol.InstrumentClassResponse.InstrumentClassResult.CLASS_UNMODIFIED)
.build ();
}
......@@ -99,7 +99,7 @@ final class RequestProcessor {
__log.error (message);
return InstrumentClassResponse.newBuilder ()
.setResult (Protocol.InstrumentClassResult.ERROR)
.setResult (Protocol.InstrumentClassResponse.InstrumentClassResult.ERROR)
.setErrorMessage (message)
.build ();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment