Executable operation specifcations in Glare-DataServices

Glare< is a Flex Remoting and Messaging server written in VisualWorks Smalltalk by Philipp Bunge. I am extending his work with Glare-DataServices which basically provides a HttpServlet that dispatches operation invocations send from a AMF client (Flex,AIR) to Smalltalk objects.

To work with Glare-DataServices, you need Smalltalk classes that implement operations and ActionScript classes that have corresponding methods in which these operations are invoked remotely. Because both classes (client and server-side) must have the same signature, it is clear that code generation can be very helpful.

One of the requirements, also applied to the Pocogese framework (Flex on Rails), is that methods of each Service class must have metadata such as function name and argument types. Using the metadata of each operation, a code generation tool is able to generate ActionScript classes with the correct signature.

For Glare-DataServices, I am exploring a different approach that combines both the specification and implementation of each operation using a Block. Let me illustrate using a simple example:

SampleService>>computeSum
  " Answer a RemoteMethod that both specifies and implements the sum computation"
       
  ^( self function: 'computeSum' )
         comment: 'Returns the sum of two numbers'
       ; argument: 'left' type: Number
       ; argument: 'right' type: Number
       ; returns: Number
       ; body: [ :left :right |  left + right ]

You will notice that sending the message “computeSum” to an instance of this service will not perform the operation but instead returns a RemoteMethod with a body.

Having the operation metadata, a RemoteMethod can easily be translated to the ActionScript function implementation:

public function computeSum ( left : Number, right : Number , resultHandler : Function ) : void {
    var ro:RemoteObject = new RemoteObject ( "SampleService" )
    ro.computeSum.addEventHandler(ResultEvent.RESULT,resultHandler)
    ro.computeSum(left,right)
}

When Glare-DataServices receives a request from Flash (running ActionScript), it will lookup the RemoteMethod using the name of the function and evaluate the body of that method like this:

serviceOperation: functionName withArguments: aCollection
    " Find the registered RemoteMethod and evaluates its body using the arguments."
    
    ^ (self lookUp: functionName) body valueWithArguments: aCollection asArray