X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/c26a92b22080c9c6cbec81ad6b49f2899864e5aa..5f8ce7344f3e7cba2f4c7a8dbe17471a0387e92d:/practicas/pipi/src/devque.cpp diff --git a/practicas/pipi/src/devque.cpp b/practicas/pipi/src/devque.cpp index fd54398..1e812ff 100644 --- a/practicas/pipi/src/devque.cpp +++ b/practicas/pipi/src/devque.cpp @@ -20,7 +20,7 @@ DevQue::DevQue(mac_type mac, key_t key, size_t mtu) throw (std::runtime_error, std::logic_error): Dev(mac, mtu) { - que_id = msgget(key, 0666); // Debe estar previamente creada + que_id = msgget(key, IPC_CREAT | 0666); if (que_id == -1) throw std::runtime_error("No se pudo crear la cola"); } @@ -28,6 +28,9 @@ DevQue::DevQue(mac_type mac, key_t key, size_t mtu) void DevQue::transmit(const std::string& data, const mac_type& mac) throw (std::runtime_error, std::logic_error) { +#ifdef DEBUG_TRACE + std::cout << "DevQue[" << que_id << "]::transmit()\n"; +#endif if (data.size() > mtu) throw std::logic_error("Tamaño de datos mayor al MTU"); Frame* f = (Frame*) malloc(sizeof(Frame) + mtu); @@ -35,11 +38,11 @@ void DevQue::transmit(const std::string& data, const mac_type& mac) throw std::runtime_error("No se puede reservar memoria"); f->mac = mac; f->size = data.size(); - memcpy(f->frame, data.c_str(), data.size()); + memcpy(f->frame, data.data(), data.size()); int res = msgsnd(que_id, f, mtu + sizeof(size_t), 0); -#ifdef DEBUG2 - std::cout << "DevQue::transmit(msgtype/mac = " << f->mac << ", size = " - << f->size << ")\n"; +#ifdef DEBUG_QUE + std::cout << "DevQue[" << que_id << "]::transmit(msgtype/mac = " + << f->mac << ", size = " << f->size << ")\n"; #endif free(f); if (res == -1) @@ -48,11 +51,17 @@ void DevQue::transmit(const std::string& data, const mac_type& mac) std::string DevQue::receive() throw (std::runtime_error) { +#ifdef DEBUG_TRACE + std::cout << "DevQue[" << que_id << "]::receive()\n"; +#endif return receive(mac); } -std::string DevQue::receive(const mac_type& mac) throw (std::runtime_error) +std::string DevQue::receive(mac_type& mac) throw (std::runtime_error) { +#ifdef DEBUG_TRACE + std::cout << "DevQue[" << que_id << "]::receive(mac)\n"; +#endif Frame* f = (Frame*) malloc(sizeof(Frame) + mtu); if (!f) throw std::runtime_error("No se puede reservar memoria"); @@ -63,12 +72,28 @@ std::string DevQue::receive(const mac_type& mac) throw (std::runtime_error) throw std::runtime_error("Error al sacar de la cola"); } std::string s((char*) f->frame, f->size); + if (mac == 0) + mac = f->mac; free(f); -#ifdef DEBUG2 - std::cout << "DevQue::receive(msgtype/mac = " << mac << ", size = " - << s.size() << ")\n"; +#ifdef DEBUG_QUE + std::cout << "DevQue[" << que_id << "]::receive(msgtype/mac = " + << mac << ", size = " << s.size() << ")\n"; #endif return s; } +/// Indica cuantos elementos hay en la cola +size_t DevQue::size() const +{ + struct msqid_ds minfo; + msgctl(que_id, IPC_STAT, &minfo); + return minfo.msg_qnum; +} + +/// Indica si está vacía la cola +bool DevQue::empty() const +{ + return size() == 0; +} + // vim: set et sw=4 sts=4 :