dol: initial dol commit
[jump.git] / dol / src / dol / visitor / hdsd / scd / scd_out_connector.h
diff --git a/dol/src/dol/visitor/hdsd/scd/scd_out_connector.h b/dol/src/dol/visitor/hdsd/scd/scd_out_connector.h
new file mode 100644 (file)
index 0000000..f8a9a9c
--- /dev/null
@@ -0,0 +1,110 @@
+#ifndef SCD_OUT_CONNECTOR_H
+#define SCD_OUT_CONNECTOR_H
+
+#include <string>
+#include <time.h>
+#include <sys/time.h>
+
+#include "scd_socket.h"
+#include "scd_sock_poller.h"
+#include "scd_simulator.h"
+#include "scd_command_writer.h"
+
+
+const time_t SCD_CON_TIMEOUT = 2;
+const time_t SCD_CON_RETRY = 1;
+
+
+/* forward declaration */
+class scd_simulator;
+
+
+/**
+ * Initiates an outgoing connection and sends the register command
+ * to the other side. If an attempt to connect failes it is
+ * retried after some time. To drive this, process() has to be called
+ * repeatedly.
+ */
+class scd_out_connector : public scd_sock_ev_handler_if
+{
+public:
+    /**
+     * Constructor.
+     * \param sim the simulator
+     * \param type what should be connected (SCD_CM_NETSIM or SCD_CM_CHANNEL)
+     * \param name the name of the ressource to connect (i.e. channel name
+     * or slave name)
+     */
+    scd_out_connector(scd_simulator& sim, uint16_t type,
+            const std::string& name);
+
+    virtual ~scd_out_connector();
+
+    /**
+     * Sets the host to connect to and starts the first connection attempt.
+     *
+     * \param host the IP or hostname to connect to
+     * \param port the port to connect to
+     */
+    void connect_to(const std::string& host, uint16_t port);
+
+    /**
+     * Indicates if this out_connector is still trying to connect or 
+     * sending the command.
+     * * \return true if this out_connector is still working
+     */
+    bool is_connecting();
+
+    /**
+     * Indicates if the command has been sent successfully and the registered
+     * connection can be collected.
+     * \return true if a connection is registered
+     */
+    bool has_connection();
+
+    /**
+     * Returns the established and registered connection.
+     */
+    scd_socket* get_connection();
+
+
+    /**
+     * Checks if a time outs have occured. A connection attempt that is running
+     * for more than SCD_CON_TIMEOUT seconds is terminated and failed
+     * connection attempts are retried after SCD_CON_RETRY seconds.
+     */
+    void process();
+
+    /**
+     * Stops to try to connect or terminates an already established
+     * connection.
+     */
+    void close();
+
+    /**
+     * Returns the name of the ressource to connect.
+     */
+    const std::string& get_name() const;
+
+    /* scd_sock_ev_handler_if */
+    void handle_sock_ev(sock_ev events);
+    const scd_socket& get_sock();
+
+private:
+    scd_simulator &_sim;
+    std::string _name;
+    std::string _host;
+    uint16_t _port;
+
+    bool _is_connecting;
+    bool _has_connection;
+    scd_socket* _socket;
+    scd_command_writer* _writer;
+
+    bool _is_connected;
+    struct timeval _last_con;
+
+    void _conn_attempt();
+};
+
+#endif