Using Hypodermic with Boost.Signals2 - signals-slots

In order to use Boost.Signals2 with Hypodermic, how would I go about connecting the slots? Would I place the connection code within the OnActivating block?
Am I right in thinking they are not duplicating each others functionality even though Hypodermic is built with Signals2?

Connecting the signal(s) in OnActivating will not "fire" the signal; that is, your signal's delegates won't be invoked.
But of course, you can do something like this to make it work:
mySignal.connect([](std::shared_ptr< Foo > foo)
{
// do some stuff with Foo being activated
});
builder.autowireType< Foo >()->onActivating(
[&mySignal](IActivatingData< Foo >& data)
{
// invoke all delegates...
mySignal(data.instance());
}
);
Hope that helps.

Related

How can you block a thread until one of many channels becomes readable?

I'm trying to have a thread (in rust) that listens to multiple channels at once.
In Ada, something similar can be done using a select statement:
loop
select
accept Task1 do
-- do something
end Task1;
or
accept Task2 do
-- do something else
end Task2;
or
accept Task3 (param : Parameter) do
-- do a third thing, with a given parameter
end Task3;
-- etc.
end select;
end loop;
This will block the thread until one of the Task methods is invoked remotely.
I'd like to do something like this in rust, but the following would be terrible, as I'd be wasting a ton of CPU time:
loop {
if let Ok(message) = rx1.try_recv() {
// do something
}
if let Ok(message) = rx2.try_recv() {
// do something else
}
}
Ideally, I'd like to block the thread entirely until one of the resources is updated. But I can't just switch out the thread, as I'd need some way of waking it back up when one of the resources is updated (which is what I think recv() does).
What's the normal way of solving this kind of problem in Rust?
AFAIK this is not possible with the standard library channels. However you can do it with crossbeam channels and select:
select! {
recv(rx1) -> msg => { /* Do something */ },
recv(rx2) -> msg => { /* Do something else */ },
}

duktape js - have multiple contexts with own global and reference to one common 'singleton'

We are in the process of embedding JS in our application, and we will use a few dozen scripts each assigned to an event. Inside these scripts we provide a minimal callback api,
function onevent(value)
{ // user javascript code here
}
which is called whenever that event happens. The scripts have to have their own global, since this funtion has always the same name and we access it from cpp code with
duk_get_global_string(js_context_duk, "onevent");
duk_push_number(js_context_duk, val);
if (duk_pcall(js_context_duk, 1) != 0)
{
printf("Duk error: %s\n", duk_safe_to_string(js_context_duk, -1));
}
duk_pop(js_context_duk); /* ignore result */
Then again we want to allow minimal communication between scripts, e.g.
Script 1
var a = 1;
function onevent(val)
{
log(a);
}
Script 2
function onevent(val)
{
a++;
}
Is there a way we achieve this? Maybe by introducing an own 'ueber-' global object, that is defined once and referencable everywhere? It should be possible to add properties to this 'ueber-global object' from any script like
Script 1
function onevent(val)
{
log(ueber.a);
}
Script 2
function onevent(val)
{
ueber.a=1;
}
Instead of simple JS files you could use modules. duktape comes with a code example to implement a module system (including its code isolation) like in Node.js. Having that in place you can export variables that should be sharable.
We have an approach that seems to work now. After creating the new context with
duk_push_thread_new_globalenv(master_ctx);
new_ctx = duk_require_context(master_ctx, -1);
duk_copy_element_reference(master_ctx, new_ctx, "ueber");
we issue this call sequence in for all properties/objects/functions created in the main context:
void duk_copy_element_reference(duk_context* src, duk_context* dst, const char* element)
{
duk_get_global_string(src, element);
duk_require_stack(dst, 1);
duk_xcopy_top(dst, src, 1);
duk_put_global_string(dst, element);
}
It seems to work (because everything is in the same heap and all is single threaded). Maybe someone with deeper insight into duktape can comment on this? Is this a feasible solution with no side effects?
edit: mark this as answer. works as expected, no memory leaks or other issues.

Is it ok to return in main?

