block by vasturiano 0cb8150e5bb2d6aba673

BottleNTP - A simple NTP service simulator in NodeJS

BottleNTP

BottleNTP is a simple NTP service using a messaging-queue simulator, written in JavaScript/Node. It features one producer which publishes NTP messages every second containing the current NTP time. The service subscribes multiple NTP consumers which log the NTP time received.

A consumer must send keep-alive messages to ensure continued subscription to the service. If no keep-alive message is received from a consumer for longer than 10 seconds, that consumer is unsubscribed from the service and will receive no more messages.

Each consumer will emit keep-alive messages every 5 seconds for N times. N is a random number between 0 and 12 and is a property of the consumer itself. This way, different consumers will be unsubscribed from the service asynchronously at different times.

Setup

Clone directly from this GitHub Gist:

git clone https://gist.github.com/0cb8150e5bb2d6aba673.git BottleNTP

If needed, install Node.js. For example, if you have Homebrew on OS X:

brew install node

Usage

node BottleNTP \

This will run the main script BottleNTP/index.js. num-consumers is a positive number representing the number of consumers to subscribe to the service.

Example

> node BottleNTP 5

Started NTP broadcast service
Subscribing consumer 0 to NTP service for 25 secs (4 keepalives)
Subscribing consumer 1 to NTP service for 60 secs (11 keepalives)
Subscribing consumer 2 to NTP service for 15 secs (2 keepalives)
Subscribing consumer 3 to NTP service for 5 secs (0 keepalives)
Subscribing consumer 4 to NTP service for 15 secs (2 keepalives)
Consumer 0 NTP time: Sat Feb 28 2015 18:07:46 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:07:46 GMT-0800 (PST)
Consumer 2 NTP time: Sat Feb 28 2015 18:07:46 GMT-0800 (PST)
Consumer 3 NTP time: Sat Feb 28 2015 18:07:46 GMT-0800 (PST)
Consumer 4 NTP time: Sat Feb 28 2015 18:07:46 GMT-0800 (PST)
Consumer 0 NTP time: Sat Feb 28 2015 18:07:47 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:07:47 GMT-0800 (PST)
Consumer 2 NTP time: Sat Feb 28 2015 18:07:47 GMT-0800 (PST)
Consumer 3 NTP time: Sat Feb 28 2015 18:07:47 GMT-0800 (PST)
Consumer 4 NTP time: Sat Feb 28 2015 18:07:47 GMT-0800 (PST)

Consumer 2 just sent its last keepalive
Consumer 4 just sent its last keepalive
Consumer 0 NTP time: Sat Feb 28 2015 18:07:55 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:07:55 GMT-0800 (PST)
Consumer 2 NTP time: Sat Feb 28 2015 18:07:55 GMT-0800 (PST)
Consumer 4 NTP time: Sat Feb 28 2015 18:07:55 GMT-0800 (PST)

Consumer 1 just sent its last keepalive
Consumer 1 NTP time: Sat Feb 28 2015 18:08:41 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:42 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:43 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:44 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:45 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:46 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:47 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:48 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:49 GMT-0800 (PST)
Consumer 1 NTP time: Sat Feb 28 2015 18:08:50 GMT-0800 (PST)
Stopped NTP service

index.js

message-broker.js

ntp-service.js

timer.js