POX component listen to events - listener

I want to discover the topology of a network emulated by mininet using POX components. I figured out that I need to write my own component, which is listening to LinkEvents. Something like:
someObject.addListenerByName("LinkEvent", someFunction)
But I don't actually know on what kind of an object i should execute this.
If I execute it as
core.openflow_discovery.addListenerByName("LinkEvent", someFunction)
as stated in the openflow.discovery module, it throws the following error:
AttributeError: 'openflow_discovery' not registered

It is easier to use pox modules named "gephi" to do this, it should be under misc directory, just add this method to the "gephi_topo.py" in "class GephiTopo":
def get_gephi_topology (self):
switchesAndLinksAndHosts=[self.switches,self.links, self.hosts]
return switchesAndLinksAndHosts
and then use it anywhere in your pox controller like:
topo=gephi_topo.GephiTopo.get_gephi_topology(core.GephiTopo)
switches= topo[0]
links=topo[1]
hosts=topo[2]

Fixed it by calling addListenerByName from within launch().

Related

Calling showOpenFilePicker : how avoid "must come from gesture" when calling class methods

Accessing the local filesystem a la: https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
If I inline all my code for the show(Open|Save)FilePicker API logic directly in my button event closure or call plain fn's then things work ok. However, if I define a class w/helper methods and call those from my event-handler, and those helpers call the file pickers, then I get this error:
Uncaught DOMException: Failed to execute 'showOpenFilePicker' on 'Window': Must be handling a user gesture to show a file picker.
I assume the the security system is looking at this to establish the gesture context - is there a way to re-establish this context with code running in my class?
Turns out the class methods were a red herring -- the issue has to do with async code and promises -- this is what looses the context and causes the error.
You need to make any calls to the FS without any promise chaining.

Correct way to pass runtime configuration to elixir processes

I'm trying to deploy an app to production and getting a little confused by environment and application variables and what is happening at compile time vs runtime.
In my app, I have a genserver process that requires a token to operate. So I use config/releases.exs to set the token variable at runtime:
# config/releases.exs
import Config
config :my_app, :my_token, System.fetch_env!("MY_TOKEN")
Then I have a bit of code that looks a bit like this:
defmodule MyApp.SomeService do
use SomeBehaviour, token: Application.get_env(:my_app, :my_token),
other_config: :stuff
...
end
In production the genserver process (which does some http stuff) gives me 403 errors suggesting the token isn't there. So can I clarify, is the use keyword getting evaluated at compile time (in which case the application environment doest exist yet)?
If so, what is the correct way of getting runtime environment variables in to a service like this. Is it more correct to define the config in application.ex when starting the process? eg
children = [
{MyApp.SomeService, [
token: Application.get_env(:my_app, :my_token),
other_config: :stuff
]}
...
]
Supervisor.start_link(children, opts)
I may have answered my own questions here, but would be helpful to get someone who knows what they're doing confirm and point me in the right way. Thanks
elixir has two stages: compilation and runtime, both written in Elixir itself. To clearly understand what happens when one should figure out, that everything is macro and Elixir, during compilation stage, expands these macros until everything is expanded. That AST comes to runtime.
In your example, use SomeBehaviour, foo: :bar is implicitly calling SomeBehaviour.__using__/1 macro. To expand the AST, it requires the argument (keyword list) to be expanded as well. Hence, Application.get_env(:my_app, :my_token) call happens in compile time.
There are many possibilities to move it to runtime. If you are the owner of SomeBehaviour, make it accept the pair {:my_app, :my_token} and call Application.get_env/2 somewhere from inside it.
Or, as you suggested, pass it as a parameter to children; this code belongs to function body, meaning it won’t be attempted to expand during compilation stage, but would rather be passed as AST to the resulting BEAM to be executed in runtime.

How to have class loaded without using "use" but call it as if I used it

