Ejemplo n.º 1
0
 private static void ProcessWatcher(IEnumerable <IWatcher> watchers, WatchedEvent watchedEvent)
 {
     foreach (IWatcher watcher in watchers)
     {
         try
         {
             if (null != watcher)
             {
                 watcher.Process(watchedEvent);
             }
         }
         catch (Exception t)
         {
             LOG.Error("Error while calling watcher ", t);
         }
     }
 }
Ejemplo n.º 2
0
        public void QueueEvent(WatchedEvent @event)
        {
            if (@event.Type == EventType.None && sessionState == @event.State)
            {
                return;
            }

            if (waitingEvents.IsAddingCompleted)
            {
                throw new InvalidOperationException("consumer has been disposed");
            }

            sessionState = @event.State;

            // materialize the watchers based on the event
            var pair = new ClientConnection.WatcherSetEventPair(conn.watcher.Materialize(@event.State, @event.Type, @event.Path), @event);

            // queue the pair (watch set & event) for later processing
            waitingEvents.Add(pair);
        }
Ejemplo n.º 3
0
 public WatcherSetEventPair(IEnumerable <IWatcher> watchers, WatchedEvent @event)
 {
     this.Watchers     = watchers;
     this.WatchedEvent = @event;
 }
Ejemplo n.º 4
0
        private void ReadResponse(byte[] content)
        {
            using (var reader = new EndianBinaryReader(EndianBitConverter.Big, new MemoryStream(content), Encoding.UTF8))
            {
                BinaryInputArchive bbia     = BinaryInputArchive.GetArchive(reader);
                ReplyHeader        replyHdr = new ReplyHeader();

                replyHdr.Deserialize(bbia, "header");
                if (replyHdr.Xid == -2)
                {
                    // -2 is the xid for pings
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.DebugFormat("Got ping response for sessionid: 0x{0:X} after {1}ms", conn.SessionId, (DateTime.UtcNow.Nanos() - lastPingSentNs) / 1000000);
                    }
                    return;
                }
                if (replyHdr.Xid == -4)
                {
                    // -2 is the xid for AuthPacket
                    // TODO: process AuthPacket here
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.DebugFormat("Got auth sessionid:0x{0:X}", conn.SessionId);
                    }
                    return;
                }
                if (replyHdr.Xid == -1)
                {
                    // -1 means notification
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.DebugFormat("Got notification sessionid:0x{0}", conn.SessionId);
                    }

                    WatcherEvent @event = new WatcherEvent();
                    @event.Deserialize(bbia, "response");

                    // convert from a server path to a client path
                    if (conn.ChrootPath != null)
                    {
                        string serverPath = @event.Path;
                        if (serverPath.CompareTo(conn.ChrootPath) == 0)
                        {
                            @event.Path = PathUtils.PathSeparator;
                        }
                        else
                        {
                            @event.Path = serverPath.Substring(conn.ChrootPath.Length);
                        }
                    }

                    WatchedEvent we = new WatchedEvent(@event);
                    if (LOG.IsDebugEnabled)
                    {
                        LOG.DebugFormat("Got {0} for sessionid 0x{1:X}", we, conn.SessionId);
                    }

                    conn.consumer.QueueEvent(we);
                    return;
                }
                Packet packet;

                /*
                 * Since requests are processed in order, we better get a response
                 * to the first request!
                 */
                if (pendingQueue.TryDequeue(out packet))
                {
                    try
                    {
                        if (packet.header.Xid != replyHdr.Xid)
                        {
                            packet.replyHeader.Err = (int)KeeperException.Code.CONNECTIONLOSS;
                            throw new IOException(new StringBuilder("Xid out of order. Got ").Append(replyHdr.Xid).Append(" expected ").Append(packet.header.Xid).ToString());
                        }

                        packet.replyHeader.Xid  = replyHdr.Xid;
                        packet.replyHeader.Err  = replyHdr.Err;
                        packet.replyHeader.Zxid = replyHdr.Zxid;
                        if (replyHdr.Zxid > 0)
                        {
                            lastZxid = replyHdr.Zxid;
                        }

                        if (packet.response != null && replyHdr.Err == 0)
                        {
                            packet.response.Deserialize(bbia, "response");
                        }

                        if (LOG.IsDebugEnabled)
                        {
                            LOG.DebugFormat("Reading reply sessionid:0x{0:X}, packet:: {1}", conn.SessionId, packet);
                        }
                    }
                    finally
                    {
                        FinishPacket(packet);
                    }
                }
                else
                {
                    throw new IOException(new StringBuilder("Nothing in the queue, but got ").Append(replyHdr.Xid).ToString());
                }
            }
        }