prrt.pyx 2.4 KB
Newer Older
Andreas Schmidt's avatar
Andreas Schmidt committed
1
from libc.stdint cimport uint32_t, uint16_t, uint8_t, int32_t
Andreas Schmidt's avatar
Andreas Schmidt committed
2 3
from libc.stdlib cimport malloc, free

4 5
cimport cprrt

6
cdef extern from "proto/applicationConstraints.c":
7 8
    pass

9
cdef extern from "proto/stores/dataPacketStore.c":
10 11
    pass

12
cdef extern from "proto/stores/forwardPacketTable.c":
13
    pass
14

15
cdef extern from "proto/stores/lossGatherer.c":
16 17
    pass

18
cdef extern from "proto/stores/packetTimeoutTable.c":
19 20
    pass

21
cdef extern from "proto/stores/repairBlockStore.c":
22 23
    pass

24
cdef extern from "proto/processes/feedbackReceiver.c":
25
    pass
26

27
cdef extern from "proto/processes/dataReceiver.c":
28
    pass
29

30
cdef extern from "proto/processes/dataTransmitter.c":
31
    pass
32

33
cdef extern from "proto/block.c":
34
    pass
35

36
cdef extern from "proto/clock.c":
37
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
38

39
cdef extern from "proto/channelStateInformation.c":
40
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
41

42
cdef extern from "proto/vdmcode/block_code.c":
43
    pass
44

45
cdef extern from "proto/codingParams.c":
46
    pass
47

48
cdef extern from "proto/receiver.c":
49
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
50

51
cdef extern from "proto/packet.c":
52
    pass
53

54
cdef extern from "proto/socket.c":
55
    pass
56 57

cdef extern from "util/bptree.c":
58
    pass
59

60 61
cdef extern from "util/bitmap.c":
    pass
62

63 64
cdef extern from "util/list.c":
    pass
65 66 67

cdef class PrrtSocket:
    cdef cprrt.PrrtSocket* _c_socket
Andreas Schmidt's avatar
Andreas Schmidt committed
68
    cdef bint isSender
69

Andreas Schmidt's avatar
Andreas Schmidt committed
70
    def __cinit__(self, port, isSender):
Andreas Schmidt's avatar
Andreas Schmidt committed
71 72
        self._c_socket = cprrt.PrrtSocket_create(isSender)
        cprrt.PrrtSocket_bind(self._c_socket, "0.0.0.0", port)
Andreas Schmidt's avatar
Andreas Schmidt committed
73

Andreas Schmidt's avatar
Andreas Schmidt committed
74
        self.isSender = isSender
75

76
    property target_delay:
77

78 79 80 81 82 83 84 85 86
        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
            return cprrt.PrrtSocket_get_sock_opt(self._c_socket, "targetdelay")

        def __set__(self, value):
            if not self.isSender:
                raise Exception("Not a sender.")
            cprrt.PrrtSocket_set_sock_opt(self._c_socket, "targetdelay", value)
87

88
    def recv(self):
Andreas Schmidt's avatar
Andreas Schmidt committed
89 90 91 92
        cdef char buffer[65536]
        cdef int32_t len
        with nogil:
            len = cprrt.PrrtSocket_recv(self._c_socket, <void*> buffer)
93
        return buffer[:len]
94 95

    def connect(self, host, port):
Andreas Schmidt's avatar
Andreas Schmidt committed
96 97 98 99
        cdef bytes encodedHost = host.encode("utf-8")
        cprrt.PrrtSocket_connect(self._c_socket, encodedHost, port)

    def send(self, data):
100
        cprrt.PrrtSocket_send(self._c_socket, data, len(data))
Andreas Schmidt's avatar
Andreas Schmidt committed
101

102 103 104
    def __dealloc__(self):
        if self._c_socket != NULL:
            cprrt.PrrtSocket_close(self._c_socket)