| 1 |
#include "SmallNet.h" |
| 2 |
|
| 3 |
SmallNet::SmallNet(int printDebug){ |
| 4 |
|
| 5 |
debug = printDebug; |
| 6 |
|
| 7 |
if ( debug == 1 ){ |
| 8 |
std::cout << "SmallNet Constructor\n"; |
| 9 |
} |
| 10 |
|
| 11 |
return; |
| 12 |
|
| 13 |
} // SmallNet |
| 14 |
|
| 15 |
|
| 16 |
int SmallNet::connectHeartBeat(string host, int port){ |
| 17 |
|
| 18 |
|
| 19 |
if ( debug == 1 ){ |
| 20 |
std::cout << "SmallNet::connectHeartBeat(" << host << "," << port << ")\n"; |
| 21 |
} |
| 22 |
|
| 23 |
socket = new iosockinet(sockbuf::sock_stream); |
| 24 |
// create a pointer to the object |
| 25 |
|
| 26 |
// connect the socket |
| 27 |
|
| 28 |
// std::cout << socket; |
| 29 |
|
| 30 |
return (*socket).rdbuf()->connect(host.c_str(), port); |
| 31 |
|
| 32 |
} // connectHeartBeat |
| 33 |
|
| 34 |
string SmallNet::heartBeatSend(string text){ |
| 35 |
|
| 36 |
char buf[1024]; |
| 37 |
|
| 38 |
if ( debug == 1 ){ |
| 39 |
std::cout << "SmallNet::heartBeatSend()\n"; |
| 40 |
} |
| 41 |
|
| 42 |
// std::cout << socket; |
| 43 |
// (*socket) << "BUNG BUNG BUNG" << flush; |
| 44 |
(*socket) << text.c_str() << "\n" << flush; // |
| 45 |
|
| 46 |
if ( debug == 1 ){ |
| 47 |
std::cout << "SmallNet::heartBeatSend->Sent:" << text << "\n"; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
(*socket).getline (buf, 1024); |
| 52 |
|
| 53 |
return buf; |
| 54 |
|
| 55 |
} // heartBeatSend |
| 56 |
|
| 57 |
|
| 58 |
void SmallNet::closeHeartBeatConnection(){ |
| 59 |
if ( debug == 1 ){ |
| 60 |
std::cout << "SmallNet::closeHeartBeatConnection()\n"; |
| 61 |
} |
| 62 |
|
| 63 |
// closes automatically |
| 64 |
// socket.close(); |
| 65 |
return; |
| 66 |
|
| 67 |
} // closeHeartBeatConnection |
| 68 |
|
| 69 |
|
| 70 |
int SmallNet::connectConfig(string host, int port){ |
| 71 |
if ( debug == 1 ){ |
| 72 |
std::cout << "SmallNet::connectConfig(" << host << "," << port << ")\n"; |
| 73 |
} |
| 74 |
|
| 75 |
return connectHeartBeat(host,port); |
| 76 |
|
| 77 |
} // makeConfigConnection |
| 78 |
|
| 79 |
string SmallNet::configSend(string text){ |
| 80 |
if ( debug == 1 ){ |
| 81 |
std::cout << "SmallNet::configSend()\n"; |
| 82 |
} |
| 83 |
|
| 84 |
return heartBeatSend(text); |
| 85 |
|
| 86 |
} // configSend |
| 87 |
|
| 88 |
void SmallNet::closeConfigConnection(){ |
| 89 |
if ( debug == 1 ){ |
| 90 |
std::cout << "SmallNet::closeConfigConnection()\n"; |
| 91 |
} |
| 92 |
|
| 93 |
closeHeartBeatConnection(); |
| 94 |
|
| 95 |
return; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
void SmallNet::sendUPDPacket( string host, int port, string message ){ |
| 100 |
|
| 101 |
if ( debug == 1 ){ |
| 102 |
std::cout << "SmallNet::sendUPDPacket:" << host << ":" << port << "\n"; |
| 103 |
std::cout << message << "\n"; |
| 104 |
} |
| 105 |
|
| 106 |
osockinet udpSocket(sockbuf::sock_dgram); |
| 107 |
int udpSize; |
| 108 |
udpSize = 8192; |
| 109 |
udpSocket->setopt(sockbuf::so_sndbuf , &udpSize, sizeof(udpSize), sockbuf::sol_socket); |
| 110 |
udpSocket->connect (host.c_str(), port); |
| 111 |
udpSocket << message.c_str() << endl; |
| 112 |
|
| 113 |
return; |
| 114 |
} |