/* * File: Comm.h * Author: penny * * Created on March 26, 2012, 1:22 PM */ #ifndef COMM_H #define COMM_H ///////////////////////////////////////////////////////////////////// // Comm.h - Enables sending Messages between machines // // ver 1.0 // // Language: C++, Netbeans // // platform: Kubuntu11.10 // // Application: Spring 2012 DO Project 2 // // Author: Yun Xing, Syracuse University, CST 4-282 // // yxing03@syr.edu // Resource: Jim Fawcett /////////////////////////////////////////////////////////////////////// /* Package Operations: =================== This package provides network communication services, using linux sockets. Four classes are provided: EndPoint: --------- Puts address information into and pulls out of strings of the form: 127.0.0.1:8000 Message: -------- Supports defining instances that hold type strings - either "text" or "file", EndPoints, and a text body. Messages have the ability to serialize themselves into XML strings and to deserialize a properly formatted XML string into a Message. Sender: ------- Supports connecting and posting messages to a remote EndPoint. Receiver: --------- Starts a listener that waits for connections, then starts a receive thread, for each connection, that frames XML Message strings into Message instances and enqueues them. It provides a GetMsg() function that retrieves messages from the queue. Public Interface: ================= EndPoint ep("127.0.0.1",8000); Receiver rcvr(8000); Sender sndr; sndr.connect(ep); Message msg("this is a msg body",ep,"text"); sndr.PostMsg(msg); sndr.wait(); Message rMsg = rcvr.GetMsg(); */ /* Build Process: ============== Required Files: Comm.h, Comm.cpp, Sockets.h, Sockets.cpp, locks.h, locks.cpp Threads.h, Threads.cpp, BlockingQ.h, BlockingQ.cpp, XmlReader.h, XmlReader.cpp, XmlWriter.h, XmlWriter.cpp Compile Command: ================ cl /EHa /DTEST_COMM Comm.cpp Sockets.cpp locks.cpp Threads.cpp BlockingQ.cpp XmlDocument.lib wsock32.lib Maintenance History: ==================== ver 1.0 : Mar27 2012 - first release */ #include "Sockets.h" #include "Threads.h" #include "BlockingQueue.h" #include "Message.h" ///////////////////////////////////////////////////////////////////////// // Supports sending Message instances // - Attempts to connect to EndPoint. If that fails, returns false. // - Has send queue so PostMsg(msg) returns quickly namespace SocketCommunicator { class Sender { public: Sender(); bool connect(EndPoint pEp); void PostMsg(const Message& pMsg); void wait(); private: BQueue sendQ; tthread* pSendThread; Socket s_; }; ///////////////////////////////////////////////////////////////////////// // Supports receiving Message instances // - listener starts on specified port when constructed. // - will throw exception if another listener already occuppies the // specified port // - Will block on GetMsg() if the receive queue is empty. class Receiver { public: Receiver(long port); Message GetMsg(); private: SocketListener s_; BQueue recvQ; tthread* pListenThread; }; } #endif /* COMM_H */