Package com.trendmicro.mist.util

Examples of com.trendmicro.mist.util.Packet


                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Get a message and ack
         */
        brk.sendMessage(true, "foo.in", "foo".getBytes());
        BytesMessage msgin = brk.getBytesMessage(true, "foo.in");
        new OnMessageRunner(sess, msgin).start();
        packet.read(socketInput);
        MistMessage.MessageBlock mBlock = MistMessage.MessageBlock.newBuilder().mergeFrom(packet.getPayload()).build();
        assertEquals("queue:foo.in", mBlock.getId());
        assertEquals("foo", new String(mBlock.getMessage().toByteArray()));

        packet.setPayload(GateTalk.Response.newBuilder().setSuccess(true).build().toByteArray());
        packet.write(socketOutput);

        brk.stop();
    }
View Full Code Here


                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Send and receive a message from foo.in
         */
        brk.sendMessage(true, "foo.in", "foo".getBytes());
        packet.read(socketInput);
        MistMessage.MessageBlock mBlock = MistMessage.MessageBlock.newBuilder().mergeFrom(packet.getPayload()).build();
        assertEquals("queue:foo.in", mBlock.getId());
        assertEquals("foo", new String(mBlock.getMessage().toByteArray()));

        packet.setPayload(GateTalk.Response.newBuilder().setSuccess(true).build().toByteArray());
        packet.write(socketOutput);

        /**
         * Test dynamic mount bar.in
         */
        brk.sendMessage(true, "bar.in", "bar".getBytes());
        clientConfig = genClientConfig("bar.in");
        sess.addClient(clientConfig);

        packet.read(socketInput);
        mBlock = MistMessage.MessageBlock.newBuilder().mergeFrom(packet.getPayload()).build();
        assertEquals("queue:bar.in", mBlock.getId());
        assertEquals("bar", new String(mBlock.getMessage().toByteArray()));

        packet.setPayload(GateTalk.Response.newBuilder().setSuccess(true).build().toByteArray());
        packet.write(socketOutput);

        brk.stop();
    }
View Full Code Here

        reply_builder.addResponse(res_builder.build());
    }

    private void writeResponse(GateTalk.Command cmd, BufferedOutputStream out) {
        try {
            Packet pack = new Packet();
            pack.setPayload(cmd.toByteArray());
            pack.write(out);
        }
        catch(IOException e) {
            logger.error(e.getMessage());
        }
    }
