dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_cont_man_master.cpp
1 #include "scd_cont_man_master.h"
2
3 #include "scd_logging.h"
4 #include "scd_exception.h"
5
6
7 scd_cont_man_master::scd_cont_man_master(scd_simulator& sim):
8     _sim(sim), scd_cont_fsm("master"),
9     _st_init(sim, *this), _st_busy(sim, *this), _st_idle(sim, *this),
10     _st_done(sim, *this), _st_time_req(sim, *this), _st_time(sim, *this),
11     _st_term_req(sim, *this), _st_terminate(sim, *this),
12     _st_terminated(sim, *this), _st_fail(sim, *this), _st_failed(sim, *this)
13 {
14     // set initial state
15     set_state(_st_init);
16 }
17
18
19 scd_cont_man_master::~scd_cont_man_master()
20 {
21     std::list<scd_cont_slave_wrapper*>::iterator it;
22
23     for (it = _slaves.begin(); it != _slaves.end(); it++)
24         delete *it;
25 }
26
27
28 void scd_cont_man_master::connect_slave(const scd_command &c, scd_socket* sock)
29 {
30     std::string name = c.get_string();
31
32     scd_cont_slave_wrapper* slave = _get_slave(name);
33
34     if (slave == NULL)
35     {
36         scd_warn("unknown slave \"" + name + "\"");
37         delete sock;
38     }
39     else
40         slave->connect(sock);
41     
42 } // connect()
43
44
45 void scd_cont_man_master::set_master(const std::string& host, uint16_t port)
46 {
47     scd_error("simulator is master, can not have another master");
48     throw scd_exception("simulator is master");
49 } // set_master()
50
51
52 void scd_cont_man_master::register_slave(const std::string& name)
53 {
54     if ( _get_slave(name) != NULL )
55     {
56         scd_warn("slave \"" + name + "\" already registered, ignoring...");
57     }
58     else
59     {
60         scd_cont_slave_wrapper* wrap;
61         wrap = new scd_cont_slave_wrapper(_sim, name);
62         _slaves.push_back(wrap);
63     }
64
65 } // register_slave
66
67
68 void scd_cont_man_master::process()
69 {
70     std::list<scd_cont_slave_wrapper*>::iterator it;
71
72     // process all slaves
73     for (it = _slaves.begin(); it != _slaves.end(); it++)
74         (*it)->process();
75
76     // process the local state
77     _state->process();
78 }
79
80
81 scd_cont_slave_wrapper* scd_cont_man_master::_get_slave(const std::string& name)
82 {
83     std::list<scd_cont_slave_wrapper*>::iterator it;
84
85     for (it = _slaves.begin(); it != _slaves.end(); it++)
86     {
87         if ( (*it)->get_name() == name )
88             return *it;
89     }
90
91     return NULL;
92
93 } // _get_slave()