Where to register mediator in puremvc? - actionscript

Currently I am working on flex using puremvc framework. Actually my question is related to where to register mediator in puremvc framework. One of my colleague is registering mediator in views(components) creationComplete method only (inside view). While my preference is send some notification from creationComplete method which could be handle by some command and command will register mediator. So which one is better approach in terms of best practice ?

Views by themselves can't register mediators. Mediators can register mediators instead. E.g. you have a Panel and a PanelMediator. If you have a ListA and ListB in this panel your PanelMediator can add creation complete event listeners to ListA and ListB. In these listeners you can register mediators like ListAMediator/ListBMediator.
The goal is to make components reusable, so your Views should deal with UI and not with your application core.

I would register your mediators in Commands.
From Best Practices...
To communicate and interact with other
parts of the system, Commands may:
Register, remove or check for the existing registration of Mediators,
Proxies, and Commands.
Send Notifications to be responded to by other Commands or Mediators.
Retrieve and Proxies and Mediators and manipulate them directly.
As mico mentioned, View's should not register their own mediators, and Proxy's should stay tied to their data sources they interface with and nothing else.

I usually have one StartUp notification which gets called right after the base Movieclip receives an added to stage event. This StartUp notification triggers a StartUp command which will register all mediators and proxies needed. Hope this helps.

Related

Server-sent events and ZF 2.5

I am developing an application with ZF2.5. I need to make an SSE (Server-Sent Events) module, but I can't manage to do that using a controller, it does not keep my connection alive (of the type: text/event-stream). So I am doing this in a separate php file, but I need authentication on that, and needed to reach Zend's service manager from this file "outside" the Zend environment.
Is it posible? Any suggestions?
Yes, you can do this from within ZF2, but it is not easy. The base of SSE is the connection is kept open. So you need somehow a while(true) or similar in php to keep the process running.
A controller is, when the function is done, terminated and the response is sent. You have to get this logic into a controller. Next, the response handler buffers all output in ZF2 and then sends all data at once. You need to reprogram the ZF2 output buffering flow, so you can send data directly from your controller without the buffering. Because otherwise, you're while(true) loop never sends data, only when you break the loop.
So the short answer: almost anything is possible in ZF2, including your needs. But it is not that straightforward.
The alternative is to load the service manager in your stand-alone script. This is also perfectly possible. Using the application config and merged other configs, you need to build your complete config and provision the SM with it. Then when instantiated, you can fully utilize its system.
Also here, only instantiating the SM can be hard. Easier here is to instantiate the application and grab the SM from it:
$app = Zend\Mvc\Application::init(include 'config/application.config.php');
$sm = $app->getServiceManager();
Note you don't `run()' the app, only bootstrap it!

Nofity User some message from a windows service

I have created a windows service which gets soem info from database and I want to notify user based on the info retreived from the DB. How can I notify user from a windows service using system tray notification? Can you please show me some sample (using IPC mechanism) to get the return value of a method used in a windows service in a system tray notification?
Thanks in advance.
There are several options such as these:
Sockets: (Not too difficult to write, has firewall problems) You can find samples for it almost everywhere.
External WinForm: (The easiest method, has security problems and might blocked by
some antivirus apps) Just create a winForm with the ability to go into
the windows notification area and then tell the service to run its
exe file.
Named Pipes: (Probably the most difficult, but it's the recommended
solution) Here is a Code Project sample.
Other tricks like: Create a hidden winform project (ShowInTask=false) and put it in StartUp. provide it with a FileSystemWatcher object and make it watch for a certain file which the service creates or deletes it to signal the winform.

Logout clients from XMPP

I have an xmpp/ejabberdb app that uses an external service to provide eventing features, but when this service becomes unavailable, I want to disconnect/logout all of my clients. Is this possible? How?
I got it working the way I needed. In fact, I didn't find any simple way to make my own server logout all connected users given some kind of situation, so I dug into ejabberd's code and figured out a way to do it myself.
In ejabberd_c2s.erl module, when a client logs out or it's socket is dropped for some reason, the FSM is terminated, doing all necessary clean up to maintain ejabberd's consistency.
What I had to do was just create an exported function shutdown/1 in this module that calls gen_fsm:send_all_state_event/2 sending a signal for it to terminate.
As for each connection there's one c2s process, I need to call this function for each user.
---UPDATING---
Actually there's no need to create this shutdown function, as ejabber_c2s already has the ability to process 'closed' signal, which does the same thing. So, instead of creating the shutdown function, simply doing ge_fsm:send_event(C2SPid, closed) might be enough.
---UPDATING---
To discover the user's c2s process PID I just use ejabberd_sm:get_session_pid/1 or ejabberd_sm:dirty_get_sessions_list/0 (for all sessions).
This worked fine for me, but if anyone has a better idea, please add here.
Thanks
I don't know the ejabberd specifics, but you could write a custom XMPP component which polls the external service (or listens for presence events, if it's another XMPP component), then logs out users when the service becomes unavailable.

Monitor the Custom Url Protocol Messages - Delphi

