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 support for the STP handler #13

Merged
merged 3 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions include/python_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ typedef struct {
bool is_present;
bool has_value;
lf_token_t* token;
FEDERATED_CAPSULE_EXTENSION
FEDERATED_GENERIC_EXTENSION
} generic_action_instance_struct;

/**
Expand Down Expand Up @@ -84,4 +84,4 @@ typedef struct {
FEDERATED_CAPSULE_EXTENSION
} generic_action_capsule_struct;

#endif
#endif
35 changes: 32 additions & 3 deletions include/python_capsule_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,44 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifdef FEDERATED
#ifdef FEDERATED_DECENTRALIZED
#define FEDERATED_CAPSULE_EXTENSION \
#define FEDERATED_GENERIC_EXTENSION \
tag_t intended_tag; \
instant_t physical_time_of_arrival;
#else // FEDERATED_CENTRALIZED

#define FEDERATED_CAPSULE_EXTENSION \
py_tag_t* intended_tag; \
instant_t physical_time_of_arrival;

#define FEDERATED_CAPSULE_MEMBER \
{"intended_tag", T_OBJECT, offsetof(generic_port_capsule_struct, intended_tag), READONLY, "Original intended tag of the event."}, \
{"physical_time_of_arrival", T_INT, offsetof(generic_port_capsule_struct, physical_time_of_arrival), READONLY, "Physical time of arrival of the original message."},

#define FEDERATED_ASSIGN_FIELDS(py_port, c_port) \
do { \
py_port->intended_tag = convert_C_tag_to_py(c_port->intended_tag); \
py_port->physical_time_of_arrival = c_port->physical_time_of_arrival; \
} while(0)

#else // FEDERATED_CENTRALIZED
#define FEDERATED_GENERIC_EXTENSION \
instant_t physical_time_of_arrival;

#define FEDERATED_CAPSULE_EXTENSION FEDERATED_GENERIC_EXTENSION

#define FEDERATED_CAPSULE_MEMBER \
{"physical_time_of_arrival", T_INT, offsetof(generic_port_capsule_struct, physical_time_of_arrival), READONLY, "Physical time of arrival of the original message."},

#define FEDERATED_ASSIGN_FIELDS(py_port, c_port) \
do { \
py_port->physical_time_of_arrival = c_port->physical_time_of_arrival; \
} while(0)
#endif // FEDERATED_DECENTRALIZED
#else // not FEDERATED
#define FEDERATED_GENERIC_EXTENSION // Empty
#define FEDERATED_CAPSULE_EXTENSION // Empty
#define FEDERATED_CAPSULE_MEMBER // Empty
#define FEDERATED_ASSIGN_FIELDS(py_port, c_port) // Empty
#define FEDERATED_COPY_FIELDS(py_port1, py_port2) // Empty
#endif // FEDERATED

#endif
#endif
4 changes: 2 additions & 2 deletions include/python_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ typedef struct {
int length;
void (*destructor) (void* value);
void* (*copy_constructor) (void* value);
FEDERATED_CAPSULE_EXTENSION
FEDERATED_GENERIC_EXTENSION
} generic_port_instance_struct;


Expand Down Expand Up @@ -95,4 +95,4 @@ typedef struct {
FEDERATED_CAPSULE_EXTENSION
} generic_port_capsule_struct;

#endif
#endif
10 changes: 9 additions & 1 deletion include/python_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@ typedef struct {
tag_t tag;
} py_tag_t;

#endif
/**
* @brief Convert C tag to `py_tag_t`
*
* @param c_tag The tag in C.
* @return py_tag_t* The tag in Python.
*/
py_tag_t* convert_C_tag_to_py(tag_t c_tag);

