use of streamer.ByteBuffer in project cloudstack by apache.
the class ServerPaletteUpdate method handleData.
@Override
public void handleData(ByteBuffer buf, Link link) {
if (verbose)
System.out.println("[" + this + "] INFO: Data received: " + buf + ".");
// (2 bytes): A 16-bit, unsigned integer. The update type. This field MUST
// be set to UPDATETYPE_PALETTE (0x0002).
int updateType = buf.readUnsignedShortLE();
if (updateType != UPDATETYPE_PALETTE)
throw new RuntimeException("Unexpected update type. Expected type: UPDATETYPE_PALETTE (0x0002), actual value: " + updateType + ", data: " + buf + ".");
// pad2Octets (2 bytes): A 16-bit, unsigned integer. Padding. Values in this
// field MUST be ignored.
buf.skipBytes(2);
// (4 bytes): A 32-bit, unsigned integer. The number of RGB triplets in the
// paletteData field. This field MUST be set to 256 (the number of entries
// in an 8 bpp palette).
int numberColors = (int) buf.readUnsignedIntLE();
if (numberColors != 256)
throw new RuntimeException("Unexpected value for number of color field in server Palette Update packet. Expected value: 256 colors, actual value: " + numberColors + ", data: " + buf + ".");
// (variable): An array of palette entries in RGB triplet format packed on
// byte boundaries. The number of triplet entries is given by the
// numberColors field.
ByteBuffer paletteEntries = buf.readBytes(numberColors * 3);
// In the case of a Palette Update, the client MUST update the global
// palette on all drawing surfaces
screen.colorMap = new IndexColorModel(8, numberColors, paletteEntries.data, paletteEntries.offset, false);
/* DEBUG */
buf.assertThatBufferIsFullyRead();
buf.unref();
}
use of streamer.ByteBuffer in project cloudstack by apache.
the class ServerTpkt method handleData.
@Override
public void handleData(ByteBuffer buf, Link link) {
if (buf == null)
return;
if (verbose)
System.out.println("[" + this + "] INFO: Data received: " + buf + ".");
// We need at least 4 bytes to get packet length
if (!cap(buf, 4, UNLIMITED, link, false))
return;
int version = buf.readUnsignedByte();
if (version != PROTOCOL_TPKT)
throw new RuntimeException("Unexpected data in TPKT header. Expected TPKT version: 0x03, actual value: " + buf + ".");
// Reserved byte
buf.skipBytes(1);
// Length of whole packet, including header
int length = buf.readUnsignedShort();
if (!cap(buf, length, length, link, false))
return;
int payloadLength = length - buf.cursor;
// Extract payload
ByteBuffer outBuf = buf.slice(buf.cursor, payloadLength, true);
buf.unref();
if (verbose) {
outBuf.putMetadata("source", this);
}
pushDataToAllOuts(outBuf);
}
use of streamer.ByteBuffer in project cloudstack by apache.
the class VncMessageHandler method main.
/**
* Example.
*/
public static void main(String[] args) {
// System.setProperty("streamer.Link.debug", "true");
System.setProperty("streamer.Element.debug", "true");
// System.setProperty("streamer.Pipeline.debug", "true");
Element source = new MockSource("source") {
{
// Split messages at random boundaries to check "pushback" logic
bufs = ByteBuffer.convertByteArraysToByteBuffers(new byte[] { // Message type: server bell
RfbConstants.SERVER_BELL, // Message type: clipboard text
RfbConstants.SERVER_CUT_TEXT, // Padding
0, 0, 0, // Length (test)
0, 0, 0, 4 }, new byte[] { // Clipboard text
't', 'e', 's', 't', // Message type: frame buffer update
RfbConstants.SERVER_FRAMEBUFFER_UPDATE, // Padding
0, // Number of rectangles
0, 3 }, new byte[] { // x, y, width, height: 0x0@4x4
0, 0, 0, 0, 0, 4, 0, 4, // Encoding: desktop size
(byte) ((RfbConstants.ENCODING_DESKTOP_SIZE >> 24) & 0xff), (byte) ((RfbConstants.ENCODING_DESKTOP_SIZE >> 16) & 0xff), (byte) ((RfbConstants.ENCODING_DESKTOP_SIZE >> 8) & 0xff), (byte) ((RfbConstants.ENCODING_DESKTOP_SIZE >> 0) & 0xff) }, new byte[] { // x, y, width, height: 0x0@4x4
0, 0, 0, 0, 0, 4, 0, 4, // Encoding: raw rect
(byte) ((RfbConstants.ENCODING_RAW >> 24) & 0xff), (byte) ((RfbConstants.ENCODING_RAW >> 16) & 0xff), (byte) ((RfbConstants.ENCODING_RAW >> 8) & 0xff), (byte) ((RfbConstants.ENCODING_RAW >> 0) & 0xff), // Raw pixel data 4x4x1 bpp
1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, new byte[] { 11, 12, 13, 14, 15, 16, // x, y, width, height: 0x0@2x2
0, 0, 0, 0, 0, 2, 0, 2, // Encoding: copy rect
(byte) ((RfbConstants.ENCODING_COPY_RECT >> 24) & 0xff), (byte) ((RfbConstants.ENCODING_COPY_RECT >> 16) & 0xff), (byte) ((RfbConstants.ENCODING_COPY_RECT >> 8) & 0xff), (byte) ((RfbConstants.ENCODING_COPY_RECT >> 0) & 0xff), // srcX, srcY: 2x2
0, 2, 0, 2 });
}
};
ScreenDescription screen = new ScreenDescription() {
{
bytesPerPixel = 1;
}
};
final Element handler = new VncMessageHandler("handler", screen);
ByteBuffer[] emptyBuf = ByteBuffer.convertByteArraysToByteBuffers(new byte[] {});
Element fburSink = new MockSink("fbur", ByteBuffer.convertByteArraysToByteBuffers(new byte[] {}, new byte[] {}));
Element bellSink = new MockSink("bell", emptyBuf);
Element clipboardSink = new MockSink("clipboard", emptyBuf);
Element desktopSizeChangeSink = new MockSink("desktop_size", emptyBuf);
Element pixelsSink = new MockSink("pixels", ByteBuffer.convertByteArraysToByteBuffers(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
Element copyRectSink = new MockSink("copy_rect", emptyBuf);
Pipeline pipeline = new PipelineImpl("test");
pipeline.addAndLink(source, handler);
pipeline.add(fburSink, bellSink, clipboardSink, desktopSizeChangeSink, pixelsSink, copyRectSink);
pipeline.link("handler >" + FRAME_BUFFER_UPDATE_REQUEST_ADAPTER_PAD, "fbur");
pipeline.link("handler >" + SERVER_BELL_ADAPTER_PAD, "bell");
pipeline.link("handler >" + SERVER_CLIPBOARD_ADAPTER_PAD, "clipboard");
pipeline.link("handler >" + PIXEL_ADAPTER_PAD, "pixels");
pipeline.runMainLoop("source", STDOUT, false, false);
}
use of streamer.ByteBuffer in project cloudstack by apache.
the class VncMessageHandler method handleCopyRect.
private boolean handleCopyRect(ByteBuffer buf, Link link, int x, int y, int width, int height) {
// We need 4 bytes with coordinates of source rectangle
if (!cap(buf, 4, UNLIMITED, link, true))
return false;
CopyRectOrder order = new CopyRectOrder();
order.srcX = buf.readUnsignedShort();
order.srcY = buf.readUnsignedShort();
order.x = x;
order.y = y;
order.width = width;
order.height = height;
if (verbose)
System.out.println("[" + this + "] INFO: Copy rect. X: " + x + ", y: " + y + ", width: " + width + ", height: " + height + ", srcX: " + order.srcX + ", srcY: " + order.srcY + ".");
pushDataToPad(PIXEL_ADAPTER_PAD, new ByteBuffer(order));
return true;
}
use of streamer.ByteBuffer in project cloudstack by apache.
the class VncMessageHandler method handleRawRectangle.
private boolean handleRawRectangle(ByteBuffer buf, Link link, int x, int y, int width, int height) {
// Raw rectangle is just array of pixels to draw on screen.
int rectDataLength = width * height * screen.getBytesPerPixel();
// rectangles.
if (!cap(buf, rectDataLength, UNLIMITED, link, true))
return false;
if (verbose)
System.out.println("[" + this + "] INFO: Raw rect. X: " + x + ", y: " + y + ", width: " + width + ", height: " + height + ", data length: " + rectDataLength + ".");
BitmapRectangle rectangle = new BitmapRectangle();
rectangle.x = x;
rectangle.y = y;
rectangle.width = width;
rectangle.height = height;
rectangle.bufferWidth = width;
rectangle.bufferHeight = height;
rectangle.bitmapDataStream = buf.readBytes(rectDataLength);
rectangle.colorDepth = screen.getColorDeph();
BitmapOrder order = new BitmapOrder();
order.rectangles = new BitmapRectangle[] { rectangle };
pushDataToPad(PIXEL_ADAPTER_PAD, new ByteBuffer(order));
return true;
}
Aggregations