本文整理汇总了C++中HttpMessage::findValue方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpMessage::findValue方法的具体用法?C++ HttpMessage::findValue怎么用?C++ HttpMessage::findValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpMessage
的用法示例。
在下文中一共展示了HttpMessage::findValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
::SetConsoleTitle("ClientHandler");
Show::attach(&std::cout);
Show::start();
Show::title("\n ClientHandler Server started");
BlockingQueue<HttpMessage> msgQ;
SocketSystem ss;
SocketListener sl(8080, Socket::IP6);
ClientHandler cp(msgQ);
sl.start(cp);
try
{
while (true)
{
HttpMessage msg = msgQ.deQ();
std::cout << "\n\n clienthandler " + msg.findValue("toAddr") + " recvd message contents:\n "
+ msg.bodyString() + "\n from client " + msg.findValue("fromAddr") + "\n";
if (msg.bodyString() == "closeServer") {
::Sleep(100);
}
}
}
catch (std::exception& exc)
{
Show::write("\n Exeception caught: ");
std::string exMsg = "\n " + std::string(exc.what()) + "\n\n";
Show::write(exMsg);
}
std::cout << "\n\n";
return 0;
}
示例2: startReceiverThread
// ------------------------------------ Starts Receiver for the this component ---------------------------
void Server::startReceiverThread()
{
BlockingQueue<HttpMessage> msgQ;
try
{
SocketSystem ss;
SocketListener sl(getServerPort(), Socket::IP6);
Receiver cp(msgQ,"Server");
sl.start(cp);
/*
* Since this is a server the loop below never terminates.
* We could easily change that by sending a distinguished
* message for shutdown.
*/
while (true)
{
HttpMessage msg = msgQ.deQ();
std::string cPort = msg.findValue("From-Port");
if (msg.attributes()[0].first == "GET") { // Checks if its an file extraction request
std::string filename = msg.findValue("file");
Show::write("\n\n Client @: "+cPort+ " => requested file :: " + filename);
sendServerFiles(msg);
}
else
Show::write("\n\n Message from client @:"+cPort +" => " + msg.bodyString());
}
}
catch (std::exception& exc)
{
Show::write("\n Exeception caught: ");
std::string exMsg = "\n " + std::string(exc.what()) + "\n\n";
Show::write(exMsg);
}
}
示例3: receiveFile
//receriveFile
void MsgClient::receiveFile(Socket& socket) {
HttpMessage msg;
while (true)
{
std::string attribString = socket.recvString('\n');
if (attribString.size() > 1)
{
HttpMessage::Attribute attrib = HttpMessage::parseAttribute(attribString);
msg.addAttribute(attrib);
}
else
{
break;
}
}
std::string filename = msg.findValue("file");
if (filename != "")
{
size_t contentSize;
std::string sizeString = msg.findValue("content-length");
if (sizeString != "")
contentSize = Converter<size_t>::toValue(sizeString);
downloadFile(filename, contentSize, socket);
Show::write("Client download file "+filename + "\n");
}
}
示例4: errorMessage
// -----------------< prepare error message >--------------------
HttpMessage ClientHandler::errorMessage(HttpMessage msg, std::string body) {
HttpMessage message;
message.addAttribute(HttpMessage::attribute("type", "error"));
message.addAttribute(HttpMessage::attribute("fromAddr", msg.findValue("toAddr")));
message.addAttribute(HttpMessage::attribute("toAddr", msg.findValue("fromAddr")));
message.addBody(body);
return message;
}
示例5: sendMessage
// ------------------< prepare and send message to recepient >-------------
void ClientHandler::sendMessage(HttpMessage msg) {
HttpMessage message;
Sender sndr(msg.findValue("fromAddr"));
sndr.start();
message = sndr.makeMessage(0, msg.bodyString(), msg.findValue("toAddr"));
sndr.postMessage(message);
sndr.postMessage(sndr.makeMessage(0, "closeServer", msg.findValue("toAddr")));
sndr.postMessage(sndr.makeMessage(0, "quit", msg.findValue("toAddr")));
sndr.wait();
}
示例6: saveFileClient
// --------------< save the file to the clients machine >------------------
void ClientHandler::saveFileClient(HttpMessage msg, Socket& socket) {
std::string filename = msg.findValue("file").substr(msg.findValue("file").find_last_of("/") + 1);
std::string path = "../TestFileClient/";
FileSystem::Directory::create(path);
size_t contentSize;
std::string sizeString = msg.findValue("content-length");
if (sizeString != "")
contentSize = Converter<size_t>::toValue(sizeString);
else
return;
readFile(filename, contentSize, socket, path);
}
示例7: getFileNameList
// ----------------< get the list of files in a specified package >-------------
HttpMessage ClientHandler::getFileNameList(HttpMessage msg) {
std::vector<std::string> fileList = FileSystem::Directory::getFiles("../TestFileServer/" + msg.findValue("dir"));
std::string body = "";
for (size_t i = 0; i < fileList.size(); ++i)
if (fileList[i] != "." && fileList[i] != "..")
body += "\n " + fileList[i];
body += "\n";
HttpMessage message;
message.addAttribute(HttpMessage::attribute("fromAddr", msg.findValue("toAddr")));
message.addAttribute(HttpMessage::attribute("toAddr", msg.findValue("fromAddr")));
message.addAttribute(HttpMessage::attribute("type", "returnList"));
message.addBody(body);
return message;
}
示例8: start
// -------------< establish client handler and start receiving messages >--------------
void Receiver::start() {
ClientHandler ch(rcvrQ);
size_t port = stoi(localUrl.substr(localUrl.find(":") + 1, 4));
SocketSystem ss;
SocketListener sl(port, Socket::IP6);
sl.start(ch);
while (true)
{
HttpMessage msg = rcvrQ.deQ();
std::cout << "\n\n " + msg.findValue("toAddr") + " recvd message contents:\n "
+ msg.bodyString() + "\n from " + msg.findValue("fromAddr");
if (msg.bodyString() == "closeServer") break;
}
}
示例9: constructMessage
// ---------------< construct a general message to be sent as acknowledgement >---------
HttpMessage ClientHandler::constructMessage(HttpMessage msg) {
msg.removeAttribute("content-length");
std::string bodyString = "<file>" + msg.findValue("file") + "</file>";
std::string sizeString = Converter<size_t>::toString(bodyString.size());
msg.addAttribute(HttpMessage::Attribute("content-length", sizeString));
msg.addBody(bodyString);
return msg;
}
示例10: getFiles
// -----------------< get the requested files, with or without dependencies >------------
HttpMessage ClientHandler::getFiles(HttpMessage msg, std::string type) {
HttpMessage message;
if (!FileSystem::Directory::exists("../TestFileServer/" + msg.bodyString()))
return errorMessage(msg, "\n no package " + msg.bodyString());
std::string path = "../TestFileServer/" + msg.bodyString() + "/";
std::vector<std::string> files = FileSystem::Directory::getFiles(path, "*.ver*");
if (files.size() == 0) return errorMessage(msg, "\n so files in package " + msg.bodyString());
std::vector<std::string> sendFiles;
for (int i = files.size() - 1; i >= 0; --i) { // figure out the latest version of the files
std::string ext = files[i].substr(files[i].find_last_of(".") + 1);
if (ext == "xml") continue;
std::string name = files[i].substr(0, files[i].find_last_of("."));
if (find(sendFiles, name) == sendFiles.size())
sendFiles.push_back(files[i]); // add only unique file names to be sent
}
std::vector<std::shared_ptr<AbstractXmlElement>> deps;
if (type == "FileWithDeps") { // figure out dependencies of files to be sent
for (int i = 0; i < sendFiles.size(); ++i) {
std::string file = sendFiles[i];
if (!FileSystem::File::exists("../TestFileServer/" + msg.bodyString() + "/" + file)) continue;
std::string xmlname = "../TestFileServer/" + msg.bodyString() + "/" + file + ".xml";
XmlDocument doc(xmlname, XmlDocument::file);
deps = doc.element("deps").descendents().select();
for (auto dep : deps)
if (dep->tag() == "") { // prevent circular dependencies to be stuck in a loop
std::string depName = dep->value().substr(0, dep->value().find("\n"));
if (find(sendFiles, depName.substr(0, depName.find_last_of("."))) == sendFiles.size()) sendFiles.push_back(depName);
}
}
}
std::string body;
for (size_t i = 0; i < sendFiles.size() - 1; ++i) body += sendFiles[i] + ",";
body += sendFiles[sendFiles.size() - 1];
message.addAttribute(HttpMessage::attribute("fromAddr", msg.findValue("toAddr")));
message.addAttribute(HttpMessage::attribute("toAddr", msg.findValue("fromAddr")));
message.addAttribute(HttpMessage::attribute("type", "returnFiles"));
message.addBody(body);
return message;
}
示例11: sendServerFiles
// -------------------- This Function sends the requested file to the client and checks if the dependencies need to be sent--------------------
void Server::sendServerFiles(HttpMessage msg)
{
SocketSystem ss;
SocketConnecter si;
std::string cPort = msg.findValue("From-Port");
int ClientPort = Converter<int>::toValue(cPort);
while (!si.connect("localhost", ClientPort))
{
::Sleep(100);
} // Searches for the revlevant folder and then sends the file
Sender send;
std::string DepsFlag = msg.findValue("ExtractWithDepsFlag");
std::string fileToSend = msg.findValue("file");
std::string path;
std::vector<std::string> dirs = FileSystem::Directory::getDirectories("./ServerDrive", fileToSend+"*");
if (dirs.size() == 0)
Show::write("No such file present in the repository => " + fileToSend + "\n");
if (dirs.size() == 1)
{
std::vector<std::string> files = FileSystem::Directory::getFiles("./ServerDrive/" + dirs[0], fileToSend + "*");
Show::write("\n Sending File :: " + files[0] + " to Client @: " + cPort);
send.sendFile("./ServerDrive/" + dirs[0] + "/" + files[0], si, getServerPort(), ClientPort);
path = "./ServerDrive/" + dirs[0] + "/";
if (DepsFlag == "Yes")
sendFileDependencies(msg, path,si);
}
if (dirs.size() > 1)
{
std::string latest_dir = getLatestDir(dirs,fileToSend);
std::vector<std::string> files = FileSystem::Directory::getFiles("./ServerDrive/" + latest_dir, fileToSend + "*");
Show::write("\n Sending File :: " + files[0] + " to Client @: " + cPort);
send.sendFile("./ServerDrive/" + latest_dir + "/" + files[0], si , getServerPort(), ClientPort);
path = "./ServerDrive/" + latest_dir + "/";
if (DepsFlag == "Yes")
sendFileDependencies(msg, path,si);
}
msg = makeMessage(1, "quit", "toAddr:localhost:"+cPort);
send.sendMessage(msg, si);
}
示例12: sendFileDependencies
// -------------------- This Function sends the Dependent files to the client--------------------
void Server::sendFileDependencies(HttpMessage msg, std::string path, Socket & socket)
{
std::string cPort = msg.findValue("From-Port");
int ClientPort = Converter<int>::toValue(cPort);
std::string requestedFile = msg.findValue("file");
Sender send;
std::vector<std::string> depFile = FileSystem::Directory::getFiles(path, "*.xml");
std::string src = path + depFile[0];
XmlParser parser(src);
XmlDocument* pDoc = parser.buildDocument();
std::vector<std::string> depFileNames;
std::vector<sPtr> found = pDoc->element("Dependencies").descendents().select(); // Read the dependencies xml
if (found.size() > 0)
{
for (auto pElem : found)
{
if (pElem->value() != "dep")
depFileNames.push_back(pElem->value());
}
}
for (std::string f : depFileNames) { // Sending each of the dependency file if exists
std::string fileToSend = Utilities::StringHelper::trim(f);
std::vector<std::string> dirs = FileSystem::Directory::getDirectories("./ServerDrive/", fileToSend +"*");
if (dirs.size() == 0)
Show::write("No such file present in the repository => " + fileToSend + "\n");
if (dirs.size() > 0)
{
std::string latest_dir = getLatestDir(dirs, fileToSend);
std::vector<std::string> files = FileSystem::Directory::getFiles("./ServerDrive/" + latest_dir, fileToSend + "*");
Show::write("\nSending Dependency File of "+ requestedFile+ " => " + files[0] + " to client @: "+cPort);
send.sendFile("./ServerDrive/" + latest_dir + "/" + files[0], socket , getServerPort(), ClientPort);
path = "./ServerDrive/" + latest_dir + "/";
}
}
msg = makeMessage(1, "quit", "toAddr:localhost:"+cPort);
send.sendMessage(msg, socket);
}
示例13: msgBody
HttpMessage MsgClient::readMessage(Socket& socket) {
Show::write("Client readMessage \n");
//connectionClosed_ = false;
HttpMessage msg;
//read attribute
while (true)
{
std::string attribString = socket.recvString('\n');
if (attribString.size() > 1)
{
HttpMessage::Attribute attrib = HttpMessage::parseAttribute(attribString);
msg.addAttribute(attrib);
}
else
{
break;
}
}
if (msg.attributes().size() == 0)
{
return msg;
}
if (msg.attributes()[0].first == "POST")
{
std::string filePath = msg.findValue("filePath");
if (filePath != "")
{
Show::write("Test for get FolderList");
size_t numBytes = 0;
size_t pos = msg.findAttribute("content-length");
if (pos < msg.attributes().size())
{
numBytes = Converter<size_t>::toValue(msg.attributes()[pos].second);
Socket::byte* buffer = new Socket::byte[numBytes + 1];
socket.recv(numBytes, buffer);
buffer[numBytes] = '\0';
std::string msgBody(buffer);
msg.addBody(msgBody);
Show::write("getFolderList:" + msgBody + "\n");
}
}
}
return msg;
}
示例14: operator
//----< receiver functionality is defined by this function >---------
void ClientHandler::operator()(Socket socket) {
std::function<void()> threadProc = [&]() {
while (true) {
HttpMessage msg = readMessage(socket); // read a message from socket
if (msg.attributes().size() == 0 || msg.bodyString() == "quit") break;
if (msg.findValue("type") == "returnList") {
sendMessage(msg);
continue;
}
else if (msg.findValue("type") == "returnFiles") {
Sender sndr(msg.findValue("fromAddr"));
sndr.start();
std::vector<std::string> files = splitString(msg.bodyString());
for (std::string file : files) {
std::string fileSpec = searchFile(file);
FileSystem::FileInfo fi(fileSpec);
FileSystem::File fileIn(fileSpec);
fileIn.open(FileSystem::File::in, FileSystem::File::binary);
if (!fileIn.isGood()) {
std::cout << "\n could not open file " << file;
continue;
}
size_t fileSize = fi.size();
std::string sizeString = Converter<size_t>::toString(fileSize);
HttpMessage message = sndr.makeMessage(4, "", msg.findValue("toAddr"));
message.addAttribute(HttpMessage::Attribute("file", fileSpec));
message.addAttribute(HttpMessage::Attribute("content-length", sizeString));
sndr.postMessage(message);
}
sndr.postMessage(sndr.makeMessage(0, "closeServer", msg.findValue("toAddr")));
sndr.postMessage(sndr.makeMessage(0, "quit", msg.findValue("toAddr")));
sndr.wait();
continue;
}
else if (msg.attributes()[0].second == "error") {
sendMessage(msg);
continue;
}
msgQ_.enQ(msg);
}
};
std::thread receiveThread(threadProc);
receiveThread.join();
}
示例15: saveFileServer
// -----------< save a file to the server >---------------
void ClientHandler::saveFileServer(HttpMessage msg, Socket& socket) {
FileSystem::Directory::create("../TestFileServer/");
std::string filename = msg.findValue("file") + ".ver1";
std::string packageName = msg.findValue("dir") + "." + getCurrentDate();
std::string path = "../TestFileServer/" + packageName + "/";
FileSystem::Directory::create(path);
std::string dependencies = msg.findValue("deps");
std::vector<std::string> deps;
if(dependencies != "") deps = findDeps(dependencies);
std::vector<std::string> files;
for (auto fileSpec : FileSystem::Directory::getFiles(path, msg.findValue("file") + ".ver*"))
if (fileSpec.substr(fileSpec.find_last_of(".")) != ".xml") files.push_back(fileSpec);
if (files.size() > 0) {
std::string name = files[files.size() - 1];
int ver = std::stoi(name.substr(name.find_last_of(".") + 4));
filename = msg.findValue("file") + ".ver" + std::to_string(ver + 1);
}
std::string xmlpath = path + filename + ".xml";
XmlDocument doc = builDocument(filename, deps);
FileSystem::File file(xmlpath);
file.open(FileSystem::File::out, FileSystem::File::text);
if (file.isGood())
file.putLine(doc.toString());
file.close();
size_t contentSize;
std::string sizeString = msg.findValue("content-length");
if (sizeString != "")
contentSize = Converter<size_t>::toValue(sizeString);
else
return;
readFile(filename, contentSize, socket, path);
updateXML(packageName, msg.findValue("file"), deps);
}