View Full Code Here

    }

    private GateTalk.Command readCommand(BufferedInputStream in) {
        GateTalk.Command cmd = null;
        try {
            Packet pack = new Packet();
            if(pack.read(in) > 0) {
                GateTalk.Command.Builder cmd_builder = GateTalk.Command.newBuilder();
                cmd_builder.mergeFrom(pack.getPayload());
                cmd = cmd_builder.build();
            }
        }
        catch(Exception e) {
            logger.error(e.getMessage(), e);
View Full Code Here

            return;
        open(false);
        retryDeliver = true;
        isReady = true;

        Packet packet = new Packet();
        List<Exchange> notRoutedDest = new ArrayList<Exchange>();

        // The main loop to handle incoming messages to be sent
        while(!detachNow) {
            try {
                int rdcnt;
                // Reads a message from socket
                // If negative, indicates the other side shutdowns
                if((rdcnt = packet.read(socketInput)) < 0)
                    return;
                // If zero, which means invalid message size, ackClient and
                // continue
                else if(rdcnt == 0) {
                    ackClient(packet, false, MistException.INVALID_MESSAGE_SIZE);
                    continue;
                }
            }
            catch(IOException e) {
                // Socket exception happens, if it is not caused by detach, log
                // the error
                if(!detachNow)
                    logger.error(e.getMessage(), e);
                return;
            }

            // Received a message from socket, try to deliver it

            // Prepare the message to be send
            MessagePrepared mp = null;
            try {
                mp = new MessagePrepared(packet.getPayload());
            }
            catch(MistException e) {
                // If any error occurs, acknowledge fail response and reason
                // to client and continue to handle next message
                ackClient(packet, false, e.getMessage());
View Full Code Here

        Method ackClient = ProducerSession.class.getDeclaredMethod("ackClient", new Class[] {
            com.trendmicro.mist.util.Packet.class, boolean.class, String.class
        });
        ackClient.setAccessible(true);

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());

        /**
         * Test success packet
         */
        ackClient.invoke(sess, new Object[] {
            packet, true, ""
        });
        packet.read(socketInput);
        assertEquals(GateTalk.Response.newBuilder().setSuccess(true).build().toByteString(), ByteString.copyFrom(packet.getPayload()));

        /**
         * Test fail packet
         */
        ackClient.invoke(sess, new Object[] {
            packet, false, "exception"
        });
        packet.read(socketInput);
        assertEquals(GateTalk.Response.newBuilder().setSuccess(false).setException("exception").build().toByteString(), ByteString.copyFrom(packet.getPayload()));

        socket.close();
    }
View Full Code Here

                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Test message too large (over 20M)
         */
        byte[] largeMsg = new byte[Daemon.MAX_MESSAGE_SIZE + 1];
        new DataOutputStream(socketOutput).writeInt(Daemon.MAX_MESSAGE_SIZE + 1);
        socketOutput.write(largeMsg);
        packet.read(socketInput);
        GateTalk.Response res = GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build();
        assertFalse(res.getSuccess());
        assertEquals(MistException.INVALID_MESSAGE_SIZE, res.getException());
    }
View Full Code Here

                break;
            Utils.justSleep(500);
        }
        assertTrue(sess.isReady());

        Packet packet = new Packet();
        BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());

        /**
         * Send a message to foo.out
         */
        MistMessage.MessageBlock msg = MistMessage.MessageBlock.newBuilder().setId("queue:foo.out").setMessage(ByteString.copyFrom("test".getBytes())).build();
        packet.setPayload(msg.toByteArray());
        packet.write(socketOutput);
        packet.read(socketInput);
        assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
        byte[] recvMsg = brk.getMessage(true, "foo.out");
        assertEquals("test", new String(recvMsg));

        /**
         * Send a message to bar.out
         */
        msg = MistMessage.MessageBlock.newBuilder().setId("queue:bar.out").setMessage(ByteString.copyFrom("test-bar".getBytes())).build();
        packet.setPayload(msg.toByteArray());
        packet.write(socketOutput);
        packet.read(socketInput);
        assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
        recvMsg = brk.getMessage(true, "bar.out");
        assertEquals("test-bar", new String(recvMsg));

        /**
         * Test Local Forwarding: foo.out->bar.in, ,log.in
         */
        Vector<Exchange> destList = new Vector<Exchange>();
        destList.add(new Exchange("bar.in"));
        destList.add(new Exchange(""));
        destList.add(new Exchange("log.in"));
        RouteFarm.getInstance().getRouteTable().put("foo.out", destList);
        // Wait until local routing cache expires
        Utils.justSleep(2000);
        msg = MistMessage.MessageBlock.newBuilder().setId("queue:foo.out").setMessage(ByteString.copyFrom("test-route".getBytes())).build();
        packet.setPayload(msg.toByteArray());
        packet.write(socketOutput);
        packet.read(socketInput);
        assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
        recvMsg = brk.getMessage(true, "bar.in");
        assertEquals("test-route", new String(recvMsg));
        recvMsg = brk.getMessage(true, "log.in");
        assertEquals("test-route", new String(recvMsg));
        brk.stop();
View Full Code Here

        private GateTalk.Command sendRequest(GateTalk.Command cmd) throws Exception {
            Socket s = null;
            try {
                s = new Socket("127.0.0.1", MISTD_PORT);
                Packet pack = new Packet();
                pack.setPayload(cmd.toByteArray());
                pack.write(new BufferedOutputStream(s.getOutputStream()));
                pack.read(new BufferedInputStream(s.getInputStream()));
                return GateTalk.Command.parseFrom(pack.getPayload());
            }
            catch(Exception e) {
                throw e;
            }
            finally {
View Full Code Here

            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                bis = new BufferedInputStream(dataChannel.getInputStream());
                bos = new BufferedOutputStream(dataChannel.getOutputStream());
                Packet packet = new Packet();
                if(isConsumer) {
                    GateTalk.Response ack = GateTalk.Response.newBuilder().setSuccess(true).build();
                    for(;;) {
                        if(packet.read(bis) <= 0)
                            break;
                        localQueue.put(new MessagePair(MessageBlock.parseFrom(packet.getPayload()), acker));
                        for(;;) {
                            synchronized(acker) {
                                acker.wait(500);
                                if(acker.acked())
                                    break;
                                else if(onClose)
                                    return;
                            }
                        }
                        packet.setPayload(ack.toByteArray());
                        packet.write(bos);
                        synchronized(acker) {
                            acker.reset();
                            acker.notify();
                        }
                        if(onClose)
                            return;
                    }
                }
                else {
                    boolean last = false;
                    do {
                        if(onClose)
                            last = true;
                        synchronized(acker) {
                            MessagePair pair = localQueue.poll(500, TimeUnit.MILLISECONDS);
                            if(pair != null) {
                                failList.add(pair.message);
                                packet.setPayload(pair.message.toByteArray());
                                packet.write(bos);
                                packet.read(bis);
                                GateTalk.Response.Builder responseBuilder = GateTalk.Response.newBuilder().mergeFrom(packet.getPayload());
                                if(!responseBuilder.getSuccess()){
                                    error = true;
                                    errorStr = responseBuilder.getException();
                                    break;
                                }
View Full Code Here

TOP

Related Classes of com.trendmicro.mist.util.Packet

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.