#ifndef COMMUNICATOR_H
#define COMMUNICATOR_H
-#include <list>
-#include <string>
+#include <vector>
#include <msg/msg.h>
#include "hostdata.h"
-
-class message {
-public:
- enum message_type { INFO, CREDIT, LOAD, CTRL_CLOSE, DATA_CLOSE };
-
- message(message_type t, double a): type(t), amount(a) { }
-
- message_type get_type() const { return type; }
- double get_amount() const { return amount; }
-
- std::string to_string();
-
-private:
- message_type type;
- double amount;
-};
+#include "messages.h"
+#include "msg_thread.h"
class communicator {
public:
communicator();
~communicator();
- void listen();
-
+ // Send a message to the "dest" mailbox
void send(const char* dest, message* msg);
- bool recv(message*& msg, m_host_t& from, bool wait);
+
+ // Try to flush pending sending communications.
+ // If "wait" is true, blocks until success.
void flush(bool wait);
- void next_close_on_ctrl_is_last();
- void next_close_on_data_is_last();
+ // Try to get a message. Returns true on success.
+ // Parameter "timeout" may be 0 for non-blocking operation, -1 for
+ // infinite waiting, or any positive timeout.
+ bool recv(message*& msg, m_host_t& from, double timeout) {
+ return received.pop(msg, from, timeout);
+ }
private:
// Myself
const hostdata* host;
// List of pending send communications
- std::list<msg_comm_t> sent_comm;
- static const int send_count_before_flush;
- int send_counter;
-
- // Control channel for receiving
- m_task_t ctrl_task;
- msg_comm_t ctrl_comm;
- bool ctrl_close_is_last;
+ typedef std::vector<msg_comm_t> sent_comm_type;
+ sent_comm_type sent_comm;
- // Data channel for receiving
- m_task_t data_task;
- msg_comm_t data_comm;
- bool data_close_is_last;
+ // Queue of received messages
+ message_queue received;
- const char* get_ctrl_mbox() const { return host->get_ctrl_mbox(); }
- const char* get_data_mbox() const { return host->get_data_mbox(); }
+ // Handling of receiving thread
+ msg_thread* receiver_thread;
+ void receiver();
+ // Used to chek if a communication is successfull before destroying it
+ static void comm_check_n_destroy(msg_comm_t comm);
+ // If comm is over, call comm_check_n_destroy(comm), and return true
static bool comm_test_n_destroy(msg_comm_t comm);
};