Fix 'occurred' typo
[bpdyndnsd] / src / net_helper.cpp
CommitLineData
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
4de6a9b8
BS
10#include "net_helper.hpp"
11#include "tcp_service.hpp"
6650af14
BS
12
13
14using namespace std;
15
16
17/**
c9d27cb6
BS
18 * Default Constructor.
19 */
20NetHelper::NetHelper()
21 : Log(new Logger())
22{
c3c84086 23 IPServicePtr = IPService::Ptr(new TCPService());
c9d27cb6
BS
24}
25
26
27/**
6650af14
BS
28 * Constructor.
29 * @param _log Logger
6650af14
BS
30 */
31NetHelper::NetHelper( const Logger::Ptr _log )
32 : Log(_log)
33{
c3c84086 34 IPServicePtr = IPService::Ptr(new TCPService());
6650af14
BS
35}
36
37
38/**
39 * Default destructor
40 */
41NetHelper::~NetHelper()
42{
43}
44
45
46/**
47 * Open the connection to the peer.
48 * @return 0 if all is fine, -1 on error.
49 */
83ae2edf 50int NetHelper::open_connection(const string& hostname, const string& port) const
6650af14
BS
51{
52 try
53 {
54 IPServicePtr->connect(hostname,port);
55 }
08a5a621 56 catch ( const boost::system::system_error& boost_exception )
6650af14
BS
57 {
58 ostringstream out;
59 out << "NetHelper::open_connection(): " << boost_exception.what() << " Host: " << hostname << " Port: " << port;
60 Log->print_network_error(out.str());
61 return -1;
62 }
08a5a621 63 catch ( const exception& e )
557b6f56
BS
64 {
65 ostringstream out;
66 out << "NetHelper::open_connection(): " << e.what() << " Host: " << hostname << " Port: " << port;
67 Log->print_network_error(out.str());
68 return -1;
69 }
70 catch ( ... )
71 {
72 ostringstream out;
73 out << "Unknown exception caught while trying to connect to Host: " << hostname << " Port: " << port;
74 Log->print_network_error(out.str());
75 return -1;
76 }
6650af14
BS
77 return 0;
78}
79
80
81/**
82 * Send the given data
83 * @param data Data to send
84 * @return 0 if all is fine, -1 on error.
85 */
31beb90a 86int NetHelper::send_data(const std::string& data) const
6650af14
BS
87{
88 try
89 {
90 IPServicePtr->write_to_socket(data);
91 }
08a5a621 92 catch ( const boost::system::system_error& boost_exception )
6650af14
BS
93 {
94 ostringstream out;
95 out << "NetHelper::send_data(): " << boost_exception.what() << " Data to be send: " << data;
96 Log->print_network_error(out.str());
97 return -1;
98 }
99 return 0;
100}
101
102
103/**
104 * Receive all available data from the peer.
105 * @return The data received.
106 */
31beb90a 107std::string NetHelper::receive_data() const
6650af14
BS
108{
109 string received_data;
110 try
111 {
112 received_data = IPServicePtr->read_from_socket();
113 }
08a5a621 114 catch ( const boost::system::system_error& boost_exception )
6650af14
BS
115 {
116 ostringstream out;
117 out << "NetHelper::receive_data(): " << boost_exception.what();
118 Log->print_network_error(out.str());
119 return "";
120 }
121 return received_data;
122}
123
124
125/**
126 * Close the active session.
127 * @return 0 if all is fine, -1 on error
128 */
31beb90a 129int NetHelper::close_connection() const
6650af14
BS
130{
131 try
132 {
133 IPServicePtr->close();
134 }
08a5a621 135 catch ( const boost::system::system_error& boost_exception )
6650af14
BS
136 {
137 ostringstream out;
138 out << "NetHelper::close_connection(): " << boost_exception.what();
139 Log->print_network_error(out.str());
140 return -1;
141 }
142 return 0;
143}