def getData(self,path): requestBytes = [0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 4]; pathLen = len(path); pathLenList = list(struct.unpack("4b",struct.pack("!i",pathLen))); for pathLenByte in pathLenList: requestBytes.append(pathLenByte);#add path lenth pathByteList = list(struct.unpack(str(pathLen)+"b",struct.pack("!" + str(pathLen) + "s",path))); for pathByte in pathByteList: requestBytes.append(pathByte);#add path requestBytes.append(0);#add ifwatch bodySize = len(requestBytes) - 4; bodySizeList = list(struct.unpack("4b",struct.pack("!i",bodySize))); for i in range(4): requestBytes[i] = bodySizeList[i]; #print requestBytes; trueBytes = bytearray(requestBytes); self.zkSocket.sendall(trueBytes); responseBytes = self.zkSocket.recv(1024); byteBuffer = ByteBuffer(responseBytes); byteBuffer.getInt();#msglen byteBuffer.getInt();#xid byteBuffer.getLong();#zxid byteBuffer.getInt();#err resultValue = byteBuffer.getString(); return resultValue;
class Message: def __init__(self, msgBytes=None): if (msgBytes != None): self.msgBytes = msgBytes self.bodySize = len(msgBytes) self.byteBuffer = ByteBuffer(msgBytes) self.version = self.byteBuffer.get() self.attribute = self.byteBuffer.get() self.crc32 = self.byteBuffer.getInt() self.msgSize = self.bodySize - 6
class Message: def __init__(self,msgBytes = None): if(msgBytes != None): self.msgBytes = msgBytes; self.bodySize = len(msgBytes); self.byteBuffer = ByteBuffer(msgBytes); self.version = self.byteBuffer.get(); self.attribute = self.byteBuffer.get(); self.crc32 = self.byteBuffer.getInt(); self.msgSize = self.bodySize - 6; #6 bytes are version size + attribute size + crc32 bytes
def getData(self, path): requestBytes = [0, 0, 0, -1, 0, 0, 0, 1, 0, 0, 0, 4] pathLen = len(path) pathLenList = list(struct.unpack("4b", struct.pack("!i", pathLen))) for pathLenByte in pathLenList: requestBytes.append(pathLenByte) #add path lenth pathByteList = list( struct.unpack( str(pathLen) + "b", struct.pack("!" + str(pathLen) + "s", path))) for pathByte in pathByteList: requestBytes.append(pathByte) #add path requestBytes.append(0) #add ifwatch bodySize = len(requestBytes) - 4 bodySizeList = list(struct.unpack("4b", struct.pack("!i", bodySize))) for i in range(4): requestBytes[i] = bodySizeList[i] #print requestBytes; trueBytes = bytearray(requestBytes) self.zkSocket.sendall(trueBytes) responseBytes = self.zkSocket.recv(1024) byteBuffer = ByteBuffer(responseBytes) byteBuffer.getInt() #msglen byteBuffer.getInt() #xid byteBuffer.getLong() #zxid byteBuffer.getInt() #err resultValue = byteBuffer.getString() return resultValue
class StringMessageSet(object): """This is a message set""" def __init__(self, allMsgBytes): self.allMsgBytes = allMsgBytes self.messageSetByteBuffer = ByteBuffer(self.allMsgBytes) self.messageSet = [] def getAllStringMessages(self): while (self.messageSetByteBuffer.remaining() > 0): msgSize = self.messageSetByteBuffer.getInt() msgBytes = self.messageSetByteBuffer.getBytes(msgSize) self.messageSet.append(StringMessage(msgBytes)) return self.messageSet
class StringMessageSet(object): """This is a message set""" def __init__(self,allMsgBytes): self.allMsgBytes = allMsgBytes; self.messageSetByteBuffer = ByteBuffer(self.allMsgBytes); self.messageSet = []; def getAllStringMessages(self): while(self.messageSetByteBuffer.remaining() > 0): msgSize = self.messageSetByteBuffer.getInt(); msgBytes = self.messageSetByteBuffer.getBytes(msgSize); self.messageSet.append(StringMessage(msgBytes)); return self.messageSet;
def sendRequest(self,request): requestBytes = self.getRequestBytes(request); self.jafkaSocket.sendall(bytearray(requestBytes)); responseBytes = self.jafkaSocket.recv(64 * 1024);#It is the same as java client # print("receive responseBytes:"+str(len(responseBytes))); responseByteBuffer = ByteBuffer(responseBytes,None); msgByteSize = responseByteBuffer.getInt(); resultCode = responseByteBuffer.getShort(); if(resultCode != 0): print("Can not get message success which resultCode:"+str(resultCode)); return; while(len(responseBytes) != msgByteSize + 4):#if not receive completely we will get when read complete responseBytes = responseBytes + self.jafkaSocket.recv(64 * 1024); responseByteBuffer = ByteBuffer(responseBytes,None); responseByteBuffer.currentIndex = responseByteBuffer.currentIndex + 6;#add 6 is because the responseBytes is new and we don't need read msgByteSize and resultCode # print("receive responseBytes length:"+str(len(responseBytes))); # print "receive message size:"+str(msgByteSize); # print("resultCode:"+str(resultCode)); messageByteBuffer = responseByteBuffer.slice(); return messageByteBuffer;
def sendRequest(self, request): requestBytes = self.getRequestBytes(request) self.jafkaSocket.sendall(bytearray(requestBytes)) responseBytes = self.jafkaSocket.recv(64 * 1024) #It is the same as java client # print("receive responseBytes:"+str(len(responseBytes))); responseByteBuffer = ByteBuffer(responseBytes, None) msgByteSize = responseByteBuffer.getInt() resultCode = responseByteBuffer.getShort() if (resultCode != 0): print("Can not get message success which resultCode:" + str(resultCode)) return while (len(responseBytes) != msgByteSize + 4): #if not receive completely we will get when read complete responseBytes = responseBytes + self.jafkaSocket.recv(64 * 1024) responseByteBuffer = ByteBuffer(responseBytes, None) responseByteBuffer.currentIndex = responseByteBuffer.currentIndex + 6 #add 6 is because the responseBytes is new and we don't need read msgByteSize and resultCode # print("receive responseBytes length:"+str(len(responseBytes))); # print "receive message size:"+str(msgByteSize); # print("resultCode:"+str(resultCode)); messageByteBuffer = responseByteBuffer.slice() return messageByteBuffer