prrt.pyx 2.96 KB
Newer Older
Andreas Schmidt's avatar
Andreas Schmidt committed
1
from libc.stdint cimport uint32_t, uint16_t, uint8_t, int32_t
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
cimport cprrt

cdef extern from "prrt/stores/forward_packet_table.c":
    int PrrtForwardPacketTable_create(cprrt.PrrtForwardPacketTable* fpt_prt);
    int PrrtForwardPacketTable_test_set_is_number_relevant(cprrt.PrrtForwardPacketTable *fpt_ptr, uint16_t seqno);
    int PrrtForwardPacketTable_test_is_block_relevant(cprrt.PrrtForwardPacketTable * forwardPacketTable, uint16_t start, uint16_t length);
    int PrrtForwardPacketTable_destroy(cprrt.PrrtForwardPacketTable* fpt_prt);


cdef extern from "prrt/processes/feedback_receiver.c":
    void receive_feedback_loop(void *ptr)

cdef extern from "prrt/processes/data_receiver.c":
    void receive_data_loop(void *ptr)

cdef extern from "prrt/processes/data_transmitter.c":
    void send_data_loop(void *ptr)

cdef extern from "prrt/block.c":
    cprrt.PrrtPacket *PrrtBlock_get_first_data(cprrt.PrrtBlock *block_ptr)

cdef extern from "prrt/vdmcode/block_code.c":
    int PrrtCoder_get_coder(cprrt.PrrtCoder **cod, uint8_t n, uint8_t k)

cdef extern from "prrt/coding_params.c":
    void PrrtCodingParams_init(cprrt.PrrtCodingParams *cpar)

cdef extern from "prrt/packet.c":
    uint8_t PrrtPacket_type(cprrt.PrrtPacket *packet_ptr)
    uint8_t PrrtPacket_priority(cprrt.PrrtPacket *packet_ptr)
    uint16_t PrrtPacket_size(cprrt.PrrtPacket *packet_ptr)
    int PrrtPacket_print(cprrt.PrrtPacket *packet_ptr)

cdef extern from "prrt/socket.c":
    cprrt.PrrtSocket* PrrtSocket_create(uint16_t port, uint8_t is_sender)

cdef extern from "util/bptree.c":
    cprrt.BPTreeNode *BPTree_insert(cprrt.BPTreeNode *root, int key, void *value)
    cprrt.BPTreeNode *BPTree_delete(cprrt.BPTreeNode *root, int key)
    cprrt.BPTreeNode *BPTree_destroy(cprrt.BPTreeNode *root)
    void *BPTree_get(cprrt.BPTreeNode *root, int key)
    void BPTree_get_range(cprrt.BPTreeNode *root, cprrt.List *list, int key_start, int key_end)

cdef extern from "util/list.c":
    void List_push(cprrt.List *list, const void *value)
    void *List_pop(const cprrt.List *list)

    void List_unshift(cprrt.List *list, const void *value)
    void *List_shift(const cprrt.List *list)

    void *List_remove(cprrt.List *list, const cprrt.ListNode *node)

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

Andreas Schmidt's avatar
Andreas Schmidt committed
58
59
60
    def __cinit__(self, port, isSender):
        self._c_socket = cprrt.PrrtSocket_create(port, isSender)
        self.isSender = isSender
61
62

    def recv(self):
Andreas Schmidt's avatar
Andreas Schmidt committed
63
64
65
66
67
        cdef char buffer[65536]
        cdef int32_t len
        with nogil:
            len = cprrt.PrrtSocket_recv(self._c_socket, <void*> buffer)
        return len, buffer
68
69

    def connect(self, host, port):
Andreas Schmidt's avatar
Andreas Schmidt committed
70
71
72
73
74
75
76
77
78
        cdef bytes encodedHost = host.encode("utf-8")
        cprrt.PrrtSocket_connect(self._c_socket, encodedHost, port)

    def send(self, data):
        cdef bytes encodedData = data.encode("utf-8")
        cprrt.PrrtSocket_send(self._c_socket, encodedData, len(data))

    def close(self):
        cprrt.PrrtSocket_close(self._c_socket)