X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/blobdiff_plain/c4bef2a8143ddaf4ab2a57084455f5617bc39399..78d6e1ce97611abe9f0d083033197773f93c33f7:/practicas/pipi/src/devque.cpp?ds=sidebyside diff --git a/practicas/pipi/src/devque.cpp b/practicas/pipi/src/devque.cpp index 5cd8633..0791354 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"); } @@ -47,6 +47,11 @@ void DevQue::transmit(const std::string& data, const mac_type& mac) } std::string DevQue::receive() throw (std::runtime_error) +{ + return receive(mac); +} + +std::string DevQue::receive(mac_type& mac) throw (std::runtime_error) { Frame* f = (Frame*) malloc(sizeof(Frame) + mtu); if (!f) @@ -58,6 +63,8 @@ std::string DevQue::receive() 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 = " @@ -66,4 +73,18 @@ std::string DevQue::receive() throw (std::runtime_error) 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 :