/** * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent */ internal WatchedEvent(WatcherEvent eventMessage) { keeperState = EnumUtil <Watcher.Event.KeeperState> .DefinedCast(eventMessage.getState()); eventType = EnumUtil <Watcher.Event.EventType> .DefinedCast(eventMessage.get_Type()); path = eventMessage.getPath(); }
public void testConvertingToEventWrapper() { WatchedEvent we = new WatchedEvent(Watcher.Event.EventType.NodeCreated, Watcher.Event.KeeperState.Expired, "blah"); WatcherEvent wew = we.getWrapper(); Assert.assertEquals((int)Watcher.Event.EventType.NodeCreated, wew.get_Type()); Assert.assertEquals((int)Watcher.Event.KeeperState.Expired, wew.getState()); Assert.assertEquals("blah", wew.getPath()); }
internal void readResponse(ByteBuffer incomingBuffer) { BigEndianBinaryReader bbis = new BigEndianBinaryReader(incomingBuffer.Stream); BinaryInputArchive bbia = BinaryInputArchive.getArchive(bbis); ReplyHeader replyHdr = new ReplyHeader(); ((Record)replyHdr).deserialize(bbia, "header"); if (replyHdr.getXid() == -2) { // -2 is the xid for pings if (LOG.isDebugEnabled()) { LOG.debug("Got ping response for sessionid: 0x" + sessionId.ToHexString() + " after " + ((TimeHelper.ElapsedNanoseconds - lastPingSentNs) / 1000000) + "ms"); } return; } if (replyHdr.getXid() == -4) { // -4 is the xid for AuthPacket if (replyHdr.getErr() == (int)KeeperException.Code.AUTHFAILED) { state.Value = ZooKeeper.States.AUTH_FAILED; queueEvent(new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.AuthFailed, null)); } if (LOG.isDebugEnabled()) { LOG.debug("Got auth sessionid:0x" + sessionId.ToHexString()); } return; } if (replyHdr.getXid() == -1) { // -1 means notification if (LOG.isDebugEnabled()) { LOG.debug("Got notification sessionid:0x" + sessionId.ToHexString()); } WatcherEvent @event = new WatcherEvent(); ((Record)@event).deserialize(bbia, "response"); // convert from a server path to a client path if (chrootPath != null) { string serverPath = @event.getPath(); if (serverPath == chrootPath) { @event.setPath("/"); } else if (serverPath.Length > chrootPath.Length) { @event.setPath(serverPath.Substring(chrootPath.Length)); } else { LOG.warn("Got server path " + @event.getPath() + " which is too short for chroot path " + chrootPath); } } WatchedEvent we = new WatchedEvent(@event); if (LOG.isDebugEnabled()) { LOG.debug("Got " + we + " for sessionid 0x" + sessionId.ToHexString()); } queueEvent(we); return; } Packet packet; lock (pendingQueue) { if (pendingQueue.size() == 0) { throw new IOException("Nothing in the queue, but got " + replyHdr.getXid()); } packet = pendingQueue.First.Value; pendingQueue.RemoveFirst(); } /* * Since requests are processed in order, we better get a response * to the first request! */ try { if (packet.requestHeader.getXid() != replyHdr.getXid()) { packet.replyHeader.setErr((int)KeeperException.Code.CONNECTIONLOSS); throw new IOException("Xid out of order. Got Xid " + replyHdr.getXid() + " with err " + +replyHdr.getErr() + " expected Xid " + packet.requestHeader.getXid() + " for a packet with details: " + packet); } packet.replyHeader.setXid(replyHdr.getXid()); packet.replyHeader.setErr(replyHdr.getErr()); packet.replyHeader.setZxid(replyHdr.getZxid()); if (replyHdr.getZxid() > 0) { lastZxid.Value = replyHdr.getZxid(); } if (packet.response != null && replyHdr.getErr() == 0) { packet.response.deserialize(bbia, "response"); } if (LOG.isDebugEnabled()) { LOG.debug("Reading reply sessionid:0x" + sessionId.ToHexString() + ", packet:: " + packet); } } finally { finishPacket(packet); } }