#endif
7 changes: 5 additions & 2 deletions lib/python_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ PyObject *py_port_iter_next(PyObject *self) {
pyport->value = cport[port->current_index]->value;
pyport->is_present = cport[port->current_index]->is_present;
pyport->width = -2;
FEDERATED_ASSIGN_FIELDS(pyport, cport[port->current_index]);

port->current_index++;

Expand Down Expand Up @@ -254,6 +255,7 @@ PyObject *py_port_capsule_get_item(PyObject *self, PyObject *key) {
pyport->value = cport[index]->value;
pyport->is_present = cport[index]->is_present;
pyport->width = -2;
FEDERATED_ASSIGN_FIELDS(pyport, cport[index]);


LF_PRINT_LOG("Getting item index %d. Is present is %d.", index, pyport->is_present);
Expand Down Expand Up @@ -359,7 +361,8 @@ PyMemberDef py_port_capsule_members[] = {
{"port", T_OBJECT, offsetof(generic_port_capsule_struct, port), READONLY, ""},
{"value", T_OBJECT, offsetof(generic_port_capsule_struct, value), READONLY, "Value of the port"},
{"is_present", T_BOOL, offsetof(generic_port_capsule_struct, is_present), READONLY, "Check if value is present at current logical time"},
{"width", T_INT, offsetof(generic_port_capsule_struct, width), READONLY, "Width of the multiport"},
{"width", T_INT, offsetof(generic_port_capsule_struct, width), READONLY, "Width of the multiport"},
FEDERATED_CAPSULE_MEMBER
{NULL} /* Sentinel */
};

Expand Down Expand Up @@ -394,4 +397,4 @@ PyTypeObject py_port_capsule_t = {
.tp_dealloc = (destructor) py_port_capsule_dealloc,
.tp_members = py_port_capsule_members,
.tp_methods = py_port_capsule_methods,
};
};
15 changes: 15 additions & 0 deletions lib/python_tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,18 @@ PyTypeObject PyTagType = {
.tp_richcompare = (richcmpfunc) Tag_richcompare,
.tp_getset = Tag_getsetters,
};

/**
* @brief Convert C tag to `py_tag_t`
*
* @param c_tag The tag in C.
* @return PyObject* The tag in Python.
*/
py_tag_t* convert_C_tag_to_py(tag_t c_tag) {
py_tag_t* py_tag = PyObject_GC_New(py_tag_t, &PyTagType);
if (py_tag == NULL) {
lf_print_error_and_exit("Failed to convert tag from C to Python.");
}
py_tag->tag = c_tag;
return py_tag;
}
10 changes: 6 additions & 4 deletions lib/pythontarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,17 @@ static PyObject* py_schedule_copy(PyObject *self, PyObject *args) {
int lf_reactor_c_main(int argc, char *argv[]);

/**
* Prototype for request_stop().
* @see reactor.c and reactor_threaded.c
* Prototype for lf_request_stop().
* @see reactor.h
*/
void request_stop();
void lf_request_stop();

///////////////// Other useful functions /////////////////////
/**
* Stop execution at the conclusion of the current logical time.
*/
static PyObject* py_request_stop(PyObject *self, PyObject *args) {
request_stop();
lf_request_stop();

Py_INCREF(Py_None);
return Py_None;
Expand Down Expand Up @@ -437,6 +437,7 @@ PyObject* convert_C_port_to_py(void* port, int width) {
// Fill in the Python port struct
((generic_port_capsule_struct*)cap)->port = capsule;
((generic_port_capsule_struct*)cap)->width = width;
FEDERATED_ASSIGN_FIELDS(((generic_port_capsule_struct*)cap), cport);

if (width == -2) {
((generic_port_capsule_struct*)cap)->is_present =
Expand Down Expand Up @@ -504,6 +505,7 @@ PyObject* convert_C_action_to_py(void* action) {
// Fill in the Python action struct
((generic_action_capsule_struct*)cap)->action = capsule;
((generic_action_capsule_struct*)cap)->is_present = trigger->status;
FEDERATED_ASSIGN_FIELDS(((generic_port_capsule_struct*)cap), ((generic_action_instance_struct*)action));

// If token is not initialized, that is all we need to set
if (trigger->token == NULL) {
Expand Down