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 |
ssocket = new SubNet(host, port); |
24 |
|
25 |
return ssocket->isConnected(); |
26 |
|
27 |
} // connectHeartBeat |
28 |
|
29 |
string SmallNet::heartBeatSend(string text){ |
30 |
|
31 |
|
32 |
|
33 |
// char buf[1024]; |
34 |
|
35 |
if ( debug == 1 ){ |
36 |
std::cout << "SmallNet::heartBeatSend()\n"; |
37 |
} |
38 |
|
39 |
return ssocket->sendTCP(text); |
40 |
|
41 |
|
42 |
} // heartBeatSend |
43 |
|
44 |
|
45 |
void SmallNet::closeHeartBeatConnection(){ |
46 |
if ( debug == 1 ){ |
47 |
std::cout << "SmallNet::closeHeartBeatConnection()\n"; |
48 |
std::cout << "Deleting socket\n"; |
49 |
} |
50 |
|
51 |
// closes automatically |
52 |
delete ssocket; |
53 |
return; |
54 |
|
55 |
} // closeHeartBeatConnection |
56 |
|
57 |
|
58 |
int SmallNet::connectConfig(string host, int port){ |
59 |
if ( debug == 1 ){ |
60 |
std::cout << "SmallNet::connectConfig(" << host << "," << port << ")\n"; |
61 |
} |
62 |
|
63 |
return connectHeartBeat(host,port); |
64 |
|
65 |
} // makeConfigConnection |
66 |
|
67 |
string SmallNet::configSend(string text){ |
68 |
if ( debug == 1 ){ |
69 |
std::cout << "SmallNet::configSend()\n"; |
70 |
} |
71 |
|
72 |
return heartBeatSend(text); |
73 |
|
74 |
} // configSend |
75 |
|
76 |
void SmallNet::closeConfigConnection(){ |
77 |
if ( debug == 1 ){ |
78 |
std::cout << "SmallNet::closeConfigConnection()\n"; |
79 |
} |
80 |
|
81 |
closeHeartBeatConnection(); |
82 |
|
83 |
return; |
84 |
|
85 |
} |
86 |
|
87 |
void SmallNet::sendUPDPacket( string host, int port, string message ){ |
88 |
|
89 |
if ( debug == 1 ){ |
90 |
std::cout << "SmallNet::sendUPDPacket:" << host << ":" << port << "\n"; |
91 |
// std::cout << message << "\n"; |
92 |
} |
93 |
|
94 |
// hard coding for testing |
95 |
/* |
96 |
host = "raptor.ukc.ac.uk"; |
97 |
port = 7776; |
98 |
*/ |
99 |
|
100 |
ssocket = new SubNet(); |
101 |
ssocket->sendUPDPacket(host, port, message); |
102 |
delete ssocket; |
103 |
|
104 |
return; |
105 |
} |
106 |
|
107 |
string SmallNet::getHostName(){ |
108 |
|
109 |
ssocket = new SubNet(); |
110 |
string name = ssocket->getHostName(); |
111 |
delete ssocket; |
112 |
return name; |
113 |
} |
114 |
|
115 |
string SmallNet::getHostIP(){ |
116 |
|
117 |
// (*socket).rdbuf()->bind(); |
118 |
// return (*socket)->localaddr(); |
119 |
return ""; |
120 |
} |