Commit a4161a17 authored by Lubomir Bulej's avatar Lubomir Bulej

Remove Message and MessageChannel which are no longer needed.

parent dc8d6c07
package ch.usi.dag.dislserver;
final class Message {
private static final int __ERROR_FLAGS__ = -1;
private static final int __SUCCESS_FLAGS__ = 0;
private static final byte [] __EMPTY_ARRAY__ = new byte [0];
//
private final int __flags;
private final byte [] __control;
private final byte [] __payload;
//
public Message (
final int flags, final byte [] control, final byte [] payload
) {
__flags = flags;
__control = control;
__payload = payload;
}
//
public int flags () {
return __flags;
}
public byte [] control () {
return __control;
}
public byte [] payload () {
return __payload;
}
//
public boolean isShutdown () {
return (__control.length == 0) && (__payload.length == 0);
}
public boolean isError () {
return (__flags == __ERROR_FLAGS__) && (__payload.length == 0);
}
//
/**
* Creates a message containing a modified class bytecode.
*
* @param bytecode
* the bytecode of the modified class.
*/
public static Message createClassModifiedResponse (final byte [] bytecode) {
//
// The flags are all reset, the control part of the network message
// is empty, and the payload contains the modified class bytecode.
//
return new Message (__SUCCESS_FLAGS__, __EMPTY_ARRAY__, bytecode);
}
/**
* Creates a message indicating that a class was not modified.
*/
public static Message createNoOperationResponse () {
//
// The flags are all reset, and both the control part and the
// payload parts of the network message are empty.
//
return new Message (__SUCCESS_FLAGS__, __EMPTY_ARRAY__, __EMPTY_ARRAY__);
}
/**
* Creates a message indicating a server-side error.
*/
public static Message createErrorResponse (final String error) {
//
// The flags are all set, the control part of the network message
// contains the error message, and the payload is empty.
//
return new Message (__ERROR_FLAGS__, error.getBytes (), __EMPTY_ARRAY__);
}
}
package ch.usi.dag.dislserver;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.SocketChannel;
final class MessageChannel implements Closeable {
private static final int __HEADER_LENGTH__ = 3 * Integer.BYTES;
//
private final SocketChannel __socket;
private final ByteBuffer __head = __allocateDirect (4096);
private final ByteBuffer [] __sendBuffers = new ByteBuffer [] {
__head, null, null
};
//
private ByteBuffer __body = __allocateDirect (128 * 1024);
//
public MessageChannel (final SocketChannel socket) {
__socket = socket;
}
//
public Message recvMessage () throws IOException {
//
// request protocol:
//
// java int - request flags
// java int - control data length (cdl)
// java int - payload data length (pdl)
// bytes[cdl] - control data (contains class name)
// bytes[pdl] - payload data (contains class code)
//
__head.clear ();
__bufferRecvFrom (__socket, __HEADER_LENGTH__, __head);
//
__head.rewind ();
final int flags = __head.getInt ();
final int controlLength = __head.getInt ();
final int payloadLength = __head.getInt ();
//
final int expectedLength = controlLength + payloadLength;
__ensureBodyCapacity (expectedLength);
__body.clear ();
__bufferRecvFrom (__socket, expectedLength, __body);
//
__body.rewind ();
final byte [] control = new byte [controlLength];
__body.get (control);
final byte [] payload = new byte [payloadLength];
__body.get (payload);
return new Message (flags, control, payload);
}
private void __bufferRecvFrom (
final SocketChannel sc, final int length, final ByteBuffer buffer
) throws IOException {
buffer.limit (buffer.position () + length);
while (buffer.hasRemaining ()) {
final int bytesRead = sc.read (buffer);
if (bytesRead < 0) {
throw new EOFException ("unexpected end of stream");
}
}
}
private void __ensureBodyCapacity (final int requiredCapacity) {
if (__body.capacity () < requiredCapacity) {
__body = __allocateDirect (__align (requiredCapacity, 12));
}
}
private static int __align (final int value, final int bits) {
final int mask = -1 << bits;
final int fill = (1 << bits) - 1;
return (value + fill) & mask;
}
//
public void sendMessage (final Message message) throws IOException {
//
// response protocol:
//
// java int - response flags
// java int - control data length (cdl)
// java int - payload data length (pdl)
// bytes[cdl] - control data (nothing, or error message)
// bytes[pdl] - payload data (instrumented class code)
//
__head.clear ();
__head.putInt (message.flags ());
final int controlLength = message.control ().length;
__head.putInt (controlLength);
final int payloadLength = message.payload ().length;
__head.putInt (payloadLength);
//
__sendBuffers [1] = ByteBuffer.wrap (message.control ());
__sendBuffers [2] = ByteBuffer.wrap (message.payload ());
//
__head.flip ();
do {
__socket.write (__sendBuffers);
} while (__sendBuffers [2].hasRemaining ());
}
@Override
public void close () throws IOException {
__socket.close ();
}
//
private static ByteBuffer __allocateDirect (final int capacity) {
return ByteBuffer.allocateDirect (capacity).order (ByteOrder.BIG_ENDIAN);
}
}
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