Skip to content

Commit

Permalink
protocol: make it possible to send json key value pairs from the clie…
Browse files Browse the repository at this point in the history
…nt to the server in the post body
  • Loading branch information
jeffkaufman committed Jan 18, 2021
1 parent 204ed17 commit 89f442a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
14 changes: 14 additions & 0 deletions html/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,16 @@ export class SingerClient extends EventTarget {
}
}

send_kv(key, value) {
if (this.connection && this.hasConnectivity) {
this.connection.set_json_kv(key, value);
} else {
console.error(
"set_json_kv should never be sent before connection, dropping:",
key, value);
}
}

send_telemetry(key, value, append) {
// We're disabling this for now.
return;
Expand Down Expand Up @@ -1226,6 +1236,10 @@ export class SingerClientConnection {
}
}

set_json_kv(key, value) {
this.server_connection.set_json_kv(key, value);
}

send_telemetry(key, value, append) {
var telemetry;
if ("client_telemetry" in this.metadata_to_send) {
Expand Down
29 changes: 26 additions & 3 deletions html/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class ServerConnection extends ServerConnectionBase {
this.write_clock = null;
this.userid = userid;
this.send_metadata = {};
this.json_kvs = {}
this.running = false;
this.receive_cb = receive_cb;
this.failure_cb = failure_cb;
Expand Down Expand Up @@ -83,6 +84,10 @@ export class ServerConnection extends ServerConnectionBase {
this.send_metadata = send_metadata;
}

set_json_kv(key, value) {
this.json_kvs[key] = value;
}

send(chunk) {
if (!this.running) {
console.warn("Not sending to server because not running");
Expand Down Expand Up @@ -110,7 +115,12 @@ export class ServerConnection extends ServerConnectionBase {
var saved_read_clock = this.read_clock;
var saved_write_clock = this.write_clock;

samples_to_server(chunk_data, this.target_url, {
let json_kvs_str = JSON.stringify(this.json_kvs);
if (json_kvs_str === "{}") {
json_kvs_str = null;
}
this.json_kvs = {};
samples_to_server(chunk_data, this.target_url, json_kvs_str, {
read_clock: this.read_clock,
write_clock: this.write_clock,
n_samples: chunk.length,
Expand Down Expand Up @@ -333,12 +343,25 @@ export async function query_server_clock(target_url) {
}

var xhrs_inflight = 0;
export async function samples_to_server(outdata, target_url, send_metadata) {
console.debug("samples_to_server send_metadata:", send_metadata);
export async function samples_to_server(
outdata, target_url, json_kvs_str, send_metadata) {
console.debug("samples_to_server send_metadata:", send_metadata,
"json_kvs_str", json_kvs_str);
if (outdata === null) {
outdata = new Uint8Array();
}

if (json_kvs_str) {
const json_kvs_bytes = (new TextEncoder()).encode(json_kvs_str);
send_metadata.json_len = json_kvs_bytes.length;

const combined_outdata =
new Uint8Array(json_kvs_bytes.length + outdata.length);
combined_outdata.set(json_kvs_bytes);
combined_outdata.set(outdata, json_kvs_bytes.length);
outdata = combined_outdata;
}

return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.onerror = () => {
Expand Down
23 changes: 17 additions & 6 deletions server_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,16 @@ def handle_post(userid, n_samples, in_data_raw,
dec = opuslib.Decoder(server.SAMPLE_RATE, CHANNELS)
users[userid] = enc, dec

in_data = np.frombuffer(in_data_raw, dtype=np.uint8)
post_body = np.frombuffer(in_data_raw, dtype=np.uint8)
parsed_params = urllib.parse.parse_qs(query_string, strict_parsing=True)
json_len, = parsed_params.get("json_len", [None])
if json_len:
json_len = int(json_len)
json_kvs = json.loads(post_body[:json_len].tobytes().decode('utf8'))
in_data = post_body[json_len:]
else:
in_data = post_body
json_kvs = {}

# If the user does not send us any data, we will treat it as silence of length n_samples. This is useful if they are just starting up.
client_no_data = len(in_data)==0
Expand Down Expand Up @@ -148,7 +157,8 @@ def handle_post(userid, n_samples, in_data_raw,
query_string += '&rms_volume=%s'%rms_volume

data, x_audio_metadata = handle_json_post(
in_data, query_string, print_status, client_address=client_address)
in_data, query_string, json_kvs, print_status,
client_address=client_address)

# Divide data into user_summary and raw audio data
n_users_in_summary, = struct.unpack(">H", data[:2])
Expand All @@ -175,13 +185,14 @@ def handle_post(userid, n_samples, in_data_raw,

return data.tobytes(), x_audio_metadata

def handle_json_post(in_data, query_string, print_status, client_address=None):
in_json = {
def handle_json_post(in_data, query_string, json_kvs, print_status,
client_address=None):
json_kvs.update({
"query_string": query_string,
"print_status": print_status,
"client_address": client_address,
}
out_json_raw, out_data = backend.handle_post(json.dumps(in_json), in_data)
})
out_json_raw, out_data = backend.handle_post(json.dumps(json_kvs), in_data)

out_json = json.loads(out_json_raw)

Expand Down

0 comments on commit 89f442a

Please sign in to comment.