Example #1
0
    def __init__(self, badThingsQueue, stateQueue, pipe):
        self.send_buffer = TwoBuffer()
        self.recv_buffer = TwoBuffer()
        send_name = ThreadNames.TCP_SENDER
        recv_name = ThreadNames.TCP_RECEIVER
        super().__init__(
            send_name,
            TCPClass.sender,
            recv_name,
            TCPClass.receiver,
            badThingsQueue,
            stateQueue,
            pipe)

        stateQueue.put([SM_COMMANDS.SEND_ADDR, [PROCESS_NAMES.TCP_PROCESS]])
        self.dawn_ip = pipe.recv()[0]

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.sock.connect((self.dawn_ip, TCP_PORT))

        proto_message = notification_pb2.Notification()
        proto_message.header = notification_pb2.Notification.SENSOR_MAPPING
        with open('namedPeripherals.csv', 'r') as mapping_file:
            sensor_mappings = csv.reader(mapping_file)
            for row in sensor_mappings:
                pair = proto_message.sensor_mapping.add()
                pair.device_student_name = row[0]
                pair.device_uid = row[1]
        self.sock.sendall(proto_message.SerializeToString())
Example #2
0
def add_timestamps(msgqueue):
    """Add timestamp messages to ``msgqueue``."""
    for _ in range(10):
        msg = notification_pb2.Notification()
        msg.header = notification_pb2.Notification.TIMESTAMP_DOWN
        msg.timestamps.append(time.perf_counter())
        msg = msg.SerializeToString()
        msgqueue.put(msg)
    return msgqueue
Example #3
0
 def package_message(data):
     """Creates a console log notification."""
     try:
         proto_message = notification_pb2.Notification()
         proto_message.header = notification_pb2.Notification.CONSOLE_LOGGING
         proto_message.console_output = data
         return proto_message.SerializeToString()
     except Exception as e:
         bad_things_queue.put(
             BadThing(sys.exc_info(),
                      "TCP packager crashed with error: " + str(e),
                      event=BAD_EVENTS.TCP_ERROR,
                      printStackTrace=True))
Example #4
0
 def package_timestamp(timestamps):
     """Creates a timestamp notification."""
     try:
         timestamp_message = notification_pb2.Notification()
         timestamp_message.header = notification_pb2.Notification.TIMESTAMP_UP
         timestamp_message.timestamps.extend(timestamps +
                                             [time.perf_counter()])
         return timestamp_message.SerializeToString()
     except Exception as e:
         bad_things_queue.put(
             BadThing(sys.exc_info(),
                      "TCP packager crashed with error: " + str(e),
                      event=BAD_EVENTS.TCP_ERROR,
                      printStackTrace=True))
Example #5
0
 def package_confirm(confirm):
     """Creates a student code notification."""
     try:
         proto_message = notification_pb2.Notification()
         if confirm:
             proto_message.header = notification_pb2.Notification.STUDENT_RECEIVED
         else:
             proto_message.header = notification_pb2.Notification.STUDENT_NOT_RECEIVED
         return proto_message.SerializeToString()
     except Exception as e:
         bad_things_queue.put(
             BadThing(sys.exc_info(),
                      "TCP packager crashed with error: " + str(e),
                      event=BAD_EVENTS.TCP_ERROR,
                      printStackTrace=True))
Example #6
0
def tcp_relay(port, msgqueue=queue.Queue()):
    """Sends and receives messages on ``port``."""
    host = '127.0.0.1'
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind((host, port))
    sock.listen(1)
    conn, _ = sock.accept()
    while True:
        if not msgqueue.empty():
            conn.send(msgqueue.get())
        next_call = time.time()
        next_call += 1.0 / DAWN_HZ
        receive_msg, _ = conn.recvfrom(2048)
        if receive_msg is None:
            continue
        else:
            parser = notification_pb2.Notification()
            parser.ParseFromString(receive_msg)
            if parser.timestamps:
                print(parser.timestamps)
        time.sleep(max(next_call - time.time(), 0))
Example #7
0
 def unpackage(data):
     """Parse received data into a notification."""
     received_proto = notification_pb2.Notification()
     received_proto.ParseFromString(data)
     return received_proto