Exemple #1
0
/*
 * - Sends a message to tell receiver a file is coming.
 * - Then sends a stream of bytes until the entire file
 *   has been sent.
 * - Sends in binary mode which works for either text or binary.
 */
bool MsgClient::sendFile(const std::string& filename, Socket& socket)
{
  // assumes that socket is connected

  std::string fqname = "../TestFiles/" + filename;
  FileSystem::FileInfo fi(fqname);
  size_t fileSize = fi.size();
  std::string sizeString = Converter<size_t>::toString(fileSize);
  FileSystem::File file(fqname);
  file.open(FileSystem::File::in, FileSystem::File::binary);
  if (!file.isGood())
    return false;
  
  HttpMessage msg = makeMessage(1, "", "localhost::8080");
  msg.addAttribute(HttpMessage::Attribute("file", filename));
  msg.addAttribute(HttpMessage::Attribute("content-length", sizeString));
  sendMessage(msg, socket);
  const size_t BlockSize = 2048;
  Socket::byte buffer[BlockSize];
  while (true)
  {
    FileSystem::Block blk = file.getBlock(BlockSize);
    if (blk.size() == 0)
      break;
    for (size_t i = 0; i < blk.size(); ++i)
      buffer[i] = blk[i];
    socket.send(blk.size(), buffer);
    if (!file.isGood())
      break;
  }
  file.close();
  return true;
}
// -------------- << Sends File via socket >> -----------------
bool Sender::sendFile(const std::string & filename, Socket & socket, int fPort, int tport)
{	
	std::string fqname = filename;
	FileSystem::FileInfo fi(fqname);
	size_t fileSize = fi.size();
	std::string sizeString = Converter<size_t>::toString(fileSize);
	std::string toPort = Converter<int>::toString(tport);
	std::string fromPort = Converter<int>::toString(fPort);
	FileSystem::File file(fqname);
	file.open(FileSystem::File::in, FileSystem::File::binary);
	if (!file.isGood())
		return false;
	HttpMessage msg = makeMessage(1, "", "localhost::"+toPort);
	msg.addAttribute(HttpMessage::Attribute("file", filename));
	msg.addAttribute(HttpMessage::Attribute("content-length", sizeString));
	msg.addAttribute(HttpMessage::Attribute("To-Port", toPort));
	msg.addAttribute(HttpMessage::Attribute("From-Port", fromPort));
	sendMessage(msg, socket);
	const size_t BlockSize = 2048;
	Socket::byte buffer[BlockSize];
	while (true)
	{
		FileSystem::Block blk = file.getBlock(BlockSize);
		if (blk.size() == 0)
			break;
		for (size_t i = 0; i < blk.size(); ++i)
			buffer[i] = blk[i];
		socket.send(blk.size(), buffer);
		if (!file.isGood())
			break;
	}
	file.close();
	return true;
}
// -----------------< 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;
}
// ----------------< 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;
}
// -------------- << Process file extraction request of client via socket >> -----------------
void Sender::getFiles(std::string filename, Socket & socket, bool ExtractWithDepsFlag, int cPort, int sPort)
{
	std::string clientPort = Converter<int>::toString(cPort);
	std::string serverPort = Converter<int>::toString(sPort);
	HttpMessage msg = makeMessage(2, "", "localhost::8080");
	msg.addAttribute(HttpMessage::Attribute("file", filename));
	msg.addAttribute(HttpMessage::Attribute("To-Port", serverPort));
	msg.addAttribute(HttpMessage::Attribute("From-Port", clientPort));
	std::string depFlag;
	if (ExtractWithDepsFlag) depFlag = "Yes";
	else
		depFlag = "No";
	msg.addAttribute(HttpMessage::Attribute("ExtractWithDepsFlag", depFlag));
	sendMessage(msg, socket);
}
Exemple #6
0
//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");
	}
}
// ---------------< 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;
}
Exemple #8
0
//-----< send Dependency using socket
void MsgClient::sendDependency(Socket& socket) {
	std::string dependency;
	dependency = "A.cpp:B.cpp Ctest.h:C.cpp";
	HttpMessage msg = makeMessage(1, dependency, "localhost::8080");
	msg.addAttribute(HttpMessage::Attribute("dependency", "dependency"));
	sendMessage(msg, socket);
	Show::write("\n\n  sending dependency " +dependency);
}
//----< this defines processing to frame messages >------------------
HttpMessage ClientHandler::readMessage(Socket& socket) {
	connectionClosed_ = false;
	HttpMessage msg;
	while (true) { // read message attributes
		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) { // If client is done, connection breaks
		connectionClosed_ = true;					// and recvString returns empty string
		return msg;
	}
	if (msg.attributes()[0].first == "POST") // read body if POST
	{
		if (msg.attributes()[0].second == "Message") msg = readBody(msg, socket); // case 0 - normal message
		else if (msg.attributes()[0].second == "File") { // case 1 - client sending file to server
			saveFileServer(msg, socket);
			msg = constructMessage(msg);
		}
		else if (msg.attributes()[0].second == "closePackage") msg = closePackage(msg); // case 7 - close a package
		else if (msg.attributes()[0].second == "returnFile") { // case 4 - server sends files to client
			saveFileClient(msg, socket);
			msg = constructMessage(msg);
		}
	}
	else if (msg.attributes()[0].first == "GET") { // read message if GET
		msg = readBody(msg, socket);
		if (msg.attributes()[0].second == "getFileList")  msg = getFileList(msg); // case 5 - request list of packages
		else if (msg.attributes()[0].second == "getFileNameList")  msg = getFileNameList(msg); // case 6 - request list of files in a packages
		else if (msg.attributes()[0].second == "File" || msg.attributes()[0].second == "FileWithDeps")
			msg = getFiles(msg, msg.attributes()[0].second);
	}
	else {
		msg.removeAttribute("content-length");
		std::string bodyString = "<msg>Error message</msg>";
		std::string sizeString = Converter<size_t>::toString(bodyString.size());
		msg.addAttribute(HttpMessage::Attribute("content-length", sizeString));
		msg.addBody(bodyString);
	}
	return msg;
}
//----< 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();
}
Exemple #11
0
/*
 * This function only creates one type of message for this demo.
 * - To do that the first argument is 1, e.g., index for the type of message to create.
 * - The body may be an empty string.
 * - EndPoints are strings of the form ip:port, e.g., localhost:8081. This argument
 *   expects the receiver EndPoint for the toAddr attribute.
 */
