What is the difference bewteen handler and callback function? - concept

In my current project, there are lots of networking code, and it use the event handler to handle the input message. Is this mechanism different with the call back function ?

Typically not much. The handler is usually used in the context of a UI application where the UI control will call the handler to handle a UI event. The callback function is traditionally used from the C days (Function pointers) and also in the C++ (Functors) world.

As a general concept I would say that the call back functions are primarily used for Asynchronous execution. Where for example, client side function must look something up on the server and it may take a while. So instead of blocking it says :"Call back at this number (myCallBackFunction) when you are done looking up stuff on the server".
Now event handlers are just that: they handle some predefined events. Usually they wait for users to do something like click a button and then they spring into action. They typically but not necessarily expect some sort of input.
Hope this helps.

Related

Intercept and transform key signals to a process in C++ or any other language

I want to intercept Russian keys in a specific application and send it something else. I don't really know the steps required to achieve it.
Is there a way to write an app in C++ that captures key presses and sends different ones to a specific program, process?
I guess I had to find the answer myself so I've duckduckgoed and found this: C++ KeyBoard hook
Seems like I have to inject a DLL in a process and call SetWindowsHookExA function that supposedly ought to intercept and transform key events that are sent to the process.

Discard message from nServiceBus mutator

I need to discard a message if a specific header is present.
I tried to implement a IMutateTransportMessages and call DoNotContinueDispatchingCurrentMessageToHandlers() method inside MutateIncoming but the message is dispatched to handlers anyway.
I can discard the message using an handler but I don't like it because I need also to specify the handlers' order.
Any solution?
Thanks,
Federico
I don't think this will be possible from a message mutator. After all, this isn't really the kind of activity a message mutator should be doing - it has nothing to do with changing the structure of the message.
I agree with you that it sounds messy to do this in a handler, because you're right - then you are very dependent upon the handler order.
Discarding a message due to the presence (or absence) of a header is an infrastructure concern, and since you are using NServiceBus V5, the best way to customize the infrastructure is by customizing the message handling pipeline.
Here's the relevant documentation that covers creating a behavior and inserting it into the pipeline but here's the short version:
You need to create a behavior that implements IBehavior<IncomingContext>. All of the behaviors together form a behavior chain that progress to the next step by calling next() - an Action present in the implementation) method.
Within the behavior, check for your header. To stop processing the message, just don't call next() and call context.DoNotInvokeAnyMoreHandlers() instead.
Create a class inheriting RegisterStep which, from its constructor, will define where in the pipeline to insert your behavior. You could register it right before the MutateIncomingTransportMessage step.
Create a class implementing INeedInitialization (note that this could be the same as the RegisterStep class) which calls busConfig.Pipeline.Register<TClassThatInheritsRegisterStep>().
Again, that's the short version. Check out the docs for the full story and an example.
Shameless plug: This process is also described in detail in Learning NServiceBus - Second Edition in Chapter 7, with an example.

How to enable TRACE_EVENT1() in WebRTC codes?

In WebRTC codes there is a macro TRACE_EVENT1(). Probably this suppose to print somewhere info traces for events.
How to enable TRACE_EVENT1() and make it work? Is it possible to print the event tracing in a file?
You need to setup an event tracer function. Whenever those macros are encountered, WebRTC would invoke your tracer function (that you have setup at the beginning). When inside your tracer function, you can print them in any convenient file.
webrtc::SetupEventTracer(getCategoryEnabledFunc, AddTraceFunction);
Now you should have AddTraceEventFunction defined as a function pointer somewhere and it should point to your logging function.
Event Tracer & Tracer Callbacks are different
Please note that the Event tracer setup like above takes care of the TRACE_EVENT macros. There's a separate notion of a Tracer callback (which is setup using the VoiceEngine::SetTraceCallback method). This is different and takes care of WEBRTC_TRACE macros.
Hope it helps
Under certain circumstances, logging is restricted in WebRTC. Add "restrict_webrtc_logging=0" to GYP_DEFINES to counter that.

Sencha Touch store sync callback

My store.sync() can return success:false, and if it does, I would like to use something similar to Ext's failure callback to react to the error appropriately, but I did not find a possibility to use any builtin ST functions for this. sync has neither callback nor success nor failure option available in ST.
What did I overlook?
PS: I did find a workaround for success callback at Why is there no sync callback in Sencha Touch? but I need failure callback.
store.sync() is not where you need to look. Take a look at the proxy. Most likely you are using an Ajax request and that in turn will deliver a detailed success and failure.
I am now calling Ext.data.Model.save() on all Ext.data.Model instances that are dirty. This won't batch everything neatly together, but in 90% of the cases, only one item is edited anyways. The best is that this allows to check for failure on each and every item, and not only on the whole batch.

Notifications in wxWidgets?

I'm working on a small application using C++/wxWidgets, where several parts of the GUI need to be updated based on e.g. received UDP datagrams. More specifically, a secondary thread tries to keep a list of available "clients" in the network (which may come and go away) and e.g. corresponding comboboxes in the UI need to be updated to reflect the changes.
The documentation mentions that for this kind of thing EVT_UPDATE_UI would be a good choice. As far as I can understand from the sparse documentation, this event is sent automatically by the system and provides some support for assisted UI change.
However, I'd feel more comfortable using a more direct approach, i.e. where e.g. a window object could register/subscribe to receive notifications (either events or callbacks) upon particular events and another part of the code is sending out these notifications when required. I could do this in C++ using my own code, however I guess if wxWidgets already supports something like that, I should make use of it. However I haven't found anything in that regards.
So, the question is: does wxWidgets support this kind of notification system (or similar alternatives) or would I be best served coding my own?
AFAIK there is nothing directly usable in wxWidgets, but doing it on your own seems easy.
What I would do:
Create a wxEvtHandler-descendent class to hold the list of available "clients" in the network. Let this class have a wxCriticalSection, and use a wxCriticalSectionLocker for that in all methods that add or delete "clients".
Create a worker thread class by inheriting wxThread to handle your UDP datagrams, using blocking calls. The thread should directly call methods of the client list object whenever a client has to be added or removed. In these methods update the list of clients, and ::wxPostEvent() an event to itself (this will execute the whole notification calls in the main GUI thread).
Handle the event in the client list class, and notify all listeners that the list of clients has changed. The observer pattern seems to me a good fit. You could either call a method of all registered listeners directly, or send a wxCommandEvent to them.
Have you tried calling Update() on the widget(s) that change? Once you update the contents of the combo box, call Update(), and the contents should update.