|
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.
|
|
From: <sng...@us...> - 2010-07-29 10:11:31
|
Revision: 116
http://dsim.svn.sourceforge.net/dsim/?rev=116&view=rev
Author: snguyenkim
Date: 2010-07-29 10:11:24 +0000 (Thu, 29 Jul 2010)
Log Message:
-----------
asynchronous log server
Modified Paths:
--------------
trunk/dsim/test/boost/asio/log_server/Makefile.am
trunk/dsim/test/boost/asio/log_server/README
trunk/dsim/test/boost/asio/log_server/asynServer.cpp
trunk/dsim/test/boost/asio/log_server/client.cpp
Added Paths:
-----------
trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp
trunk/dsim/test/boost/asio/log_server/synServer.cpp
trunk/dsim/test/boost/asio/log_server/test.txt
Removed Paths:
-------------
trunk/dsim/test/boost/asio/log_server/server.cpp
Modified: trunk/dsim/test/boost/asio/log_server/Makefile.am
===================================================================
--- trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-26 15:45:19 UTC (rev 115)
+++ trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-29 10:11:24 UTC (rev 116)
@@ -5,17 +5,17 @@
MAINTAINERCLEANFILES = Makefile.in
-check_PROGRAMS = client server asynServer
+check_PROGRAMS = client synServer asynServer
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 =
+synServer_SOURCES = synServer.cpp
+synServer_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS)
+synServer_LDFLAGS = $(BOOST_LIBS) $(BOOST_DATE_TIME_LIB) $(BOOST_ASIO_LIB)
+synServer_LDADD =
asynServer_SOURCES = asynServer.cpp
asynServer_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS)
Modified: trunk/dsim/test/boost/asio/log_server/README
===================================================================
--- trunk/dsim/test/boost/asio/log_server/README 2010-07-26 15:45:19 UTC (rev 115)
+++ trunk/dsim/test/boost/asio/log_server/README 2010-07-29 10:11:24 UTC (rev 116)
@@ -1,7 +1,3 @@
-* 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
+* synServer is synchronous and asynServer is asynchronous log server
+* asynServer log to file corresponding to client's IP while synServer uses only server.log
+* asynServer creates a seperate thread for handling each client
Modified: trunk/dsim/test/boost/asio/log_server/asynServer.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/asynServer.cpp 2010-07-26 15:45:19 UTC (rev 115)
+++ trunk/dsim/test/boost/asio/log_server/asynServer.cpp 2010-07-29 10:11:24 UTC (rev 116)
@@ -5,6 +5,7 @@
#include <ctime>
#include <iostream>
#include <string>
+#include <fstream>
// Boost.ASIO
#include <iostream>
#include <boost/asio.hpp>
@@ -28,8 +29,8 @@
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);
+ static Pointer_T create (boost::asio::io_service& ioIOService, string lLogFile) {
+ TCPConnection* oConnectionPtr = new TCPConnection (ioIOService, lLogFile);
assert (oConnectionPtr != NULL);
return Pointer_T (oConnectionPtr);
}
@@ -41,23 +42,48 @@
/** Process the incoming client request, by giving it back the time of day. */
void start() {
+ ostringstream ostr;
+ ostr << _socket.remote_endpoint();
+ string clientFullName=ostr.str(); //127.0.0.1:73784
+ string clientName= clientFullName.substr(0,clientFullName.find(":")); //127.0.0.1
- _message = make_daytime_string();
+ ostringstream oss;
+ oss << clientName << ".log";
+ log_file= oss.str(); // each client log into a seperate file which ends by log. Ex:127.0.0.1.log
+ cout << "Received client: " << clientName << " Corresponding log file:" << log_file << endl;
+
+ ofstream out (log_file.c_str(), ios::app); // open log file
+ if (out == NULL){
+ cout << log_file << " :openning problem!" << endl;
+ return ;
+ }
+
+ boost::system::error_code lTransferError;
+ boost::array<char, 1024> lBuffer;
+ for(;;){
+ size_t lLength = _socket.read_some (boost::asio::buffer (lBuffer), lTransferError);
+ out.write(lBuffer.data(),lLength);
- 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));
+ if (lTransferError == boost::asio::error::eof) {
+ // Connection closed cleanly by peer.
+ cout << "EOF found: Transfer finished\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n" ;
+ break;
+ } else if (lTransferError) {
+ // Some other error.
+ throw boost::system::system_error (lTransferError);
+ }
+// std::cout.write (lBuffer.data(), lLength);
+ }
+ out.close();
}
private:
// //////////// Constructors & Destructors /////////////
/** Constructor. */
- TCPConnection (boost::asio::io_service& ioIOService)
+ TCPConnection (boost::asio::io_service& ioIOService, string lLogFile)
: _socket (ioIOService) {
+ log_file = lLogFile;
}
void handleWrite (const boost::system::error_code& iErrorCode,
@@ -77,6 +103,9 @@
/** TCP/IP socket. */
boost::asio::ip::tcp::socket _socket;
+
+ /** Log file prefix */
+ string log_file;
};
@@ -85,11 +114,12 @@
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)) {
+ <br>Create a listener for IP/TCP v4 and listening on port lPort */
+ TCPServer (boost::asio::io_service& ioIOService, int lPort, string lLogFile) : _acceptor (ioIOService,
+ boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), lPort)) {
+ log_file = lLogFile;
startAccept();
+
}
@@ -97,8 +127,7 @@
// ///////////////// Technical Methods ///////////////////
/** Accept (socket) connection from any client. */
void startAccept() {
- TCPConnection::Pointer_T lConnection =
- TCPConnection::create (_acceptor.io_service());
+ TCPConnection::Pointer_T lConnection = TCPConnection::create (_acceptor.io_service(), log_file);
boost::asio::ip::tcp::socket& lSocket = lConnection->socket();
_acceptor.async_accept (lSocket,
@@ -109,9 +138,11 @@
/** Process the (socket) connection from any client. */
void handleAccept (TCPConnection::Pointer_T ioConnection,const boost::system::error_code& iError) {
- cout << "received 1 client" << endl;
+ static int nbConnections = 0;
+ nbConnections ++ ;
+ cout << "Has received:" << nbConnections << " clients" << endl;
if (!iError) {
- ioConnection->start();
+ ioConnection -> start();
startAccept();
}
}
@@ -121,23 +152,40 @@
// /////////// Attributes /////////////
/** Connection acceptor. */
boost::asio::ip::tcp::acceptor _acceptor;
+ string log_file;
};
// //////////////////// M A I N /////////////////////////////
int main (int argc, char* argv[]) {
- try {
+ string log_file = "abcd.log";
+ int listening_port = 2624;//corresponding to the "aria" service, as specified within the /etc/services file
- boost::asio::io_service lIOService;
- TCPServer lServer (lIOService);
+ cout << "Syntax: ./asynServer [file_to_log] [listening_port]" << endl;
+ cout << "By default, server listens on port 2624 and file_to_log=\"abcd.log\"" << endl;
- lIOService.run();
+ if (argc > 2){
+ listening_port = atoi(argv[2]);
+ log_file = string(argv[1]);
+ }
+ cout << "=======================================================================================" << endl;
+ cout << "Server log to file:" << log_file << endl;
+ cout << "Server listening on port:" << listening_port << endl;
+ cout << "=======================================================================================" << endl;
- } catch (std::exception& lException) {
- std::cerr << lException.what() << std::endl;
- }
+
+ try {
- return 0;
+ boost::asio::io_service lIOService;
+ TCPServer lServer (lIOService, listening_port, log_file);
+
+ 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-26 15:45:19 UTC (rev 115)
+++ trunk/dsim/test/boost/asio/log_server/client.cpp 2010-07-29 10:11:24 UTC (rev 116)
@@ -8,28 +8,33 @@
#include <boost/array.hpp>
// Sleep funtion
#include <unistd.h>
+//Some network functions
+#include "networkFunctions.cpp"
-std::string lHostname = "localhost";
-const std::string lServiceName = "aria"; // The "aria" service corresponds to the port 2624 see /etc/services
+
+std::string lServerName = "localhost";
+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
+int lServerPort = 2624;
/* Send filename's content to server */
void send_file (std::string filename){
using namespace std;
-
+ // testing if file exists
+ fstream fst (filename.c_str(), ios::in);
+ if (fst == NULL){
+ cout << filename << " does not exist !" << endl;
+ exit(1);
+ }
+
// try to get a socket (communication canal)
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::query lQuery (lServerName, lServiceName);
boost::asio::ip::tcp::resolver::iterator lEnd;
boost::system::error_code lError = boost::asio::error::host_not_found;
@@ -83,18 +88,22 @@
int main (int argc, char* argv[]) {
using namespace std;
- cout << "Syntax: ./client [file_to_send] [server_name] " << endl;
- cout << "By default, file_to_send= \"test.txt\" and server_name=\"localhost\" " << endl;
+ cout << "Syntax: ./client [file_to_send] [server_name] [server_port] " << endl;
+ cout << "By default, file_to_send= \"test.txt\" and server_name=\"localhost\" and server_port=2624 " << endl;
- if (argc > 2) {
+ if (argc > 3) {
sendFile = string(argv[1]);
- lHostname = string (argv[2]);
+ lServerName = string (argv[2]);
+ lServerPort = atoi(argv[3]);
+ lServiceName = port_to_service(lServerPort);
}else if (argc == 2)
sendFile = string(argv[1]);
+ cout << "=======================================================================================" << endl;
cout << "File to send:" << sendFile << endl;
- cout << "Server:" << lHostname << endl;
- cout << "====================================================================" << endl;
+ cout << "Server name:" << lServerName << endl;
+ cout << "Server port:" << lServerPort << endl;
+ cout << "=======================================================================================" << endl;
send_file(sendFile);
return 0;
Added: trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp 2010-07-29 10:11:24 UTC (rev 116)
@@ -0,0 +1,64 @@
+// STL
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <ctime>
+#include <unistd.h>
+
+#include <netdb.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+using namespace std;
+
+/* return the port corresponding to service name s using /etc/services
+For ex: aria returns 2624
+*/
+int service_to_port(string s){
+ servent* a = getservbyname(s.c_str(), NULL);
+ endservent();
+
+ if (a!= NULL)
+ return htons(a -> s_port);
+ else{
+ cout << "No service with name:" << s << " is found" << endl;
+ return -1;
+ }
+}
+string port_to_service(int p){
+ servent* a = getservbyport(htons(p), NULL);
+ endservent();
+
+ if (a!= NULL)
+ return a -> s_name;
+ else{
+ cout << "No service with port:" << p << " is found" << endl;
+ return "";
+ }
+}
+/* Ex: Takes 172.16.134.217 , returns nceorilnx03.nce.amadeus.net*/
+string ip_to_hostname(string s){
+ struct hostent *he;
+ struct in_addr ipv4addr;
+ inet_pton(AF_INET, s.c_str(), &ipv4addr);
+ he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
+ cout << "finish searching\n";
+ if (he!= NULL)
+ return he->h_name;
+ else{
+ cout << "Can't find hostname corresponding to: " << s << endl;
+ return "";
+ }
+
+
+
+
+}
+// int main(){
+// cout << service_to_port("ariaa") << endl;
+// cout << port_to_service(22) << endl;
+//
+// // string add="172.16.134.217";
+// string add="173.163.134.217";
+// cout << ip_to_hostname(add) << endl;
+// return 0;
+// }
\ No newline at end of file
Deleted: trunk/dsim/test/boost/asio/log_server/server.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/server.cpp 2010-07-26 15:45:19 UTC (rev 115)
+++ trunk/dsim/test/boost/asio/log_server/server.cpp 2010-07-29 10:11:24 UTC (rev 116)
@@ -1,83 +0,0 @@
-// 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>
-
-#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 listening_port
- boost::asio::ip::tcp::acceptor lAcceptor (lIOService,
- 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); //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 (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 found: Transfer finished\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;
-}
-
Copied: trunk/dsim/test/boost/asio/log_server/synServer.cpp (from rev 115, trunk/dsim/test/boost/asio/log_server/server.cpp)
===================================================================
--- trunk/dsim/test/boost/asio/log_server/synServer.cpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/synServer.cpp 2010-07-29 10:11:24 UTC (rev 116)
@@ -0,0 +1,83 @@
+// 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>
+
+#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 << "=======================================================================================" << endl;
+ 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 listening_port
+ boost::asio::ip::tcp::acceptor lAcceptor (lIOService,
+ 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); //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 lTransferError;
+ boost::array<char, 1024> lBuffer;
+
+ 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 found: Transfer finished\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \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;
+}
+
Added: trunk/dsim/test/boost/asio/log_server/test.txt
===================================================================
--- trunk/dsim/test/boost/asio/log_server/test.txt (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/test.txt 2010-07-29 10:11:24 UTC (rev 116)
@@ -0,0 +1,86 @@
+//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 = "localhost";
+ // Service name (as specified within /etc/services)
+ // The "aria" service corresponds to the port 2624
+ const std::string lServiceName = "aria";
+ int attempt=0;
+ // try to get a socket (communication canal)
+ while(1){
+ 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 at:" << attempt << " attempts" << std::endl;
+ attempt ++;
+ sleep(1);
+ continue;
+ 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);
+
+ 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);
+ return 0;
+}
+
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <sng...@us...> - 2010-07-29 12:06:27
|
Revision: 117
http://dsim.svn.sourceforge.net/dsim/?rev=117&view=rev
Author: snguyenkim
Date: 2010-07-29 12:06:21 +0000 (Thu, 29 Jul 2010)
Log Message:
-----------
tinyLog library
Modified Paths:
--------------
trunk/dsim/test/boost/asio/log_server/client.cpp
trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp
Added Paths:
-----------
trunk/dsim/test/boost/asio/log_server/Makefile1
trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp
trunk/dsim/test/boost/asio/log_server/test.cpp
trunk/dsim/test/boost/asio/log_server/tinyLog.cpp
trunk/dsim/test/boost/asio/log_server/tinyLog.hpp
Added: trunk/dsim/test/boost/asio/log_server/Makefile1
===================================================================
--- trunk/dsim/test/boost/asio/log_server/Makefile1 (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/Makefile1 2010-07-29 12:06:21 UTC (rev 117)
@@ -0,0 +1,12 @@
+CC=g++ -L/usr/lib/ -lboost_system -lboost_thread-mt -lboost_date_time -lboost_regex -lboost_serialization \
+ -lpthread -lc -lrt -licuuc -licui18n -licudata
+all: client test
+
+client: client.cpp
+ $(CC) client.cpp -L. -lNetworkFunctions -o client
+
+test: test.cpp
+ g++ test.cpp -L. -ltinyLog -o test
+
+clean: client test
+ rm -fv client test
Modified: trunk/dsim/test/boost/asio/log_server/client.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/client.cpp 2010-07-29 10:11:24 UTC (rev 116)
+++ trunk/dsim/test/boost/asio/log_server/client.cpp 2010-07-29 12:06:21 UTC (rev 117)
@@ -9,7 +9,7 @@
// Sleep funtion
#include <unistd.h>
//Some network functions
-#include "networkFunctions.cpp"
+#include "networkFunctions.hpp"
std::string lServerName = "localhost";
Modified: trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp 2010-07-29 10:11:24 UTC (rev 116)
+++ trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp 2010-07-29 12:06:21 UTC (rev 117)
@@ -1,64 +1,50 @@
-// STL
-#include <fstream>
-#include <iostream>
-#include <string>
-#include <ctime>
-#include <unistd.h>
+#include "networkFunctions.hpp"
-#include <netdb.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-using namespace std;
-/* return the port corresponding to service name s using /etc/services
-For ex: aria returns 2624
-*/
-int service_to_port(string s){
+int service_to_port(std::string s){
servent* a = getservbyname(s.c_str(), NULL);
endservent();
if (a!= NULL)
return htons(a -> s_port);
else{
- cout << "No service with name:" << s << " is found" << endl;
+ std::cout << "No service with name:" << s << " is found" << std::endl;
return -1;
}
}
-string port_to_service(int p){
+
+std::string port_to_service(int p){
servent* a = getservbyport(htons(p), NULL);
endservent();
if (a!= NULL)
return a -> s_name;
else{
- cout << "No service with port:" << p << " is found" << endl;
+ std::cout << "No service with port:" << p << " is found" << std::endl;
return "";
}
}
-/* Ex: Takes 172.16.134.217 , returns nceorilnx03.nce.amadeus.net*/
-string ip_to_hostname(string s){
+
+std::string ip_to_hostname(std::string s){
struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, s.c_str(), &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
- cout << "finish searching\n";
+ std::cout << "finish searching\n";
if (he!= NULL)
return he->h_name;
else{
- cout << "Can't find hostname corresponding to: " << s << endl;
+ std::cout << "Can't find hostname corresponding to: " << s << std::endl;
return "";
}
-
-
-
-
}
+
// int main(){
-// cout << service_to_port("ariaa") << endl;
-// cout << port_to_service(22) << endl;
+// std::cout << service_to_port("ariaa") << std::endl;
+// std::cout << port_to_service(22) << std::endl;
//
// // string add="172.16.134.217";
// string add="173.163.134.217";
-// cout << ip_to_hostname(add) << endl;
+// std::cout << ip_to_hostname(add) << std::endl;
// return 0;
// }
\ No newline at end of file
Added: trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp 2010-07-29 12:06:21 UTC (rev 117)
@@ -0,0 +1,28 @@
+
+#ifndef __NETWORKFUNCTIONS__
+#define __NETWORKFUNCTIONS__
+
+
+#include <iostream>
+#include <string>
+
+#include <netdb.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+
+/* return the port corresponding to service name s using /etc/services
+* For ex: aria returns 2624
+*/
+int service_to_port(std::string s);
+
+
+/* contrary of service_to_port
+* ex: 22 -> ssh
+*/
+std::string port_to_service(int p);
+
+/* Ex: Takes 172.16.134.217 , returns nceorilnx03.nce.amadeus.net*/
+std::string ip_to_hostname(std::string s);
+
+
+#endif
\ No newline at end of file
Added: trunk/dsim/test/boost/asio/log_server/test.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/test.cpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/test.cpp 2010-07-29 12:06:21 UTC (rev 117)
@@ -0,0 +1,5 @@
+#include "tinyLog.hpp"
+
+int main(){
+ send_string("localhost", 2624, "test");
+}
\ No newline at end of file
Added: trunk/dsim/test/boost/asio/log_server/tinyLog.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/tinyLog.cpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/tinyLog.cpp 2010-07-29 12:06:21 UTC (rev 117)
@@ -0,0 +1,118 @@
+#include "tinyLog.hpp"
+
+
+
+void send_file (std::string lServerName, int lServerPort, std::string filename){
+ using namespace std;
+ int attempt=0; //Nb of attemps to connect to server
+ // testing if file exists
+ fstream fst (filename.c_str(), ios::in);
+ if (fst == NULL){
+ cout << filename << " does not exist !" << endl;
+ exit(1);
+ }
+ std::string lServiceName= port_to_service(lServerPort);
+ // try to get a socket (communication canal)
+ while(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 (lServerName, lServiceName);
+
+ 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;
+
+ lSocket.close();
+ lSocket.connect (lEndPoint, lError);
+ ++itEndPoint;
+ }
+
+ 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);
+
+ 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);
+
+ cout << "Transfer finished" << endl;
+ return;
+ } catch (std::exception& lException) {
+ std::cerr << lException.what() << std::endl;
+ }
+ }
+}
+
+
+void send_string(std::string lServerName, int lServerPort, std::string buffer){
+ using namespace std;
+ int attempt=0; //Nb of attemps to connect to server
+
+ std::string lServiceName= port_to_service(lServerPort);
+ // try to get a socket (communication canal)
+ while(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 (lServerName, lServiceName);
+
+ 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;
+
+ lSocket.close();
+ lSocket.connect (lEndPoint, lError);
+ ++itEndPoint;
+ }
+
+ 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";
+
+ 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;
+ }
+ }
+}
+
+
+
Added: trunk/dsim/test/boost/asio/log_server/tinyLog.hpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/tinyLog.hpp (rev 0)
+++ trunk/dsim/test/boost/asio/log_server/tinyLog.hpp 2010-07-29 12:06:21 UTC (rev 117)
@@ -0,0 +1,31 @@
+#ifndef __TINYLOG__
+#define __TINYLOG__
+
+// String, stream functions
+#include <iostream>
+#include <string>
+#include <fstream>
+// Boost.ASIO
+#include <boost/asio.hpp>
+#include <boost/array.hpp>
+// Sleep funtion
+#include <unistd.h>
+//Some network functions
+#include "networkFunctions.cpp"
+
+/* Send filename's content to server
+* lServerName: name of server,ex: localhost
+* lServerPort: listening port on server
+* filename: file to send to server
+*/
+void send_file (std::string lServerName, int lServerPort, std::string filename);
+
+/* Send string buffer to server
+* lServerName: name of server,ex: localhost
+* lServerPort: listening port on server
+* filename: file to send to server
+*/
+void send_string(std::string lServerName, int lServerPort, std::string buffer);
+
+
+#endif
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <den...@us...> - 2010-07-29 13:56:01
|
Revision: 118
http://dsim.svn.sourceforge.net/dsim/?rev=118&view=rev
Author: denis_arnaud
Date: 2010-07-29 13:55:55 +0000 (Thu, 29 Jul 2010)
Log Message:
-----------
[Alpha dev] Comestic changes in the Boost.ASIO log server.
Modified Paths:
--------------
trunk/dsim/test/boost/asio/log_server/Makefile.am
trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp
trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp
trunk/dsim/test/boost/asio/log_server/tinyLog.cpp
trunk/dsim/test/boost/asio/log_server/tinyLog.hpp
Property Changed:
----------------
trunk/dsim/test/boost/asio/log_server/
Property changes on: trunk/dsim/test/boost/asio/log_server
___________________________________________________________________
Modified: svn:ignore
- .deps
.libs
Makefile.in
Makefile
client
server
server.log
+ .deps
.libs
Makefile.in
Makefile
client
server
server.log
synServer
asynServer
Modified: trunk/dsim/test/boost/asio/log_server/Makefile.am
===================================================================
--- trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-29 12:06:21 UTC (rev 117)
+++ trunk/dsim/test/boost/asio/log_server/Makefile.am 2010-07-29 13:55:55 UTC (rev 118)
@@ -7,7 +7,7 @@
check_PROGRAMS = client synServer asynServer
-client_SOURCES = client.cpp
+client_SOURCES = client.cpp networkFunctions.cpp
client_CXXFLAGS = $(BOOST_CFLAGS) $(BOOST_CFLAGS)
client_LDFLAGS = $(BOOST_LIBS) $(BOOST_DATE_TIME_LIB) $(BOOST_ASIO_LIB)
client_LDADD =
Modified: trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp 2010-07-29 12:06:21 UTC (rev 117)
+++ trunk/dsim/test/boost/asio/log_server/networkFunctions.cpp 2010-07-29 13:55:55 UTC (rev 118)
@@ -1,19 +1,27 @@
+// STL
+#include <iostream>
+#include <string>
+//
+#include <netdb.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+//
#include "networkFunctions.hpp"
-int service_to_port(std::string s){
+int service_to_port (const std::string& s) {
servent* a = getservbyname(s.c_str(), NULL);
endservent();
- if (a!= NULL)
+ if (a!= NULL) {
return htons(a -> s_port);
- else{
+ } else {
std::cout << "No service with name:" << s << " is found" << std::endl;
return -1;
}
}
-std::string port_to_service(int p){
+std::string port_to_service (const int p){
servent* a = getservbyport(htons(p), NULL);
endservent();
@@ -25,7 +33,7 @@
}
}
-std::string ip_to_hostname(std::string s){
+std::string ip_to_hostname (const std::string& s){
struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, s.c_str(), &ipv4addr);
@@ -47,4 +55,4 @@
// string add="173.163.134.217";
// std::cout << ip_to_hostname(add) << std::endl;
// return 0;
-// }
\ No newline at end of file
+// }
Modified: trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp 2010-07-29 12:06:21 UTC (rev 117)
+++ trunk/dsim/test/boost/asio/log_server/networkFunctions.hpp 2010-07-29 13:55:55 UTC (rev 118)
@@ -1,28 +1,25 @@
-#ifndef __NETWORKFUNCTIONS__
-#define __NETWORKFUNCTIONS__
+#ifndef __DSIM_NETWORKFUNCTIONS__
+#define __DSIM_NETWORKFUNCTIONS__
-#include <iostream>
-#include <string>
+/**
+ * \brief Return the port corresponding to service name s using /etc/services
+ * <br>For ex: aria returns 2624
+ */
+int service_to_port (const std::string&);
-#include <netdb.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-/* return the port corresponding to service name s using /etc/services
-* For ex: aria returns 2624
-*/
-int service_to_port(std::string s);
+/**
+ * \brief contrary of service_to_port
+ * <br>ex: 22 -> ssh
+ */
+std::string port_to_service (const int p);
+/**
+ * \brief Ex: Takes 172.16.134.217 , returns nceorilnx03.nce.amadeus.net
+ */
+std::string ip_to_hostname (const std::string& s);
-/* contrary of service_to_port
-* ex: 22 -> ssh
-*/
-std::string port_to_service(int p);
-/* Ex: Takes 172.16.134.217 , returns nceorilnx03.nce.amadeus.net*/
-std::string ip_to_hostname(std::string s);
-
-
-#endif
\ No newline at end of file
+#endif // __DSIM_NETWORKFUNCTIONS__
Modified: trunk/dsim/test/boost/asio/log_server/tinyLog.cpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/tinyLog.cpp 2010-07-29 12:06:21 UTC (rev 117)
+++ trunk/dsim/test/boost/asio/log_server/tinyLog.cpp 2010-07-29 13:55:55 UTC (rev 118)
@@ -1,8 +1,19 @@
+
+// String, stream functions
+#include <string>
+#include <fstream>
+// Boost.ASIO
+#include <boost/asio.hpp>
+#include <boost/array.hpp>
+// Sleep funtion
+#include <unistd.h>
+//Some network functions
+#include "networkFunctions.cpp"
+//
#include "tinyLog.hpp"
-
-
-void send_file (std::string lServerName, int lServerPort, std::string filename){
+void send_file (const std::string& lServerName, const int lServerPort,
+ const std::string& filename) {
using namespace std;
int attempt=0; //Nb of attemps to connect to server
// testing if file exists
@@ -66,8 +77,8 @@
}
}
-
-void send_string(std::string lServerName, int lServerPort, std::string buffer){
+void send_string (const std::string& lServerName, const int lServerPort,
+ const std::string& buffer) {
using namespace std;
int attempt=0; //Nb of attemps to connect to server
Modified: trunk/dsim/test/boost/asio/log_server/tinyLog.hpp
===================================================================
--- trunk/dsim/test/boost/asio/log_server/tinyLog.hpp 2010-07-29 12:06:21 UTC (rev 117)
+++ trunk/dsim/test/boost/asio/log_server/tinyLog.hpp 2010-07-29 13:55:55 UTC (rev 118)
@@ -1,31 +1,26 @@
-#ifndef __TINYLOG__
-#define __TINYLOG__
+#ifndef __DSIM_TINYLOG__
+#define __DSIM_TINYLOG__
// String, stream functions
-#include <iostream>
#include <string>
-#include <fstream>
-// Boost.ASIO
-#include <boost/asio.hpp>
-#include <boost/array.hpp>
-// Sleep funtion
-#include <unistd.h>
-//Some network functions
-#include "networkFunctions.cpp"
-/* Send filename's content to server
-* lServerName: name of server,ex: localhost
-* lServerPort: listening port on server
-* filename: file to send to server
+/**
+ * \brief Send filename's content to server
+ * \param[in] lServerName: name of server,ex: localhost
+ * \param[in] lServerPort: listening port on server
+ * \param[in] filename: file to send to server
*/
-void send_file (std::string lServerName, int lServerPort, std::string filename);
+void send_file (const std::string& lServerName, const int lServerPort,
+ const std::string& filename);
-/* Send string buffer to server
-* lServerName: name of server,ex: localhost
-* lServerPort: listening port on server
-* filename: file to send to server
+/**
+ * \brief Send string buffer to server
+ * \param[in] lServerName: name of server,ex: localhost
+ * \param[in] lServerPort: listening port on server
+ * \param[in] filename: file to send to server
*/
-void send_string(std::string lServerName, int lServerPort, std::string buffer);
+void send_string (const std::string& lServerName, const int lServerPort,
+ const std::string& buffer);
-#endif
\ No newline at end of file
+#endif // __DSIM_TINYLOG__
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|