diff --git a/src/config.cc b/src/config.cc index e91b2b7..165f1b3 100644 --- a/src/config.cc +++ b/src/config.cc @@ -8,8 +8,9 @@ using namespace std; Config::Config(int argc, char **argv) { _port = -1; + _index = -1; _mode = Protocol::TYPE_UNKNOWN; - + int groupPort; while (1) { @@ -22,13 +23,14 @@ Config::Config(int argc, char **argv) { {"group", required_argument, 0, 'g'}, {"port", required_argument, 0, 'p'}, + {"index", required_argument, 0, 'i'}, {0, 0, 0, 0} }; int option_index = 0; - int c = getopt_long(argc, argv, "TACg:p:", + int c = getopt_long(argc, argv, "TACg:p:i:", long_options, &option_index); /* detect the end of options */ @@ -55,6 +57,15 @@ Config::Config(int argc, char **argv) { _mode = Protocol::TYPE_TEST; break; } + case 'i': + { + stringstream s; + printf("Index -> %s\n",optarg); + + s << string(optarg); + s >> _index; + } + break; case 'p': { stringstream s; @@ -111,6 +122,11 @@ bool Config::isValid() { } valid++; + if (_index > 0) { + score++; + } + valid++; + return (valid == score); } @@ -122,6 +138,10 @@ Protocol::Type Config::getMode(){ return _mode; } +short Config::getIndex(){ + return _index; +} + int Config::getPort(){ return _port; } diff --git a/src/config.h b/src/config.h index e32aed8..956871b 100644 --- a/src/config.h +++ b/src/config.h @@ -14,6 +14,7 @@ class Config { std::list _group_hosts; int _port; Protocol::Type _mode; + short _index; protected: @@ -25,6 +26,7 @@ class Config { bool isValid(); std::list getGroupHosts(); int getPort(); + short getIndex(); Protocol::Type getMode(); }; #endif // _GYR_CONFIG_H diff --git a/src/dabroadcast.cc b/src/dabroadcast.cc index 69c91f8..85b3945 100644 --- a/src/dabroadcast.cc +++ b/src/dabroadcast.cc @@ -26,11 +26,11 @@ int main(int argc, char ** argv){ if (config.isValid()){ Glib::thread_init(); - Group grp(config.getGroupHosts()); + Group grp(config.getGroupHosts(), config.getIndex()); Clock * clk; //FIXME non-dynamic port ! - int portHigh = 2310; + int portHigh = 2710; switch(config.getMode()){ case Protocol::TYPE_TEST: diff --git a/src/group.cc b/src/group.cc index 5f0129c..173029e 100644 --- a/src/group.cc +++ b/src/group.cc @@ -5,9 +5,10 @@ #define DEBUG 0 -Group::Group(std::list group){ +Group::Group(std::list group, short index){ _hosts = group; _socket_desc = socket(AF_INET, SOCK_DGRAM, 0); + _index = index; /* et l'autre variante : AF_UNIX */ if (_socket_desc < 0){ @@ -79,8 +80,11 @@ void Group::broadcast(Message & msg){ printf("Group::broadcast -- exit\n"); } +short Group::getIndex(){ + return _index; +} -void Group::sendto(Message &msg, int index){ +void Group::sendto(Message &msg, short index){ sockaddr_in * addr = _addrs[index]; if (DEBUG) diff --git a/src/group.h b/src/group.h index b062f62..394a4ae 100644 --- a/src/group.h +++ b/src/group.h @@ -27,13 +27,16 @@ class Group { std::list _hosts; std::vector _addrs; int _socket_desc; + short _index; protected: public: - Group(std::list group); + Group(std::list group, short myindex); - void sendto(Message &msg, int index); + void sendto(Message &msg, short index); void broadcast(Message &msg); + short getIndex(); + }; #endif diff --git a/src/lowreceiver.cc b/src/lowreceiver.cc index 77dc87a..df57099 100644 --- a/src/lowreceiver.cc +++ b/src/lowreceiver.cc @@ -72,7 +72,7 @@ void LowReceiver::run(){ strncpy(str, mesg->getData(), mesg->getDataSize()); str[mesg->getDataSize()] = '\0'; printf("LowReceiver::run -- READ '%s'\n", str); - + this->manage(mesg); delete(mesg); @@ -86,17 +86,17 @@ void LowReceiver::manage(Message * mesg){ switch(mesg->getType()){ case Protocol::TYPE_TEST : { - printf("LowReceiver::manage -- NOT IMPLEMENTED\n"); + printf("LowReceiver::manage -- NOT IMPLEMENTED\n"); } break; case Protocol::TYPE_ABCAST : { - this->manage_abcast(mesg); + this->manage_abcast(mesg); } break; case Protocol::TYPE_CBCAST : { - this->manage_cbcast(mesg); + this->manage_cbcast(mesg); } break; default: @@ -109,34 +109,45 @@ void LowReceiver::manage_abcast(Message * mesg) { static std::list fifo; std::list::iterator iter; printf("LowReceiver::manage_abcast -- init\n"); - - // FIXME: on suppose ne pas etre l'emetteur + // identifiant = horloge + id_site_emeteur bool firstSeenMessage = true; - for (iter = fifo.begin(); iter != fifo.end(); iter++){ - MessageCellAb * cur = *iter; - if (cur->message == mesg) { - printf("LowReceiver::manage_abcast -- message seen\n"); - firstSeenMessage = false; - break; - } + bool iAmTheEmitter = false; + + if (mesg->getStamp().getIndex() == _group.getIndex()){ + iAmTheEmitter = true; } - if (firstSeenMessage){ - // si le message est vu pour la premiere fois: - // - on l'ajoute dans la liste d'attente - MessageCellAb * cell = new MessageCellAb(); - cell->message = new Message(*mesg); //FIXME: make a copy; - cell->type = MessageCellAb::TYPE_TEMPORARY; - // - on retourne une estampille(reception) a l'emeteur - + if (iAmTheEmitter){ + printf("LowReceiver::manage_abcast - Received my own message \n"); + //FIXME: faire la gestion du abcast/send ici, c'est plus simple que + //de partager une variable+mutex avec le sender } else { - // sinon - // - l'estampille du message est mise a jour - TimeStamp * stamp = new TimeStamp (Protocol::TYPE_ABCAST, cell->message->getData(), cell->message->getDataSize()); + for (iter = fifo.begin(); iter != fifo.end(); iter++){ + MessageCellAb * cur = *iter; + if (cur->message == mesg) { + printf("LowReceiver::manage_abcast -- message seen\n"); + firstSeenMessage = false; + break; + } + } - // - le message est marqué comme final - // - on défile les estampille finale la + if (firstSeenMessage){ + // si le message est vu pour la premiere fois: + // - on l'ajoute dans la liste d'attente + MessageCellAb * cell = new MessageCellAb(); + cell->message = new Message(*mesg); //FIXME: make a copy; + cell->type = MessageCellAb::TYPE_TEMPORARY; + // - on retourne une estampille(reception) a l'emeteur + + } else { + // sinon + // - l'estampille du message est mise a jour + TimeStamp * stamp = new TimeStamp (Protocol::TYPE_ABCAST, mesg->getData(), mesg->getDataSize()); + + // - le message est marqué comme final + // - on défile les estampille finale la + } } }