For more details, please see ourCookie Policy.

Learn OpenScript

Event Driven Programming with OpenScript

by Derek_Kang on ‎01-09-2012 02:25 PM (724 Views)


ADX OpenScript Perl Scripts are “Event Driven”. It means that users write event handlers in a script that are called when the attached event occurs. Examples of an event that will trigger execution of a script include: arrival of a TCP SYN packet or a HTTP Request. The event context largely determines the extensions to be used in the handler. For example, in the event of an http_request handler, you can use the methods in the OS_HTTP_REQUEST extension (and possibly the TCP and IP extensions). Using OS_HTTP_RESPONSE in a request context will have unpredictable results.

Accordingly, the structure of a perl script written for OpenScript differs slightly from the standard free-flowing script program usually associated with Perl. In a regular perl script, methods (subs) can be freely defined and invoked from the main body of the script. To provide for optimal packet processing, scripts written for the OpenScript environment build their behavior around the scripting engine with application and protocol event handlers. These handlers are a published list of events that a user script can catch by enclosing their event handling code in a Perl function or sub named after the event.

Error Handling

All OpenScript API methods return an empty list in list context, the undefined value in scalar context, and nothing in the void context. Note that a perl program will not crash on accessing these return values in the case of an error condition. The ADX OpenScript Extension provides a facility to retrieve the last encountered error as shown.

$mothod = OS_HTTP_REQUEST::method;

print OS_SYSTEM::get_last_error unless defined $req;

Data Modification

The OpenScript engine does not buffer application data by default. The script must initiate a request to buffer data by using OS_TCP::collect or OS_HTTP_REQUEST::collect on the request, or OS_TCP::collect or OS_HTTP_RESPONSE::collect on the response. Both TCP collect and HTTP collect can be defined at the same time; this type of requests can be initiated in client and/or server flows. Packets from a flow being collected are not forwarded until the requested amount of data is available and processed by any user defined content handlers. A script wishing to modify the default forwarding behavior must request a hold on data being buffered.

Default Action

In the request events, such as HTTP_REQUEST and TCP_CLIENT_DATA, a default action will be taken if no SLB action decision is made in the event handler. The default is the Layer 4 forward, equivalent as OS_SLB::forward.

Object Oriented Programming

The OpenScript API supports object oriented programming. The object can be retrieved through the package get API. The APIs other than "get" can be called either through the package or through the object. For instance, the API "method" of OS_HTTP_REQUEST can be called in two ways:

  • Through the package:
My $http_method = OS_HTTP_REQUEST::method;

  • Through the object:

My $req_obj = OS_HTTP_REQUEST::get;
My $http_method = $req_obj->method;