コード例 #1
0
ファイル: QueueManager.cs プロジェクト: xxjeng/nuxleus
        private static string Push (string queueName, string message) {
            // Notification
            Notification n = new Notification();
            n.Action = "publish";
            n.Expires = DateTime.Now.AddHours(1);
            n.Categories = new Category[2];
            n.Categories[0] = new Category();
            n.Categories[0].Term = message;
            n.Categories[1] = new Category();
            n.Categories[1].Term = "indie";

            // The actual Queue Message to send
            Message pm = new Message();
            pm.Op.Type = OperationType.PushMessage;
            pm.QueueId = queueName;

            Console.WriteLine(queueName);
            Console.WriteLine(n.ToString());
            pm.Payload = Convert.ToBase64String(Notification.Serialize(n));

            // An event handler
            MessageEvent me = new MessageEvent();
            me.Message = pm;
            // Let's see what the response of the server was
            me.MessageReceived += new MessageEventHandler(queue_MessageReceived);

            QueueClientPool.Enqueue(me);

            return "message sent";
        }
コード例 #2
0
ファイル: QueueClient.cs プロジェクト: xxjeng/nuxleus
 public MessageStateEventArgs ( string xml ) {
     try {
         message = Message.Parse(xml);
     } catch (Exception ex) {
         Console.WriteLine(ex.ToString());
         throw ex;
     }
 }
コード例 #3
0
ファイル: QSToLLUPHandler.cs プロジェクト: xxjeng/nuxleus
 private void CheckQueuesForNewMessages ( object info ) {
     if (poller.Service.Connection != null) {
         Nuxleus.Bucker.Message lm = new Nuxleus.Bucker.Message();
         lm.Op.Type = OperationType.ListMessages;
         foreach (string queueId in monitoredQueues) {
             lm.QueueId = queueId;
             poller.Service.Connection.BeginSend(Nuxleus.Bucker.Message.Serialize(lm));
         }
         lm = null;
     }
 }
コード例 #4
0
        private void FailureRaised ( ISocketConnection sender, Exception ex ) {
            // since the client generated an exceptipon we will disconnect it
            // as soon as the error message below is sent
            clientsToDisconnect.Add(sender.SocketHandle);

            // we warn the client something happened
            Nuxleus.Bucker.Message m = new Nuxleus.Bucker.Message();
            m.Type = "error";
            m.Op = null;
            m.Error = new Nuxleus.Bucker.Error();
            m.Error.Type = "internal-server-error";
            m.Error.Code = 500;
            sender.BeginSend(Nuxleus.Bucker.Message.Serialize(m));
        }
コード例 #5
0
        private void MessageReceived(ISocketConnection sender, IMessage message)
        {
            if (clientsToDisconnect.Contains(sender.SocketHandle))
            {
                // if the client has been scheduled to be closed we don't process any of its
                // incoming data
                return;
            }

            Nuxleus.Bucker.Message m = Nuxleus.Bucker.Message.Parse(message.InnerMessage);
            Nuxleus.Bucker.Message responseToSend = null;

            switch (m.Op.Type)
            {
                case OperationType.GetMessage:
                    responseToSend = HandleGetMessageRequest(m);
                    break;
                case OperationType.ListMessages:
                    responseToSend = HandleListMessagesRequest(m);
                    break;
                case OperationType.PushMessage:
                    Console.WriteLine(m.ToString());
                    responseToSend = HandlePushMessageRequest(m);
                    break;
                case OperationType.DeleteMessage:
                    responseToSend = HandleDeleteMessageRequest(m);
                    break;
                case OperationType.NewQueue:
                    responseToSend = HandleNewQueueRequest(m);
                    break;
                case OperationType.DeleteQueue:
                    responseToSend = HandleDeleteQueueRequest(m);
                    break;
                case OperationType.ListQueues:
                    responseToSend = HandleListQueuesRequest(m);
                    break;
                default:
                    responseToSend = new Nuxleus.Bucker.Message();
                    responseToSend.Type = "error";
                    responseToSend.Op = null;
                    responseToSend.Error = new Nuxleus.Bucker.Error();
                    responseToSend.Error.Type = "operation-not-allowed";
                    responseToSend.Error.Code = 405;
                    break;
            }

            if (responseToSend != null)
            {
                sender.BeginSend(Nuxleus.Bucker.Message.Serialize(responseToSend));
                responseToSend = null;
            }
        }
