Debian Codegraph @

demo/wuserver.r

raw symbols SCIP
1 ### Weather updates server as in the ZeroMQ guide.
2 # SHELL> Rscript wuserver.r &
3 # SHELL> Rscript wuclient.r
4 # SHELL> rm weather.ipc
5
6 suppressMessages(library(pbdZMQ, quietly = TRUE))
7
8 ### Initial.
9 context <- zmq.ctx.new()
10 publisher <- zmq.socket(context, ZMQ.ST()$PUB)
11 zmq.bind(publisher, "tcp://*:5556")
12 if(.Platform$OS.type != "windows"){ # Windows does not support ipc.
13 zmq.bind(publisher, "ipc://weather.ipc")
14 }
15
16 ### Send message to all subscribers.
17 cat("Press Ctrl+C or Esc to stop wuserver.\n")
18 zipcodes <- c(50011, 37831, 37996, 20993, sample.int(100000, size = 5))
19 while(TRUE){
20 zipcode <- sample(zipcodes, 1)
21 temperature <- sample.int(215, size = 1) - 80
22 humidity <- sample.int(50, size = 1) + 10
23 ret <- sprintf("%05d %d %d", zipcode, temperature, humidity)
24 zmq.send(publisher, ret)
25 Sys.sleep(runif(1, 0.1, 0.5))
26 }
27
28 ### Finish.
29 zmq.close(publisher)
30 zmq.ctx.destroy(context)
31
32 ### Remove ipc file if exists.
33 if(file.exists("weather.ipc")){
34 file.remove("weather.ipc")
35 }