summaryrefslogtreecommitdiff
path: root/maximus/sci/src/SciServer.cpp
blob: 6e9a38624f1171bb379a6ed67bfa030c0ea99073 (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/************************************************************************
                        SciServer.cpp - Copyright buret

Here you can write a license for your code, some comments or any other
information you want to have in your generated code. To to this simply
configure the "headings" directory in uml to point to a directory
where you have your heading files.

or you can just replace the contents of this file with your own.
If you want to do this, this file is located at

/usr/share/apps/umbrello/headings/heading.cpp

-->Code Generators searches for heading files based on the file extension
   i.e. it will look for a file name ending in ".h" to include in C++ header
   files, and for a file name ending in ".java" to include in all generated
   java code.
   If you name the file "heading.<extension>", Code Generator will always
   choose this file even if there are other files with the same extension in the
   directory. If you name the file something else, it must be the only one with that
   extension in the directory to guarantee that Code Generator will choose it.

you can use variables in your heading files which are replaced at generation
time. possible variables are : author, date, time, filename and filepath.
just write %variable_name%

This file was generated on %date% at %time%
The original location of this file is /home/buret/eclipse/maximus/sci/src/SciServer.cpp
**************************************************************************/

#include "SciServer.h"
#include "Station.h"
#include "SciMsg.h"
#include "ClockSciMsg.h"
#include "SystemManager.h"
#include "Error.h"

#include <stdlib.h>
#include <errno.h>
#include <sys/select.h>
#include <iomanip>
#include <netinet/in.h> // for 'ntohl' and 'ntohs' functions

// For 'cout'
//
#include <iostream>
using namespace std;


// Constructors/Destructors
//  

  
SciServer::SciServer ( ) :
server_thread(0),
status(MAXIMUS_SCI_SERVER_STATUS_NONE),
//server_mutex(),
mpSpecializedSciMsgArray(NULL),
mArraySize(0),
mpSystemManager(NULL),
mpListOfStations(NULL)
{
  initAttributes();

  // start the server thread
  int status;
  status = pthread_create(&server_thread, NULL, SciServer::serverThread, this);
  if(status != 0)
    throw new Error(__FUNCTION__, "pthread_create", errno);
}


void SciServer::initAttributes ( )
{
  // Init array
  //
  mArraySize = 10;
  mpSpecializedSciMsgArray = new SciMsg * [mArraySize];
  for (unsigned int i=0; i<mArraySize; i++)
  {
    *(mpSpecializedSciMsgArray+i) = NULL;
  }
  
  // TEMPORARY FOR TEST PRUPOSE
  // TO DO: When creating the SciServer, set a pointer to SystemManager or directly to StationsList
  //
  mpSystemManager = new SystemManager ();
  if (NULL != mpSystemManager )
  {
    mpListOfStations = mpSystemManager->getListOfStations();
  }
  
  pthread_mutex_init(&server_mutex, NULL);
}

/*
SciServer ( const SciServer & ) throw (Error_Id)
{

}
*/

SciServer::~SciServer ( )
{
  // Free array
  //
  for (unsigned int i=0; i<mArraySize; i++)
  {
  	delete(*(mpSpecializedSciMsgArray+i));
  }
  delete [] mpSpecializedSciMsgArray;
  mpSpecializedSciMsgArray = NULL;
  
  // Free stations list
  //

  pthread_mutex_destroy(&server_mutex);
}

//  
// Methods
//  


// Other methods
// 


// public methods
//  


bool SciServer::sendSciMsg ( SciMsg * sci_msg_to_send )
{
  sci_msg_to_send = 0;
  return true;
}


void SciServer::registerSpecializedSciMsg ( Sci_Msg_Type sci_msg_type, SciMsg * sci_msg )
{
  *(mpSpecializedSciMsgArray + sci_msg_type) = sci_msg;
}


bool SciServer::receiveMsg ( Sci_Msg_Header * header, unsigned long data_length, unsigned char * received_data )
{
  cout << "SciServer::receiveMsg" << endl;
  bool bReceiveMsg = false;

  // Log the SCI message header
  // 
  displayMsgHeader (header);
    
  // Create a specialized SCI message according to the message header type
  //
  SciMsg * sciMsg = NULL;
  createSciMsg (header->type, &sciMsg);
  
  // Process the created specialized SCI message
  //
  if (NULL != sciMsg)
  {
    fillSciMsg (header, data_length, received_data, &sciMsg);
    cout << "data_length = " << data_length << endl;
    cout << "received_data = ";
    for (unsigned long i=0; i<data_length; i++)
    {
      cout << *(received_data+i);
    }
    cout << endl;
    processSciMsg (sciMsg);
    bReceiveMsg = true;
  }

  // Free allocated memory
  //
  delete sciMsg;

  return bReceiveMsg;
}


// private methods
//  


void *SciServer::serverThread(void *arg)
{
  SciServer *sci_server;
  if(arg == NULL)
    return NULL;
  sci_server = (SciServer *)arg;
  
  int fd_max, fd_index;
  fd_set read_fds;
  struct timeval timeout;
  int result;
  Station *station;
  StationsList::iterator it;
  
  while(sci_server->getStatus() == MAXIMUS_SCI_SERVER_STATUS_RUNNING)
  {
    // timeout set to 1sec
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    FD_ZERO(&read_fds);
    fd_max = 0;
    pthread_mutex_lock(&sci_server->server_mutex);
    for(it = sci_server->mpListOfStations->begin() ; it != sci_server->mpListOfStations->end(); it++ )
    {
      station = *it;
      FD_SET(station->getInputFileDescriptor(), &read_fds);
      if(station->getInputFileDescriptor() > fd_max)
        fd_max = station->getInputFileDescriptor();
    }
        
    result = select(fd_max + 1, &read_fds, NULL, NULL, &timeout);
    if(result > 0)
    {
      // parcours de la liste...
      for(fd_index = 0; fd_index <= fd_max; fd_index++)
      {
        if(FD_ISSET(fd_index, &read_fds))
        {
          int len, header_len;
          struct Sci_Msg_Header header;
          unsigned char *buffer;
          header_len = sizeof(struct Sci_Msg_Header);
          if((len = read(fd_index, &header, sizeof(struct Sci_Msg_Header))) < header_len)
          {
            cout << "header read error: len=" << len << ", errno=" << errno << endl;
            continue;
          }
          else
          {
            // check for header
            if(memcmp(&header.magic_id, MAXIMUS_SCI_MSG_MAGIC, 4))
            {
              cout << "bad magic id: " << hex << htonl(header.magic_id) << endl;
              continue;
            }
            header.length = ntohs (header.length);
            if((header.type <= MAXIMUS_SCI_MSG_TYPE_NONE)
              || (header.type >= MAXIMUS_SCI_MSG_TYPE_NB))
            {
              cout << "bad type: " << dec << header.type << endl;
              continue;
            }
            if(header.version != MAXIMUS_SCI_MSG_VERSION)
            {
              cout << "bad version: " << header.version << endl;
              continue;
            }
            header.msg_id = ntohs (header.msg_id);
            header.station_id = ntohs(header.station_id);
            header.netclock_high = ntohl(header.netclock_high);
            header.netclock_low = ntohl(header.netclock_low);
            header.reserved = ntohs(header.reserved);
            header.flags = ntohs(header.flags);
  
            buffer = (unsigned char *)malloc(header.length);
            if((len = read(station->getInputFileDescriptor(), buffer, header.length)) != header.length)
            {
              cout << "data read error: len=" << len << ", errno=" << errno << endl;
              free(buffer);
              continue;
            }
            sci_server->receiveMsg(&header, header.length, buffer);
            free(buffer);
          }
        }
      }
    }
    pthread_mutex_unlock(&sci_server->server_mutex);
  }
  sci_server->setStatus(MAXIMUS_SCI_SERVER_STATUS_STOPPED);
  return NULL;
}


void SciServer::displayMsgHeader ( Sci_Msg_Header * msg_header )
{
  cout << "SciServer::displayMsgHeader" << endl;
  
  cout << hex;
  cout << "magic_id = 0x" << msg_header->magic_id << endl;
  cout << "length = 0x" << msg_header->length << endl;
  cout << "type = 0x" << msg_header->type << endl;
  cout << "version = 0x" << msg_header->version << endl;
  cout << "msg_id = 0x" << msg_header->msg_id << endl;
  cout << "station_id = 0x" << msg_header->station_id << endl;
  cout << "netclock_high = 0x" << msg_header->netclock_high << endl;
  cout << "netclock_low = 0x" << msg_header->netclock_low << endl;
  cout << "reserved = 0x" << msg_header->reserved << endl;
  cout << "flags = 0x" << msg_header->flags << endl;
  cout << dec;
}


bool SciServer::createSciMsg ( uint8_t type, SciMsg ** received_sci_msg )
{
  cout << "SciServer::createSpecializedSciMsg" << endl;
  bool bCreateMsg = false;
  
  // Create a specialized SCI message according to the header type
  //
  * received_sci_msg = (*(mpSpecializedSciMsgArray + type))->create();
  
  if (NULL != * received_sci_msg)
  {
    bCreateMsg = true;
  }

  return bCreateMsg;
}


bool SciServer::fillSciMsg ( Sci_Msg_Header * p_msg_header, unsigned long data_length, unsigned char * p_received_data, SciMsg ** created_sci_msg )
{
  cout << "SciServer::fillSciMsg" << endl;
  bool bFillMsg = false;

  // Fill SCI message contents
  //
  if (NULL != created_sci_msg)
  {
    (*created_sci_msg)->setSciMsgHeader(p_msg_header);
    (*created_sci_msg)->setSciMsgDataLength(data_length);
    (*created_sci_msg)->setSciMsgData(p_received_data);
    (*created_sci_msg)->identifySpecializedSciMsgHeader(data_length, p_received_data);
    (*created_sci_msg)->identifySpecializedSciMsgData(data_length, p_received_data);
    cout << "data_length = " << data_length << endl;
    cout << "p_received_data = ";
    for (unsigned long i=0; i<data_length; i++)
    {
      cout << *(p_received_data+i);
    }
    cout << endl;
    bFillMsg = true;
  }
  
  return bFillMsg;
}


bool SciServer::processSciMsg ( SciMsg * received_sci_msg )
{
  cout << "SciServer::processSciMsg" << endl;
  bool bProcessMsg = false;
  
  if (NULL != received_sci_msg)
  {
    received_sci_msg->dispatchMsg();
    bProcessMsg = true;
  }
  
  return bProcessMsg;
}


// protected methods
//


// Accessor methods
//  


// public attribute accessor methods
//  


Sci_Server_Status SciServer::getStatus (void)
{
  return status;
}


void SciServer::setStatus(Sci_Server_Status new_status) throw (Error)
{
  if((new_status < 0) || (new_status >= MAXIMUS_SCI_SERVER_STATUS_NB))
    throw new Error(__FUNCTION__, "", EINVAL);
  status = new_status;
  return;
}


//fd_set getStationFdset( void )
//{
//  pthread_mutex_lock(&server_mutex);
//  memcpy(station_fds_copy, station_read_fds, sizeof(station_read_fds));
//  pthread_mutex_unlock(&server_mutex);
//  return station_fds_copy);
//}


// private attribute accessor methods
//  


SciMsg ** SciServer::getSpecializedSciMsgArray ( )
{
  return mpSpecializedSciMsgArray;
}


unsigned int SciServer::getArraySize ( )
{
  return mArraySize; 
}


// protected attribute accessor methods
//