-
Notifications
You must be signed in to change notification settings - Fork 9
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
Added sequences support on pyopendds [#18] #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
from __future__ import annotations | ||
|
||
from .Topic import Topic | ||
from .constants import StatusKind | ||
from .util import TimeDurationType, normalize_time_duration | ||
|
||
from typing import TYPE_CHECKING | ||
if TYPE_CHECKING: | ||
from .Publisher import Publisher | ||
|
||
|
||
class DataWriter: | ||
pass | ||
|
||
def __init__(self, publisher: Publisher, topic: Topic, qos=None, listener=None): | ||
self.topic = topic | ||
self.qos = qos | ||
self.listener = listener | ||
self.publisher = publisher | ||
publisher.writers.append(self) | ||
|
||
from _pyopendds import create_datawriter | ||
create_datawriter(self, publisher, topic) | ||
|
||
def wait_for(self, status: StatusKind, timeout: TimeDurationType): | ||
from _pyopendds import datareader_wait_for | ||
return datareader_wait_for(self, status, *normalize_time_duration(timeout)) | ||
|
||
def write(self, sample): | ||
return self.topic._ts_package.write(self, sample) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,7 @@ class IntegerType { | |
|
||
static PyObject* get_python_class() | ||
{ | ||
return PyLong_Type; | ||
return PyLong_FromLong(0); | ||
} | ||
|
||
static void cpp_to_python(const T& cpp, PyObject*& py) | ||
|
@@ -56,13 +56,17 @@ class IntegerType { | |
"Integer Value is Out of Range for IDL Type", PyExc_ValueError); | ||
} | ||
if (value == -1 && PyErr_Occurred()) throw Exception(); | ||
cpp = value; | ||
cpp = T(value); | ||
} | ||
|
||
}; | ||
|
||
typedef ::CORBA::Long i32; | ||
template<> class Type<i32>: public IntegerType<i32> {}; | ||
|
||
typedef ::CORBA::Short i16; | ||
template<> class Type<i16>: public IntegerType<i16> {}; | ||
|
||
// TODO: Put Other Integer Types Here | ||
|
||
const char* string_data(const std::string& cpp) | ||
|
@@ -90,7 +94,7 @@ class StringType { | |
public: | ||
static PyObject* get_python_class() | ||
{ | ||
return PyUnicode_Type; | ||
return PyUnicode_FromString(""); | ||
} | ||
|
||
static void cpp_to_python(const T& cpp, PyObject*& py, const char* encoding) | ||
|
@@ -101,9 +105,16 @@ class StringType { | |
py = o; | ||
} | ||
|
||
static void python_to_cpp(PyObject* py, T& cpp) | ||
static void python_to_cpp(PyObject* py, T& cpp, const char* encoding) | ||
{ | ||
// TODO: Encode or Throw Unicode Error | ||
PyObject* repr = PyObject_Str(py); | ||
if (!repr) throw Exception(); | ||
PyObject* str = PyUnicode_AsEncodedString(repr, encoding, NULL); | ||
if (!str) throw Exception(); | ||
const char *bytes = PyBytes_AS_STRING(str); | ||
cpp = T(bytes); | ||
Py_XDECREF(repr); | ||
Py_XDECREF(str); | ||
} | ||
}; | ||
|
||
|
@@ -127,6 +138,7 @@ class TopicTypeBase { | |
virtual const char* type_name() = 0; | ||
virtual void register_type(PyObject* pyparticipant) = 0; | ||
virtual PyObject* take_next_sample(PyObject* pyreader) = 0; | ||
virtual PyObject* write(PyObject* pywriter, PyObject* pysample) = 0; | ||
|
||
typedef std::shared_ptr<TopicTypeBase> Ptr; | ||
typedef std::map<PyObject*, Ptr> TopicTypes; | ||
|
@@ -215,15 +227,15 @@ class TopicType : public TopicTypeBase { | |
DDS::WaitSet_var ws = new DDS::WaitSet; | ||
ws->attach_condition(read_condition); | ||
DDS::ConditionSeq active; | ||
const DDS::Duration_t max_wait_time = {10, 0}; | ||
const DDS::Duration_t max_wait_time = {60, 0}; | ||
if (Errors::check_rc(ws->wait(active, max_wait_time))) { | ||
throw Exception(); | ||
} | ||
ws->detach_condition(read_condition); | ||
reader_impl->delete_readcondition(read_condition); | ||
|
||
IdlType sample; | ||
DDS::SampleInfo info; | ||
DDS::SampleInfo info; | ||
if (Errors::check_rc(reader_impl->take_next_sample(sample, info))) { | ||
throw Exception(); | ||
} | ||
|
@@ -234,6 +246,34 @@ class TopicType : public TopicTypeBase { | |
return rv; | ||
} | ||
|
||
PyObject* write(PyObject* pywriter, PyObject* pysample) | ||
{ | ||
DDS::DataWriter* writer = get_capsule<DDS::DataWriter>(pywriter); | ||
if (!writer) throw Exception(); | ||
|
||
DataWriter* writer_impl = DataWriter::_narrow(writer); | ||
if (!writer_impl) { | ||
throw Exception( | ||
"Could not narrow writer implementation", Errors::PyOpenDDS_Error()); | ||
} | ||
|
||
IdlType rv; | ||
Type<IdlType>::python_to_cpp(pysample, rv); | ||
|
||
DDS::ReturnCode_t rc = writer_impl->write(rv, DDS::HANDLE_NIL); | ||
if (Errors::check_rc(rc)) { | ||
throw Exception(); | ||
} | ||
// Wait for samples to be acknowledged | ||
DDS::Duration_t timeout = { 30, 0 }; | ||
if (writer_impl->wait_for_acknowledgments(timeout) != DDS::RETCODE_OK) { | ||
throw Exception( | ||
"wait_for_acknowledgments error : ", Errors::PyOpenDDS_Error()); | ||
} | ||
Comment on lines
+267
to
+272
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a separate operation like it is in OpenDDS, because this could be a lengthy process depending on how much stuff is being sent and to how many peers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree, it's needed as a separate function on our application too. |
||
|
||
return pysample; | ||
} | ||
|
||
PyObject* get_python_class() | ||
{ | ||
return Type<IdlType>::get_python_class(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was meant to return the class itself, not an instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get a compilation error without this modification.