Skip to content

Commit

Permalink
port cli/ops/resources.rs to json ops
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Aug 23, 2019
1 parent 8577cd8 commit b2ce5c4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 75 deletions.
13 changes: 0 additions & 13 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ union Any {
ReadlinkRes,
Remove,
Rename,
Resources,
ResourcesRes,
Run,
RunRes,
RunStatus,
Expand Down Expand Up @@ -374,17 +372,6 @@ table ReadlinkRes {
path: string;
}

table Resources {}

table Resource {
rid: uint32;
repr: string;
}

table ResourcesRes {
resources: [Resource];
}

table Symlink {
oldname: string;
newname: string;
Expand Down
2 changes: 0 additions & 2 deletions cli/ops/dispatch_flatbuffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use super::performance::op_now;
use super::permissions::{op_permissions, op_revoke_permission};
use super::process::{op_kill, op_run, op_run_status};
use super::random::op_get_random_values;
use super::resources::op_resources;
use super::timers::{op_global_timer, op_global_timer_stop};
use super::workers::{
op_create_worker, op_host_get_message, op_host_get_worker_closed,
Expand Down Expand Up @@ -172,7 +171,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<CliDispatchFn> {
msg::Any::Readlink => Some(op_read_link),
msg::Any::Remove => Some(op_remove),
msg::Any::Rename => Some(op_rename),
msg::Any::Resources => Some(op_resources),
msg::Any::Run => Some(op_run),
msg::Any::RunStatus => Some(op_run_status),
msg::Any::Seek => Some(op_seek),
Expand Down
7 changes: 7 additions & 0 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub const OP_ACCEPT: OpId = 22;
pub const OP_DIAL: OpId = 23;
pub const OP_SHUTDOWN: OpId = 24;
pub const OP_LISTEN: OpId = 25;
pub const OP_RESOURCES: OpId = 26;

pub fn dispatch(
state: &ThreadSafeState,
Expand Down Expand Up @@ -84,6 +85,12 @@ pub fn dispatch(
OP_LISTEN => {
dispatch_json::dispatch(net::op_listen, state, control, zero_copy)
}
OP_RESOURCES => dispatch_json::dispatch(
resources::op_resources,
state,
control,
zero_copy,
),
OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy),
_ => panic!("bad op_id"),
};
Expand Down
50 changes: 5 additions & 45 deletions cli/ops/resources.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use super::dispatch_flatbuffers::serialize_response;
use super::utils::ok_buf;
use super::utils::CliOpResult;
use crate::msg;
use super::dispatch_json::{JsonOp, Value};
use crate::resources::table_entries;
use crate::state::ThreadSafeState;
use deno::*;
use flatbuffers::FlatBufferBuilder;

pub fn op_resources(
_state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let cmd_id = base.cmd_id();

let builder = &mut FlatBufferBuilder::new();
_args: Value,
_zero_copy: Option<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
let serialized_resources = table_entries();

let res: Vec<_> = serialized_resources
.iter()
.map(|(key, value)| {
let repr = builder.create_string(value);

msg::Resource::create(
builder,
&msg::ResourceArgs {
rid: *key,
repr: Some(repr),
},
)
})
.collect();

let resources = builder.create_vector(&res);
let inner = msg::ResourcesRes::create(
builder,
&msg::ResourcesResArgs {
resources: Some(resources),
},
);

ok_buf(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::ResourcesRes,
..Default::default()
},
))
Ok(JsonOp::Sync(json!(serialized_resources)))
}
2 changes: 2 additions & 0 deletions js/dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const OP_ACCEPT = 22;
export const OP_DIAL = 23;
export const OP_SHUTDOWN = 24;
export const OP_LISTEN = 25;
export const OP_RESOURCES = 26;

export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
switch (opId) {
Expand All @@ -35,6 +36,7 @@ export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void {
case OP_DIAL:
case OP_SHUTDOWN:
case OP_LISTEN:
case OP_RESOURCES:
json.asyncMsgFromRust(opId, ui8);
break;
default:
Expand Down
20 changes: 5 additions & 15 deletions js/resources.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { assert } from "./util";
import { sendSync, msg, flatbuffers } from "./dispatch_flatbuffers";
import * as dispatch from "./dispatch";
import { sendSync } from "./dispatch_json";

export interface ResourceMap {
[rid: number]: string;
Expand All @@ -10,20 +10,10 @@ export interface ResourceMap {
* representation.
*/
export function resources(): ResourceMap {
const builder = flatbuffers.createBuilder();
const inner = msg.Resource.createResource(builder, 0, 0);
const baseRes = sendSync(builder, msg.Any.Resources, inner);
assert(baseRes !== null);
assert(msg.Any.ResourcesRes === baseRes!.innerType());
const res = new msg.ResourcesRes();
assert(baseRes!.inner(res) !== null);

const res = sendSync(dispatch.OP_RESOURCES) as Array<[number, string]>;
const resources: ResourceMap = {};

for (let i = 0; i < res.resourcesLength(); i++) {
const item = res.resources(i)!;
resources[item.rid()!] = item.repr()!;
for (const resourceTuple of res) {
resources[resourceTuple[0]] = resourceTuple[1];
}

return resources;
}

0 comments on commit b2ce5c4

Please sign in to comment.