Example #1
0
    def run(self):
        self.running = True

        if not self.started:
            self.http_server = ThreadWebServer((self.bindip, self.port), MyHttpHandler)
            self.http_server.id = self.id
            self.http_server.nodeid = self.nodeid
            self.http_server.sleep_time = self.sleep_time
            self.http_server.qIn = self.qIn
            self.http_server.qOut = self.qOut
            self.http_server.process = self
            self.http_server.req_count = 0

            # SSL
            ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_ctx.verify_mode = ssl.CERT_REQUIRED
            ssl_ctx.options |= ssl.OP_SINGLE_ECDH_USE
            ssl_ctx.options |= ssl.OP_NO_COMPRESSION
            ssl_ctx.load_cert_chain(certfile=os.path.join(self.exec_path, 'cert', 'server.pem'), keyfile=os.path.join(self.exec_path, 'cert', 'server.key'))
            ssl_ctx.load_verify_locations(cafile=os.path.join(self.exec_path, 'cert', 'ca.pem'))
            ssl_ctx.set_ciphers('ECDH+AESGCM')
            self.http_server.socket = ssl_ctx.wrap_socket (self.http_server.socket)

            Thread(target=self.http_server.serve).start()

            self.started = True

            while self.running:
                try:
                    # self.http_server.handle_request()
                    if self.sleep_time != 0:
                        if self.attentiveness_iteration_remaining > 0:
                            self.attentiveness_iteration_remaining -= 1
                            time.sleep(self.sleep_time / 10)
                        else:
                            time.sleep(self.sleep_time)

                    has_message = False
                    try:
                        m = self.qIn.get(False if self.sleep_time != 0 else True)
                        has_message = True
                        self.attentiveness_iteration_remaining = 10
                    except Exception as e:
                        pass

                    if has_message is True:
                        if m.command == "pill":
                            continue
                        elif m.command == "kill":
                            if self.started:
                                self.http_server.stop()
                                self.running = False

                        elif m.command == "tick":
                            self.uptime = m.arguments[0]
                            continue

                        elif m.communication_id is not None:
                            self.response_long_running_request(m.communication_id, m.arguments)
                            continue

                        #return_message = Message(self.id, "Logger", "print", ["done by %s: %s" % (self.id, m.command)])
                        #self.qOut.put(return_message)
                except Exception as e2:
                    m = Message("ComWebServer", "System", "exception", [traceback.format_exc()])
                    self.qOut.put(m)
Example #2
0
class ComWebServer(Process):
    def __init__(self, sleep_time, exec_path, bindip, qIn, qOut, port, nodeid, id):
        Process.__init__(self)
        self.exec_path = exec_path
        self.bindip = bindip
        self.daemon = False
        self.cancelled = False
        self.sleep_time = sleep_time
        self.attentiveness_iteration_remaining = 0
        self.qIn = qIn
        self.qOut = qOut
        self.port = port
        self.nodeid = nodeid
        self.id = id

        self.started = False
        self.http_server = None
        self.http_handler = None

        self.uptime = 0

        self.long_running_request = {}

    def run(self):
        self.running = True

        if not self.started:
            self.http_server = ThreadWebServer((self.bindip, self.port), MyHttpHandler)
            self.http_server.id = self.id
            self.http_server.nodeid = self.nodeid
            self.http_server.sleep_time = self.sleep_time
            self.http_server.qIn = self.qIn
            self.http_server.qOut = self.qOut
            self.http_server.process = self
            self.http_server.req_count = 0

            # SSL
            ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
            ssl_ctx.verify_mode = ssl.CERT_REQUIRED
            ssl_ctx.options |= ssl.OP_SINGLE_ECDH_USE
            ssl_ctx.options |= ssl.OP_NO_COMPRESSION
            ssl_ctx.load_cert_chain(certfile=os.path.join(self.exec_path, 'cert', 'server.pem'), keyfile=os.path.join(self.exec_path, 'cert', 'server.key'))
            ssl_ctx.load_verify_locations(cafile=os.path.join(self.exec_path, 'cert', 'ca.pem'))
            ssl_ctx.set_ciphers('ECDH+AESGCM')
            self.http_server.socket = ssl_ctx.wrap_socket (self.http_server.socket)

            Thread(target=self.http_server.serve).start()

            self.started = True

            while self.running:
                try:
                    # self.http_server.handle_request()
                    if self.sleep_time != 0:
                        if self.attentiveness_iteration_remaining > 0:
                            self.attentiveness_iteration_remaining -= 1
                            time.sleep(self.sleep_time / 10)
                        else:
                            time.sleep(self.sleep_time)

                    has_message = False
                    try:
                        m = self.qIn.get(False if self.sleep_time != 0 else True)
                        has_message = True
                        self.attentiveness_iteration_remaining = 10
                    except Exception as e:
                        pass

                    if has_message is True:
                        if m.command == "pill":
                            continue
                        elif m.command == "kill":
                            if self.started:
                                self.http_server.stop()
                                self.running = False

                        elif m.command == "tick":
                            self.uptime = m.arguments[0]
                            continue

                        elif m.communication_id is not None:
                            self.response_long_running_request(m.communication_id, m.arguments)
                            continue

                        #return_message = Message(self.id, "Logger", "print", ["done by %s: %s" % (self.id, m.command)])
                        #self.qOut.put(return_message)
                except Exception as e2:
                    m = Message("ComWebServer", "System", "exception", [traceback.format_exc()])
                    self.qOut.put(m)





    def add_long_running_request(self, id):
        ret = {"cond": Condition(), "response": None}
        self.long_running_request[id] = ret
        return ret

    def response_long_running_request(self, id, resp):
        if self.long_running_request[id] is not None:
            self.long_running_request[id]["cond"].acquire()
            self.long_running_request[id]["cond"].notify()
            self.long_running_request[id]["response"] = resp
            self.long_running_request[id]["cond"].release()
            self.long_running_request.pop(id, False)