Пример #1
0
 internal Packet(RequestHeader header, ReplyHeader replyHeader, IRecord request, IRecord response, byte[] data, ZooKeeper.WatchRegistration watchRegistration, string serverPath, string clientPath)
 {
     this.header = header;
     this.replyHeader = replyHeader;
     this.request = request;
     this.response = response;
     this.serverPath = serverPath;
     this.clientPath = clientPath;
     if (data != null)
     {
         this.data = data;
     } 
     else
     {
         try
         {
             MemoryStream ms = new MemoryStream();
             using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, ms, Encoding.UTF8))
             {
                 BinaryOutputArchive boa = BinaryOutputArchive.getArchive(writer);
                 boa.WriteInt(-1, "len"); // We'll fill this in later
                 if(header != null)
                 {
                     header.Serialize(boa, "header");
                 }
                 if (request != null)
                 {
                     request.Serialize(boa, "request");
                 }                        
                 ms.Position = 0;
                 int len = Convert.ToInt32(ms.Length); // now we have the real length
                 writer.Write(len - 4); // update the length info
                 this.data = ms.ToArray();
             }
         }
         catch (IOException e)
         {
             LOG.Warn("Ignoring unexpected exception", e);
         }
     }
     this.watchRegistration = watchRegistration;
 }
Пример #2
0
        public int CompareTo(object obj)
        {
            RequestHeader header = (RequestHeader)obj;

            if (header == null)
            {
                throw new InvalidOperationException("Comparing different types of records.");
            }
            int num = 0;

            num = (this.Xid == header.Xid) ? 0 : ((this.Xid < header.Xid) ? -1 : 1);
            if (num == 0)
            {
                num = (this.Type == header.Type) ? 0 : ((this.Type < header.Type) ? -1 : 1);
                if (num != 0)
                {
                    return(num);
                }
            }
            return(num);
        }
Пример #3
0
        public int CompareTo(object obj)
        {
            RequestHeader peer = (RequestHeader)obj;

            if (peer == null)
            {
                throw new InvalidOperationException("Comparing different types of records.");
            }
            int ret = 0;

            ret = (Xid == peer.Xid)? 0 :((Xid < peer.Xid)?-1:1);
            if (ret != 0)
            {
                return(ret);
            }
            ret = (Type == peer.Type)? 0 :((Type < peer.Type)?-1:1);
            if (ret != 0)
            {
                return(ret);
            }
            return(ret);
        }
Пример #4
0
        public int CompareTo(object obj)
        {
            RequestHeader requestHeader = (RequestHeader)obj;

            if (requestHeader == null)
            {
                throw new InvalidOperationException("Comparing different types of records.");
            }
            int num1 = this.Xid == requestHeader.Xid ? 0 : (this.Xid < requestHeader.Xid ? -1 : 1);

            if (num1 != 0)
            {
                return(num1);
            }
            int num2 = this.Type == requestHeader.Type ? 0 : (this.Type < requestHeader.Type ? -1 : 1);

            if (num2 != 0)
            {
                return(num2);
            }
            return(num2);
        }
Пример #5
0
 public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration, object callback, object ctx)
 {
     return producer.QueuePacket(h, r, request, response, clientPath, serverPath, watchRegistration);
 }
