///////////////////////////////////////////////////////////////////// // Message.cpp - Defines EndPoints and Messages // // ver 1.1 // // Language: Visual C++, 2008 // // Platform: Dell Dimension T7400, Win 7 Pro // // Application: Utility for CSE687 projects // // Author: Jim Fawcett, Syracuse University, CST 4-187 // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// #include "Message.h" #include "EndPoint.h" #include "IXmlDocument.h" #include #include using namespace XmlProcessing; /////////////////////////////////////////////////////////////// // Build Messages Message::Message(const std::string& text) : body_(text), addr_(EndPoint("127.0.0.1",8000)), type_("text") {} Message::Message(const std::string& body, EndPoint addr, const std::string& type) : body_(body), addr_(addr), type_(type) {} Message::Message(const Message& msg) : body_(msg.body_), addr_(msg.addr_), type_(msg.type_) {} std::string& Message::body() { return body_; } std::string& Message::address() { return addr_.ToString(); } std::string& Message::type() { return type_; } //std::string& Message::getEndPoint() { return addr_.ToString(); } // ////----< return string form of Message type >----------------------------- // //std::string Message::TypeToString() //{ // switch(type_) // { // case text: // return "text"; // case file: // return "file"; // } // return "unknown type"; //} //----< serialize message to string >------------------------------------ std::string Message::ToString() { std::string temp = ""; temp += std::string("") + type() + ""; temp += std::string("
") + address() + "
"; temp += std::string("") + body() + "
"; return temp; } //----< remove quotes from quoted string >------------------------------- std::string Message::trimQuotes(const std::string& s) { if(s.size() > 0 && s[0] == '"') return s.substr(1,s.size()-2); if(s.size() > 0 && s[0] == '\'') return s.substr(1,s.size()-2); return s; } //----< deserialize message string into Message instance >--------------- Message Message::FromString(const std::string& xml) { std::vector children; Message msg(""); std::string temp; XmlDocumentFactory factory; IXmlDocument* pDoc = factory.CreateXmlDocument(); pDoc->verbose() = false; pDoc->loadString(xml); IXmlNode* pNode = pDoc->findElement("type",IXmlDocument::tag); if(pNode) { children = pNode->getChildren(); msg.type() = children[0]->value(); //if(children[0]->value() =="text") // msg.type() = Message::text; //else // msg.type() = Message::file; } pNode = pDoc->findElement("address",IXmlDocument::tag); if(pNode) { children = pNode->getChildren(); msg.address() = children[0]->value(); } pNode = pDoc->findElement("body",IXmlDocument::tag); if(pNode) { children = pNode->getChildren(); msg.body() = trimQuotes(children[0]->value()); } return msg; } IMessage* IMessage::CreateMessage(const std::string& body) { return new Message(body); } EndPoint testEndPoint() { EndPoint ep("127.0.0.1", 8000); std::cout << "\n EndPoint = " << ep.ToString(); std::cout << "\n Address = " << ep.address(); std::cout << "\n Port = " << ep.port(); return ep; } Message testMessage(EndPoint ep) { Message msg("This is a message", ep, "text"); std::cout << "\n Message body = " << msg.body(); std::cout << "\n Message return address = " << msg.address(); std::cout << "\n Message type = " << msg.type(); std::cout << "\n"; std::cout << "\n Message:"; std::cout << "\n " << msg.ToString(); return msg; } //----< test stub >------------------------------------------------------ #ifdef TEST_MESSAGE void main() { std::cout << "\n Demonstrating EndPoint and Message Operations"; std::cout << "\n ===============================================\n"; EndPoint ep = testEndPoint(); std::cout << "\n"; Message msg = testMessage(ep); IMessage* pMsg = IMessage::CreateMessage("message on heap"); pMsg->type() = "file"; pMsg->address() = "127.0.0.1:8001"; std::cout << "\n Message:\n " << pMsg->ToString(); std::cout << "\n\n"; } #endif