Package org.cometd

Examples of org.cometd.Message


                for (Message message : messages)
                    ((MessageImpl)message).decRef();
            }
        }

        Message pollReply=null;
        // Do we need to wait for messages
        if (transport!=null)
        {
            pollReply=transport.getPollReply();
            if (pollReply!=null)
            {
                if (_bayeux.isLogDebug())
                    _bayeux.logDebug("doPost: transport is polling");
                long timeout=client.getTimeout();
                if (timeout==0)
                    timeout=_bayeux.getTimeout();

                Continuation continuation=ContinuationSupport.getContinuation(request,client);

                // Get messages or wait
                synchronized (client)
                {
                    if (!client.hasMessages() && !continuation.isPending()&& received<=1)
                    {
                        // save state and suspend
                        ((ContinuationClient)client).setContinuation(continuation);
                        request.setAttribute(CLIENT_ATTR,client);
                        request.setAttribute(TRANSPORT_ATTR,transport);
                        continuation.suspend(timeout);
                    }
                   
                    if (!continuation.isPending())
                        client.access();

                    continuation.reset();
                }

                ((ContinuationClient)client).setContinuation(null);
                transport.setPollReply(null);

                for (Extension e:_bayeux.getExtensions())
                    pollReply=e.sendMeta(pollReply);
            }
            else if (client!=null)
            {
                client.access();
            }
        }

        // Send any messages.
        if (client!=null)
        {
            synchronized(client)
            {
                client.doDeliverListeners();
                ArrayQueue<Message> messages= (ArrayQueue)client.getQueue();
                int size=messages.size();
                boolean flushed=false;
               
                try
                {
                    if (pollReply!=null)
                    {
                        // can we bypass response generation?
                        if (_refsThreshold>0 && size==1 && transport instanceof JSONTransport)
                        {
                            MessageImpl message = (MessageImpl)messages.peek();
                           
                            // is there a response already prepared
                            ByteBuffer buffer = message.getBuffer();
                            if (buffer!=null)
                            {
                                request.setAttribute("org.mortbay.jetty.ResponseBuffer",buffer);
                                ((MessageImpl)message).decRef();
                                flushed=true;
                            }
                            else if (message.getRefs()>=_refsThreshold)
                            {                               
                                byte[] contentBytes = ("[{\""+Bayeux.SUCCESSFUL_FIELD+"\":true,\""+
                                        Bayeux.CHANNEL_FIELD+"\":\""+Bayeux.META_CONNECT+"\"},"+
                                        message.getJSON()+"]").getBytes(StringUtil.__UTF8);
                                int contentLength = contentBytes.length;

                                String headerString = "HTTP/1.1 200 OK\r\n"+
                                "Content-Type: text/json; charset=utf-8\r\n" +
                                "Content-Length: " + contentLength + "\r\n" +
                                "\r\n";

                                byte[] headerBytes = headerString.getBytes(StringUtil.__UTF8);

                                buffer = ByteBuffer.allocateDirect(headerBytes.length+contentLength);
                                buffer.put(headerBytes);
                                buffer.put(contentBytes);
                                buffer.flip();

                                message.setBuffer(buffer);
                                request.setAttribute("org.mortbay.jetty.ResponseBuffer",buffer);
                                ((MessageImpl)message).decRef();
                                flushed=true;
                            }
                            else
                                transport.send(pollReply);
                        }
                        else
                            transport.send(pollReply);                       
                    }
                   
                    if (!flushed)
                    {
                        Message message = null;
                        for (int i = 0;i<size;i++)
                        {
                            message=messages.getUnsafe(i);
                            transport.send(message);
                        }
View Full Code Here


     * @param data
     * @param msgId
     */
    protected void doPublish(ChannelId to, Client from, Object data, String msgId)
    {
        Message msg = newMessage();
        msg.put(CHANNEL_FIELD,to.toString());
       
        if (msgId==null)
        {
            long id=msg.hashCode()
            ^(to==null?0:to.hashCode())
            ^(from==null?0:from.hashCode());
            id=id<0?-id:id;
            msg.put(ID_FIELD,Long.toString(id,36));
        }
        else
            msg.put(ID_FIELD,msgId);
           
        msg.put(DATA_FIELD,data);
       
        for (Extension e:_extensions)
            msg=e.send(msg);
        _root.doDelivery(to,from,msg);
        ((MessageImpl)msg).decRef();
View Full Code Here

            }
          
            // reply to connect message
            String id=message.getId();

            Message reply=newMessage(message);
           
            reply.put(CHANNEL_FIELD,META_CONNECT);
            reply.put(SUCCESSFUL_FIELD,Boolean.TRUE);
            if (advice!=null)
                reply.put(ADVICE_FIELD,advice);
            if (id!=null)
                reply.put(ID_FIELD,id);

            if (polling)
                transport.setPollReply(reply);
            else
            {
View Full Code Here

    /*
     * QueueListener examples
     */
    public void testDeleteWhenFullQueue() throws Exception
    {
        Message delete =  m("delete", "a");
        Message keep = m("keep", "b");
        Message add = m("add", "c");
       
        _client.setMaxQueue(2);
        _client.addListener(new DeleteWhenFullQueueListener());

        _client.deliver(delete);
View Full Code Here

        assertEquals(resultsList(keep,add), _client.takeMessages());
    }
   
    public void testDiscardNewMessageQueue() throws Exception
    {    
        Message keep1 = m("keep1", "a");
        Message keep2 = m("keep2", "b");
        Message discard = m("discard", "c");
       
        _client.setMaxQueue(2);
        _client.addListener(new DiscardNewMessageQueueListener());
       
        _client.deliver(keep1);
View Full Code Here

        assertEquals(resultsList(keep1, keep2), _client.takeMessages());
    }
   
    public void testModifyExistingMessagesQueue() throws Exception
    {
        Message keep = m("keep", "a");
        Message delete = m("delete", "b");
        Message add = m("add", "c");
               
        _client.setMaxQueue(2);
        _client.addListener(new ModifyExistingMessagesQueueListener());
       
        _client.deliver(keep);
View Full Code Here

            {
                called[0]=true;
            }
        });

        Message ping = m("ping", "hello");
        _client.deliver(ping);
        assertFalse(called[0]);
       
        _client.doDeliverListeners();
View Full Code Here

    }
*/   
   
    private Message m(String key, String value)
    {
        Message message = new MessageImpl();
        message.put(key, value);
        return message;
    }
View Full Code Here

        {
            Iterator<Message> queueIterator = client.getQueue().iterator();
            boolean removed = false;
            while(queueIterator.hasNext())
            {
                Message m = queueIterator.next();
                if(m.get("delete")!=null)
                {
                    queueIterator.remove();
                    removed=true;
                }
            }
View Full Code Here

            if (isLogInfo())
                logInfo("Disconnect "+client.getId());
               
            client.remove(false);
           
            Message reply=newMessage(message);
            reply.put(CHANNEL_FIELD,META_DISCONNECT);
            reply.put(SUCCESSFUL_FIELD,Boolean.TRUE);
            String id=message.getId();
            if (id!=null)
                reply.put(ID_FIELD,id);

            for (Extension e:_extensions)
                reply=e.sendMeta(reply);
           
            Message pollReply = transport.getPollReply();
            if (pollReply!=null)
            {
                for (Extension e:_extensions)
                    pollReply=e.sendMeta(pollReply);
                transport.send(pollReply);
View Full Code Here

TOP

Related Classes of org.cometd.Message

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.