]> rtime.felk.cvut.cz Git - frescor/fna.git/blob - src_frescan/frescan_acceptor_threads.c
01153218ce135570637ba203210c0515f0e74486
[frescor/fna.git] / src_frescan / frescan_acceptor_threads.c
1 /*!
2  * @file frescan_acceptor_threads.c
3  *
4  * @brief FRESCAN acceptor threads
5  *
6  * This module contains the acceptor threads, with an operation to create them.
7  *
8  * @version 0.01
9  *
10  * @date 2-Apr-2008
11  *
12  * @author Daniel Sangorrin <daniel.sangorrin@unican.es>
13  *
14  */
15
16 #include <assert.h>
17 #include "fosa_threads_and_signals.h" // fosa_thread_attr_init...
18 #include "frescan_acceptor_threads.h"
19 #include "frescan_config.h"
20 #include "frescan_debug.h"
21 #include "frescan_data.h"
22 #include "frescan_negotiation_messages.h"
23
24 static void *frescan_acceptor_thread(void *arg);
25
26 /**
27  * frescan_acceptor_thread_create()
28  */
29
30 int frescan_acceptor_thread_create(frescan_network_t net)
31 {
32         int ret;
33         fosa_thread_attr_t attr;
34
35         ret = fosa_thread_attr_init(&attr);
36         if (ret != 0) {
37                 ERROR("could not init thread attributes\n");
38                 return ret;
39         }
40
41         ret = fosa_thread_attr_set_prio(&attr, FRESCAN_ACCEPTOR_THREAD_PRIO);
42         if (ret != 0) {
43                 ERROR("could not set acceptor thread prio %d\n",
44                       FRESCAN_ACCEPTOR_THREAD_PRIO);
45                 return ret;
46         }
47
48         ret = fosa_thread_create(&the_networks[net].neg_thread_id,
49                                   &attr,
50                                   frescan_acceptor_thread,
51                                   (void *)(uint32_t)net);
52
53         if (ret != 0) {
54                 ERROR("could not create the negotiator thread\n");
55                 return ret;
56         }
57
58         ret = fosa_thread_attr_destroy(&attr);
59         if (ret != 0) {
60                 ERROR("could not destroy thread attributes\n");
61                 return ret;
62         }
63
64         return 0;
65 }
66
67 /**
68  * frescan_acceptor_thread()
69  *
70  * a loop waiting for negotiation requests on the network. When it receives
71  * a request it acts like a normal thread requesting a negotiation to the
72  * negotiator thread. Once it gets the results it sends them back to the
73  * node that asked the negotiation.
74  */
75
76 static void *frescan_acceptor_thread(void *arg)
77 {
78         int ret;
79         frescan_recv_params_t params;
80         uint8_t msg[200];
81         size_t recv_bytes;
82         frescan_node_t from;
83         frescan_prio_t prio;
84
85         DEBUG(FRESCAN_ACCEPTOR_THREAD_ENABLE_DEBUG,
86               "master acceptor thread starts\n");
87
88         params.net = (frescan_network_t)(uint32_t)arg;
89         params.channel = FRESCAN_NEG_CHANNEL;
90         params.flags = FRESCAN_SYNC;
91
92         while(1) {
93                 DEBUG(FRESCAN_ACCEPTOR_THREAD_ENABLE_DEBUG,
94                       "waiting for msg, net:%u chan:%u flags:%u\n",
95                       params.net, params.channel, params.flags);
96
97                 ret = frescan_recv(&params, msg, 200,
98                                    &recv_bytes, &from, &prio);
99                 assert(ret == 0);
100
101                 DEBUG(FRESCAN_ACCEPTOR_THREAD_ENABLE_DEBUG,
102                       "msg received, from:%u size:%u prio:%u\n",
103                       from, recv_bytes, prio);
104
105                 ret = frescan_message_parse(params.net, msg, recv_bytes, from);
106                 assert(ret == 0);
107         }
108
109         return NULL;
110 }