Commit | Line | Data |
---|---|---|
6650af14 BS |
1 | /** @file |
2 | * @brief TcpIpHelper class implementation. This class represents a Helper to easily perform tcp/ip operations. | |
3 | * | |
4 | * | |
5 | * | |
6 | * @copyright Intra2net AG | |
7 | * @license GPLv2 | |
8 | */ | |
9 | ||
10 | #include "net_helper.h" | |
11 | #include "tcp_service.h" | |
12 | ||
13 | ||
14 | using namespace std; | |
15 | ||
16 | ||
17 | /** | |
18 | * Constructor. | |
19 | * @param _log Logger | |
20 | * @param _host Host | |
21 | * @param _port Port | |
22 | */ | |
23 | NetHelper::NetHelper( const Logger::Ptr _log ) | |
24 | : Log(_log) | |
25 | { | |
26 | IPService::Ptr _ip_service_ptr(new TCPService()); | |
27 | IPServicePtr.swap(_ip_service_ptr); | |
28 | } | |
29 | ||
30 | ||
31 | /** | |
32 | * Default destructor | |
33 | */ | |
34 | NetHelper::~NetHelper() | |
35 | { | |
36 | } | |
37 | ||
38 | ||
39 | /** | |
40 | * Open the connection to the peer. | |
41 | * @return 0 if all is fine, -1 on error. | |
42 | */ | |
43 | int NetHelper::open_connection(const string& hostname, const string& port) | |
44 | { | |
45 | try | |
46 | { | |
47 | IPServicePtr->connect(hostname,port); | |
48 | } | |
49 | catch ( boost::system::system_error boost_exception ) | |
50 | { | |
51 | ostringstream out; | |
52 | out << "NetHelper::open_connection(): " << boost_exception.what() << " Host: " << hostname << " Port: " << port; | |
53 | Log->print_network_error(out.str()); | |
54 | return -1; | |
55 | } | |
56 | return 0; | |
57 | } | |
58 | ||
59 | ||
60 | /** | |
61 | * Send the given data | |
62 | * @param data Data to send | |
63 | * @return 0 if all is fine, -1 on error. | |
64 | */ | |
65 | int NetHelper::send_data(const std::string& data) | |
66 | { | |
67 | try | |
68 | { | |
69 | IPServicePtr->write_to_socket(data); | |
70 | } | |
71 | catch ( boost::system::system_error boost_exception ) | |
72 | { | |
73 | ostringstream out; | |
74 | out << "NetHelper::send_data(): " << boost_exception.what() << " Data to be send: " << data; | |
75 | Log->print_network_error(out.str()); | |
76 | return -1; | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | ||
82 | /** | |
83 | * Receive all available data from the peer. | |
84 | * @return The data received. | |
85 | */ | |
86 | std::string NetHelper::receive_data() | |
87 | { | |
88 | string received_data; | |
89 | try | |
90 | { | |
91 | received_data = IPServicePtr->read_from_socket(); | |
92 | } | |
93 | catch ( boost::system::system_error boost_exception ) | |
94 | { | |
95 | ostringstream out; | |
96 | out << "NetHelper::receive_data(): " << boost_exception.what(); | |
97 | Log->print_network_error(out.str()); | |
98 | return ""; | |
99 | } | |
100 | return received_data; | |
101 | } | |
102 | ||
103 | ||
104 | /** | |
105 | * Close the active session. | |
106 | * @return 0 if all is fine, -1 on error | |
107 | */ | |
108 | int NetHelper::close_connection() | |
109 | { | |
110 | try | |
111 | { | |
112 | IPServicePtr->close(); | |
113 | } | |
114 | catch ( boost::system::system_error boost_exception ) | |
115 | { | |
116 | ostringstream out; | |
117 | out << "NetHelper::close_connection(): " << boost_exception.what(); | |
118 | Log->print_network_error(out.str()); | |
119 | return -1; | |
120 | } | |
121 | return 0; | |
122 | } |