Пример #6
0
 public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration)
 {
     ReplyHeader r = new ReplyHeader();
     Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null);
     
     if (!p.WaitUntilFinishedSlim(SessionTimeout))
     {
         throw new TimeoutException(new StringBuilder("The request ").Append(request).Append(" timed out while waiting for a response from the server.").ToString());
     }
     return r;
 }
        public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration)
        {
            lock (outgoingQueueLock)
            {
                //lock here for XID?
                if (h.Type != (int)OpCode.Ping && h.Type != (int)OpCode.Auth)
                {
                    h.Xid = Xid;
                }

                Packet p = new Packet(h, r, request, response, null, watchRegistration, clientPath, serverPath);
                p.clientPath = clientPath;
                p.serverPath = serverPath;

                if (!zooKeeper.State.IsAlive())
                    ConLossPacket(p);
                else
                {
                    outgoingQueue.AddLast(p);
                    Monitor.PulseAll(outgoingQueueLock);
                }

                return p;
            }
        }
 private void SendPing()
 {
     lastPingSentNs = DateTime.Now.Nanos();
     RequestHeader h = new RequestHeader(-2, (int)OpCode.Ping);
     conn.QueuePacket(h, null, null, null, null, null, null, null, null);
 }
        private void PrimeConnection(TcpClient client)
        {
            LOG.Info(string.Format("Socket connection established to {0}, initiating session", client.Client.RemoteEndPoint));

            ConnectRequest conReq = new ConnectRequest(0, lastZxid, Convert.ToInt32(conn.SessionTimeout.TotalMilliseconds), conn.SessionId, conn.SessionPassword);

            byte[] buffer;
            using (MemoryStream ms = new MemoryStream())
            using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, ms, Encoding.UTF8))
            {
                BinaryOutputArchive boa = BinaryOutputArchive.getArchive(writer);
                boa.WriteInt(-1, "len");
                conReq.Serialize(boa, "connect");
                ms.Position = 0;
                writer.Write(ms.ToArray().Length - 4);
                buffer = ms.ToArray();
            }
            lock (outgoingQueueLock)
            {
                if (!ClientConnection.disableAutoWatchReset && (!zooKeeper.DataWatches.IsEmpty() || !zooKeeper.ExistWatches.IsEmpty() || !zooKeeper.ChildWatches.IsEmpty()))
                {
                    var sw = new SetWatches(lastZxid, zooKeeper.DataWatches, zooKeeper.ExistWatches, zooKeeper.ChildWatches);
                    var h = new RequestHeader();
                    h.Type = (int)OpCode.SetWatches;
                    h.Xid = -8;
                    Packet packet = new Packet(h, new ReplyHeader(), sw, null, null, null, null, null);
                    outgoingQueue.AddFirst(packet);
                }

                foreach (ClientConnection.AuthData id in conn.authInfo)
                {
                    outgoingQueue.AddFirst(new Packet(new RequestHeader(-4, (int)OpCode.Auth), null, new AuthPacket(0, id.scheme, id.data), null, null, null, null, null));
                }
                outgoingQueue.AddFirst((new Packet(null, null, null, null, buffer, null, null, null)));
            }

            lock (this)
            {
                EnableWrite();
            }

            if (LOG.IsDebugEnabled)
            {
                LOG.Debug("Session establishment request sent on " + client.Client.RemoteEndPoint);
            }
        }
        public Packet QueuePacket(RequestHeader h, ReplyHeader r, IRecord request, IRecord response, string clientPath, string serverPath, ZooKeeper.WatchRegistration watchRegistration)
        {
            if (h.Type != (int)OpCode.Ping && h.Type != (int)OpCode.Auth)
                h.Xid = Xid;

            Packet p = new Packet(h, r, request, response, null, watchRegistration, clientPath, serverPath);

            if (!zooKeeper.State.IsAlive() || closing || Interlocked.CompareExchange(ref isDisposed, 0, 0) == 1)
            {
                if(LOG.IsDebugEnabled)
                    LOG.DebugFormat("Connection closing. Sending ConLossPacket. IsAlive: {0}, closing: {1}", zooKeeper.State.IsAlive(), closing);
                ConLossPacket(p);
            }
            else
            {
                if (h.Type == (int)OpCode.CloseSession)
                    closing = true;
                // enqueue the packet when zookeeper is connected
                addPacketLast(p);
            }
            return p;
        }
        private void PrimeConnection()
        {
            LOG.InfoFormat("Socket connection established to {0}, initiating session", client.Client.RemoteEndPoint);
            ConnectRequest conReq = new ConnectRequest(0, lastZxid, Convert.ToInt32(conn.SessionTimeout.TotalMilliseconds), conn.SessionId, conn.SessionPassword);

            lock (outgoingQueue)
            {
                if (!ClientConnection.DisableAutoWatchReset && (!zooKeeper.DataWatches.IsEmpty() || !zooKeeper.ExistWatches.IsEmpty() || !zooKeeper.ChildWatches.IsEmpty()))
                {
                    var sw = new SetWatches(lastZxid, zooKeeper.DataWatches, zooKeeper.ExistWatches, zooKeeper.ChildWatches);
                    var h = new RequestHeader();
                    h.Type = (int)OpCode.SetWatches;
                    h.Xid = -8;
                    Packet packet = new Packet(h, new ReplyHeader(), sw, null, null, null, null, null);
                    //outgoingQueue.AddFirst(packet);
                    addPacketFirst(packet);
                }

                foreach (ClientConnection.AuthData id in conn.authInfo)
                    addPacketFirst(
                        new Packet(new RequestHeader(-4, (int) OpCode.Auth), null, new AuthPacket(0, id.Scheme, id.GetData()), null, null, null, null, null));

                addPacketFirst(new Packet(null, null, conReq, null, null, null, null, null));
                
            }
            packetAre.Set();
            if (LOG.IsDebugEnabled)
                LOG.DebugFormat("Session establishment request sent on {0}",client.Client.RemoteEndPoint);
        }
Пример #12
0
 public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration)
 {
     ReplyHeader r = new ReplyHeader();
     Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null);
     lock (p)
     {
         while (!p.Finished)
         {
             if (!Monitor.Wait(p, SessionTimeout))
                 throw new TimeoutException(string.Format("The request {0} timed out while waiting for a resposne from the server.", request));
         }
     }
     return r;
 }