[Click] Errors trying to get arguments in my element

Ian Rose ianrose at eecs.harvard.edu
Fri Mar 25 07:00:51 EDT 2011


Ah!  Ok that is easy (sorta) to fix.

configure() is used to configure the ELEMENT ITSELF at "configuration 
time" (which is one of the early stages of router initialization before 
it actually starts "running".  It is used mainly to parse and handler 
arguments TO THE ELEMENT.  So, for example, the Paint() element takes 1 
required and 1 optional parameter; the configure function handles this.
See:
http://read.cs.ucla.edu/gitweb?p=click;a=blob;f=elements/standard/paint.cc;h=a85989dd8c7492f7bd57edadc420baa421c7876e;hb=refs/heads/master

If you want to create a WRITE handler that takes arguments, that is 
pretty easy. Check out the Switch element for an example:
http://read.cs.ucla.edu/gitweb?p=click;a=blob;f=elements/standard/switch.cc;h=17edafcd5597203f6469ba906c0dc673b8536872;hb=refs/heads/master

Note that it defined read AND write handlers with the name "switch" -- 
pay attention to the write handler.  In write_param() you can see that a 
single integer parameter is parsed out of 's' which is a function 
argument which holds a single string containing ALL of the inputs.  So 
in this case it should just be "5" or something like that, but in your 
case it might be "1, 5" or even "STAGE 5, COLOR blue".

Creating a read handler that takes arguments is a bit trickier.  First 
you need to declare the handler something like so:
"set_handler("route_query", Handler::OP_READ | Handler::READ_PARAM, 
query_handler);"

"route_query" is the name that I chose for the handler itself
and "query_handler" is the function that gets called when the handler is 
invoked

Here is my query_handler function:
> int
> WifiOverlay::query_handler(int, String &s, Element *e, const Handler*, ErrorHandler *errh)
> {
>     WifiOverlay *elt = static_cast<WifiOverlay *>(e);
>     EtherAddress ether, bssid;
>     if (cp_ethernet_address(s, &ether, elt)) {
>         IPAddress *ip = elt->_bss_mappings.findp(ether);
>         if (ip == NULL)
>             s = "";
>         else
>             s = ip->unparse();
> 	return 0;
>     } else
>         return errh->error("expected Ethernet address, not '%s'", s.c_str());
> }

Obviously yours will differ, but as you can see my function takes a 
single, required, parameter of type EthernetAddress, does a lookup (via 
elt->_bss_mappings.findp(ether) -- obviously this is element-specific 
code so it shouldn't mean anything to you), and then returns the 
resulting IP address (in string form) if found.

The first argument to the function (which I am not using, and thus leave 
unnamed) is either Handler::h_read (if invoked as a read handler - as 
mine always is) or Handler::h_write (if invoked as a write context).

I hope that helps get you started.  I recommend starting with just a 
SINGLE, REQUIRED argument to your handler, get that working, and then 
get fancier with your supported arguments.  If you need help parsing 
arguments, just send your code to this list.

cheers,
- Ian


On 03/25/2011 05:03 AM, Fabrice Schuler wrote:
>    Hello, and thank you for your answers. I'll try to be more precise :
>
> On 03/24/11 19:00, Ian Rose wrote:
>> Next, you said you are having problems with your handlers, but I don't
>> think you actually spelled out what the problem is.  Are you getting a
>> compile error (if so what)?  Are the runtime results different than you
>> expect?  If so, please provide your text script, actual results
>> observed, and expected results.
>
> Indeed, the only thing I'm trying to do is to write an element that
> takes two arguments, and do some stuff on those. That's all, but I
> forgot to mention it, sorry for that. So the real question is : which
> functions do I need to do this ? I would say at least a "configure"
> function, and for the handlers, I don't know.
>
> At first, I wrote an element that does not take any argument, which is OK.
> I try to add the args now. To read them, I go through the configure
> function, which seems now correct (reads and stores args). But now that
> this function exists, the "simple_action" function is no more called.
> What am I doing wrong ?
>
> Sorry for those basics questions, but I could not find a tutorial on the
> subject.
>
> Best regards,
> Fabrice
> _______________________________________________
> click mailing list
> click at amsterdam.lcs.mit.edu
> https://amsterdam.lcs.mit.edu/mailman/listinfo/click


More information about the click mailing list