|
From: <sng...@us...> - 2010-07-26 15:45:26
|
Revision: 115
http://dsim.svn.sourceforge.net/dsim/?rev=115&view=rev
Author: snguyenkim
Date: 2010-07-26 15:45:19 +0000 (Mon, 26 Jul 2010)
Log Message:
-----------
Update log server
Modified Paths:
--------------
trunk/dsim/test/boost/asio/log_server/Makefile.am
trunk/dsim/test/boost/asio/log_server/client.cpp
trunk/dsim/test/boost/asio/log_server/server.cpp
Added Paths:
-----------
trunk/dsim/test/boost/asio/log_server/asynServer.cpp
Modified: trunk/dsim/test/boost/asio/log_server/Makefile.am
===================================================================
--- trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-24 19:00:16 UTC (rev 114)
+++ trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-26 15:45:19 UTC (rev 115)
@@ -5,7 +5,7 @@
MAINTAINERCLEANFILES = Makefile.in
-check_PROGRAMS = client server
+check_PROGRAMS = client server asynServer
client_SOURCES = client.cpp
client_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS)
@@ -17,3 +17,7 @@
server_LDFLAGS = $(BOOST_LIBS) $(BOOST_DATE_TIME_LIB) $(BOOST_ASIO_LIB)
server_LDADD =
+asynServer_SOURCES = asynServer.cpp
+asynServer_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS)
+asynServer_LDFLAGS = $(BOOST_LIBS) $(BOOST_DATE_TIME_LIB) $(BOOST_ASIO_LIB)
+asynServer_LDADD =
\ No newline at end of file
Added: trunk/dsim/test/boost/asio/log_server/asynServer.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/asynServer.cpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/asynServer.cpp 2010-07-26 15:45:19 UTC (rev 115)
@@ -0,0 +1,143 @@
+// Boost.ASIO Tutorial - Daytime3:
+// http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/tutorial/tutdaytime3.html
+// STL
+#include <cassert>
+#include <ctime>
+#include <iostream>
+#include <string>
+// Boost.ASIO
+#include <iostream>
+#include <boost/asio.hpp>
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+using namespace std;
+// //////////////////////////////////////////////////////////
+std::string make_daytime_string() {
+ const std::time_t now = std::time(0);
+ return std::ctime (&now);
+}
+
+// //////////////////////////////////////////////////////////
+/** Class handling TCP connections for a given server. */
+class TCPConnection : public boost::enable_shared_from_this<TCPConnection> {
+ public:
+ /** Pointer on a TCP connection. */
+ typedef boost::shared_ptr<TCPConnection> Pointer_T;
+
+ /** Create a TCP connection, from a given Boost.ASIO service. */
+ static Pointer_T create (boost::asio::io_service& ioIOService) {
+ TCPConnection* oConnectionPtr = new TCPConnection (ioIOService);
+ assert (oConnectionPtr != NULL);
+ return Pointer_T (oConnectionPtr);
+ }
+
+ /** Get the underlying TCP socket. */
+ boost::asio::ip::tcp::socket& socket() {
+ return _socket;
+ }
+
+ /** Process the incoming client request, by giving it back the time of day. */
+ void start() {
+
+ _message = make_daytime_string();
+
+ boost::system::error_code lIgnoredError;
+ boost::asio::async_write (_socket, boost::asio::buffer (_message),
+ boost::bind (&TCPConnection::handleWrite,
+ shared_from_this(),
+ boost::asio::placeholders::error,
+ boost::asio::placeholders::bytes_transferred));
+ }
+
+
+ private:
+ // //////////// Constructors & Destructors /////////////
+ /** Constructor. */
+ TCPConnection (boost::asio::io_service& ioIOService)
+ : _socket (ioIOService) {
+ }
+
+ void handleWrite (const boost::system::error_code& iErrorCode,
+ const size_t iTransferredBytes) {
+ // start();
+ }
+
+
+ private:
+ // ///////////////// Technical Methods ///////////////////
+
+
+ private:
+ // /////////// Attributes /////////////
+ /** Time of day. */
+ std::string _message;
+
+ /** TCP/IP socket. */
+ boost::asio::ip::tcp::socket _socket;
+};
+
+
+/** Class starting a TCP server, and handling incoming requests. */
+class TCPServer {
+ public:
+ // //////////// Constructors & Destructors /////////////
+ /** Constructor.
+ <br>Create a listener for IP/TCP v4, listening on port 2624 (corresponding
+ to the "aria" service, as specified within the /etc/services file) */
+ TCPServer (boost::asio::io_service& ioIOService) : _acceptor (ioIOService,
+ boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), 2624)) {
+ startAccept();
+ }
+
+
+ private:
+ // ///////////////// Technical Methods ///////////////////
+ /** Accept (socket) connection from any client. */
+ void startAccept() {
+ TCPConnection::Pointer_T lConnection =
+ TCPConnection::create (_acceptor.io_service());
+
+ boost::asio::ip::tcp::socket& lSocket = lConnection->socket();
+ _acceptor.async_accept (lSocket,
+ boost::bind (&TCPServer::handleAccept, this,
+ lConnection,
+ boost::asio::placeholders::error));
+ }
+
+ /** Process the (socket) connection from any client. */
+ void handleAccept (TCPConnection::Pointer_T ioConnection,const boost::system::error_code& iError) {
+ cout << "received 1 client" << endl;
+ if (!iError) {
+ ioConnection->start();
+ startAccept();
+ }
+ }
+
+
+ private:
+ // /////////// Attributes /////////////
+ /** Connection acceptor. */
+ boost::asio::ip::tcp::acceptor _acceptor;
+ };
+
+
+// //////////////////// M A I N /////////////////////////////
+int main (int argc, char* argv[]) {
+
+ try {
+
+ boost::asio::io_service lIOService;
+ TCPServer lServer (lIOService);
+
+ lIOService.run();
+
+ } catch (std::exception& lException) {
+ std::cerr << lException.what() << std::endl;
+ }
+
+ return 0;
+}
+
Modified: trunk/dsim/test/boost/asio/log_server/client.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/client.cpp 2010-07-24 19:00:16 UTC (rev 114)
+++ trunk/dsim/test/boost/asio/log_server/client.cpp 2010-07-26 15:45:19 UTC (rev 115)
@@ -9,71 +9,94 @@
// Sleep funtion
#include <unistd.h>
+std::string lHostname = "localhost";
+const std::string lServiceName = "aria"; // The "aria" service corresponds to the port 2624 see /etc/services
+std::string sendFile="test.txt";
+int attempt=0; //Nb of attemps to connect to server
+
/* Send filename's content to server */
void send_file (std::string filename){
using namespace std;
- std::string lHostname = "localhost";
- // Service name (as specified within /etc/services)
- // The "aria" service corresponds to the port 2624
- const std::string lServiceName = "aria";
// try to get a socket (communication canal)
- try {
+ while(1){
+ // testing if file exists
+ fstream fst (filename.c_str(), ios::in);
+ if (fst == NULL){
+ cout << filename << " does not exist !" << endl;
+ exit(1);
+ }
boost::asio::io_service lIOService;
boost::asio::ip::tcp::socket lSocket (lIOService);
-
boost::asio::ip::tcp::resolver lResolver (lIOService);
boost::asio::ip::tcp::resolver::query lQuery (lHostname, lServiceName);
- boost::asio::ip::tcp::resolver::iterator itEndPoint =lResolver.resolve (lQuery);
+
boost::asio::ip::tcp::resolver::iterator lEnd;
boost::system::error_code lError = boost::asio::error::host_not_found;
+ try {
+ boost::asio::ip::tcp::resolver::iterator itEndPoint =lResolver.resolve (lQuery);
- while (lError && itEndPoint != lEnd) {
- const boost::asio::ip::tcp::endpoint lEndPoint = *itEndPoint;
+ while (lError && itEndPoint != lEnd) {
+ const boost::asio::ip::tcp::endpoint lEndPoint = *itEndPoint;
- // DEBUG
-// std::cout << "Testing end point: " << lEndPoint << std::endl;
-// std::cout << "Testing lError: " << lError << std::endl;
- lSocket.close();
- lSocket.connect (lEndPoint, lError);
- ++itEndPoint;
- }
+ // std::cout << "Testing end point: " << lEndPoint << std::endl;
+ // std::cout << "Testing lError: " << lError << std::endl;
+ lSocket.close();
+ lSocket.connect (lEndPoint, lError);
+ ++itEndPoint;
+ }
- if (lError) {
- std::cout << "Cannot find corresponding endpoint" << std::endl;
- throw boost::system::system_error (lError);
- }
- assert (!lError);
- cout << "Socket is opened !\n";
- // File sending part
- fstream fst (filename.c_str(), ios::in);
- char * buffer; //contains file's content
+ if (lError) {
+ std::cout << "Cannot find corresponding endpoint at:" << attempt << " attempts" << std::endl;
+ attempt ++;
+ sleep(2); // not good to be too active
+ continue;
+ throw boost::system::system_error (lError);
+ }
+ assert (!lError);
+// cout << "Socket is opened !\n";
+
+ // File sending part
+ char * buffer; //contains file's content
- // get length of file:
- fst.seekg (0, ios::end);
- int length = fst.tellg();
- fst.seekg (0, ios::beg);
+ // get length of file:
+ fst.seekg (0, ios::end);
+ int length = fst.tellg();
+ fst.seekg (0, ios::beg);
- cout << "file length:" << length << endl;
- buffer = new char[length];
- fst.read(buffer,length);
+ cout << "file length:" << length << endl;
+ buffer = new char[length];
+ fst.read(buffer,length);
- boost::system::error_code lIgnoredError;
- boost::asio::write (lSocket, boost::asio::buffer (buffer),boost::asio::transfer_all(), lIgnoredError);
-
- } catch (std::exception& lException) {
- std::cerr << lException.what() << std::endl;
+ boost::system::error_code lIgnoredError;
+ boost::asio::write (lSocket, boost::asio::buffer (buffer),boost::asio::transfer_all(), lIgnoredError);
+
+ cout << "Transfer finished" << endl;
+ return;
+ } catch (std::exception& lException) {
+ std::cerr << lException.what() << std::endl;
+ }
}
}
// /////////// M A I N ////////////////
int main (int argc, char* argv[]) {
using namespace std;
- string filename = "input";
- if (argc >= 2) {
- filename = string(argv[1]);
- }
- send_file(filename);
+
+ cout << "Syntax: ./client [file_to_send] [server_name] " << endl;
+ cout << "By default, file_to_send= \"test.txt\" and server_name=\"localhost\" " << endl;
+
+ if (argc > 2) {
+ sendFile = string(argv[1]);
+ lHostname = string (argv[2]);
+ }else if (argc == 2)
+ sendFile = string(argv[1]);
+
+ cout << "File to send:" << sendFile << endl;
+ cout << "Server:" << lHostname << endl;
+ cout << "====================================================================" << endl;
+
+ send_file(sendFile);
return 0;
}
Modified: trunk/dsim/test/boost/asio/log_server/server.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/server.cpp 2010-07-24 19:00:16 UTC (rev 114)
+++ trunk/dsim/test/boost/asio/log_server/server.cpp 2010-07-26 15:45:19 UTC (rev 115)
@@ -7,40 +7,63 @@
// Boost.ASIO
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
-using namespace std;
+#include <unistd.h>
+
+
// //////////////////// M A I N /////////////////////////////
int main (int argc, char* argv[]) {
using namespace std;
+
+ int listening_port = 2624;//corresponding to the "aria" service, as specified within the /etc/services file
+ string log_file= "server.log";
+
+ cout << "Syntax: ./server [file_to_log] [listening_port]" << endl;
+ cout << "By default, server listens on port 2624 and file_to_log=\"server.log\"" << endl;
+
+ if (argc > 2){
+ listening_port = atoi(argv[2]);
+ log_file = string(argv[1]);
+ }else if (argc == 2)
+ log_file = string(argv[1]);
+
+ cout << "Server log to file:" << log_file << endl;
+ cout << "Server listening on port:" << listening_port << endl;
+ cout << "======================================================================" << endl;
+
+
try {
boost::asio::io_service lIOService;
- // Create a listener for IP/TCP v4, listening on port 2624 (corresponding
- // to the "aria" service, as specified within the /etc/services file)
+ // Create a listener for IP/TCP v4, listening on listening_port
boost::asio::ip::tcp::acceptor lAcceptor (lIOService,
- boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), 2624));
+ boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), listening_port));
int nbConnections = 0;
for (;;) {
boost::asio::ip::tcp::socket lSocket (lIOService);
- lAcceptor.accept (lSocket);
+ lAcceptor.accept (lSocket); //will block until a new connection has been accepted successfully or an error occurs.
nbConnections ++;
std::cout << "Nb of connections:" << nbConnections << std::endl;
+ cout << "received client: " << lSocket.remote_endpoint() << endl;
boost::system::error_code lIgnoredError;
boost::system::error_code lTransferError;
boost::array<char, 1024> lBuffer;
- ofstream out ("server.log", ios::app); //file to write log
-
+ ofstream out (log_file.c_str(), ios::app); // open log file
+ if (out == NULL){
+ cout << log_file << " does not exist !" << endl;
+ return -1;
+ }
for(;;){
size_t lLength = lSocket.read_some (boost::asio::buffer (lBuffer), lTransferError);
out.write(lBuffer.data(),lLength);
-
+
if (lTransferError == boost::asio::error::eof) {
// Connection closed cleanly by peer.
- cout << "EOF error\n" ;
+ cout << "EOF found: Transfer finished\n" ;
break;
} else if (lTransferError) {
// Some other error.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|