Skip to content

Commit

Permalink
Add some methods with varying parameter requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
locka99 committed Jun 11, 2020
1 parent 235f715 commit cfc398a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,4 @@ This work is note earmarked for any release and is aspirational in nature:
- Tokio codec - use a codec and frame writer to write message chunks
- Model enforcement rules for address space data coherence. At present, the server is expected to just know what it is
doing. Perhaps that is a reasonable thing to assume.
- There should be some helper macros for Methods that enforce the number of args and ensure the type of arguments
68 changes: 64 additions & 4 deletions samples/demo-server/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ pub fn add_methods(server: &mut Server, ns: u16) {
.organized_by(ObjectId::ObjectsFolder)
.insert(&mut address_space);

// HelloWorld takes zero args and returns "Hello World" in a result parameter
// NoOp has 0 inputs and 0 outputs
let fn_node_id = NodeId::new(ns, "NoOp");
MethodBuilder::new(&fn_node_id, "NoOp", "NoOp")
.component_of(object_id.clone())
.callback(Box::new(NoOp))
.insert(&mut address_space);


// HelloWorld has 0 inputs and 1 output - returns "Hello World" in a result parameter
let fn_node_id = NodeId::new(ns, "HelloWorld");
MethodBuilder::new(&fn_node_id, "HelloWorld", "HelloWorld")
.component_of(object_id.clone())
Expand All @@ -31,7 +39,7 @@ pub fn add_methods(server: &mut Server, ns: u16) {
.callback(Box::new(HelloWorld))
.insert(&mut address_space);

// HelloX takes one arg and returns "Hello World" in a result parameter
// HelloX has 1 one input and 1 output - "Hello Foo" in a result parameter
let fn_node_id = NodeId::new(ns, "HelloX");
MethodBuilder::new(&fn_node_id, "HelloX", "HelloX")
.component_of(object_id.clone())
Expand All @@ -43,9 +51,61 @@ pub fn add_methods(server: &mut Server, ns: u16) {
])
.callback(Box::new(HelloX))
.insert(&mut address_space);

// Boop has 1 one input and 0 output
let fn_node_id = NodeId::new(ns, "Boop");
MethodBuilder::new(&fn_node_id, "Boop", "Boop")
.component_of(object_id.clone())
.input_args(&mut address_space, &[
("Ping", DataTypeId::String).into()
])
.callback(Box::new(HelloX))
.insert(&mut address_space);
}

pub struct HelloWorld;
struct NoOp;

impl callbacks::Method for NoOp {
fn call(&mut self, _session: &mut Session, _request: &CallMethodRequest) -> Result<CallMethodResult, StatusCode> {
Ok(CallMethodResult {
status_code: StatusCode::Good,
input_argument_results: None,
input_argument_diagnostic_infos: None,
output_arguments: None,
})
}
}

struct Boop;

impl callbacks::Method for Boop {
fn call(&mut self, _session: &mut Session, request: &CallMethodRequest) -> Result<CallMethodResult, StatusCode> {
// Validate input to be a string
let in1_result = if let Some(ref input_arguments) = request.input_arguments {
if let Some(in1) = input_arguments.get(0) {
if let Variant::String(in1) = in1 {
StatusCode::Good
} else {
StatusCode::BadInvalidArgument
}
} else if input_arguments.len() == 0 {
StatusCode::BadArgumentsMissing
} else {
// Shouldn't get here because there is 1 argument
StatusCode::BadTooManyArguments
}
} else {
StatusCode::BadArgumentsMissing
};
Ok(CallMethodResult {
status_code: StatusCode::Good,
input_argument_results: Some(vec![in1_result]),
input_argument_diagnostic_infos: None,
output_arguments: None,
})
}
}
struct HelloWorld;

impl callbacks::Method for HelloWorld {
fn call(&mut self, _session: &mut Session, _request: &CallMethodRequest) -> Result<CallMethodResult, StatusCode> {
Expand All @@ -59,7 +119,7 @@ impl callbacks::Method for HelloWorld {
}
}

pub struct HelloX;
struct HelloX;

impl callbacks::Method for HelloX {
fn call(&mut self, _session: &mut Session, request: &CallMethodRequest) -> Result<CallMethodResult, StatusCode> {
Expand Down

0 comments on commit cfc398a

Please sign in to comment.