Getting started with Glare-DataServices

Glare-DataServices is a framework for building Flex Remoting services in VisualWorks Smalltalk. In this post, I will guide you through the steps for creating a small application that demonstrates the use of the framework.

Install the bundle

First, you have to connect to the Cincom Public Store Repository. Then open the list of Published Items and load the bundle GlareDataServices. It includes the Glare bundle and Glare-DataServices packages. The required dependencies AT MetaNumerics and Opentalk are automatically loaded if not present.

Start the server

Class GlareServer has a singleton instance that will dispatch messages sent from a Flex application to Smalltalk services. To start the server, evaluate:

GlareServer current start

Now, you can visit http://localhost:8888 to see the current class registrations. You can change the portNumber on which the server is listening. For more settings and run actions, see the GlareServer class methods.

Create Smalltalk class: MethodFinderService

In this example, we are going to create a small AIR application called MethodFinder. Its GUI will have a search box and a list of method names that match the search criteria. The search itself will be performed by a Smalltalk service called MethodFinderService.

These are the steps for creating the service:

  1. New Package: MethodFinder
  2. New Class: MethodFinderService with superclass Glare.RemoteObject
  3. Add instance method category operations
  4. Define the method getSelectorsMatching
getSelectorsMatching

  ^(self function: #getSelectorsMatching)
    argument: #pattern type: String
    ; returns: SortedCollection
    ; body:[ :pattern |
		| sortedMatches |
		sortedMatches := SortedCollection new.
		"to keep things simple, we enumerate all symbols"
		Symbol allSymbolsDo:[ :e | (pattern match: e) ifTrue:[sortedMatches add: e]].
		sortedMatches]

Registration of this service to the GlareServer is required and done by evaluating:

MethodFinderService install

Create Flex application: MethodFinder

Now that the Smalltalk service is up and running, it is time to build the client application. Using the Adobe Flex/Flash Builder, create a new Flex AIR project called GlareMethodFinder. Open the GlareMethodFinder application component and paint a List (id=methods), a TextInput (id=pattern) and some Labels. On every change of the pattern, a new search will be requested.

  • missing image /2009/05/picture-6.png

Your MXML file may look like this.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="344" height="508">
	<mx:TextInput id="pattern" x="63" y="40" width="246" change="doSearch()"/>
	<mx:List id="methods" x="10" y="70" width="299" height="403"></mx:List>
	<mx:Label x="10" y="10" text="Glare Method Finder" fontSize="18"/>
</mx:WindowedApplication>

Configure Flex Remoting

The Flex compiler of the application needs to known about which AMF channels and destinations are going to be used. For this, two configuration files must be added. In the folder src of the project, add the file services-config.xml with:

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
    <services>
        <service-include file-path="remoting-config.xml" />
    </services>
    <channels>
        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint uri="http://localhost:8888/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
        </channel-definition>
    </channels>
</services-config>

In the same folder (src) add the file remoting-config.xml with:

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
    class="flex.messaging.services.RemotingService"
    messageTypes="flex.messaging.messages.RemotingMessage">
    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>
    <destination id="MethodFinderService" />
</service>

Now that we have the configuration files in place, we need to add a Flex compiler option. Open the Project properties and select “Flex Compiler” in the list. Change the options to the following line. (Windows developers need to change the path separator)

-locale en_US  -services ${DOCUMENTS}/GlareMethodFinder/src/services-config.xml

From Button click to Service call

Add the following script (using mx:Script) that implements the doSearch function. It creates a RemoteObject that will bind to the Smalltalk MethodFinderService. Using this remoteObject, you can send it the message getSelectorsMatching with the pattern argument.

private function doSearch():void {
	if (pattern.text.length == 0) return
	var service:RemoteObject = new RemoteObject("MethodFinderService")
	service.addEventListener(ResultEvent.RESULT, handleSearchResult)
	service.getSelectorsMatching(pattern.text + '*')
}
private function handleSearchResult(event:ResultEvent):void {
	methods.dataProvider = event.result as Array
}

Now the application is ready to find some methods. Run the application and start entering a pattern.

For this application to be really useful, we can add two buttons “Browse Implementors” and “Browse Senders”. We want these to open Smalltalk browsers in our running image. For this to work we can add two more operations to the MethodFinderService like browseImplementorsOf and browseSendersOf.

As can be expected, this exercise is left to reader.