| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
suppressMessages(library(pbdZMQ, quietly = TRUE)) |
| 7 |
|
| 8 |
|
| 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"){ |
| 13 |
zmq.bind(publisher, "ipc://weather.ipc") |
| 14 |
} |
| 15 |
|
| 16 |
|
| 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 |
|
| 29 |
zmq.close(publisher) |
| 30 |
zmq.ctx.destroy(context) |
| 31 |
|
| 32 |
|
| 33 |
if(file.exists("weather.ipc")){ |
| 34 |
file.remove("weather.ipc") |
| 35 |
} |