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

C handle init #76

Merged
merged 15 commits into from
Mar 12, 2024
Merged
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
14 changes: 9 additions & 5 deletions helics/capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,11 +1392,13 @@ def message_id(self, v):


class HelicsQuery(_HelicsCHandle):
pass
def __init__(self, handle, cleanup=False):
super().__init__(handle, cleanup)


class HelicsQueryBuffer(_HelicsCHandle):
pass
def __init__(self, handle, cleanup=False):
super().__init__(handle, cleanup)


class HelicsEndpoint(_HelicsCHandle):
Expand Down Expand Up @@ -2823,7 +2825,8 @@ class HelicsCallbackFederate(HelicsCombinationFederate):
pass

class HelicsDataBuffer(_HelicsCHandle):
pass
def __init__(self, handle, cleanup=False):
super().__init__(handle, cleanup)


class HelicsException(Exception):
Expand Down Expand Up @@ -7269,7 +7272,7 @@ def helicsTranslatorGetName(translator: HelicsTranslator) -> str:

**Returns**: A string with the name of the translator.
"""
f = loadSym("helicsPublicationGetName")
f = loadSym("helicsTranslatorGetName")
result = f(translator.handle)
return ffi.string(result).decode()

Expand Down Expand Up @@ -9364,7 +9367,8 @@ def helicsQueryBufferFill(buffer: HelicsQueryBuffer, string: str):
"""
f = loadSym("helicsQueryBufferFill")
err = helicsErrorInitialize()
f(buffer.handle, string, len(string), err)
str_ptr = ffi.new('char[]', string.encode('utf-8'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is HELICS expecting a utf-8 encoded string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what Mark Eberlein put in and it works so I guess, "yes"? It might make more sense to choose ascii. Let me know if you want me to change something.

Copy link
Member

@nightlark nightlark Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the test code in HELICS is just shoving a regular char * into the query buffer, which seems to imply that any encoding is possible? I think this function should have an argument that specifies the encoding to use (or no encoding/raw bytes). @phlptp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phlptp; any input on what you think we should do here? I don't have a philosophical problem with supporting data structures (raw bytes) as query responses if there's a need to do so BUT I suspect restricting this to ASCII or UTF-8 will make it easier to maintain the code and easier for users to understand what they are doing. Off the top of my head, I can't think of a use case where it would be important to support a raw bytes data structure where something like JSON wouldn't also be sufficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would be fine restricting it in python. In the lower levels there are a few use cases for queries/commands getting raw binary data that I want to leave open, but that would be really complicated to use in python so don't really think we would need to support it, and if someone wanted and had the ability they could directly access the library calls.

f(buffer.handle, str_ptr, len(string), err)
if err.error_code != 0:
raise HelicsException("[" + str(err.error_code) + "] " + ffi.string(err.message).decode())

Expand Down
2 changes: 1 addition & 1 deletion tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_filter_type_tests_info():
destroyBroker(broker)


def test_filter_types_tests_core_fitler_registration():
def test_filter_types_tests_core_filter_registration():

core1 = h.helicsCreateCore("inproc", "core1", "--autobroker")

Expand Down
28 changes: 14 additions & 14 deletions tests/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@


def test_python_api0():
broker = h.helicsCreateBroker("zmq", "", "-f 1 --name=mainbroker")
broker = h.helicsCreateBroker("zmq", "", "-f 1 --name=mainbroker0")
fedinfo = h.helicsCreateFederateInfo()
assert "HelicsFederateInfo()" in repr(fedinfo)
fedinfo.core_name = "TestFederate"
fedinfo.core_type = "zmq"
fedinfo.core_init = "-f 1 --broker=mainbroker"
fedinfo.core_init = "-f 1 --broker=mainbroker0 --name=core0"
mFed = h.helicsCreateCombinationFederate("TestFederate", fedinfo)

assert (
Expand Down Expand Up @@ -61,12 +61,12 @@ def test_python_api0():

def test_python_api1():

broker = h.helicsCreateBroker("zmq", "", "-f 1 --name=mainbroker")
broker = h.helicsCreateBroker("zmq", "", "-f 1 --name=mainbroker1")
fedinfo = h.helicsCreateFederateInfo()
assert "HelicsFederateInfo()" in repr(fedinfo)
fedinfo.core_name = "TestFederate"
fedinfo.core_type = "zmq"
fedinfo.core_init = "-f 1 --broker=mainbroker"
fedinfo.core_init = "-f 1 --broker=mainbroker1 --name=core0"
mFed = h.helicsCreateCombinationFederate("TestFederate", fedinfo)

assert (
Expand Down Expand Up @@ -266,7 +266,7 @@ def test_python_api1():

def test_python_api2():

broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --loglevel=warning")
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --loglevel=warning --name=mainbroker2")
assert broker.is_connected()

broker.set_global("hello", "world")
Expand All @@ -280,7 +280,7 @@ def test_python_api2():
assert broker.query("hello", "world") == {"error": {"code": 404, "message": "query not valid"}}

fi = h.helicsCreateFederateInfo()
fi.core_init = "--federates 1"
fi.core_init = "--federates 1 --brokername=mainbroker2 --name=core2"
fi.property[h.HELICS_PROPERTY_INT_LOG_LEVEL] = 2

fed = h.helicsCreateCombinationFederate("test1", fi)
Expand Down Expand Up @@ -436,15 +436,15 @@ def test_python_api2():


def test_python_api3():
core1 = h.helicsCreateCore("inproc", "core1", "--autobroker")
core1 = h.helicsCreateCore("inproc", "core3", "--autobroker")

assert """HelicsCore(identifier = "core1", address = "core1")""" in repr(core1)
assert """HelicsCore(identifier = "core3", address = "core3")""" in repr(core1)

core2 = core1.clone()

assert core1.identifier == "core1"
assert core1.identifier == "core3"

source_filter1 = core1.register_filter(h.HELICS_FILTER_TYPE_DELAY, "core1SourceFilter")
source_filter1 = core1.register_filter(h.HELICS_FILTER_TYPE_DELAY, "core3SourceFilter")

source_filter1.add_source_target("ep1")

Expand Down Expand Up @@ -504,7 +504,7 @@ def test_python_api4():


def test_python_api5():
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1")
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --name=mainbroker5")
fi = h.helicsCreateFederateInfo()

fed = h.helicsCreateCombinationFederate("test1", fi)
Expand All @@ -527,7 +527,7 @@ def test_python_api5():


def test_python_api6():
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1")
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --name=mainbroker6")
fi = h.helicsCreateFederateInfo()

fed = h.helicsCreateCombinationFederate("test1", fi)
Expand All @@ -549,7 +549,7 @@ def test_python_api6():

@pt.mark.skip(reason="Fails to pass on windows and linux")
def test_python_api7():
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1")
broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --name=mainbroker7")
fi = h.helicsCreateFederateInfo()

fed = h.helicsCreateCombinationFederate("test1", fi)
Expand Down Expand Up @@ -612,7 +612,7 @@ def test_python_api7():

def test_python_api8():

broker = h.helicsCreateBroker("zmq", "", "-f 1 --name=mainbroker")
broker = h.helicsCreateBroker("zmq", "", "-f 1 --name=mainbroker8")

cfed = h.helicsCreateCombinationFederateFromConfig(os.path.join(CURRENT_DIRECTORY, "combinationfederate.json"))

Expand Down
Loading