summaryrefslogtreecommitdiff
path: root/maximus/coreengine/src/Maximus.cpp
blob: 4de4f7b83efe47ebf100a705c9a50f233b9f69e1 (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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/************************************************************************
                        Maximus.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/coreengine/src/Maximus.cpp
**************************************************************************/

#include "Maximus.h"

#include "Sta.h"
#include "Msg.h"
#include "CoreEngine.h"
#include "SystemManager.h"
#include "FunctionCallManager.h"
#include "NetworkClockProcessor.h"
#include "PhyProcessor.h"
#include "SciServer.h"

#include "Error.h"
#include "Logger.h"

#include <stdio.h> // for 'remove()'
#include <iostream> // for 'cout', 'cerr' and 'clog'
#include <fstream> // for 'ofstream'
#include <getopt.h> // for 'getopt_long()'
#include <signal.h> // for 'signal()' and 'SIGINT'
using namespace std;

// For Maximus log
Logger logger(LOG_ERROR);
//Logger logger;

// To catch signals
Maximus * pMaximus;


// Constructors/Destructors
//  


Maximus::Maximus ( ):
mpCoreEngine(NULL),
mMaximusLogFile(0),
mMaxTickValue(0)
{
  logFunction();
  
  try
  {
    mpCoreEngine = new CoreEngine();
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
}


Maximus::~Maximus ( )
{
  logFunction();
  
  try
  {
    clog << logger(LOG_FATAL) << "*** Exit Fulminata Maximus simulator ***" << endl;  
    
    if (NULL != mpCoreEngine)
    {
      delete mpCoreEngine;
      mpCoreEngine = NULL;
    }
    if (mMaximusLogFile.is_open())
    {
      mMaximusLogFile.flush();
      mMaximusLogFile.close();
    }
  }
  catch ( Error &e ) 
  {
    e.display();
  }
}


//  
// Methods
//  


// public methods


void Maximus::init ( int argc, char * argv[] )
{
  logFunction();
  
  try
  {
    // To catch signals
    pMaximus = this;
    signal(SIGINT, Maximus::wrapper);
  
    struct option longOptions[] = {
                                    {"station-executable", required_argument, 0, 'e'},
                                    {"station-log", required_argument, 0, 's'},
                                    {"maximus-log", required_argument, 0, 'm'},
                                    {"log-level", required_argument, 0, 'l'},
                                    {"max-tick-value", required_argument, 0, 't'},
                                    {"debugger", required_argument, 0, 'd'}
                                  };
    int optionIndex = 0;
    int optionChar = '0';
    
    // Station executable
    string stationExecutable;
    bool isStationExecutableSet = false;
    
    // Station log
    string stationLog("-");
    
    // Maximus log
    string maximusLog("maximus_log");
    
    // Log level
    int logLevel = LOG_ERROR;
      
    // Debugger
    string debugger("xterm -e gdb %e %p\0");
    
    while (EOF != optionChar)
    {
      optionChar = getopt_long(argc, argv, "e:s:m:l:t:d", longOptions, &optionIndex);
   
      switch (optionChar)
      {
        case 'e':
          clog << logger(LOG_INFO) << "station executable = " << optarg << endl;
          stationExecutable = optarg;
          isStationExecutableSet = true;
          break;
        case 's':
          clog << logger(LOG_INFO) << "station log = " << optarg << endl;
          stationLog = optarg;
          break;
        case 'm':
          clog << logger(LOG_INFO) << "maximus log = " << optarg << endl;
          maximusLog = optarg;
          break;
        case 'l':
          clog << logger(LOG_INFO) << "log level = " << optarg << endl;
          logLevel = atoi(optarg);
          break;
        case 't':
          clog << logger(LOG_INFO) << "max tick value = " << optarg << endl;
          setMaxTickValue(atoi(optarg));
          break;
        case 'd':
          clog << logger(LOG_INFO) << "debugger = " << optarg << endl;
          debugger = optarg;
          break;
        case '?':
          throw Error(__PRETTY_FUNCTION__, "Usage: -e station_executable [-s station_log -m maximus_log -l log_level -t max_tick_value -d debugger]");
          break;
      }
    }
 
    if (!isStationExecutableSet)
    {
      throw Error(__PRETTY_FUNCTION__, "Usage: -e station_executable [-s station_log -m maximus_log -l log_level -t max_tick_value -d debugger]");
    }
    
    // Maximus log
    if ( (!maximusLog.empty()) && (maximusLog.compare("-")) )
    {
      // In case of the file already exists, delete it
      remove(maximusLog.c_str());
      
      mMaximusLogFile.open(maximusLog.c_str(), ios_base::app | ios_base::out);
      if (mMaximusLogFile.is_open())
      {
        clog << logger(LOG_INFO) << "Maximus logs will be output into " << maximusLog << endl;
        logger.setLogFile(mMaximusLogFile);
      }
      else
      {
        clog << logger(LOG_ERROR) << "error while opening Maximus log file" << endl;
        clog << logger(LOG_ERROR) << "Maximus logs will be output on standard output" << endl;
      }
    }
    else
    {
      clog << logger(LOG_ERROR) << "Maximus logs will be output on standard output" << endl;
    }

    // Log level
    logger.setLogLevel(logLevel);

    mpCoreEngine->init(stationExecutable, stationLog, debugger);
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
}


void Maximus::process ( )
{
  logFunction();
  
  try
  {
    if (0 == getMaxTickValue())
    {
      getCoreEngine()->process();
    }
    else
    {
      if (getNetworkClockProcessor()->getCurrentTickValue() < getMaxTickValue())
      {
        getCoreEngine()->process();
      }
      else
      {
        stop();
      }
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }

}


Sta & Maximus::create_sta ( )
{
  logFunction();
  static Sta * pCreatedSta;
  
  try
  {
    pCreatedSta = new Sta(getSystemManager());
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return (*pCreatedSta);
}


Msg & Maximus::create_fc ( const std::string & name )
{
  logFunction();
  static Msg * pCreatedMsg;
        
  try
  {
    pCreatedMsg = new Msg(this, getFunctionCallManager(), getSystemManager(), name);
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return (*pCreatedMsg);
}


Msg & Maximus::create_probe ( )
{
  logFunction();
  static Msg * pCreatedMsg;
  
  try
  {
    pCreatedMsg = new Msg(this, getFunctionCallManager(), getSystemManager());
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }

  return (*pCreatedMsg);
}


void Maximus::wait ( const tick_t value )
{
  logFunction();
  
  try
  {
    while(getNetworkClockProcessor()->getCurrentTickValue() > value)
    {
      process();
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
}


void Maximus::wait ( )
{
  logFunction();
  
  try
  {
    while ( (NULL != getFunctionCallManager()->getListOfCallbacks())
            && (!getFunctionCallManager()->getListOfCallbacks()->empty()) )
    {
      process(); 
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
}


// private methods
//


CoreEngine * Maximus::getCoreEngine ( )
{
  try
  {
    if (NULL == mpCoreEngine)
    {
      throw Error(__PRETTY_FUNCTION__, "Core engine pointer is NULL");
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return mpCoreEngine;
}


SystemManager * Maximus::getSystemManager ( )
{
  SystemManager * pSystemManager = NULL;
  
  try
  {
    if ( NULL == (pSystemManager = getCoreEngine()->getSystemManager()) )
    {
      throw Error(__PRETTY_FUNCTION__, "System manager pointer is NULL");
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return pSystemManager;
}


FunctionCallManager * Maximus::getFunctionCallManager ( )
{
  FunctionCallManager * pFunctionCallManager = NULL;
  
  try
  {
    if ( NULL == (pFunctionCallManager = getCoreEngine()->getFunctionCallManager()) )
    {
      throw Error(__PRETTY_FUNCTION__, "Function call manager pointer is NULL");
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return pFunctionCallManager;
}


NetworkClockProcessor * Maximus::getNetworkClockProcessor ( )
{
  NetworkClockProcessor * pNetworkClockProcessor = NULL;

  try
  {
    if ( NULL == (pNetworkClockProcessor = getCoreEngine()->getNetworkClockProcessor()) )
    {
      throw Error(__PRETTY_FUNCTION__, "Network clock processor pointer is NULL");
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return pNetworkClockProcessor;
}


PhyProcessor * Maximus::getPhyProcessor ( )
{
  PhyProcessor * pPhyProcessor = NULL;
  
  try
  {
    if ( NULL == (pPhyProcessor = getCoreEngine()->getPhyProcessor()) )
    {
      throw Error(__PRETTY_FUNCTION__, "Phy processor pointer is NULL");
    }
   }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return pPhyProcessor;
}


SciServer * Maximus::getSciServer ( )
{
  SciServer * pSciServer = NULL;
  
  try
  {
    if ( NULL == (pSciServer = getCoreEngine()->getSciServer()) )
    {
      throw Error(__PRETTY_FUNCTION__, "SCI server pointer is NULL");
    }
  }
  catch ( Error &e ) 
  {
    e.display();
    stop();
  }
  
  return pSciServer;
}


// public methods
//


void Maximus::wrapper ( int n )
{
  logFunction();
  
  try
  {
    if (NULL != pMaximus)
    {
      pMaximus->stop();
    }
  }
  catch ( Error &e ) 
  {
    e.display();
  }
}


void Maximus::stop ( )
{
  logFunction();
  
  try
  {
    clog << logger(LOG_FATAL) << "*** Exit Fulminata Maximus simulator ***" << endl;  
    
    if (NULL != mpCoreEngine)
    {
      delete mpCoreEngine;
      mpCoreEngine = NULL;
    }
    if (mMaximusLogFile)
    {
      mMaximusLogFile.flush();
      mMaximusLogFile.close();
    }
    kill(getpid(), SIGTERM);
  }
  catch ( Error &e ) 
  {
    e.display();
  }
}

Network_Clock_Tick Maximus::getMaxTickValue ( ) const
{
  return mMaxTickValue;  
}


bool Maximus::setMaxTickValue ( const Network_Clock_Tick max_tick_value )
{
  mMaxTickValue = max_tick_value;
  return true;  
}