commit c19f51daadff77ec6985cfd532677a4a5039f7cd
Author: David Sorber <david.sorber@gmail.com>
Date:   Thu Jan 3 21:30:53 2019 -0500

    Adding command server prototype. Clean shutdown does not work properly, need to figure that out.

diff --git a/software/hbkcd/server.py b/software/hbkcd/server.py
new file mode 100755
index 0000000..759d13c
--- /dev/null
+++ b/software/hbkcd/server.py
@@ -0,0 +1,109 @@
+#!/usr/bin/python3
+from http.server import BaseHTTPRequestHandler, HTTPServer
+import json
+from multiprocessing import Process
+import sys
+import time
+
+# https://blog.gocept.com/2011/08/04/shutting-down-an-httpserver/
+# https://docs.aws.amazon.com/polly/latest/dg/example-Python-server-code.html
+ 
+class CommandServer(Process):
+    
+    def __init__(self):
+        Process.__init__(self)
+        
+        self.server_address = ('127.0.0.1', 8081)
+        self.httpd = HTTPServer(self.server_address, CommandHandler)
+        self.keep_running = True
+    
+    def stop(self):
+        print('stop called')
+        self.keep_running = False
+        # ~ if self.httpd:
+            # ~ print('pre shutdown')
+            # ~ self.httpd.shutdown()
+            # ~ print('post shutdown')
+        self.httpd.shutdown()
+        self.httpd.socket.close()
+        # ~ self.httpd.server_close()
+        print('stop returning')
+        
+    def run(self):
+        print('command server running')
+        
+        # ~ while self.keep_running:
+        self.httpd.serve_forever()
+        
+        # ~ while self.keep_running:
+            # ~ self.httpd.handle_request()
+        
+        print('command server stopping')
+    
+ 
+# HTTPRequestHandler class
+class CommandHandler(BaseHTTPRequestHandler):
+ 
+    # GET
+    def do_GET(self):
+        # Strip off leading and/or trailing slashes
+        path = self.path.strip('/')
+        
+        # Defaults
+        response_code = 400
+        response_type = 'text/html'
+        response_data = ''
+      
+        # Send response status code
+        if path == 'queue':
+            response_code = 200
+            response_dict = {'queue': ['alpha', 'bravo', 'charlie']}
+            response_data = json.dumps(response_dict).encode()
+        
+        elif not path:
+            response_code = 200
+            response = 'Why hello there'
+            response_data = response.encode('utf-8')
+        
+        # Send response and data, if any
+        self.send_response(response_code)
+        
+        if response_data:
+            self.send_header('Content-type', response_type)
+            
+        self.end_headers()
+        
+        if response_data:
+            self.wfile.write(response_data)
+            
+        return
+        
+ 
+def main():
+    print('starting server...')
+ 
+    # Server settings
+    # Choose port 8080, for port 80, which is normally used for a http server, 
+    # you need root access
+    # ~ server_address = ('127.0.0.1', 8081)
+    # ~ httpd = HTTPServer(server_address, CommandHandler)
+    # ~ print('running server...')
+    # ~ httpd.serve_forever()
+    
+    cmd_server = CommandServer()
+    cmd_server.start()
+    
+    # Run for a while 
+    for idx in range(20):
+        print(idx)
+        time.sleep(1)
+        
+    # Simluate a clean shutdown
+    cmd_server.stop()
+    print('post stop')
+    cmd_server.join()
+        
+    print('fin')
+ 
+if __name__ == '__main__':
+    sys.exit(main())
