Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add named socket in python #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions python/example/const_named.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sys
import pyportus as portus

class ConstFlow():
INIT_RATE = 1000000

def __init__(self, datapath, datapath_info):
self.datapath = datapath
self.datapath_info = datapath_info
self.rate = ConstFlow.INIT_RATE
self.datapath.set_program("default", [("Rate", self.rate)])

def on_report(self, r):
self.datapath.update_field("Rate", self.rate)


class Const(portus.AlgBase):

def datapath_programs(self):
return {
"default" : """\
(def (Report
(volatile acked 0)
(volatile loss 0)
(volatile rtt 0)
))
(when true
(:= Report.rtt Flow.rtt_sample_us)
(:= Report.acked (+ Report.acked Ack.bytes_acked))
(:= Report.loss Ack.lost_pkts_sample)
(report)
)
"""
}

def new_flow(self, datapath, datapath_info):
return ConstFlow(datapath, datapath_info)

alg = Const()

# You will find socket in /tmp/ccp/hello in ubuntu
portus.start("unix", alg, "hello")
7 changes: 5 additions & 2 deletions python/pyportus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ def assert_implements_interface(cls, C):
))
return True

def start(ipc, alg):
'''
@identifier: ipc = "unix" only. It will be used to create unix socket to allow multi CCP run together without interference with each other, default is "portus"
'''
def start(ipc, alg, identifier="portus"):
cls = alg.__class__
if not issubclass(cls, object):
raise Exception(cls.__name__ + " must be a subclass of object")
if issubclass(cls, AlgBase):
AlgBase.assert_implements_interface(cls)
checker._check_datapath_programs(cls)
return start_inner(ipc, alg)
return start_inner(ipc, alg, identifier)
else:
raise Exception(cls.__name__ + " must be a subclass of portus.AlgBase")
8 changes: 4 additions & 4 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ impl PyDatapath {
#[pymodule]
fn pyportus(_py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m)]
fn start_inner(py: Python, ipc_str: String, alg: PyObject) -> PyResult<i32> {
fn start_inner(py: Python, ipc_str: String, alg: PyObject, identifier: String) -> PyResult<i32> {
simple_signal::set_handler(&[Signal::Int, Signal::Term], move |_signals| {
tracing::info!("exiting");
::std::process::exit(1);
});

py_start_inner(py, ipc_str, alg)
py_start_inner(py, ipc_str, alg, identifier)
}

#[pyfn(m)]
Expand All @@ -219,7 +219,7 @@ fn pyportus(_py: Python, m: &PyModule) -> PyResult<()> {
Ok(())
}

fn py_start_inner<'p>(py: Python<'p>, ipc: String, alg: PyObject) -> PyResult<i32> {
fn py_start_inner<'p>(py: Python<'p>, ipc: String, alg: PyObject, identifier: String) -> PyResult<i32> {
// Check args
if let Err(e) = portus::algs::ipc_valid(ipc.clone()) {
raise!(PyValueError, e);
Expand All @@ -236,7 +236,7 @@ fn py_start_inner<'p>(py: Python<'p>, ipc: String, alg: PyObject) -> PyResult<i3
match ipc.as_str() {
"unix" => {
use ipc::unix::Socket;
let b = Socket::<ipc::Blocking>::new("portus")
let b = Socket::<ipc::Blocking>::new(identifier.as_str())
.map(|sk| BackendBuilder { sock: sk })
.expect("create unix socket");
portus::RunBuilder::new(b).default_alg(py_cong_alg).run()
Expand Down