Skip to content

Commit

Permalink
sedeve-kit, binding
Browse files Browse the repository at this point in the history
  • Loading branch information
ybbh committed May 11, 2024
1 parent 1a8bf02 commit bde50d6
Show file tree
Hide file tree
Showing 18 changed files with 495 additions and 131 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[[bin]]
name = "player"
name = "sedeve_player"
path = "src/player/bin/main.rs"

[[bin]]
name = "rust_gen"
name = "sedeve_rust_gen"
path = "src/rust_gen/bin/main.rs"

[[bin]]
name = "trace_gen"
name = "sedeve_trace_gen"
path = "src/trace_gen/main.rs"


Expand Down Expand Up @@ -67,5 +67,6 @@ num = "0.4"
num-derive = "0.4"
num-traits = "0.2"
md-5 = "0.10.6"
toml = "0.8.12"


40 changes: 26 additions & 14 deletions binding/automata_c/out/include/_sedeve_kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@

extern "C" {

/// Initialize an automata
/// Set up an automata
/// `_name`, the automata's name
/// `_client_id`, client node id
/// `_server_id`, server(dtm player) node id
/// `_server_addr`, server(deterministic player to connect) network address [ip:port]
void automata_init_setup(const char *_name,
uint64_t _client_id,
uint64_t _server_id,
const char *_server_addr);
/// `_tested_nid`, tested node's node id
/// `_player_nid`, deterministic player's node id
/// `_player_addr`, deterministic player's network address [ip:port]
void automata_setup(const char *_name,
uint64_t _tested_nid,
uint64_t _player_nid,
const char *_player_addr);

/// Set up an automata
/// `_name`, the automata's name
/// `_tested_nid`, tested node's node id
/// `_tested_nid`, tested node's network address [ip:port]
/// `_player_nid`, deterministic player's node id
/// `_player_addr`, deterministic player's network address [ip:port]
void automata_setup_with_input(const char *_name,
uint64_t _tested_nid,
const char *_tested_addr,
uint64_t _player_nid,
const char *_player_addr);

/// Clean an automata setting
/// `_name`, the automata's name
Expand Down Expand Up @@ -46,11 +58,11 @@ int32_t automata_next_input(const char *_name,
/// `_source_node_id`, source node id
/// `_dest_node_id`, dest node id
/// `_message_json_string`, message in json reprensentaion
void automata_action_to_player(const char *_name,
uint64_t _action_type,
uint64_t _action_begin_end,
uint64_t _source_node_id,
uint64_t _dest_node_id,
const char *_message_json_string);
void automata_action(const char *_name,
uint64_t _action_type,
uint64_t _action_begin_end,
uint64_t _source_node_id,
uint64_t _dest_node_id,
const char *_message_json_string);

} // extern "C"
8 changes: 4 additions & 4 deletions binding/automata_c/out/include/sedeve_kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ constexpr static const uint64_t C_ACTION_INTERNAL = 4;

#ifdef ENABLE_DTM

#define INPUT_ACTION(name, source, dest, action) { automata_action_to_player(name, C_ACTION_END, C_ACTION_INPUT, source, dest, action); }
#define INPUT_ACTION(name, source, dest, action) { automata_action(name, C_ACTION_END, C_ACTION_INPUT, source, dest, action); }

#define OUTPUT_ACTION(name, source, dest, action) { automata_action_to_player(name, C_ACTION_BEGIN, C_ACTION_OUTPUT, source, dest, action); }
#define OUTPUT_ACTION(name, source, dest, action) { automata_action(name, C_ACTION_BEGIN, C_ACTION_OUTPUT, source, dest, action); }

#define INTERNAL_ACTION_BEGIN(name, source, dest, action) { automata_action_to_player(name, C_ACTION_BEGIN, C_ACTION_INTERNAL, source, dest, action); }
#define INTERNAL_ACTION_BEGIN(name, source, dest, action) { automata_action(name, C_ACTION_BEGIN, C_ACTION_INTERNAL, source, dest, action); }

#define INTERNAL_ACTION_END(name, source, dest, action) { automata_action_to_player(name, C_ACTION_END, C_ACTION_INTERNAL, source, dest, action); }
#define INTERNAL_ACTION_END(name, source, dest, action) { automata_action(name, C_ACTION_END, C_ACTION_INTERNAL, source, dest, action); }


#else
Expand Down
50 changes: 37 additions & 13 deletions binding/automata_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,48 @@ use sedeve_kit::dtm::automata;
use num::FromPrimitive;


/// Initialize an automata
/// Set up an automata
/// `_name`, the automata's name
/// `_client_id`, client node id
/// `_server_id`, server(dtm player) node id
/// `_server_addr`, server(deterministic player to connect) network address [ip:port]
/// `_tested_nid`, tested node's node id
/// `_player_nid`, deterministic player's node id
/// `_player_addr`, deterministic player's network address [ip:port]
#[no_mangle]
pub extern "C" fn automata_init_setup(
pub extern "C" fn automata_setup(
_name: *const c_char,
_client_id:u64,
_server_id: u64,
_server_addr: *const c_char
_tested_nid:u64,
_player_nid: u64,
_player_addr: *const c_char
) {
let name = unsafe { CStr::from_ptr(_name) }.to_str().unwrap().to_string();
let server_addr = unsafe { CStr::from_ptr(_server_addr) }.to_str().unwrap().to_string();
automata::automata_init_setup(
let server_addr = unsafe { CStr::from_ptr(_player_addr) }.to_str().unwrap().to_string();
automata::automata_setup(
name.as_str(),
_client_id,
_server_id,
_tested_nid,
_player_nid,
server_addr.as_str()
);
}

/// Set up an automata
/// `_name`, the automata's name
/// `_tested_nid`, tested node's node id
/// `_tested_nid`, tested node's network address [ip:port]
/// `_player_nid`, deterministic player's node id
/// `_player_addr`, deterministic player's network address [ip:port]
#[no_mangle]
pub extern "C" fn automata_setup_with_input(
_name: *const c_char,
_tested_nid:u64,
_tested_addr:*const c_char,
_player_nid: u64,
_player_addr: *const c_char
) {
let name = unsafe { CStr::from_ptr(_name) }.to_str().unwrap().to_string();
let server_addr = unsafe { CStr::from_ptr(_player_addr) }.to_str().unwrap().to_string();
automata::automata_setup(
name.as_str(),
_tested_nid,
_player_nid,
server_addr.as_str()
);
}
Expand Down Expand Up @@ -93,7 +117,7 @@ pub extern "C" fn automata_next_input(
/// `_dest_node_id`, dest node id
/// `_message_json_string`, message in json reprensentaion
#[no_mangle]
pub extern "C" fn automata_action_to_player(
pub extern "C" fn automata_action(
_name:*const c_char,
_action_type:u64,
_action_begin_end:u64,
Expand Down
1 change: 1 addition & 0 deletions binding/automata_py/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pytest>=3.5.0
setuptools_rust~=1.0.0
pip>=21.3
wheel
patchelf
111 changes: 82 additions & 29 deletions binding/automata_py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,76 @@ use pyo3::prelude::*;

use sedeve_kit::action::action_type::{ActionBeginEnd, ActionType};
use sedeve_kit::dtm::automata;
use sedeve_kit::action::action_type;


#[pyfunction]
pub fn action_begin() -> PyResult<u64> {
Ok(action_type::C_ACTION_BEGIN)
}

/// A Python module implemented in Rust.
#[pymodule]
fn automata_py(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(automata_init_setup, m)?)?;
m.add_function(wrap_pyfunction!(automata_clear, m)?)?;
m.add_function(wrap_pyfunction!(automata_enable, m)?)?;
m.add_function(wrap_pyfunction!(automata_next_input, m)?)?;
m.add_function(wrap_pyfunction!(automata_action_to_player, m)?)?;
Ok(())
#[pyfunction]
pub fn action_end() -> PyResult<u64> {
Ok(action_type::C_ACTION_END)
}

#[pyfunction]
pub fn action_input() -> PyResult<u64> {
Ok(action_type::C_ACTION_INPUT)
}

#[pyfunction]
pub fn action_internal() -> PyResult<u64> {
Ok(action_type::C_ACTION_INTERNAL)
}

/// Initialize an automata

#[pyfunction]
pub fn action_output() -> PyResult<u64> {
Ok(action_type::C_ACTION_OUTPUT)
}

/// Initialize and set up an automata
/// `_name`, the automata's name
/// `_client_id`, client node id
/// `_server_id`, server(dtm player) node id
/// `_server_addr`, server(deterministic player to connect) network address [ip:port]
/// `_tested_nid`, tested node's node id
/// `_player_nid`, deterministic player's node id
/// `_player_addr`, deterministic player's network address [ip:port]
#[pyfunction]
pub fn automata_init_setup(
pub fn automata_setup(
_name: String,
_client_id: u64,
_server_id: u64,
_server_addr: String,
_tested_nid: u64,
_player_nid: u64,
_player_addr: String,
) {
automata::automata_init_setup(
automata::automata_setup(
_name.as_str(),
_client_id,
_server_id,
_server_addr.as_str(),
_tested_nid,
_player_nid,
_player_addr.as_str(),
);
}


/// Initialize and set up an automata
/// `_name`, the automata's name
/// `_tested_nid`, tested node's node id
/// `_player_addr`, tested node's network address [ip:port]
/// `_player_nid`, deterministic player's node id
/// `_player_addr`, deterministic player's network address [ip:port]
#[pyfunction]
pub fn automata_setup_with_input(
_name: String,
_tested_nid: u64,
_tested_addr:String,
_player_nid: u64,
_player_addr: String,
) {
automata::automata_setup_with_input(
_name.as_str(),
_tested_nid,
_tested_addr.as_str(),
_player_nid,
_player_addr.as_str(),
);
}

Expand All @@ -54,12 +92,10 @@ pub fn automata_enable(_name: String) -> bool {

/// Read next input action of automata, receive message from deterministic player
/// `_name`, the automata's name
/// `_output_source_node_id`, source node id
/// `_output_dest_node_id`, dest node id
/// `_output_action_type`, action type
/// `_output_buf_output_action_json`, output buffer
/// `_buf_len`, the buffer length
/// `_output_len`, write bytes to output buffer
/// `_source_node_id`, source node id
/// `_dest_node_id`, dest node id
/// `_action_type`, action type
/// `_output_json_message`, output message
#[pyfunction]
pub fn automata_next_input(
_name: String,
Expand All @@ -83,9 +119,9 @@ pub fn automata_next_input(
/// `_action_begin_end`, begin an action, or end an action
/// `_source_node_id`, source node id
/// `_dest_node_id`, dest node id
/// `_message_json_string`, message in json reprensentaion
/// `_message_json_string`, message in json representation
#[pyfunction]
pub fn automata_action_to_player(
pub fn automata_action(
_name: String,
_action_type: u64,
_action_begin_end: u64,
Expand All @@ -107,3 +143,20 @@ pub fn automata_action_to_player(
)
}

/// A Python module implemented in Rust.
#[pymodule]
fn automata_py(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(action_begin, m)?)?;
m.add_function(wrap_pyfunction!(action_end, m)?)?;
m.add_function(wrap_pyfunction!(action_input, m)?)?;
m.add_function(wrap_pyfunction!(action_internal, m)?)?;
m.add_function(wrap_pyfunction!(action_output, m)?)?;

m.add_function(wrap_pyfunction!(automata_setup, m)?)?;
m.add_function(wrap_pyfunction!(automata_setup_with_input, m)?)?;
m.add_function(wrap_pyfunction!(automata_clear, m)?)?;
m.add_function(wrap_pyfunction!(automata_enable, m)?)?;
m.add_function(wrap_pyfunction!(automata_next_input, m)?)?;
m.add_function(wrap_pyfunction!(automata_action, m)?)?;
Ok(())
}
4 changes: 2 additions & 2 deletions doc/how_to_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ The trace format is represented in JSON, similar to [this JSON file](../src/data
The [action incoming interface](../src/player/action_incoming.rs) can be used to read traces.


The trace_gen's command lines:
The sedeve_trace_gen's command lines:

```
Usage: trace_gen [OPTIONS]
Usage: sedeve_trace_gen [OPTIONS]
Options:
-d, --dot-path <DOT_PATH>
Expand Down
34 changes: 34 additions & 0 deletions example/echo/dtmt_echo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

import automata_py
import subprocess

this_node_id = 2
player_node_id = 1

AUTO_ECHO = "echo"

automata_py.automata_setup_with_input(AUTO_ECHO, this_node_id, "127.0.0.1:24002", player_node_id, "127.0.0.1:24001")

print("setup ok")

loop = True
while loop:
(source, dest, action, message) = automata_py.automata_next_input(AUTO_ECHO)

print((source, dest, action, message))

automata_py.automata_action_to_player(
automata_py.action_input(),
automata_py.action_end(),
this_node_id,
source,
dest,
message
)

assert action == automata_py.action_input()

# <port> <id> <player id> <player ip> <player port>
cmd = "echo_server 8001 1 2 127.0.0.1 8002"
t = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read()
print(t)
Loading

0 comments on commit bde50d6

Please sign in to comment.