From: Leandro Lucarella Date: Tue, 13 Jun 2006 04:41:04 +0000 (+0000) Subject: Se crea una interfaz genérica para interfaces de red (Dev) y se pasa la X-Git-Tag: svn_import~43 X-Git-Url: https://git.llucax.com/z.facultad/75.74/practicos.git/commitdiff_plain/c4bef2a8143ddaf4ab2a57084455f5617bc39399 Se crea una interfaz genérica para interfaces de red (Dev) y se pasa la implementanción a través de colas a otra clase (DevQue). --- diff --git a/practicas/pipi/src/Makefile b/practicas/pipi/src/Makefile index 9980baf..532b6b9 100644 --- a/practicas/pipi/src/Makefile +++ b/practicas/pipi/src/Makefile @@ -17,6 +17,9 @@ CFLAGS = -Wall -ansi -pedantic-errors # Para valgrind o debug CFLAGS += -ggdb -DDEBUG +# Para más verbose +CFLAGS += -DDEBUG2 + # Opciones para el compilador C++. CXXFLAGS = $(CFLAGS) -fno-inline @@ -43,17 +46,17 @@ all: depend $(targets) tests: depend $(tests) -test_send: test_send.o dev.o +test_send: test_send.o devque.o -test_recv: test_recv.o dev.o +test_recv: test_recv.o devque.o test_ipaddr: test_ipaddr.o ipaddr.o ipheader.o -test_ipin: test_ipin.o ipin.o ipaddr.o ipheader.o dev.o +test_ipin: test_ipin.o ipin.o ipaddr.o ipheader.o devque.o -test_ipout: test_ipout.o ipout.o ipaddr.o ipheader.o dev.o routetable.o +test_ipout: test_ipout.o ipout.o ipaddr.o ipheader.o devque.o routetable.o -ip: ip.o ipout.o ipin.o ipaddr.o ipheader.o dev.o routetable.o +ip: ip.o ipout.o ipin.o ipaddr.o ipheader.o devque.o routetable.o depend: @makedepend $(fuentes) > /dev/null 2>&1 @@ -63,23 +66,24 @@ clean: # 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 +devque.o: devque.h dev.h /usr/include/unistd.h /usr/include/features.h +devque.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h +devque.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h +devque.o: /usr/include/bits/wordsize.h /usr/include/bits/typesizes.h +devque.o: /usr/include/bits/confname.h /usr/include/getopt.h +devque.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h +devque.o: /usr/include/sys/types.h /usr/include/time.h /usr/include/endian.h +devque.o: /usr/include/bits/endian.h /usr/include/sys/select.h +devque.o: /usr/include/bits/select.h /usr/include/bits/sigset.h +devque.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h +devque.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h +devque.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h +devque.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h +devque.o: /usr/include/bits/msq.h ip.o: ipout.h ipaddr.h ipheader.h /usr/include/stdint.h ip.o: /usr/include/features.h /usr/include/sys/cdefs.h ip.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h -ip.o: /usr/include/bits/wordsize.h routetable.h dev.h ipin.h +ip.o: /usr/include/bits/wordsize.h routetable.h dev.h ipin.h devque.h ip.o: /usr/include/unistd.h /usr/include/bits/posix_opt.h ip.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h ip.o: /usr/include/bits/confname.h /usr/include/getopt.h /usr/include/fcntl.h @@ -130,23 +134,23 @@ test_ipaddr.o: /usr/include/bits/wordsize.h test_ipin.o: ipin.h ipaddr.h ipheader.h /usr/include/stdint.h test_ipin.o: /usr/include/features.h /usr/include/sys/cdefs.h test_ipin.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h -test_ipin.o: /usr/include/bits/wordsize.h dev.h /usr/include/unistd.h -test_ipin.o: /usr/include/bits/posix_opt.h /usr/include/bits/types.h -test_ipin.o: /usr/include/bits/typesizes.h /usr/include/bits/confname.h -test_ipin.o: /usr/include/getopt.h /usr/include/fcntl.h -test_ipin.o: /usr/include/bits/fcntl.h /usr/include/sys/types.h -test_ipin.o: /usr/include/time.h /usr/include/endian.h -test_ipin.o: /usr/include/bits/endian.h /usr/include/sys/select.h -test_ipin.o: /usr/include/bits/select.h /usr/include/bits/sigset.h -test_ipin.o: /usr/include/bits/time.h /usr/include/sys/sysmacros.h -test_ipin.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sched.h -test_ipin.o: /usr/include/sys/ipc.h /usr/include/bits/ipctypes.h -test_ipin.o: /usr/include/bits/ipc.h /usr/include/sys/msg.h -test_ipin.o: /usr/include/bits/msq.h +test_ipin.o: /usr/include/bits/wordsize.h dev.h devque.h +test_ipin.o: /usr/include/unistd.h /usr/include/bits/posix_opt.h +test_ipin.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h +test_ipin.o: /usr/include/bits/confname.h /usr/include/getopt.h +test_ipin.o: /usr/include/fcntl.h /usr/include/bits/fcntl.h +test_ipin.o: /usr/include/sys/types.h /usr/include/time.h +test_ipin.o: /usr/include/endian.h /usr/include/bits/endian.h +test_ipin.o: /usr/include/sys/select.h /usr/include/bits/select.h +test_ipin.o: /usr/include/bits/sigset.h /usr/include/bits/time.h +test_ipin.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h +test_ipin.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h +test_ipin.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h +test_ipin.o: /usr/include/sys/msg.h /usr/include/bits/msq.h test_ipout.o: ipout.h ipaddr.h ipheader.h /usr/include/stdint.h test_ipout.o: /usr/include/features.h /usr/include/sys/cdefs.h test_ipout.o: /usr/include/gnu/stubs.h /usr/include/bits/wchar.h -test_ipout.o: /usr/include/bits/wordsize.h routetable.h dev.h +test_ipout.o: /usr/include/bits/wordsize.h routetable.h dev.h devque.h test_ipout.o: /usr/include/unistd.h /usr/include/bits/posix_opt.h test_ipout.o: /usr/include/bits/types.h /usr/include/bits/typesizes.h test_ipout.o: /usr/include/bits/confname.h /usr/include/getopt.h @@ -159,7 +163,7 @@ test_ipout.o: /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h test_ipout.o: /usr/include/bits/sched.h /usr/include/sys/ipc.h test_ipout.o: /usr/include/bits/ipctypes.h /usr/include/bits/ipc.h test_ipout.o: /usr/include/sys/msg.h /usr/include/bits/msq.h -test_recv.o: dev.h /usr/include/unistd.h /usr/include/features.h +test_recv.o: devque.h 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 @@ -173,7 +177,7 @@ 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: devque.h 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 diff --git a/practicas/pipi/src/dev.h b/practicas/pipi/src/dev.h index bae7e17..f06e8b1 100644 --- a/practicas/pipi/src/dev.h +++ b/practicas/pipi/src/dev.h @@ -3,7 +3,6 @@ #include -#define DEV_DEFAULT_KEY 0x1abcdef1 #define DEV_MAX_MTU 1500 /// Dispositivo de red (capa de enlace) @@ -19,20 +18,25 @@ struct Dev /// MTU size_t mtu; - /// Identificador de la cola a usar - int que_id; - /// Constructor - Dev(mac_type mac, key_t key = DEV_DEFAULT_KEY, size_t mtu = DEV_MAX_MTU) - throw (std::runtime_error, std::logic_error); + Dev(mac_type mac, size_t mtu = DEV_MAX_MTU) + throw (std::runtime_error, std::logic_error): + mac(mac), mtu(mtu) + { + if (mtu > DEV_MAX_MTU) + throw std::logic_error("MTU más grande que DEV_MAX_MTU"); + } /// Envía un frame - void transmit(const std::string& data, const mac_type& mac) - throw (std::runtime_error, std::logic_error); + virtual void transmit(const std::string& data, const mac_type& mac) + throw (std::runtime_error, std::logic_error) = 0; /// Recibe un frame - std::string receive() - throw (std::runtime_error); + virtual std::string receive() + throw (std::runtime_error) = 0; + + /// Destructor virtual por si las moscas + virtual ~Dev() {} // Nada de andar copiando placas... private: diff --git a/practicas/pipi/src/dev.cpp b/practicas/pipi/src/devque.cpp similarity index 72% rename from practicas/pipi/src/dev.cpp rename to practicas/pipi/src/devque.cpp index 57e1d3d..5cd8633 100644 --- a/practicas/pipi/src/dev.cpp +++ b/practicas/pipi/src/devque.cpp @@ -1,4 +1,4 @@ -#include "dev.h" +#include "devque.h" #include #include #include @@ -16,18 +16,16 @@ struct Frame char frame[1]; }; -Dev::Dev(mac_type mac, key_t key, size_t mtu) +DevQue::DevQue(mac_type mac, key_t key, size_t mtu) throw (std::runtime_error, std::logic_error): - mac(mac), mtu(mtu) + Dev(mac, mtu) { - if (mtu > DEV_MAX_MTU) - throw std::logic_error("MTU más grande que DEV_MAX_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) +void DevQue::transmit(const std::string& data, const mac_type& mac) throw (std::runtime_error, std::logic_error) { if (data.size() > mtu) @@ -39,16 +37,16 @@ void Dev::transmit(const std::string& data, const mac_type& 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 << ")\n"; +#ifdef DEBUG2 + std::cout << "DevQue::transmit(msgtype/mac = " << f->mac << ", size = " + << f->size << ")\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) +std::string DevQue::receive() throw (std::runtime_error) { Frame* f = (Frame*) malloc(sizeof(Frame) + mtu); if (!f) @@ -61,9 +59,9 @@ std::string Dev::receive() throw (std::runtime_error) } std::string s((char*) f->frame, f->size); free(f); -#ifdef DEBUG - //std::cout << "Dev::receive(msgtype/mac = " << mac << ", size = " - // << s.size() << ")\n"; +#ifdef DEBUG2 + std::cout << "DevQue::receive(msgtype/mac = " << mac << ", size = " + << s.size() << ")\n"; #endif return s; } diff --git a/practicas/pipi/src/devque.h b/practicas/pipi/src/devque.h new file mode 100644 index 0000000..2ee298d --- /dev/null +++ b/practicas/pipi/src/devque.h @@ -0,0 +1,32 @@ +#ifndef _DEVQUE_H_ +#define _DEVQUE_H_ + +#include "dev.h" + +#define DEVQUE_DEFAULT_KEY 0x1abcdef1 + +/// Dispositivo de red (capa de enlace) implementado con una cola +struct DevQue: Dev +{ + + /// Identificador de la cola a usar + int que_id; + + /// Constructor + DevQue(mac_type mac, key_t key = DEVQUE_DEFAULT_KEY, + size_t mtu = DEV_MAX_MTU) + throw (std::runtime_error, std::logic_error); + + /// Envía un frame + void transmit(const std::string& data, const mac_type& mac) + throw (std::runtime_error, std::logic_error); + + /// Recibe un frame + std::string receive() + throw (std::runtime_error); + +}; + +#endif // _DEVQUE_H_ + +// vim: set et sw=4 sts=4 : diff --git a/practicas/pipi/src/ip.cpp b/practicas/pipi/src/ip.cpp index dd38b9b..db99e1a 100644 --- a/practicas/pipi/src/ip.cpp +++ b/practicas/pipi/src/ip.cpp @@ -3,7 +3,7 @@ #include "ipin.h" #include "ipaddr.h" #include "routetable.h" -#include "dev.h" +#include "devque.h" #include #include #include @@ -28,7 +28,7 @@ int main(int argc, char* argv[]) bool router = false; bool forward = false; unsigned proto = 0; - key_t queue_id = DEV_DEFAULT_KEY; + key_t queue_id = DEVQUE_DEFAULT_KEY; std::string fname = "route.txt"; if (argc < 2) { @@ -49,12 +49,12 @@ int main(int argc, char* argv[]) proto = atoi(argv[6]); // Creo colas int que_id = msgget(queue_id, IPC_CREAT | 0666); assert(que_id != -1); - que_id = msgget(DEV_DEFAULT_KEY-1, IPC_CREAT | 0666); assert(que_id != -1); + que_id = msgget(DEVQUE_DEFAULT_KEY-1, IPC_CREAT | 0666); assert(que_id != -1); // Abro archivo con rutas std::ifstream ifs(fname.c_str()); assert(ifs); // Creo medio físico y cola para forwarding - Dev dev(addr, queue_id); - Dev fwque(addr, DEV_DEFAULT_KEY-1); + DevQue dev(addr, queue_id); + DevQue fwque(addr, DEVQUE_DEFAULT_KEY-1); // Creo procesos pid_t pid_send = fork(); if (pid_send == -1) diff --git a/practicas/pipi/src/test_ipin.cpp b/practicas/pipi/src/test_ipin.cpp index e170b38..12e84e0 100644 --- a/practicas/pipi/src/test_ipin.cpp +++ b/practicas/pipi/src/test_ipin.cpp @@ -1,7 +1,7 @@ #include "ipin.h" #include "ipaddr.h" -#include "dev.h" +#include "devque.h" #include #include #include @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) bool router = false; bool forward = false; unsigned proto = 0; - key_t queue_id = DEV_DEFAULT_KEY; + key_t queue_id = DEVQUE_DEFAULT_KEY; if (argc > 1) addr = IPAddr(argv[1]); if (argc > 2) @@ -35,10 +35,10 @@ int main(int argc, char* argv[]) queue_id = atoi(argv[6]); int que_id = msgget(queue_id, IPC_CREAT | 0666); assert(que_id != -1); - Dev dev(addr, mtu, queue_id); + DevQue dev(addr, mtu, queue_id); que_id = msgget(queue_id+1, IPC_CREAT | 0666); assert(que_id != -1); - Dev fwque(addr, DEV_MAX_MTU, queue_id+1); + DevQue fwque(addr, DEV_MAX_MTU, queue_id+1); IPIn ipin(addr, dev, fwque, router, forward, std::cerr); struct msqid_ds minfo; for (msgctl(dev.que_id, IPC_STAT, &minfo); minfo.msg_qnum; diff --git a/practicas/pipi/src/test_ipout.cpp b/practicas/pipi/src/test_ipout.cpp index b8e75c9..255416e 100644 --- a/practicas/pipi/src/test_ipout.cpp +++ b/practicas/pipi/src/test_ipout.cpp @@ -2,7 +2,7 @@ #include "ipout.h" #include "ipaddr.h" #include "routetable.h" -#include "dev.h" +#include "devque.h" #include #include #include @@ -16,7 +16,7 @@ // Uso: ./test_ipout ip dst mtu routes_file proto queue_id -void add_routes(RouteTable& rt, std::istream& is, Dev& dev); +void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev); int main(int argc, char* argv[]) { @@ -24,7 +24,7 @@ int main(int argc, char* argv[]) IPAddr dst("10.10.10.1"); unsigned mtu = 25; unsigned proto = 0; - key_t queue_id = DEV_DEFAULT_KEY; + key_t queue_id = DEVQUE_DEFAULT_KEY; std::string fname = "route.txt"; if (argc > 1) addr = IPAddr(argv[1]); @@ -41,10 +41,10 @@ int main(int argc, char* argv[]) int que_id = msgget(queue_id, IPC_CREAT | 0666); assert(que_id != -1); que_id = msgget(queue_id+1, IPC_CREAT | 0666); assert(que_id != -1); std::ifstream ifs(fname.c_str()); assert(ifs); - Dev dev(addr, mtu, queue_id); - Dev fwque(addr, DEV_MAX_MTU, queue_id+1); + DevQue dev(addr, mtu, queue_id); + DevQue fwque(addr, DEV_MAX_MTU, queue_id+1); RouteTable table(dev); - add_routes(table, ifs, dev); + add_routes(table, ifs, mtu, dev); IPOut ipout(addr, table, fwque, std::cerr); std::string msg; while (std::getline(std::cin, msg)) @@ -57,7 +57,7 @@ int main(int argc, char* argv[]) return 0; } -void add_routes(RouteTable& rt, std::istream& is, Dev& dev) +void add_routes(RouteTable& rt, std::istream& is, size_t mtu, Dev& dev) { std::string line; while (std::getline(is, line)) @@ -69,7 +69,7 @@ void add_routes(RouteTable& rt, std::istream& is, Dev& dev) iss >> net >> gw >> metric; if (net == "0") net = "0.0.0.0"; if (gw == "0") gw = "0.0.0.0"; - rt.add(net.c_str(), gw.c_str(), metric, dev); + rt.add(net.c_str(), gw.c_str(), mtu, metric, dev); } } diff --git a/practicas/pipi/src/test_recv.cpp b/practicas/pipi/src/test_recv.cpp index 2471b4a..fb8be43 100644 --- a/practicas/pipi/src/test_recv.cpp +++ b/practicas/pipi/src/test_recv.cpp @@ -1,5 +1,5 @@ -#include "dev.h" +#include "devque.h" #include #include #include @@ -9,7 +9,7 @@ int main() { - Dev dev(4321); + DevQue dev(4321); struct msqid_ds minfo; for (msgctl(dev.que_id, IPC_STAT, &minfo); minfo.msg_qnum; msgctl(dev.que_id, IPC_STAT, &minfo)) diff --git a/practicas/pipi/src/test_send.cpp b/practicas/pipi/src/test_send.cpp index 618b862..5b78b2c 100644 --- a/practicas/pipi/src/test_send.cpp +++ b/practicas/pipi/src/test_send.cpp @@ -1,5 +1,5 @@ -#include "dev.h" +#include "devque.h" #include #include #include @@ -10,9 +10,9 @@ int main() { - int que_id = msgget(DEV_DEFAULT_KEY, IPC_CREAT | 0666); + int que_id = msgget(DEVQUE_DEFAULT_KEY, IPC_CREAT | 0666); assert(que_id != -1); - Dev dev(1234); + DevQue dev(1234); dev.transmit("hola mundo", 4321); std::cout << "Enviado 'hola mundo' a 4321\n"; return 0;