all: depend $(targets)
-test_send: test_send.o media.o
+test_send: test_send.o dev.o
-test_recv: test_recv.o media.o
+test_recv: test_recv.o dev.o
test_ipaddr: test_ipaddr.o ipaddr.o
# DO NOT DELETE
+dev.o: dev.h /usr/include/unistd.h /usr/include/features.h
+dev.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
+dev.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
+dev.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
+dev.o: /usr/include/bits/confname.h /usr/include/getopt.h
+dev.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
+dev.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/endian.h
+dev.o: /usr/include/bits/endian.h /usr/include/sys/select.h
+dev.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
+dev.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
+dev.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
+dev.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
+dev.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h /usr/include/bits/msq.h
ipaddr.o: ipaddr.h
-media.o: media.h frame.h /usr/include/unistd.h /usr/include/features.h
-media.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
-media.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
-media.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
-media.o: /usr/include/bits/confname.h /usr/include/getopt.h
-media.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
-media.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/endian.h
-media.o: /usr/include/bits/endian.h /usr/include/sys/select.h
-media.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
-media.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
-media.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
-media.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
-media.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h
-media.o: /usr/include/bits/msq.h
test_ipaddr.o: ipaddr.h
-test_recv.o: ethernetframe.h frame.h media.h /usr/include/unistd.h
-test_recv.o: /usr/include/features.h /usr/include/sys/cdefs.h
-test_recv.o: /usr/include/gnu/stubs.h /usr/include/bits/posix_opt.h
-test_recv.o: /usr/include/bits/types.h /usr/include/bits/wordsize.h
-test_recv.o: /usr/include/bits/typesizes.h /usr/include/bits/confname.h
-test_recv.o: /usr/include/getopt.h /usr/include/fcntl.h
-test_recv.o: /usr/include/bits/fcntl.h /usr/include/sys/types.h
-test_recv.o: /usr/include/time.h /usr/include/endian.h
-test_recv.o: /usr/include/bits/endian.h /usr/include/sys/select.h
-test_recv.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
-test_recv.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
-test_recv.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
-test_recv.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
-test_recv.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h
-test_recv.o: /usr/include/bits/msq.h dev.h
-test_send.o: ethernetframe.h frame.h media.h /usr/include/unistd.h
-test_send.o: /usr/include/features.h /usr/include/sys/cdefs.h
-test_send.o: /usr/include/gnu/stubs.h /usr/include/bits/posix_opt.h
-test_send.o: /usr/include/bits/types.h /usr/include/bits/wordsize.h
-test_send.o: /usr/include/bits/typesizes.h /usr/include/bits/confname.h
-test_send.o: /usr/include/getopt.h /usr/include/fcntl.h
-test_send.o: /usr/include/bits/fcntl.h /usr/include/sys/types.h
-test_send.o: /usr/include/time.h /usr/include/endian.h
-test_send.o: /usr/include/bits/endian.h /usr/include/sys/select.h
-test_send.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
-test_send.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h
-test_send.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h
-test_send.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h
-test_send.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h
-test_send.o: /usr/include/bits/msq.h dev.h
+test_recv.o: dev.h /usr/include/unistd.h /usr/include/features.h
+test_recv.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
+test_recv.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
+test_recv.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
+test_recv.o: /usr/include/bits/confname.h /usr/include/getopt.h
+test_recv.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
+test_recv.o: /usr/include/sys/types.h /usr/include/time.h
+test_recv.o: /usr/include/endian.h /usr/include/bits/endian.h
+test_recv.o: /usr/include/sys/select.h /usr/include/bits/select.h
+test_recv.o: /usr/include/bits/sigset.h /usr/include/bits/time.h
+test_recv.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h
+test_recv.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h
+test_recv.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h
+test_recv.o: /usr/include/sys/msg.h /usr/include/bits/msq.h
+test_send.o: dev.h /usr/include/unistd.h /usr/include/features.h
+test_send.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
+test_send.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h
+test_send.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h
+test_send.o: /usr/include/bits/confname.h /usr/include/getopt.h
+test_send.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h
+test_send.o: /usr/include/sys/types.h /usr/include/time.h
+test_send.o: /usr/include/endian.h /usr/include/bits/endian.h
+test_send.o: /usr/include/sys/select.h /usr/include/bits/select.h
+test_send.o: /usr/include/bits/sigset.h /usr/include/bits/time.h
+test_send.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h
+test_send.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h
+test_send.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h
+test_send.o: /usr/include/sys/msg.h /usr/include/bits/msq.h
--- /dev/null
+#include "dev.h"
+#include <cstring>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#ifdef DEBUG
+#include <iostream>
+#endif
+
+struct Frame
+{
+ Dev::mac_type mac;
+ size_t size;
+ char frame[1];
+};
+
+Dev::Dev(mac_type mac, size_t mtu, key_t key) throw (std::runtime_error):
+ mac(mac), mtu(mtu)
+{
+ que_id = msgget(key, 0666); // Debe estar previamente creada
+ if (que_id == -1)
+ throw std::runtime_error("No se pudo crear la cola");
+}
+
+void Dev::transmit(const std::string& data, const mac_type& mac)
+ throw (std::runtime_error, std::logic_error)
+{
+ if (data.size() > mtu)
+ throw std::logic_error("Tamaño de datos mayor al MTU");
+ Frame* f = (Frame*) malloc(sizeof(Frame) + mtu);
+ if (!f)
+ throw std::runtime_error("No se puede reservar memoria");
+ f->mac = mac;
+ f->size = data.size();
+ memcpy(f->frame, data.c_str(), data.size());
+ int res = msgsnd(que_id, f, mtu + sizeof(size_t), 0);
+#ifdef DEBUG
+ std::cout << "Dev::transmit(msgtype/mac = " << f->mac << ", size = "
+ << f->size << ", data = " << f->frame << ")\n";
+#endif
+ free(f);
+ if (res == -1)
+ throw std::runtime_error("Error al poner en la cola");
+}
+
+std::string Dev::receive() throw (std::runtime_error)
+{
+ Frame* f = (Frame*) malloc(sizeof(Frame) + mtu);
+ if (!f)
+ throw std::runtime_error("No se puede reservar memoria");
+ int res = msgrcv(que_id, f, mtu + sizeof(size_t), mac, 0);
+ if (res == -1)
+ {
+ free(f);
+ throw std::runtime_error("Error al sacar de la cola");
+ }
+ std::string s((char*) f->frame, f->size);
+ free(f);
+#ifdef DEBUG
+ std::cout << "Dev::receive(msgtype/mac = " << mac << ", size = "
+ << s.size() << ", data = " << s << ")\n";
+#endif
+ return s;
+}
+
+// vim: set et sw=4 sts=4 :
+++ /dev/null
-#include "media.h"
-#ifdef DEBUG
-#include <iostream>
-#endif
-
-Media::Media(key_t key) throw (std::runtime_error)
-{
- que_id = msgget(key, 0666); // Debe estar previamente creada
- if (que_id == -1)
- throw std::runtime_error("No se pudo crear la cola");
-}
-
-void Media::transmit(const Frame& frame)
-{
-#ifdef DEBUG
- std::cout << "Media::transmit(msgtype/mac = " << *(((int*)&frame)+1)
- << ", size = " << frame.size() << ", data = " << frame.data() << ")\n";
-#endif
- // HACK ASQUEROSO para pasarle por encima a la vtable... :-O~
- if (msgsnd(que_id, ((int*)&frame)+1, frame.size(), 0) == -1)
- throw std::runtime_error("Error al poner en la cola");
-}
-
-void Media::receive(Frame& frame)
-{
- // HACK ASQUEROSO para pasarle por encima a la vtable... :-O~
- if (msgrcv(que_id, ((int*)&frame)+1, frame.size(), frame.mac(), 0) == -1)
- throw std::runtime_error("Error al sacar de la cola");
-#ifdef DEBUG
- std::cout << "Media::receive(msgtype/mac = " << *(((int*)&frame)+1)
- << ", size = " << frame.size() << ", data = " << frame.data() << ")\n";
-#endif
-}
-
-// vim: set et sw=4 sts=4 :