prrt.pyx 3.65 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 25 26
cdef extern from "proto/stores/receiveDataQueue.c":
    pass

Andreas Schmidt's avatar
Andreas Schmidt committed
27 28 29
cdef extern from "proto/types/packetTimeout.c":
    pass

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

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

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

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

42
cdef extern from "proto/clock.c":
43
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
44

45
cdef extern from "proto/channelStateInformation.c":
46
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
47

48
cdef extern from "proto/vdmcode/block_code.c":
49
    pass
50

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

54
cdef extern from "proto/receiver.c":
55
    pass
Andreas Schmidt's avatar
Andreas Schmidt committed
56

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

60
cdef extern from "proto/socket.c":
61
    pass
62 63

cdef extern from "util/bptree.c":
64
    pass
65

66 67
cdef extern from "util/bitmap.c":
    pass
68

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

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

Andreas Schmidt's avatar
Andreas Schmidt committed
75 76 77
cdef extern from "util/pipe.c":
    pass

78 79
cdef class PrrtSocket:
    cdef cprrt.PrrtSocket* _c_socket
Andreas Schmidt's avatar
Andreas Schmidt committed
80
    cdef bint isSender
81

82
    def __cinit__(self, port, isSender, target_delay = 1, thread_pinning = False):
83 84
        target_delay_us = target_delay * 1000**2
        self._c_socket = cprrt.PrrtSocket_create(isSender, target_delay_us)
85 86
        if thread_pinning:
            cprrt.PrrtSocket_enable_thread_pinning(self._c_socket)
Andreas Schmidt's avatar
Andreas Schmidt committed
87
        cprrt.PrrtSocket_bind(self._c_socket, "0.0.0.0", port)
Andreas Schmidt's avatar
Andreas Schmidt committed
88

Andreas Schmidt's avatar
Andreas Schmidt committed
89
        self.isSender = isSender
90

91 92 93 94 95
    property thread_pinning:

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

96
    property target_delay:
97

98 99 100
        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
101
            return cprrt.PrrtSocket_get_sock_opt(self._c_socket, "targetdelay") * 0.000001
102

103 104 105 106 107 108 109 110 111 112 113
    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)

114
    property rtt:
115 116 117 118
        def __get__(self):
            if not self.isSender:
                raise Exception("Not a sender.")
            return cprrt.PrrtSocket_get_rtt(self._c_socket) * 0.000001
119

Andreas Schmidt's avatar
Andreas Schmidt committed
120
    def recv(self):
Andreas Schmidt's avatar
Andreas Schmidt committed
121 122 123 124
        cdef char buffer[65536]
        cdef int32_t len
        with nogil:
            len = cprrt.PrrtSocket_recv(self._c_socket, <void*> buffer)
125
        return buffer[:len]
126

127 128 129 130 131 132 133 134
    def recv_sync(self, granularity):
        cdef char buffer[65536]
        cdef int32_t len
        cdef uint32_t time_window_us = granularity * (1000**2)
        with nogil:
            len = cprrt.PrrtSocket_recv_sync(self._c_socket, <void*> buffer, time_window_us)
        return buffer[:len]

135
    def connect(self, host, port):
Andreas Schmidt's avatar
Andreas Schmidt committed
136 137 138 139
        cdef bytes encodedHost = host.encode("utf-8")
        cprrt.PrrtSocket_connect(self._c_socket, encodedHost, port)

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

142 143 144
    def __dealloc__(self):
        if self._c_socket != NULL:
            cprrt.PrrtSocket_close(self._c_socket)