HttpMessage MsgClient::makeMessage(size_t n, const std::string& body, const EndPoint& ep)
{
  HttpMessage msg;
  HttpMessage::Attribute attrib;
  EndPoint myEndPoint = "localhost:8081";  // ToDo: make this a member of the sender
                                           // given to its constructor.
  switch (n)
  {
  case 1:
    msg.clear();
    msg.addAttribute(HttpMessage::attribute("POST", "Message"));
    msg.addAttribute(HttpMessage::Attribute("mode", "oneway"));
    msg.addAttribute(HttpMessage::parseAttribute("toAddr:" + ep));
    msg.addAttribute(HttpMessage::parseAttribute("fromAddr:" + myEndPoint));

    msg.addBody(body);
    if (body.size() > 0)
    {
      attrib = HttpMessage::attribute("content-length", Converter<size_t>::toString(body.size()));
      msg.addAttribute(attrib);
    }
    break;
  default:
    msg.clear();
    msg.addAttribute(HttpMessage::attribute("Error", "unknown message type"));
  }
  return msg;
}
// -----------------< 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;
}
// ----------------< close a package to prevent addditional updating >-------------
HttpMessage ClientHandler::closePackage(HttpMessage msg) {
	HttpMessage message;
	if (!FileSystem::Directory::exists("../TestFileServer/" + msg.findValue("dir"))) {
		message.addAttribute(HttpMessage::attribute("type", "error"));
		message.addAttribute(HttpMessage::attribute("fromAddr", msg.findValue("toAddr")));
		message.addAttribute(HttpMessage::attribute("toAddr", msg.findValue("fromAddr")));
		std::string str = "\n  no package with the name " + msg.findValue("dir");
		message.addBody(str);
		return message;
	}
	std::string name = "../TestFileServer/" + msg.findValue("dir") + "/" + msg.findValue("dir") + "." + getCurrentDate() + ".xml";
	XmlDocument doc(name, XmlDocument::file);
	sPtr close = makeTaggedElement("close");
	close->addChild(makeTextElement("true"));
	doc.xmlRoot()->addChild(close);
	FileSystem::File::remove(name);
	FileSystem::File xml(name);
	xml.open(FileSystem::File::out, FileSystem::File::text);
	if (xml.isGood()) xml.putLine(doc.toString());
	xml.close();
	return message;
}
Exemple #14
0
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;
}
int main()
{
  Utils::Title("Testing HttpMessage class", '=');

  HttpMessage msg;
  msg.addAttribute(HttpMessage::attribute("Command", "GetFiles"));
  msg.addAttribute(HttpMessage::attribute("ToAddr", "127.0.0.1:8080"));
  msg.addAttribute(HttpMessage::attribute("FromAddr", "127.0.0.1:8081"));
  msg.addAttribute(HttpMessage::attribute("Mode", "OneWay"));
  msg.addAttribute(HttpMessage::attribute("content_length", "10"));

  msg.addBody(std::vector<HttpMessage::byte> { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });

  std::cout << "\n" << Utils::addHeaderAndFooterLines(msg.toString());
  
  Utils::title("testing headerString(), bodyString(), and toString()");
  std::cout << "\nheader string:";
  std::cout << "\n" << msg.headerString();
  std::cout << "\nbody string:";
  std::cout << "\n" << msg.bodyString();
  std::cout << "\n\nmessage string:";
  std::cout << "\n" << StringHelper::addHeaderAndFooterLines(msg.toString());

  Utils::title("Testing removeAttribute");
  putline();
  msg.removeAttribute("content_length");
  std::cout << msg.toString();
  putline();

  Utils::title("Testing addBody(const std::string&)");
  std::string msgBody = "<msg>this is a message</msg>";
  msg.addAttribute(HttpMessage::Attribute("content_length", Converter<size_t>::toString(msgBody.size())));
  msg.addBody(msgBody);
  std::cout << "\n" << StringHelper::addHeaderAndFooterLines(msg.toString());

  Utils::title("Testing parseAttribute(const std::string&)");
  std::string test2 = "name:value";
  std::cout << "\n  input = \"" << test2 << "\"";
  Attribute attrib2 = HttpMessage::parseAttribute(test2);
  std::cout << "\n  result is: " << HttpMessage::attribString(attrib2);

  test2 = " name : value ";
  std::cout << "\n  input = \"" << test2 << "\"";
  attrib2 = HttpMessage::parseAttribute(test2);
  std::cout << "\n  result is: " << HttpMessage::attribString(attrib2);

  Utils::title("Testing Message parsing");
  MockSocket sock(msg);
  HttpMessage msg2;
  while (true)
  {
    std::string line = sock.recvString();
    if (line.size() == 0)
      break;
    Attribute attrib = HttpMessage::parseAttribute(line);
    msg2.addAttribute(attrib);
  }
  Value val = msg2.findValue("content_length");
  if (val.size() > 0)
  {
    size_t numBytes = Converter<size_t>::toValue(val);
    byte* pBuffer = new byte[numBytes];
    sock.recv(numBytes, pBuffer);
    msg2.addBody(numBytes, pBuffer);
  }
  std::cout << "\n" << Utils::addHeaderAndFooterLines(msg2.toString());
  std::cout << "\n\n";
}