-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwuclient.pas
58 lines (47 loc) · 1.48 KB
/
wuclient.pas
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
{
Weather update client
Connects SUB socket to tcp://localhost:5556
Collects weather updates and finds avg temp in zipcode
@author cpicanco <[email protected]>
}
program wuclient;
{$MODE objfpc}{$H+}{$COPERATORS ON}
uses SysUtils, zmq, zmq.helpers;
var
context : Pointer;
subscriber : Pointer;
rc: Integer;
filter : string;
update_nbr : Integer;
total_temp : LongInt = 0;
total_humid : LongInt = 0;
LString : shortstring;
zipcode, temperature, relhumidity : integer;
begin
// Socket to talk to server
WriteLn('Collecting updates from weather server…');
context := zmq_ctx_new;
subscriber := zmq_socket(context, ZMQ_SUB);
rc := zmq_connect(subscriber, 'tcp://localhost:5556');
Assert(rc = 0);
// Subscribe to zipcode, default is NYC, 10001
if ParamCount > 1 then filter := ParamStr(1) else filter := '10001';
rc := zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, @filter[1], Length(filter));
Assert(rc = 0);
// Process 100 updates
for update_nbr := 0 to 100 -1 do
begin
LString := s_recv(subscriber);
ReadStr(LString, zipcode, temperature, relhumidity);
total_temp += temperature;
total_humid += relhumidity;
LString := '';
end;
update_nbr += 1;
WriteLn(Format('Average temperature for zipcode %s was %.2f',
[filter, total_temp/update_nbr]));
WriteLn(Format('Average relative humidity for zipcode %s was %.2f',
[filter, total_humid/update_nbr]));
zmq_close(subscriber);
zmq_ctx_destroy(context);
end.