Since mains return type is an empty tuple (), is it considered a work-around to use return; in fn main() ? I'd like to end my program but don't want to panic! , I just want to calmly end. Is there a standard way to end main early? Or is this ok to do? I come from a C++ background where if you need to return early from a function which does not return any value, you probably shouldn't be using a void to begin with, so I'm wondering if this is the same case with no return type for main()?
fn main() {
// ...
// if no cline args display usage and end
if matches.free.is_empty() {
print_usage(&program, options);
return;
// program continues
}
It's perfectly fine at the language level to return early like this. In this case, you might also like the std::process::exit function, which also allows setting the return code of the process.

RequireJS and Globals

I have a number of event handlers in my page that were accessing global functions (functions defined in Script tags on the page). For instance:
<button id="ClearText" onclick="cleartb()">Clear Text Box</button>
That cleartb() function simply sits on the page:
<script>
function cleartb()
{
vm.essayText('');
return;
}
</script>
Now, vm is my page's view model (but for this question, all that matters is that it was simply a global variable available to the entire page) and I use functions and values it exposes in several event handlers, alert messages, etc.
The problem is that I've moved the definition of vm into a RequireJS AMD module called vm.js:
define(["knockout", "jquery"], function (ko, $) {
var essayText = 'Hello World!';
...
return {
essayText: essayText
}
});
When my onlick event handler runs or I refer to vm in any manner, I get a "vm undefined" error (as expected).
Question 1:
How can I give my page access to the vm variable defined in an AMD module especially if I don't want to "pollute" the global namespace? Is there a best-practice here?
Question 2:
Ultimately, I don't even want cleartb() on the page because it really is a view-model-specific operation. Although I think I can figure out what to do once I have the (an?) answer to Question 1, I would be interested to know how best to move the cleartb function into the vm AMD module so that I still can call it from my onlick event handler.
Note that I want values and function still to be exposed from a vm variable so that I can continue to use vm.cleartb() or inspect the value of vm.essayText() (it's a KO observable). (In other words, I don't want to solve the problem with a cleartb(vm) solution.)
Thank you for any help!
<script>
function cleartb()
{
vm.essayText('');
return;
}
alert(window.cleartb);
</script>
Actually, this way is already pollute the global window variable. So I think your first requirement don't make sense. And then you can do this way:
define(["knockout", "jquery"], function (ko, $) {
var essayText = 'Hello World!', varToBeExported;
...
window.varToBeExported = {
'cleartb': cleartb
};
return {
essayText: essayText
}
});
But if unnecessary, you should using requireJs way - require(['your moudle'],.... .

Puppet run order

I've got a question on how to best do this senario, I've got a number of exec commands which need to be run in this order
Exec['A'] -> Exec['B']-> if var = 1(Exec['x']) if var = 2(Exec['y']) if var = 3 (Exec['z]) -> Exec['C'] -> Exec['D']
Basically I have an Exec then I have if statements and then I need to call an exec.
At this moment I've done the following
Exec['A'] { before Exec['B'] }
Exec['B'] { subscribe Exec['A'] }
if var = 1(Exec['x']) { before Exec['C'] }
if var = 2(Exec['y']) { before Exec['C'] }
if var = 3 (Exec['z]) { before Exec['C'] }
Exec['C'] { before Exec['D'] }
Exec['D']
I'm not sure if this code is idempotent, I just want to confirm with the community in case they see anything I may not be seeing. I have a feeling that the if statements could run before the Exec['A'] which I do not want.
The syntax is off, but this does indeed work
Exec['A'] { before => Exec['B'] }
if $var == 1 {
Exec['x'] { before => Exec['C'] }
}
else {
Exec['y'] { before => Exec['C'] }
}
Note that this code assumes that all the exec resources A, B, C, x and y have already been declared without any respective before parameters.
If you want x,y, and z to run after A, then add require => Exec['A'].
Also, it appears all of the execs will run with every puppet run. Add a onlyif, unless, creates, or refreshonly attribute.
To automatically test idempotence of your script, you may be interested in this tool: https://github.com/citac/citac
Citac systematically executes your Puppet manifest in various configurations, different resource execution orders, and more. The generated test reports inform you about issues with non-idempotent resources, convergence-related issues, etc.
The tool uses Docker containers for execution, hence your system remains untouched while testing. State changes are tracked during execution of the Puppet script, and detailed test reports are generated.
Please feel free to provide feedback, pull requests, etc. Happy testing!

Resources