I have studied these 2 sources, but none of those works for me.
http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html
Yii2 - How do I AutoLoad a custom class?
I have created custom helper class which I want to include in every model, controller and view in my application. I am using Yii2 advanced app version 2.0.11, IDE I am using is PHPStorm
QUESTION:
What I want to achieve is to not use use keyword at the beggining of every class but still be able to simply call AppHelper::myMethod() in models, controllers and views.
How is that possible?
Closest I got it working was using this solution https://stackoverflow.com/a/35160997/5395463
All other solutions did not work for me. I am getting errors like:
PHP Fatal Error – yii\base\ErrorException
Class 'frontend\controllers\AppHelper' not found
when I simply dont include use commons\commands\AppHelper;
or when not using namespace as they suggest there with other settings:
Fatal error: Uncaught exception 'yii\base\UnknownClassException'
with message 'Unable to find 'common\commands\AppHelper'
in file: C:\xampp\htdocs\domain.com\web\common/commands/AppHelper.php.
Namespace missing?' in ...
SOLUTION:
Thanks for your responses, I got it working finaly. https://stackoverflow.com/a/42330631/5395463 solution works best for me.
So I removed namespace from that class, but left it in common\commands folder, added require to frontend/web/index.php and backend/web/index.php files (not sure if I should add it to yii file in root too, I didnt, so far it is working good anyways), and changed calls of class from AppHelper::myMethod() to \AppHelper::myMethod() looks like eveything is working now.
In Yii2 You can use an explicit way adding \ to full namespaced name
\frontend\controllers\AppHelper
so you can sue your method
\frontend\controllers\AppHelper::yourMethod();
Solution for not lazy coders:
create component with your class so you can use it like \Yii::$app->my_component
if even this is too much and your IDE is better than Windows Notepad prepare macro that will print this for you
Solution for lazy coders:
Save your class in single PHP file without namespace.
Modify you entry script to include this class - i.e. for Basic Project Template it's /web/index.php; add there
require(__DIR__ . '/path/to/MyClass.php');
For Advanced Project Template modify it properly.
Now you can use your class by calling it like \MyClass (yes, \ is required and yes, your IDE probably will modify it anyway to MyClass with use MyClass; added at the beginning.

Trying to clean merged JS/CSS cache, but model "core/design_package" is a non-object

I have made a new PHP file at the root directory of Magento (next to index.php) and it contains this code:
require 'app/bootstrap.php';
require 'app/Mage.php';
Mage::getModel('core/design_package')->cleanMergedJsCss();
Produces this error:
Fatal error: Call to a member function getModelInstance() on a non-object
in /home/edpadev/public_html/stage/bsr/app/Mage.php on line 463
From my understanding, Magento should dig up that particular method in /app/code/core/Mage/Core/Model/Design/Package.php.
I cannot var_dump it since it cannot instantiate it, I just get the same error when trying to do that.
I am able to call our third-party cache module's observer model and use its methods, and use the simple Mage::app()->cleanCache();, but the core function above does not work, though that is how it appears in examples by other developers in Google search results.
Try this. You might need to setup the store
umask(0);
require 'app/Mage.php';
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Achieve Do after and before using ES6 Proxy

I've a View and Utility classes and wanted to hook-in the Utility method once View's job is done. So wanted to call the utility method (to add behavior to the view) once View's render call is done.
Using ES6 Proxy API, Is there a way to execute a method before / after executing the main method ? (aka method interceptions)
Similar to YUI3 Do API.
http://yuilibrary.com/yui/docs/api/classes/Do.html
Yes there is a way. Actually I'm trying to build the same thing and as long as you use a synchronous context I already got it working. Have a look at the code examples in the issue I created for my problem: stack overflow when returning an ES6 proxy through a promise
What you need to do to intercept the call is to use the ES6 proxy to get notified of the get-call that takes place to retrieve the function before the method is actually called.
In my example you can see that you get all the information about the call like what method was called with which parameters and also who called it and on which target it was originally called.