As you know the custom url protocol is a feature that allow us to make a communication between a web page and our application. For example, Y! Messenger uses this protocol when you want to send a pm to another person through a web page:
Click to send pm
Now, the question is how can I monitor(hook) all custom url protocol messages in the windows? Is it possible to catch them?
Iterate over the contents of the registry, looking for registered custom protocols. Those will be children of the HKey_Classes_Root key that have a value named URL Protocol. Each time you find one, record the default value of the shell\open\command key, and then replace it with a command line pointing to your program.
When your program is invoked, do whatever you need to do, and then invoke the original program using the command line you recorded earlier.
You can register your own protocol: Can Delphi be used to create and handle a custom protocol handler? Just add few keys in user's registry (protocol name and application to launch). It is similar to register file extension. Simple example is here.
I'm not sure that you can catch every request. Antivirus programs can do that, but at driver-level.
You might want to take a look at Fiddler HTTP Web Debugger, which intercepts all HTTP traffic by being a temporary proxy.
It is not written in Delphi, but should give you a good idea on how to approach your problem.

Ideas for web application with external input and realtime notification

I am to build a web application which will accept different events from external sources and present them quickly to the user for further actions. I want to use Ruby on Rails for the web application. This project is a internal development project. I would prefer simple and easy to use solutions for rapid development over high reliable and complex systems.
What it should do
The user has the web application opened in his browser. Now an phone call comes is. The phone call is registered by a PBX monitoring daemon. In this case via the Asterisk Manager Interface. The daemon sends the available information (remote extension, local extension, call direction, channel status, start time, end time) somehow to the web application. Next the user receives a notified about the phone call event. The user now can work with this. For example by entering a summary or by matching the call to a customer profile.
The duration from the first event on the PBX (e.g. the creation of a new channel) to the popup notification in the browser should be short. Given a fast network I would like to be within two seconds. The single pieces of information about an event are created asynchronously. The local extension may be supplied separate from the remote extension. The user can enter a summary before the call has ended. The end time, new status etc. will show up on the interface as soon as one party has hung up.
The PBX monitor is just one data source. There will be more monitors like email or a request via a web form. The monitoring daemons will not necessarily run on the same host as the database or web server. I do not image the application will serve thousands of logged in users or concurrent requests soon. But from the design 200 users with maybe about the same number of events per minute should not be a scalability issue.
How should I do?
I am interested to know how you would design such an application. What technologies would you suggest? How do the daemons communicate their information? When and by whom is the data about an event stored into the main database? How does the user get notified? Should the browser receive a complete dataset on behalf of a daemon or just a short note that new data is available? Which JS library to use and how to create the necessary code on the server side?
On my research I came across a lot of possibilities: Message brokers, queue services, some rails background task solutions, HTTP Push services, XMPP and so on. Some products I am going to look into: ActiveMQ, Starling and Workling, Juggernaut and Bosh.
Maybe I am aiming too hight? If there is a simpler or easier way, like just using the XML or JSON interface of Rails, I would like to read this even more.
I hope the text is not too long :)
Thanks.
If you want to skip Java and Flash, perhaps it makes sense to use a technology in the Comet family to do the push from the server to the browser?
http://en.wikipedia.org/wiki/Comet_%28programming%29
For the sake of simplicity, for notifications from daemons to the Web browser, I'd leave Rails in the middle, create a RESTful interface to that Rails application, and have all of the daemons report to it. Then in your daemons you can do something as simple as use curl or libcurl to post the notifications. The Rails app would then be responsible for collecting the incoming notifications from the various sources and reporting them to the browser, either via JavaScript using a Comet solution or via some kind of fatter client implemented using Flash or Java.
You could approach this a number of ways but my only comment would be: Push, don't pull. For low latency it's not only quicker it's more efficient, as your server now doesn't have to handle n*clients once a second polling the db/queue. ActiveMQ is OK, but Starling will probably serve you better if you're not looking for insane levels of persistence.
You'll almost certainly end up using Flash on the client side (Juggernaut uses it last time I checked) or Java. This may be an issue for your clients (if they don't have Flash/Java installed) but for most people it's not an issue; still, a fallback mechanism onto a pull notification system might be prudent to implement.
Perhaps http://goldfishserver.com might be of some use to you. It provides a simple API to allow push notifications to your web pages. In short, when your data updates, send it (some payload data) to the Goldfish servers and your client browsers will be notified, with the same data.
Disclaimer: I am a developer working on goldfish.
The problem
There is an event - either external (or perhaps internally within your app).
Users should be notified.
One solution
I am myself facing this problem. I haven't solved it yet, but this is how I intend to do it. It may help you too:
(A) The app must learn about the event (via an exposed end point)
Expose an end point by which you app can be notified about external events.
When the end point is hit (and after authentication then users need to be notified).
(B) Notification
You can notify the user directly by changing the DOM on the current web page they are on.
You can notify users by using the Push API (but you need to make sure your browsers can target that).
All of these notification features should be able to be handled via Action Cable: (i) either by updating the DOM to notify you when a phone call comes in, or (ii) via a push notification that pops up in your browser.
Summary: use Action Cable.
(Also: why use an external service like Pusher, when you have ActionCable at your disposal? Some people say scalability, and infrastructure management. But I do not know enough to comment on these issues. )

Resources