コード例 #6
0
        private Nuxleus.Bucker.Message HandleGetMessageRequest(Nuxleus.Bucker.Message m)
        {
            Nuxleus.Bucker.Message err = CheckQueue(m.QueueId);
            if (err != null)
                return err;

            err = CheckMessageId(m.MessageId);
            if (err != null)
                return err;

            string unreadKey = String.Format("{0}.new", m.QueueId);

            string[] keys = { m.QueueId, unreadKey, m.MessageId };
            IDictionary<string,object> result = mc.Get(keys);

            object keyValue;
            if (!mc.TryGet(m.QueueId, out keyValue))
            {
                m.Type = "error";
                m.Op = null;
                m.Error = new Nuxleus.Bucker.Error();
                m.Error.Type = "not-found";
                m.Error.Code = 404;
                m.Error.Message = String.Format("'{0}' is not an existing queue", m.QueueId);
                return m;
            }

            string mids = (string)result[m.QueueId];

            object messageValue;
            if (mc.TryGet(m.MessageId, out messageValue))
            {
                if (m.Op.Peek == false)
                {
                    string unread = (string)result[unreadKey];
                    if (unread.Contains(m.MessageId))
                    {
                        unread = unread.Replace(m.MessageId, "");
                        unread = unread.Replace(",,", "");
                        unread = unread.Trim(comma);
                        mc.Store(StoreMode.Set, unreadKey, unread);
                    }
                }
                string visibilityKey = String.Format("{0}.visibility", m.MessageId);
                double visibilityTimeout = (double)mc.Get(visibilityKey);
                double now = UnixTimestampNow;
                if (now > visibilityTimeout)
                {
                    if (m.Op.Peek == false)
                        mc.Store(StoreMode.Set, visibilityKey, now + 30);
                    string payload = String.Empty;
                    if (result.ContainsKey(m.MessageId))
                    {
                        payload = (string)result[m.MessageId];
                        if (payload == null)
                            payload = String.Empty;
                    }
                    m.Type = "response";
                    m.Payload = payload;
                }
                else
                {
                    err = new Nuxleus.Bucker.Message();
                    err.Type = "error";
                    err.Op = null;
                    err.Error = new Nuxleus.Bucker.Error();
                    err.Error.Type = "invisible";
                    err.Error.Code = 404;
                    err.Error.Message = String.Format("'{0}' is not visible", m.MessageId);
                }
            }
            else
            {
                err = new Nuxleus.Bucker.Message();
                err.Type = "error";
                err.Op = null;
                err.Error = new Nuxleus.Bucker.Error();
                err.Error.Type = "not-found";
                err.Error.Code = 404;
                err.Error.Message = String.Format("'{0}' was not found in the queue '{1}'", m.MessageId, m.QueueId);
            }

            return m;
        }
コード例 #7
0
        private Nuxleus.Bucker.Message HandleListQueuesRequest(Nuxleus.Bucker.Message m)
        {
            string queues = (string)mc.Get(rootQueueId);
            if (queues == null)
                queues = String.Empty;
            string[] queuesList = queues.Split(',');

            Nuxleus.Bucker.Message lm = new Nuxleus.Bucker.Message();
            lm.Type = "response";
            lm.Op.Type = OperationType.ListQueues;
            if (queues == String.Empty)
            {
                lm.Queues = new string[0];
            }
            else
            {
                lm.Queues = new string[queuesList.Length];
                Array.Copy(queuesList, lm.Queues, queuesList.Length);
            }

            return lm;
        }
コード例 #8
0
        private Nuxleus.Bucker.Message HandleDeleteQueueRequest(Nuxleus.Bucker.Message m)
        {
            Nuxleus.Bucker.Message err = CheckQueueId(m.QueueId);
            if (err != null)
                return err;

            string qid = m.QueueId;
            string keys = (string)mc.Get(qid);

            if ((keys != null) && (keys != String.Empty))
            {
                if (m.Op.Force == false)
                {
                    m = new Nuxleus.Bucker.Message();
                    m.Type = "error";
                    m.Op = null;
                    m.Error = new Nuxleus.Bucker.Error();
                    m.Error.Type = "queue-not-empty";
                    m.Error.Code = 400;
                    m.Error.Message = String.Format("'{0}' is not empty. Either delete all the messages first or set the force attribute on the 'op' element", qid);
                    return m;
                }
                else
                {
                    // Deleting all the messages attached to that queue
                    string[] keysList = keys.Split(',');
                    foreach (string key in keysList)
                    {
                        mc.Remove(String.Format("{0}.visibility", key));
                        mc.Remove(key);
                    }
                }
            }

            mc.Remove(qid);
            mc.Remove(String.Format("{0}.new", qid));

            string queues = (string)mc.Get(rootQueueId);
            if (queues == null)
                queues = String.Empty;
            if (queues.Contains(qid))
            {
                queues = queues.Replace(qid, "");
                queues = queues.Replace(",,", "");
                queues = queues.Trim(comma);
                mc.Store(StoreMode.Set, rootQueueId, queues);
            }

            m.Type = "response";
            return m;
        }
