|
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.
|