-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.d
113 lines (84 loc) · 2 KB
/
sender.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import std.stdio : writeln;
import std.socket;
import std.datetime;
import std.getopt;
import std.file;
import std.conv;
interface Hardware {
float[] getTemperature();
}
//class WindowsOS : Hardware {
// override float[] getTemperature() {return [];};
//}
version(Posix)
{
class PosixOS : Hardware {
this() {
assert(exists(dir), "Directory with temp files is not exists");
auto files = dirEntries(
dir,
"*_input",
SpanMode.depth,
false);
foreach(d; files) {
coreFilenames ~= d.name;
}
}
override float[] getTemperature() {
float[] temp;
foreach(file; coreFilenames) {
auto data = to!string(read(file));
temp ~= to!float(data[0..$-4]);
}
return temp;
}
enum dir = "/sys/devices/platform/coretemp.0/hwmon/";
string[] coreFilenames;
}
}
void main(string[] args) {
int durationInMS = 1000;
string ip = "localhost:2015";
string name = "Sender";
auto result = getopt(args,
"p", "period", &durationInMS,
"i", "ip", &ip,
"n", "name of sender", &name);
if(result.helpWanted) {
defaultGetoptPrinter("Some info about the program.", result.options);
}
writeln("The sending period is set to ", durationInMS, " mseconds");
auto address = new InternetAddress(2015);
address.parse(ip);
auto listenerSocket = new TcpSocket();
listenerSocket.connect(address);
writeln("The connection is established!");
auto received = listenerSocket.send(name);
if(received == Socket.ERROR) {
writeln("Error.");
listenerSocket.close();
}
version(Posix) {
auto hard = new PosixOS;
}
//version(Windows) {
// auto hard = new WindowsOS;
//}
auto ct = Clock.currTime;
while(listenerSocket.isAlive()) {
auto nt = Clock.currTime;
auto dr = (nt - ct);
if(dr.total!"msecs" >= durationInMS) {
ct = Clock.currTime;
string buffer = to!string(hard.getTemperature());
received = listenerSocket.send(buffer);
if(received == Socket.ERROR) {
writeln("Error");
listenerSocket.close();
}
}
}
scope(exit) {
listenerSocket.close();
}
}