Skip to content

Commit

Permalink
Fixed (some) python errors and in c++ receiving on UDP_HEADER from th…
Browse files Browse the repository at this point in the history
…e buffer should also go the same post-processing steps as when receiving from socket
  • Loading branch information
AndreiCostinescu committed Apr 23, 2021
1 parent bc6e3f2 commit 64a2fcd
Show file tree
Hide file tree
Showing 18 changed files with 628 additions and 88 deletions.
Binary file added data/Lena.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 12 additions & 7 deletions python/comm/communication/Communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def sendData(self, socketType: SocketType, data: CommunicationData, withMessageT
while not serializeDone:
serializeDone = data.serialize(self.sendBuffer, verbose)
self.errorCode = 0
if not self.send(type, retries, verbose):
if not self.send(socketType, retries, verbose):
if self.errorCode == 0:
if verbose:
print("Socket closed: Can not send data serialized bytes...")
Expand All @@ -113,7 +113,7 @@ def recvMessageType(self, socketType: SocketType, retries: int = 0, verbose: boo
if self.errorCode < 1:
return True, MessageType.NOTHING
return False, MessageType.NOTHING
return True, MessageType(self.recvBuffer.getChar(0))
return True, MessageType.intToMessageType(self.recvBuffer.getChar(0))

def recvData(self, socketType: SocketType, data: CommunicationData, retries: int = 0, verbose: bool = False) \
-> Tuple[bool, CommunicationData]:
Expand All @@ -136,21 +136,26 @@ def recvData(self, socketType: SocketType, data: CommunicationData, retries: int
if verbose:
print("In Communication::recvData: dataLocalDeserializeBuffer =", dataLocalDeserializeBuffer,
"expectedSize =", expectedSize, " deserializeState =", deserializeState)
if not dataLocalDeserializeBuffer:
if dataLocalDeserializeBuffer is not None:
receiveResult = self.recv((socketType, dataLocalDeserializeBuffer, expectedSize, expectedSize), retries,
verbose)
assert isinstance(receiveResult, tuple)
recvSuccess = receiveResult[0]
buffer = Buffer()
buffer.setData(receiveResult[1], receiveResult[2])
if verbose:
print("ReceiveResult = ", receiveResult)
else:
self.recvBuffer.setBufferContentSize(expectedSize)
receiveResult = self.recv(socketType, retries, verbose)
assert isinstance(receiveResult, bool)
"""
print("Expecting " + str(self.recvBuffer.getBufferContentSize()) + "bytes... (expectedSize = " +
str(expectedSize) + ")")
# """
recvSuccess = self.recv(socketType, retries, verbose)
assert isinstance(recvSuccess, bool)
buffer = self.recvBuffer

if not receiveResult[0]:
if not recvSuccess:
if self.errorCode >= 0:
print("Stop loop: Can not recv data serialized bytes... error", self.errorCode,
"deserializeState =", deserializeState)
Expand All @@ -162,7 +167,7 @@ def recvData(self, socketType: SocketType, data: CommunicationData, retries: int
data.resetDeserializeState()
return False, data

assert (receiveResult or self.errorCode == -1)
assert (recvSuccess or self.errorCode == -1)
# if we received something...
if self.errorCode != -1:
if verbose:
Expand Down
11 changes: 11 additions & 0 deletions python/comm/data/CommunicatorState.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ class CommunicatorState(enum.Enum):
COMMUNICATOR_ACTIVE = 1,
COMMUNICATOR_DONE = 2,

@staticmethod
def intToMessageType(i: int):
if i == 0:
return CommunicatorState.COMMUNICATOR_IDLE
elif i == 1:
return CommunicatorState.COMMUNICATOR_ACTIVE
elif i == 2:
return CommunicatorState.COMMUNICATOR_DONE
else:
raise RuntimeError("Unknown CommunicatorState " + str(i))

@staticmethod
def stringToCommunicatorMethod(s: str):
if s == "idle":
Expand Down
3 changes: 2 additions & 1 deletion python/comm/data/ImageData.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def imageCVType(image: np.ndarray):

@staticmethod
def imageDim(imageType: int) -> int:
return imageType >> 3
return (imageType >> 3) + 1

headerSize = 20

Expand Down Expand Up @@ -122,6 +122,7 @@ def deserialize(self, buffer: Buffer, start: int, verbose: bool) -> bool:
self.imageWidth = buffer.getInt(start + 8)
self.imageType = buffer.getInt(start + 12)
self.contentSize = buffer.getInt(start + 16)
# print("Deserializing image:", self.imageHeight, self.imageWidth, self.imageType, self.contentSize)
self.deserializeState = 1
return False
elif self.deserializeState == 1:
Expand Down
13 changes: 13 additions & 0 deletions python/comm/data/MessageType.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ class MessageType(enum.Enum):
IMAGE = 2,
COORDINATE = 3,

@staticmethod
def intToMessageType(i: int):
if i == 0:
return MessageType.NOTHING
elif i == 1:
return MessageType.STATUS
elif i == 2:
return MessageType.IMAGE
elif i == 3:
return MessageType.COORDINATE
else:
raise RuntimeError("Unknown MessageType " + str(i))

@staticmethod
def stringToMessageType(s: str):
if s == "nothing":
Expand Down
44 changes: 23 additions & 21 deletions python/comm/data/StatusData.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from python.comm.data.MessageType import MessageType
from python.comm.data.Messages import Messages
from python.comm.socket.Buffer import Buffer
from python.comm.socket.utils import prepareBuffer, strcmp, memcpy
from python.comm.socket.utils import prepareBuffer, strcmp, memcpy, strToCStr
from typing import Optional


class StatusData(CommunicationData):
headerSize = 4
statusDataType = MessageType.STATUS
statusDataType = MessageType.STATUS.value[0]

def __init__(self, value: str or int = None):
super().__init__()
Expand All @@ -24,11 +24,13 @@ def __init__(self, value: str or int = None):
self.setData(value)

def getMessageType(self):
return MessageType(self.getDataType())
# print("Status Data messageType:", self.getDataType())
return MessageType.intToMessageType(self.getDataType())

def serialize(self, buffer: Buffer, verbose: bool) -> bool:
if self.serializeState == 0:
buffer.setBufferContentSize(StatusData.headerSize)
# print("This dataSize = " + str(self.dataSize))
buffer.setInt(self.dataSize, 0)
if verbose:
dataBuffer = buffer.getBuffer()
Expand Down Expand Up @@ -70,7 +72,7 @@ def deserialize(self, buffer: Buffer, start: int, verbose: bool) -> bool:

def reset(self):
self.dataSize = -1
self.dataType = int(MessageType.NOTHING.value[0]) # .value returns a tuple
self.dataType = MessageType.NOTHING.value[0] # .value returns a tuple

def setCommand(self, command: str):
if command == "_reset":
Expand All @@ -79,47 +81,47 @@ def setCommand(self, command: str):
elif command == "ping":
commandData = Messages.PING_MESSAGE
self.dataSize = Messages.PING_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "quit":
commandData = Messages.QUIT_MESSAGE
self.dataSize = Messages.QUIT_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "start":
commandData = Messages.START_MESSAGE
self.dataSize = Messages.START_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "stop":
commandData = Messages.STOP_MESSAGE
self.dataSize = Messages.STOP_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "wait":
commandData = Messages.WAIT_MESSAGE
self.dataSize = Messages.WAIT_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "accept":
commandData = Messages.ACCEPT_MESSAGE
self.dataSize = Messages.ACCEPT_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "ready":
commandData = Messages.READY_MESSAGE
self.dataSize = Messages.READY_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "control":
commandData = Messages.CONTROL_MESSAGE
self.dataSize = Messages.CONTROL_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "upload":
commandData = Messages.UPLOAD_MESSAGE
self.dataSize = Messages.UPLOAD_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "select":
commandData = Messages.SELECT_MESSAGE
self.dataSize = Messages.SELECT_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif command == "reject":
commandData = Messages.REJECT_MESSAGE
self.dataSize = Messages.REJECT_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
else:
raise RuntimeError("Unknown command: " + command)

Expand All @@ -135,15 +137,15 @@ def setStatus(self, status: str):
elif status == "idle":
statusData = Messages.IDLE_MESSAGE
self.dataSize = Messages.IDLE_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif status == "active":
statusData = Messages.ACTIVE_MESSAGE
self.dataSize = Messages.ACTIVE_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
elif status == "done":
statusData = Messages.DONE_MESSAGE
self.dataSize = Messages.DONE_MESSAGE_LENGTH
self.dataType = MessageType.STATUS
self.dataType = MessageType.STATUS.value[0]
else:
raise RuntimeError("Unknown status: " + status)

Expand All @@ -158,11 +160,12 @@ def setData(self, _data: str or bytes, _dataSize: int = -1):

if isinstance(_data, str):
_data = bytes(_data, "ascii")
if not _data.endswith("\x00"):
if not _data.endswith(b"\x00"):
_data += b"\x00"
if _dataSize == -1:
_dataSize = len(_data)

# print("Setting status data with:", _data, _dataSize)
self.data, self.dataLength = prepareBuffer(self.data, self.dataLength, _dataSize)
self.data = memcpy(self.data, 0, _data, 0, _dataSize)
self.dataSize = _dataSize
Expand All @@ -171,11 +174,10 @@ def setData(self, _data: str or bytes, _dataSize: int = -1):
def getData(self) -> Optional[bytes]:
if self.dataSize < 0:
return None
return self.data
return strToCStr(self.data)

def getDataSize(self) -> int:
return self.dataSize

def getDataType(self) -> int:
return self.dataType

2 changes: 1 addition & 1 deletion python/comm/socket/Buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def setShort(self, data: int, position: int):

def setInt(self, data: int, position: int):
self.checkBufferContentSize(position + 4, True)
intToNetworkBytes(self.buffer, position, data)
self.buffer = intToNetworkBytes(self.buffer, position, data)
self.useReferenceBuffer = False

def setLongLong(self, data: int, position: int):
Expand Down
Loading

0 comments on commit 64a2fcd

Please sign in to comment.