From: <sng...@us...> - 2010-07-16 12:05:43
|
Revision: 96 http://dsim.svn.sourceforge.net/dsim/?rev=96&view=rev Author: snguyenkim Date: 2010-07-16 12:05:35 +0000 (Fri, 16 Jul 2010) Log Message: ----------- Simple log server using Boost Asio Modified Paths: -------------- trunk/dsim/test/boost/asio/daytime1.cpp trunk/dsim/test/boost/asio/timer1.cpp trunk/dsim/test/boost/asio/timer2.cpp Added Paths: ----------- trunk/dsim/test/boost/asio/log_server/ trunk/dsim/test/boost/asio/log_server/Makefile.am trunk/dsim/test/boost/asio/log_server/README trunk/dsim/test/boost/asio/log_server/client.cpp trunk/dsim/test/boost/asio/log_server/server.cpp Modified: trunk/dsim/test/boost/asio/daytime1.cpp =================================================================== --- trunk/dsim/test/boost/asio/daytime1.cpp 2010-07-15 13:56:57 UTC (rev 95) +++ trunk/dsim/test/boost/asio/daytime1.cpp 2010-07-16 12:05:35 UTC (rev 96) @@ -28,6 +28,7 @@ boost::asio::ip::tcp::resolver lResolver (lIOService); boost::asio::ip::tcp::resolver::query lQuery (lHostname, lServiceName); +// boost::asio::ip::tcp::resolver::query lQuery (lHostname, "daytime"); boost::asio::ip::tcp::resolver::iterator itEndPoint = lResolver.resolve (lQuery); Added: trunk/dsim/test/boost/asio/log_server/Makefile.am =================================================================== --- trunk/dsim/test/boost/asio/log_server/Makefile.am (rev 0) +++ trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-16 12:05:35 UTC (rev 96) @@ -0,0 +1,19 @@ +## test/boost/asio sub-directory +include $(top_srcdir)/Makefile.common + +## + +MAINTAINERCLEANFILES = Makefile.in + +check_PROGRAMS = client server + +client_SOURCES = client.cpp +client_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS) +client_LDFLAGS = $(BOOST_LIBS) $(BOOST_DATE_TIME_LIB) $(BOOST_ASIO_LIB) +client_LDADD = + +server_SOURCES = server.cpp +server_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS) +server_LDFLAGS = $(BOOST_LIBS) $(BOOST_DATE_TIME_LIB) $(BOOST_ASIO_LIB) +server_LDADD = + Added: trunk/dsim/test/boost/asio/log_server/README =================================================================== --- trunk/dsim/test/boost/asio/log_server/README (rev 0) +++ trunk/dsim/test/boost/asio/log_server/README 2010-07-16 12:05:35 UTC (rev 96) @@ -0,0 +1,7 @@ +* Log server modele using ASIO + Client 1 will take input1 to send to server + Client 2 will take inpu2 to send to server + Server will log these two files into server.log + +* For verifying: + Client 1 & Client 2 don't intefere -> in server.log, 2 parts (for client 1 & client 2) are seperated \ No newline at end of file Added: trunk/dsim/test/boost/asio/log_server/client.cpp =================================================================== --- trunk/dsim/test/boost/asio/log_server/client.cpp (rev 0) +++ trunk/dsim/test/boost/asio/log_server/client.cpp 2010-07-16 12:05:35 UTC (rev 96) @@ -0,0 +1,80 @@ +//Client +// STL +#include <iostream> +#include <string> +#include <fstream> +// Boost.ASIO +#include <boost/asio.hpp> +#include <boost/array.hpp> +// Sleep funtion +#include <unistd.h> + +/* Send filename's content to server */ +void send_file (std::string filename){ + using namespace std; + std::string lHostname = "fed2"; + // 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 { + 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; + + 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; + } + + 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 + + // 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); + + 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; + } +} + +// /////////// 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); + return 0; +} + + Added: trunk/dsim/test/boost/asio/log_server/server.cpp =================================================================== --- trunk/dsim/test/boost/asio/log_server/server.cpp (rev 0) +++ trunk/dsim/test/boost/asio/log_server/server.cpp 2010-07-16 12:05:35 UTC (rev 96) @@ -0,0 +1,60 @@ +// Log server +// STL +#include <fstream> +#include <iostream> +#include <string> +#include <ctime> +// Boost.ASIO +#include <boost/asio.hpp> +#include <boost/date_time/posix_time/posix_time.hpp> +using namespace std; + +// //////////////////// M A I N ///////////////////////////// +int main (int argc, char* argv[]) { + using namespace std; + 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) + boost::asio::ip::tcp::acceptor lAcceptor (lIOService, + boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), 2624)); + + int nbConnections = 0; + for (;;) { + boost::asio::ip::tcp::socket lSocket (lIOService); + lAcceptor.accept (lSocket); + nbConnections ++; + std::cout << "Nb of connections:" << nbConnections << std::endl; + + boost::system::error_code lIgnoredError; + boost::system::error_code lTransferError; + boost::array<char, 4> lBuffer; + + ofstream out ("server.log", ios::app); //file to write log + + 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" ; + break; + } else if (lTransferError) { + // Some other error. + throw boost::system::system_error (lTransferError); + } +// std::cout.write (lBuffer.data(), lLength); + } + out.close(); + } + + } catch (std::exception& lException) { + std::cerr << lException.what() << std::endl; + } + + return 0; +} + Modified: trunk/dsim/test/boost/asio/timer1.cpp =================================================================== --- trunk/dsim/test/boost/asio/timer1.cpp 2010-07-15 13:56:57 UTC (rev 95) +++ trunk/dsim/test/boost/asio/timer1.cpp 2010-07-16 12:05:35 UTC (rev 96) @@ -9,12 +9,15 @@ // /////////// M A I N //////////////// int main (int argc, char* argv[]) { - boost::asio::io_service lIOService; - boost::asio::deadline_timer lTimer (lIOService, boost::posix_time::seconds(1)); + boost::asio::io_service lIOService; + boost::asio::deadline_timer lTimer (lIOService, boost::posix_time::seconds(5)); - lTimer.wait(); + lTimer.wait(); - std::cout << "We have waited 1 second" << std::endl; + //This line won't be printed immediately(), contrary to case lTimer.async_wait() + std::cout << "Waiting 5s...." << std::endl; - return 0; + std::cout << "We have waited 5 second" << std::endl; + + return 0; } Modified: trunk/dsim/test/boost/asio/timer2.cpp =================================================================== --- trunk/dsim/test/boost/asio/timer2.cpp 2010-07-15 13:56:57 UTC (rev 95) +++ trunk/dsim/test/boost/asio/timer2.cpp 2010-07-16 12:05:35 UTC (rev 96) @@ -8,25 +8,28 @@ // //////////////////////////////////////////////////////// void print (const boost::system::error_code& iErrorCode) { - std::cout << "The call-back function has been triggered on a slave task"; - std::cout << ", after having waited for 1 second" << std::endl; + std::cout << "The call-back function has been triggered on a slave task"; + std::cout << ", after having waited for 5 second" << std::endl; } // /////////////////////// M A I N ///////////////////////////// int main (int argc, char* argv[]) { - boost::asio::io_service lIOService; - boost::asio::deadline_timer lTimer (lIOService, boost::posix_time::seconds(1)); + boost::asio::io_service lIOService; + boost::asio::deadline_timer lTimer (lIOService, boost::posix_time::seconds(5)); - // Asynchronous wait: when the timer reaches the deadline, the call-back - // (here, the print() function) is called - lTimer.async_wait (print); + // Asynchronous wait: when the timer reaches the deadline, the call-back + // (here, the print() function) is called + lTimer.async_wait (print); - // Wait until the timer reaches the deadline. At that moment, the call-back - // is called - lIOService.run(); + //This line will be printed immediately(), contrary to case lTimer.wait() + std::cout << "Waiting 5s...." << std::endl; - std::cout << "The master task has come back in foreground" << std::endl; - - return 0; + // Wait until the timer reaches the deadline. At that moment, the call-back + // is called + lIOService.run(); + + std::cout << "The master task has come back in foreground" << std::endl; + + return 0; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |