summaryrefslogtreecommitdiff
path: root/cesar/maximus/sci/src/SciServer.cpp
blob: 051956ab1f9bf9bd602d066b79b32a4c8227ce81 (plain)
1
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Maximus project {{{
 *
 * Copyright (C) 2012 MStar Semiconductor
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    maximus/sci/inc/SciServer.cpp
 * \ingroup maximus_sci
 *
 */
#include "maximus/sci/inc/SciServer.h"
#include "maximus/sci/inc/SciMsg.h"
#include "maximus/processors/inc/Processor.h"
#include "maximus/interface/station/inc/Station.h"
#include "maximus/utils/inc/Error.h"
#include "maximus/utils/inc/Logger.h"
#include <assert.h>
#include <unistd.h>
#include <fstream>
#include <sys/select.h>
#include <netinet/in.h>
#include <iostream>
#include <cstring>

SciServer::SciServer (stations_list_t &ref1):
    sta_list (ref1),
    mNetworkClockTick (NULL),
    is_server_ready (false)

{
    logFunction ();

    for (unsigned int i = 0; i < SCI_MSG_TYPE_NB; i ++)
        processor_array[i] = NULL;
}

SciServer::~SciServer ()
{
    logFunction ();
}

void
SciServer::process () const
{
    logFunction ();

    /* polling mode. */
    struct timeval timeout;
    timeout.tv_sec = 0;
    timeout.tv_usec = 0;

    fd_set read_fds;
    FD_ZERO (&read_fds);
    int fd_max, fd_min, fd_index;
    bool first_fd = true;
    stations_list_t::const_iterator it;

    assert (is_server_ready);

    for (it = sta_list.begin () ;
         it != sta_list.end ();
         it ++)
    {
        Station *station = *it;

        FD_SET (station->getInputFileDescriptor (), &read_fds);
        if (first_fd || (fd_min > station->getInputFileDescriptor ()))
            fd_min = station->getInputFileDescriptor ();

        if (first_fd || (fd_max < station->getInputFileDescriptor ()))
            fd_max = station->getInputFileDescriptor ();

        first_fd = false;
    }

    while (select (fd_max + 1, &read_fds, NULL, NULL, &timeout) > 0)
    {
        for (fd_index = fd_min; fd_index <= fd_max; fd_index++)
        {
            if (!FD_ISSET (fd_index, &read_fds))
                continue;

            const static int header_size = sizeof (struct Sci_Msg_Header);
            struct Sci_Msg_Header sci_header;
            size_t data_length_received;
            int status;

            status = read (fd_index, &sci_header, header_size);
            assert (status == header_size);

            /* Be careful, the check is done before ntoh transformation. */
            assert (!memcmp (&(sci_header.magic_id), SCI_MSG_MAGIC, 4));
            assert (sci_header.type != SCI_MSG_TYPE_NONE);
            assert (sci_header.type < SCI_MSG_TYPE_NB);
            assert (processor_array[sci_header.type]);
            assert (sci_header.version == SCI_MSG_VERSION);

            data_length_received = ntohl (sci_header.length);

            unsigned char *msg_alloc =
                new unsigned char [data_length_received + header_size];
            unsigned char *buffer_data = msg_alloc + header_size;

            status = read (fd_index, buffer_data, data_length_received);
            assert (status == (int) data_length_received);

            Sci_Msg_Type t = static_cast <Sci_Msg_Type> (sci_header.type);
            SciMsg *msg = NULL;
            msg = processor_array[t]->create_sci_msg (
                sci_header, msg_alloc);

            maximus_log (LOG_INFO, "---> SciMsg received");
            msg->display_sci_header ();
            msg->display_specialized_header();

            processor_array[t]->dispatchMsgProc (msg);
            break;
        }
    }

    /* Read log pipes. */
    log ();
}

void
SciServer::log () const
{
#if CONFIG_LOG
    logFunction ();

    std::ofstream stationLogFile;
    if ((!mStationLog.empty ()) && (mStationLog.compare ("-")))
    {
        stationLogFile.open (mStationLog.c_str (),
                             std::ios_base::app
                             | std::ios_base::ate
                             | std::ios_base::out);
    }

    int length;
    int maxLength = 1024;
    char pBuffer[maxLength];

    stations_list_t::const_iterator it;
    for (it = sta_list.begin ();
         it != sta_list.end ();
         ++ it)
    {
        assert (*it);
        /* Read logs coming from all stations (from log pipes). */
        length = read ((*it)->getLogFileDescriptor (),
                       pBuffer, maxLength);
        assert (length >= 0);

        if (stationLogFile.is_open ())
            stationLogFile.write (pBuffer, length);
        else
            std::cout.write (pBuffer, length);
    }

    stationLogFile.close ();
#endif
}

inline void
SciServer::send_SciMsg_to_Station (
    const SciMsg &sci_msg,
    Station &dsta) const
{
    logFunction ();
    const Station_Status dsta_status = dsta.getStationStatus ();

    /* Station status refused are :
     * - MAXIMUS_STATION_STATUS_NONE
     * - MAXIMUS_STATION_STATUS_DEACTIVATED */
    if (dsta_status != MAXIMUS_STATION_STATUS_IDLE
        && dsta_status != MAXIMUS_STATION_STATUS_BUSY)
    {
        maximus_log (LOG_ERROR,
                     "|--- did not send "
                     << " SCI message to station "
                     << std::dec << dsta.getStationId () << " (0x"
                     << std::hex << dsta.getStationId () << ")");
        sci_msg.display_sci_header ();
        sci_msg.display_specialized_header ();
        return;
    }

    const size_t msg_size = sci_msg.get_sci_alloc_size ();
    const unsigned char *p_data = sci_msg.get_sci_alloc_addr ();

    /* Pipe is declared as blocking. So we can send totally the sci message
     * without loop implementation around the write function. */
    int status = write (dsta.getOutputFileDescriptor (),
                        p_data, msg_size);
    assert (status == (int) msg_size);

    dsta.setStationStatus (MAXIMUS_STATION_STATUS_BUSY);
    dsta.incrementStationIdleCounter ();

    maximus_log (LOG_COM,
                 "<--- send "
                 << " SCI message to station "
                 << std::dec << dsta.getStationId () << " (0x"
                 << std::hex << dsta.getStationId () << ")");
    sci_msg.display_sci_header ();
    sci_msg.display_specialized_header ();
}

void
SciServer::sendSciMsg (
    const SciMsg &sci_msg_to_send,
    const Sci_Msg_Station_Id station_id) const
{
    logFunction ();

    assert (is_server_ready);

    stations_list_t::const_iterator it;
    Station *psta = NULL;

    for (it = sta_list.begin ();
         psta == NULL && it != sta_list.end (); ++it)
    {
        if ((*it)
            && (station_id == (*it)->getStationId ()))
            psta = *it;
    }

    assert (psta);
    send_SciMsg_to_Station (sci_msg_to_send, *psta);
}

bool
SciServer::sendSciMsgToAllActiveStations (
    SciMsg &sci_msg_to_send) const
{
    logFunction ();

    assert (is_server_ready);

    /* Get id of the transmitting station. */
    const Sci_Msg_Station_Id txStationId =
        sci_msg_to_send.get_sci_stationid ();
    bool bSend = false;
    stations_list_t::const_iterator it;

    for (it = sta_list.begin ();
         it != sta_list.end (); ++it)
    {
        Station *psta = *it;

        /* ... except the sender ... and hidden stations */
        if (psta
            && (MAXIMUS_STATION_STATUS_DEACTIVATED != psta->getStationStatus ())
            && (txStationId != psta->getStationId ())
            && (psta->getHiddenStationsList ().find (txStationId)
                == psta->getHiddenStationsList ().end ()))
        {
            bSend = true;
            send_SciMsg_to_Station (sci_msg_to_send, *psta);
        }
    }

    return bSend;
}

void
SciServer::register_processor (
    const Sci_Msg_Type sci_msg_type,
    Processor *client)
{
    logFunction ();

    assert (client);

    /* Check if Sci message type exceeds specialized SCI Processor
     * array size. */
    assert (SCI_MSG_TYPE_NB > sci_msg_type);

    /* Check if this Processor have been already registered. */
    assert (!processor_array[sci_msg_type]);

    processor_array[sci_msg_type] = client;
}

void
SciServer::unregister_processor (
    const Sci_Msg_Type sci_msg_type)
{
    logFunction ();

    assert (SCI_MSG_TYPE_NB > sci_msg_type);
    assert (processor_array[sci_msg_type]);

    processor_array[sci_msg_type] = NULL;
}

void
SciServer::check_if_server_ready ()
{
    if (mNetworkClockTick)
        is_server_ready = true;
    else
        is_server_ready = false;
}