1
0
Эх сурвалжийг харах

Catch exceptions and add logging

JDierkse 5 жил өмнө
parent
commit
4ee3f56bd7
2 өөрчлөгдсөн 55 нэмэгдсэн , 43 устгасан
  1. 50 39
      message.cpp
  2. 5 4
      protocol.cpp

+ 50 - 39
message.cpp

@@ -31,57 +31,68 @@ std::string Decode(const std::string& data)
 
 void message::processMessage(const std::string& message)
 {
-	message::state::type state = message::state::header;
-	std::string line;
-	std::istringstream stream(message);
-	while (std::getline(stream, line, '\r'))
+	try
 	{
-		boost::erase_all(line, "\n");
-		switch (state)
+		std::cout << "message::processMessage enter" << std::endl;
+		message::state::type state = message::state::header;
+		std::string line;
+		std::istringstream stream(message);
+		while (std::getline(stream, line, '\r'))
 		{
-		case state::header:
-			state = processHeaderLine(line);
-			break;
-		case state::data:
-			state = processDataLine(line);
-			break;
-		case state::mimeBoundary:
-			state = processMimeBoundary(line);
-			break;
-		case state::image:
-			state = processImage(line);
-			break;
+			boost::erase_all(line, "\n");
+			switch (state)
+			{
+			case state::header:
+				state = processHeaderLine(line);
+				break;
+			case state::data:
+				state = processDataLine(line);
+				break;
+			case state::mimeBoundary:
+				state = processMimeBoundary(line);
+				break;
+			case state::image:
+				state = processImage(line);
+				break;
+			}
 		}
-	}
 
-	if (boost::algorithm::contains(m_contentEncoding, "base64"))
-	{
-		std::string decoded = Decode(m_subject);
-		if (decoded.size() > 0)
-			m_subject = decoded;
-	}
+		if (boost::algorithm::contains(m_contentEncoding, "base64"))
+		{
+			std::string decoded = Decode(m_subject);
+			if (decoded.size() > 0)
+				m_subject = decoded;
+		}
 
-	outputImage image(m_images);
+		outputImage image(m_images);
 
-	boost::filesystem::path path(m_context.path);
-	path = boost::filesystem::canonical(path);
-	path /= m_address;
+		boost::filesystem::path path(m_context.path);
+		path = boost::filesystem::canonical(path);
+		path /= m_address;
 
-	if (!boost::filesystem::exists(path))
-		boost::filesystem::create_directory(path);
+		if (!boost::filesystem::exists(path))
+			boost::filesystem::create_directory(path);
 
-	std::string fileName(getTimeString());
-	fileName.append(".jpg");
-	path /= fileName;
+		std::string fileName(getTimeString());
+		fileName.append(".jpg");
+		path /= fileName;
 
-	image.save(path.string());
+		image.save(path.string());
 
-	if (!m_context.url.empty())
+		if (!m_context.url.empty())
+		{
+			std::stringstream url;
+			url << m_context.url << "?ip=" << m_address << "&file=" << fileName;
+			m_context.client.GetUrlContents(url.str());
+		}
+	}
+	catch (std::exception& e)
 	{
-		std::stringstream url;
-		url << m_context.url << "?ip=" << m_address << "&file=" << fileName;
-		m_context.client.GetUrlContents(url.str());
+		std::cout << "Exception: " << e.what() << std::endl;
+		std::cerr << "Exception: " << e.what() << std::endl;
 	}
+
+	std::cout << "message::processMessage exit" << std::endl;
 }
 
 message::state::type message::processHeaderLine(const std::string& line)

+ 5 - 4
protocol.cpp

@@ -13,7 +13,7 @@ protocol::result protocol::openConnection(const std::string& address)
 {
 	m_address = address;
 	m_internalState = internalState::connect;
-	//std::cout << "Connection from " << address << std::endl;
+	std::cout << "Connection from " << address << std::endl;
 	return answer(protocol::state::connected, "220 AlarmServer");
 }
 
@@ -25,15 +25,18 @@ protocol::result protocol::processMessage(const std::string& message)
 
 		if (boost::iends_with(message, ".\r\n"))
 		{
+			std::cout << "-> " << " . " << std::endl;
 			::message msg(m_address, m_context, m_message);
+			std::cout << "-> " << " . " << std::endl;
 			m_internalState = internalState::helo;
+			std::cout << "-> " << " . " << std::endl;
 			return answer(protocol::state::connected, "250 Ok");
 		}
 
 		return answer(protocol::state::connected, "");
 	}
 	
-	//std::cout << "-> " << message << std::endl;
+	std::cout << "-> " << message << std::endl;
 
 	if (m_internalState == internalState::auth_login_user)
 	{
@@ -95,10 +98,8 @@ protocol::result protocol::processMessage(const std::string& message)
 
 protocol::result protocol::answer(protocol::state::type state, std::string answer) const
 {
-/*
 	if (answer.size() > 0)
 		std::cout << "<- " << answer << std::endl;
-*/
 
 	return result(state, answer);
 }