Commit 062b86e2 authored by Kento HASEGAWA's avatar Kento HASEGAWA
Browse files

Merge branch 'comm' into 'master'

Improve UI and add a stoppable thread for a solver

See merge request adc2018/adc2018-system!3
parents c592b577 55491c32
Loading
Loading
Loading
Loading
+19 −60
Changes for comm/client/main.py: 19 added lines, 60 removed lines.
Original line number Diff line number Diff line
@@ -17,65 +17,17 @@ from urllib.parse import urlparse

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver')
import BoardStr
import pynqrouter
import adc2018solver as pynqrouter

app = Flask(__name__)
args = {}
pynq_thread = None
# pynq_thread = None
client_baseurl = ""

# Reference: https://teratail.com/questions/52593
class StoppableThread(threading.Thread):
    def __init__(self, target, qname, qstr, qseed):
        super(StoppableThread, self).__init__(target=target)
        self._th_stop = threading.Event()
        self._qname = qname
        self._qstr = qstr
        self._qseed = int(qseed)
        self._answer = None

    def run(self):
        global client_baseurl
        global pynq_thread
        global args

        # Main funciton (the solver should be placed here)
        boardstr = BoardStr.conv_boardstr(self._qstr.split('\n'), 'random', self._qseed)
        result = pynqrouter.solve(boardstr, self._qseed)
        if result['solved']:
            answer = result['solution']
        else:
            answer = None
            pynq_thread = None
            return

        res = {
            "client": client_baseurl,
            "qname": self._qname,
            "answer": answer,
            "cputime": result['elapsed']
        }
        self._answer = answer
        r = requests.post("http://{}/post".format(args["host"]), data=res)

        if args["verbose"]:
            print(res)

        pynq_thread = None

    def stop(self):
        self._th_stop.set()

    def stopped(self):
        return self._th_stop.isSet()

    def get_answer(self):
        return self._answer

@app.route('/start', methods=["POST"])
def start():

    global pynq_thread
    # global pynq_thread
    global args
    global client_baseurl

@@ -84,13 +36,19 @@ def start():
    if args["verbose"]:
        print(request.form)

    if pynq_thread is None:
    if pynqrouter.solver_thread is not None:
        pynqrouter.stop_solver()

    if pynqrouter.solver_thread is None:
        qstr = request.form["question"]
        qname = request.form["qname"]
        qseed = request.form["qseed"]
        pynq_thread = StoppableThread(target=StoppableThread, qname=qname, qstr=qstr, qseed=qseed)
	
        pynq_thread.start()
        boardstr = BoardStr.conv_boardstr(qstr.split('\n'), 'random', int(qseed))
        option = {"name": qname, "host": args['host'], "client": client_baseurl} 

        pynqrouter.start_solver(boardstr, qseed, option)
        # pynq_thread = pynqrouter.start_solver(boardstr, qseed, option)

        # 実行だけ開始する
        ans["status"] = "Processing"
@@ -106,26 +64,27 @@ def start():
@app.route('/stop')
def stop():

    global pynq_thread
    # global pynq_thread

    if pynq_thread is None:
    if pynqrouter.solver_thread is None:
        ans = {"status": "No threads"}
    else:
        pynq_thread.stop()
        # pynq_thread.stop()
        pynqrouter.stop_solver()
        ans = {"status": "Stopped"}

    pynq_thread = None
    # pynq_thread = None

    return json.dumps(ans)

@app.route("/status")
def status():

    global pynq_thread
    # global pynq_thread

    res_mes = ""

    if pynq_thread is None:
    if pynqrouter.solver_thread is None:
        res_mes = "Ready"
    else:
        res_mes = "Working"
+1 −5
Changes for comm/server/main.py: 1 added line, 5 removed lines.
Original line number Diff line number Diff line
@@ -185,7 +185,7 @@ def start():
    else:
        # Format check
        #cmd = "/usr/bin/python /home/pi/conmgr/adc2017/server/nlcheck.py --input {} --target {}".format(probpath, outpath)
        cmd = "/usr/bin/python /home/pi/adc2017/conmgr/adc2017/server/nlcheck.py --input {} --target {}".format(probpath, outpath)
        cmd = "/usr/bin/python /home/pi/adc2018/conmgr/adc2018/server/nlcheck.py --input {} --target {}".format(probpath, outpath)
        print("`{}`".format(cmd))
        subprocess.call(cmd.strip().split(" "))

@@ -318,7 +318,6 @@ def main(args):
if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="PYNQ control panel.")
    parser.add_argument("--no-gui", action="store_false", dest="gui", default=True, help="No GUI")
    parser.add_argument("-c", "--client", action="store", type=str, default=None, required=True, help="Client list.")
    parser.add_argument("-q", "--question", action="store", type=str, default="./problems", help="Path to the question folder.")
    parser.add_argument("-o", "--out", action="store", type=str, default="./answers", help="Path to the output folder.")
@@ -328,9 +327,6 @@ if __name__ == "__main__":
    args = vars(parser.parse_args())
    app_args = args

    if args["gui"]:
    if args["debug"]:
        app.debug = True
    app.run(host='0.0.0.0', threaded=True)
    else:
        main(args)
+1 −0

File added.

Preview size limit exceeded, changes collapsed.

+56 −0
Changes for comm/server/static/css/pynq-manager.css: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
html, body{
    /*width: 800px;*/
    /*height: 500px;*/
    font-size: 11px;
    overflow: hidden;
}
body{
    padding-top: 10px;
}

/* scrollbar settings */
::-webkit-scrollbar{
    width: 25px;
    border: 1px solid rgba(0, 0, 50, .2);
}
::-webkit-scrollbar-thumb{
    background-color: rgba(0, 0, 50, .5);
}

#question-table-wrapper{
    height: 300px;
    overflow-y: scroll;
    overflow-x: hidden;
}

#question-table-wrapper th.large-cell,
#question-table-wrapper td.large-cell{
    width: 24%;
}
#question-table-wrapper th.small-cell,
#question-table-wrapper td.small-cell{
    width: 14%;
}

#question-table-wrapper tr.question-row,
#question-table-wrapper tr.question-row td{
    cursor: pointer;
}

#question-table-wrapper tr.question-row.q-selected{
    background-color: rgba(200, 100, 100, .3);
}

#client-control-pane{
    height: 330px;
    overflow: scroll;
}

#client-control-pane table th,
#client-control-pane table td{
    width: 50%;
}

#question-control-pane h3{
    display: inline-block;
}
+1 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading