#ifndef COMMUNICATOR_H #define COMMUNICATOR_H ///////////////////////////////////////////////////////////////////// // Communicator.h - communication channel based on sockets // // // // Lanaguage: Visual C++, VS2005 // // Platfrom: Dell Dimension 9150, Windows XP Pro, SP 2.0 // // Application: Demonstration for CSE775 - Distributed Objects // // Author: Jim Fawcett, CST 2-187, Syracuse University // // (315) 443-3980, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// /* Module Operations: ================== Establishes two-way message-passing communication, based on sockets. Public Interface: ================= Communicator client; Communicator server; client.listen(2048); // client listens for connects server.listen(2049); // server listens for connects client.connect("localhost",2049); // connect channel to server server.connect("localhost",2048); // connect channel to client client.postMessage("hi"); // send message to connected listener message msg = server.getMessage(); // blocks if receive queue is empty if(server.hasMessage()) // avoids blocking on empty queue mst = server.getMessage(); */ /* Build Process: ============== Required Files: Communicator.h, Communicator.cpp, Sockets.h, Sockets.cpp, BlockingQueue.h, BlockingQueue.cpp, Threads.h, Threads.cpp, Locks.h, Locks.cpp Compiler Command: cl /EHa Communicator.cpp Sockets.cpp BlockingQueue.cpp \ Threads.cpp Locks.cpp */ #include #include #include "Sockets.h" #include "Threads.h" #include "BlockingQueue.h" typedef std::string message; class SendProc; class RecvProc; //----< convert insertable types to strings >------------------------ template std::string convert(const T& t) { std::ostringstream temp; temp.precision(8); temp << t; return temp.str(); } // ///////////////////////////////////////////////////////////////////// // Communicator provides send and receive sockets, threads, and queues // class Communicator { friend class ListenProc; public: Communicator(); ~Communicator(); bool connect(const std::string& url, int port); void listen(int port); bool hasMessage(); message getMessage(); void postMessage(const message& msg); // future addition void stopRecvr() { /* gracefully stop receiving */ } void stopSendr() { /* gracefully stop sending */ } void stopLisnr() { /* gracefully stop listening */ } private: SocketSystem ss; // listener thread* pListenThread; SocketListener* pListener; ListenProc* pListenProc; bool listenrStop; // sender thread* pSendThread; BQueue* pSendQ; Socket* pSendSock; SendProc* pSendProc; bool sendrStop; // receiver thread* pRecvThread; BQueue* pRecvQ; Socket* pRecvSock; RecvProc* pRecvProc; bool recvrStop; }; #endif