I have here a simple program using coffeescript which creates a server on a Raspberry pi, and this receives data from a python script running on a Windows machine on the same network, does some calculations with that data and sends back the results.
What I would like to do, is keep the server open, running and accepting data until a parameter sent by the python script asks it to close.
To try this, I have created a loop which blocks the code until these flags are set; however, this is, well, blocking the code so the flags are not being set (the python script is not even able to connect). So I imagine I need to use some sort of asynchronous control here/promises.
# Functions
process_data = (data) ->
double_value: data * 2
triple_value: data * 3
clean_up_actions = () ->
console.log "Cleaning up..."
# Create server
net = require('net')
domain = '10.1.1.28'
port = 9001
program_complete = false
clean = false
console.log("Creating server...")
server = net.createServer (socket) ->
console.log "#{socket.remoteAddress} has connected"
# When data is received, do something and send back a response
socket.on 'data', (data) ->
console.log("Data received...")
if not program_complete
incoming_data = JSON.parse(data)
some_useful_value = incoming_data["some_useful_value"]
result = process_data(some_useful_value)
socket.write JSON.stringify(result)
# Check if end of program
program_complete = incoming_data["end_program"]
if program_complete
clean_up_actions()
clean = true
console.log "Listening to #{domain}:#{port}"
server.listen port, domain
console.log("Waiting until complete...")
loop
break if program_complete and clean
console.log "--- PROGRAM FINISHED ---"
To test this, I have used Q.delay(10000) instead of the loop, and it works how I expect it to work (I get a connection and can pass data back and forth not problem), except instead of exiting when the end_program is set to True, it will exit after 10 seconds (as expected).
...
# console.log("Waiting until complete...")
# loop
# break if program_complete and clean
# console.log "--- PROGRAM FINISHED ---"
Q = require('q')
Q.delay(10000).then ->
console.log "Done"
.finally ->
console.log('--- PROGRAM END ---')
process.exit(0)
My question is, how can I use a promise or other asynchronous control (like Q.delay(n)) but instead of delaying for a time, it waits until these flags are set.
If interested, or want to test it, the python code is below
import socket
import json
import time
REMOTE_HOST = '10.1.1.28'
REMOTE_PORT = 9001
# Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect to the remote host and port
sock.connect((REMOTE_HOST, REMOTE_PORT))
# Send some data
data_dictionary = {
'some_useful_value' : 21,
'end_program' : False
}
data_to_send = json.dumps(data_dictionary).encode('utf-8')
sock.send(data_to_send)
print(sock.recv(1024))
time.sleep(0.5)
# Send some data, but this time, end
data_dictionary = {
'some_useful_value' : 13,
'end_program' : True
}
data_to_send = json.dumps(data_dictionary).encode('utf-8')
sock.send(data_to_send)
print(sock.recv(1024))
time.sleep(0.5)
# Terminate
sock.close()