From 9050bd6afc387a6c84b9c085e65aee9df621ae9b Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Fri, 17 Apr 2020 16:45:39 +0200 Subject: [PATCH 1/3] Remove old "lastConnection" logic --- src/broadcast/state.ts | 12 +++++------- stateserver.py | 26 ++++++++------------------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/broadcast/state.ts b/src/broadcast/state.ts index 628037c..90e9a14 100644 --- a/src/broadcast/state.ts +++ b/src/broadcast/state.ts @@ -118,7 +118,7 @@ export const registerForShow = (): AppThunk => async (dispatch, getState) => { const timeslotid = state.currentTimeslot?.timeslot_id; const wsId = getState().broadcast.wsID; if (wsId === null) { - console.warn("Tried to register for broadcast with no wsID"); + throw new Error("Tried to register for broadcast with no wsID"); } console.log("Attempting to Register for Broadcast."); var sourceid = getState().broadcast.sourceID; @@ -127,7 +127,7 @@ export const registerForShow = (): AppThunk => async (dispatch, getState) => { timeslotid, memberid, sourceid, - wsId || undefined + wsId ); console.log(connID); if (connID !== undefined) { @@ -197,16 +197,14 @@ export function sendBroadcastRegister( timeslotid: number | undefined, memberid: number | undefined, sourceid: number, - wsID?: string + wsID: string ): Promise { const payload = { memberid: memberid, timeslotid: timeslotid, - sourceid: sourceid + sourceid: sourceid, + wsid: wsID } as any; - if (typeof wsID === "string") { - payload["wsid"] = wsID; - } return broadcastApiRequest("/registerTimeslot", "POST", payload); } export function sendBroadcastCancel( diff --git a/stateserver.py b/stateserver.py index dfc7879..aa329e1 100755 --- a/stateserver.py +++ b/stateserver.py @@ -80,7 +80,6 @@ SOURCES = [SOURCE_JUKEBOX, SOURCE_OB, SOURCE_WS, SOURCE_OFFAIR] # This array will only hold connections we've validated to be authorised to broadcast. connections: List[Connection] = [] wsSessions: Dict[str, Dict[str, str]] = {} -lastConnectionIDToRegister = -1 def getCurrentShowConnection() -> Optional[Connection]: @@ -228,7 +227,6 @@ def post_cancelCheck() -> Any: @app.route('/api/v1/registerTimeslot', methods=['POST']) def post_registerCheck() -> Any: global connections - global lastConnectionIDToRegister content = request.json if not content: @@ -241,6 +239,8 @@ def post_registerCheck() -> Any: return genFail("Request missing valid source.") if not content["sourceid"] in SOURCES: return genFail("Request missing valid source.") + if not isinstance(content["wsid"], str): + return genFail("Request missing valid wsID") member = myradioApiRequest("user/" + str(content["memberid"])) if not member: @@ -294,23 +294,19 @@ def post_registerCheck() -> Any: 'autoNewsBeginning': True, 'autoNewsMiddle': True, 'autoNewsEnd': True, - 'wsid': None + 'wsid': content["wsid"] } - if "wsid" in content: - connection["wsid"] = content["wsid"] - if start_time + datetime.timedelta(minutes=2) < now_time: - # they're late, bring them live now - print("({}, {}) late, bringing on air now".format(connection["connid"], connection["wsid"])) - do_ws_srv_telnet(connection["wsid"]) - subprocess.Popen(['sel', '5']) + if start_time + datetime.timedelta(minutes=2) < now_time: + # they're late, bring them live now + print("({}, {}) late, bringing on air now".format(connection["connid"], connection["wsid"])) + do_ws_srv_telnet(connection["wsid"]) + subprocess.Popen(['sel', '5']) assert connection is not None connections.append(connection) print(connections) - lastConnectionIDToRegister = connection["connid"] - return genPayload(connection) @@ -345,7 +341,6 @@ def post_settingsCheck() -> Any: def post_wsSessions() -> Any: global connections global wsSessions - global lastConnectionIDToRegister content = request.json # if not content: # return genFail("No parameters provided.") @@ -368,11 +363,6 @@ def post_wsSessions() -> Any: print("wsSessions which have appeared:", wsids_to_add) for conn in connections: - if conn["connid"] == lastConnectionIDToRegister: - if conn["wsid"] is None and len(wsids_to_add) == 1: - conn["wsid"] = wsids_to_add[0] - print("({}, {}) hello".format(conn["connid"], conn["wsid"])) - if conn["wsid"] in wsids_to_add: if conn["startTimestamp"] + 120 < datetime.datetime.now().timestamp(): # they're late, bring them on air now From cbd6c0b4e77ebc993376c4dbf8776fe39bbc72d4 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Fri, 17 Apr 2020 17:31:37 +0100 Subject: [PATCH 2/3] Fix index error when cleaning out connections. --- stateserver.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stateserver.py b/stateserver.py index aa329e1..02b635f 100755 --- a/stateserver.py +++ b/stateserver.py @@ -109,7 +109,8 @@ def getNextHourConnection() -> Optional[Connection]: def cleanOldConnections() -> None: global connections - for i in range(len(connections)): + #Go backwards round the loop so that pop's don't interfere with the index. + for i in range(len(connections)-1,-1,-1): if connections[i]["endTimestamp"] < datetime.datetime.now().timestamp(): connections.pop(i) From 2c299565a568ad5410d3562a44c537ca763daa96 Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Fri, 17 Apr 2020 17:51:38 +0100 Subject: [PATCH 3/3] Re-registers no longer double connections. --- stateserver.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stateserver.py b/stateserver.py index 02b635f..f3bcdd7 100755 --- a/stateserver.py +++ b/stateserver.py @@ -275,7 +275,11 @@ def post_registerCheck() -> Any: print("found existing connection {} for {}".format(conn["connid"], conn["timeslotid"])) connection = conn + new_connection = False if connection is None: + + new_connection = True + if start_time - now_time > datetime.timedelta(hours=1): return genFail("This show too far away, please try again within an hour of starting your show.") @@ -305,7 +309,8 @@ def post_registerCheck() -> Any: subprocess.Popen(['sel', '5']) assert connection is not None - connections.append(connection) + if new_connection: + connections.append(connection) print(connections) return genPayload(connection)