Fix 'occurred' typo
[bpdyndnsd] / src / net_helper.cpp
... / ...
CommitLineData
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.hpp"
11#include "tcp_service.hpp"
12
13
14using namespace std;
15
16
17/**
18 * Default Constructor.
19 */
20NetHelper::NetHelper()
21 : Log(new Logger())
22{
23 IPServicePtr = IPService::Ptr(new TCPService());
24}
25
26
27/**
28 * Constructor.
29 * @param _log Logger
30 */
31NetHelper::NetHelper( const Logger::Ptr _log )
32 : Log(_log)
33{
34 IPServicePtr = IPService::Ptr(new TCPService());
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 */
50int NetHelper::open_connection(const string& hostname, const string& port) const
51{
52 try
53 {
54 IPServicePtr->connect(hostname,port);
55 }
56 catch ( const boost::system::system_error& boost_exception )
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 }
63 catch ( const exception& e )
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 }
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 */
86int NetHelper::send_data(const std::string& data) const
87{
88 try
89 {
90 IPServicePtr->write_to_socket(data);
91 }
92 catch ( const boost::system::system_error& boost_exception )
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 */
107std::string NetHelper::receive_data() const
108{
109 string received_data;
110 try
111 {
112 received_data = IPServicePtr->read_from_socket();
113 }
114 catch ( const boost::system::system_error& boost_exception )
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 */
129int NetHelper::close_connection() const
130{
131 try
132 {
133 IPServicePtr->close();
134 }
135 catch ( const boost::system::system_error& boost_exception )
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}