コード例 #9
0
        private Nuxleus.Bucker.Message HandleNewQueueRequest(Nuxleus.Bucker.Message m)
        {
            Nuxleus.Bucker.Message err = CheckQueueId(m.QueueId);
            if (err != null)
                return err;

            string queues = (string)mc.Get(rootQueueId);
            if (queues == null)
            { // just in case
                queues = String.Empty;
            }

            if (!queues.Contains(m.QueueId))
            {
                //mc.Add(m.QueueId, String.Empty);
                //mc.Add(String.Format("{0}.new", m.QueueId), String.Empty);
                //if (queues == String.Empty) {
                //    queues = m.QueueId;
                //} else {
                //    queues = String.Format("{0},{1}", queues, m.QueueId);
                //}
                //mc.Set(rootQueueId, queues);
                m.Type = "response";
            }
            else
            {
                // Houston we have a conflict!
                string qid = m.QueueId;

                m = new Nuxleus.Bucker.Message();
                m.Type = "error";
                m.Op = null;
                m.Error = new Nuxleus.Bucker.Error();
                m.Error.Type = "conflict";
                m.Error.Code = 409;
                m.Error.Message = String.Format("'{0}' is already an existing queue", qid);
            }

            return m;
        }
コード例 #10
0
        private Nuxleus.Bucker.Message CheckQueue(string queueId)
        {
            Nuxleus.Bucker.Message err = CheckQueueId(queueId);
            if (err != null)
                return err;

            string queues = (string)mc.Get(rootQueueId);
            if ((queues == null) || (queues == String.Empty) || !queues.Contains(queueId))
            {
                Nuxleus.Bucker.Message m = new Nuxleus.Bucker.Message();
                m.Type = "error";
                m.Op = null;
                m.Error = new Nuxleus.Bucker.Error();
                m.Error.Type = "not-found";
                m.Error.Code = 404;
                m.Error.Message = String.Format("'{0}' is not an existing queue", queueId);
                return m;
            }

            return null;
        }
コード例 #11
0
        private Nuxleus.Bucker.Message CheckMessageId(string messageId)
        {
            if ((messageId == null) || (messageId == String.Empty))
            {
                Nuxleus.Bucker.Message m = new Nuxleus.Bucker.Message();
                m.Type = "error";
                m.Op = null;
                m.Error = new Nuxleus.Bucker.Error();
                m.Error.Type = "message-invalid";
                m.Error.Code = 400;
                m.Error.Message = "Missing message id value";
                return m;
            }

            return null;
        }
コード例 #12
0
ファイル: QueueClient.cs プロジェクト: xxjeng/nuxleus
 public MessageStateEventArgs ( Message m ) {
     message = m;
 }
コード例 #13
0
ファイル: QueueClient.cs プロジェクト: xxjeng/nuxleus
 /// <summary> 
 /// Send a message instance.
 /// </summary>   
 /// <param name="m">Message instance.</param>
 public void Send ( Message m ) {
     this.Send(m.ToString());
 }
コード例 #14
0
ファイル: QSToLLUPHandler.cs プロジェクト: xxjeng/nuxleus
        private void HandleMessageReceived ( Nuxleus.Bucker.Message m ) {
            // When a message is received we delete it from the queue as
            // it has no purpose anymore
            Nuxleus.Bucker.Message dm = new Nuxleus.Bucker.Message();
            dm.Op.Type = OperationType.DeleteMessage;
            dm.QueueId = m.QueueId;
            dm.MessageId = m.MessageId;
            Service.Connection.BeginSend(Nuxleus.Bucker.Message.Serialize(dm));

            LLUP.Notification n = LLUP.Notification.Parse(Convert.FromBase64String(m.Payload));
            postOffice.Post(n);
        }
コード例 #15
0
ファイル: QSToLLUPHandler.cs プロジェクト: xxjeng/nuxleus
 private void HandleListOfNewMessages ( Nuxleus.Bucker.Message m ) {
     if (m.Messages != null) {
         Nuxleus.Bucker.Message gm = new Nuxleus.Bucker.Message();
         gm.Op.Type = OperationType.GetMessage;
         gm.QueueId = m.QueueId;
         foreach (string mid in m.Messages) {
             gm.MessageId = mid;
             Service.Connection.BeginSend(Nuxleus.Bucker.Message.Serialize(gm));
         }
     }
 }
コード例 #16
0
ファイル: QSToLLUPHandler.cs プロジェクト: xxjeng/nuxleus
        private void ClientConnected ( ISocketConnection sender ) {
            // we are now connected to the queue server
            // let's ensure the queues we monitor are created too
            Nuxleus.Bucker.Message nq = new Nuxleus.Bucker.Message();
            nq.Op.Type = OperationType.NewQueue;
            foreach (string queueId in monitoredQueues) {
                nq.QueueId = queueId;
                sender.BeginSend(Nuxleus.Bucker.Message.Serialize(nq));
            }

            StartMonitoring();
        }
コード例 #17
0
ファイル: Message.cs プロジェクト: xxjeng/nuxleus
 public static byte[] Serialize ( Message message, Encoding encoding ) {
     return encoding.GetBytes(message.ToString());
 }
コード例 #18
0
ファイル: Message.cs プロジェクト: xxjeng/nuxleus
 public static byte[] Serialize ( Message message ) {
     return Encoding.UTF8.GetBytes(message.ToString());
 }