prrt.pyx 5.24 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/deliveredPacketTable.c":
13
    pass
14

Andreas Schmidt's avatar
Andreas Schmidt committed
15 16 17 18
cdef extern from "proto/stores/receptionTable.c":
    pass

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

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

24
cdef extern from "proto/stores/repairBlockStore.c":
25 26
    pass

27
cdef extern from "proto/stores/packetDeliveryStore.c":
28 29
    pass

Andreas Schmidt's avatar
Andreas Schmidt committed
30 31 32
cdef extern from "proto/types/packetTimeout.c":
    pass

Andreas Schmidt's avatar
Andreas Schmidt committed
33 34 35
cdef extern from "proto/types/lossStatistics.c":
    pass

36
cdef extern from "proto/processes/feedbackReceiver.c":
37
    pass
38

39
cdef extern from "proto/processes/dataReceiver.c":
40
    pass
41

42
cdef extern from "proto/processes/dataTransmitter.c":
43
    pass
44

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

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

51
cdef extern from "proto/channelStateInformation.c":
52
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
53

54
cdef extern from "proto/vdmcode/block_code.c":
55
    pass
56

57
cdef extern from "proto/codingParams.c":
58
    pass
59

60
cdef extern from "proto/receiver.c":
61
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
62

63
cdef extern from "proto/packet.c":
64
    pass
65

66
cdef extern from "proto/socket.c":
67
    pass
68 69

cdef extern from "util/bptree.c":
70
    pass
71

72 73
cdef extern from "util/bitmap.c":
    pass
74

75 76 77
cdef extern from "util/common.c":
    pass

78 79
cdef extern from "util/list.c":
    pass
80

Andreas Schmidt's avatar
Andreas Schmidt committed
81 82 83
cdef extern from "util/pipe.c":
    pass

Andreas Schmidt's avatar
Andreas Schmidt committed
84 85 86
cdef extern from "util/mpsc_queue.c":
    pass

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
cdef class PrrtCodingConfiguration:
    cdef cprrt.PrrtCodingParams* _c_config

    def __cinit__(self):
        self._c_config = cprrt.PrrtCodingParams_create()

    def __dealloc__(self):
        if self._c_config != NULL:
            cprrt.PrrtCodingParams_destroy(self._c_config)

    cdef copy(self, cprrt.PrrtCodingParams* other):
        cprrt.PrrtCodingParams_destroy(self._c_config)
        self._c_config = other

    property k:
        def __get__(self):
            return int(self._c_config.k)
    property n:
        def __get__(self):
            return int(self._c_config.n)
    property r:
        def __get__(self):
            return int(self._c_config.r)
    property c:
        def __get__(self):
            return int(self._c_config.c)
    property n_cycle:
        def __get__(self):
            return list(<uint8_t[:self._c_config.c]> self._c_config.n_cycle)

117 118
cdef class PrrtSocket:
    cdef cprrt.PrrtSocket* _c_socket
Andreas Schmidt's avatar
Andreas Schmidt committed
119
    cdef bint isSender
120

121
    def __cinit__(self, port, isSender, target_delay = 1, thread_pinning = False):
122 123
        target_delay_us = target_delay * 1000**2
        self._c_socket = cprrt.PrrtSocket_create(isSender, target_delay_us)
124 125
        if thread_pinning:
            cprrt.PrrtSocket_enable_thread_pinning(self._c_socket)
Andreas Schmidt's avatar
Andreas Schmidt committed
126
        cprrt.PrrtSocket_bind(self._c_socket, "0.0.0.0", port)
Andreas Schmidt's avatar
Andreas Schmidt committed
127

Andreas Schmidt's avatar
Andreas Schmidt committed
128
        self.isSender = isSender
129

Andreas Schmidt's avatar
Andreas Schmidt committed
130 131 132 133 134 135 136
    property plr:

        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
            return cprrt.PrrtSocket_get_plr(self._c_socket)

137 138 139 140 141
    property thread_pinning:

        def __get__(self):
            return cprrt.PrrtSocket_uses_thread_pinning(self._c_socket)

142
    property target_delay:
143

144 145 146
        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
147
            return cprrt.PrrtSocket_get_sock_opt(self._c_socket, "targetdelay") * 0.000001
148

149 150 151 152 153 154 155 156 157 158 159
    property app_queue_size:
        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
            return cprrt.PrrtSocket_get_sock_opt(self._c_socket, "app_queue_size")

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

160
    property rtt:
161 162 163
        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
164
            return cprrt.PrrtSocket_get_rtprop(self._c_socket) * 0.000001
165

166 167 168 169 170 171 172 173 174 175
    property coding_configuration:

        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
            res = PrrtCodingConfiguration()
            cdef cprrt.PrrtCodingParams *params = cprrt.PrrtSocket_get_coding_parameters(self._c_socket)
            res.copy(params)
            return res

Andreas Schmidt's avatar
Andreas Schmidt committed
176
    def recv(self):
Andreas Schmidt's avatar
Andreas Schmidt committed
177 178 179 180
        cdef char buffer[65536]
        cdef int32_t len
        with nogil:
            len = cprrt.PrrtSocket_recv(self._c_socket, <void*> buffer)
181
        return buffer[:len]
182

183
    def receive_ordered_wait(self, time_window):
184 185
        cdef char buffer[65536]
        cdef int32_t len
186
        cdef uint32_t time_window_us = time_window * (1000**2)
187
        with nogil:
188
            len = cprrt.PrrtSocket_receive_ordered_wait(self._c_socket, <void*> buffer, time_window_us)
189 190
        return buffer[:len]

191
    def connect(self, host, port):
Andreas Schmidt's avatar
Andreas Schmidt committed
192 193 194 195
        cdef bytes encodedHost = host.encode("utf-8")
        cprrt.PrrtSocket_connect(self._c_socket, encodedHost, port)

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

198 199 200
    def __dealloc__(self):
        if self._c_socket != NULL:
            cprrt.PrrtSocket_close(self._c_socket)