dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_out_connector.h
1 #ifndef SCD_OUT_CONNECTOR_H
2 #define SCD_OUT_CONNECTOR_H
3
4 #include <string>
5 #include <time.h>
6 #include <sys/time.h>
7
8 #include "scd_socket.h"
9 #include "scd_sock_poller.h"
10 #include "scd_simulator.h"
11 #include "scd_command_writer.h"
12
13
14 const time_t SCD_CON_TIMEOUT = 2;
15 const time_t SCD_CON_RETRY = 1;
16
17
18 /* forward declaration */
19 class scd_simulator;
20
21
22 /**
23  * Initiates an outgoing connection and sends the register command
24  * to the other side. If an attempt to connect failes it is
25  * retried after some time. To drive this, process() has to be called
26  * repeatedly.
27  */
28 class scd_out_connector : public scd_sock_ev_handler_if
29 {
30 public:
31     /**
32      * Constructor.
33      * \param sim the simulator
34      * \param type what should be connected (SCD_CM_NETSIM or SCD_CM_CHANNEL)
35      * \param name the name of the ressource to connect (i.e. channel name
36      * or slave name)
37      */
38     scd_out_connector(scd_simulator& sim, uint16_t type,
39             const std::string& name);
40
41     virtual ~scd_out_connector();
42
43     /**
44      * Sets the host to connect to and starts the first connection attempt.
45      *
46      * \param host the IP or hostname to connect to
47      * \param port the port to connect to
48      */
49     void connect_to(const std::string& host, uint16_t port);
50
51     /**
52      * Indicates if this out_connector is still trying to connect or 
53      * sending the command.
54      * * \return true if this out_connector is still working
55      */
56     bool is_connecting();
57
58     /**
59      * Indicates if the command has been sent successfully and the registered
60      * connection can be collected.
61      * \return true if a connection is registered
62      */
63     bool has_connection();
64
65     /**
66      * Returns the established and registered connection.
67      */
68     scd_socket* get_connection();
69
70
71     /**
72      * Checks if a time outs have occured. A connection attempt that is running
73      * for more than SCD_CON_TIMEOUT seconds is terminated and failed
74      * connection attempts are retried after SCD_CON_RETRY seconds.
75      */
76     void process();
77
78     /**
79      * Stops to try to connect or terminates an already established
80      * connection.
81      */
82     void close();
83
84     /**
85      * Returns the name of the ressource to connect.
86      */
87     const std::string& get_name() const;
88
89     /* scd_sock_ev_handler_if */
90     void handle_sock_ev(sock_ev events);
91     const scd_socket& get_sock();
92
93 private:
94     scd_simulator &_sim;
95     std::string _name;
96     std::string _host;
97     uint16_t _port;
98
99     bool _is_connecting;
100     bool _has_connection;
101     scd_socket* _socket;
102     scd_command_writer* _writer;
103
104     bool _is_connected;
105     struct timeval _last_con;
106
107     void _conn_attempt();
108 };
109
110 #endif