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:

[sourcecode language=“ruby”] 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
[sourcecode language=“ruby”] 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:

[sourcecode language=“ruby”] 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.

[caption id=“attachment_264” align=“alignnone” width=“188” caption=“Glare Method Finder”]Glare Method Finder[/caption]

Your MXML file may look like this.

[sourcecode language=“ruby”] <?xml version=“1.0” encoding=“utf-8”?> /mx:List /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:

[sourcecode language=“ruby”] <?xml version=“1.0” encoding=“UTF-8”?>

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

[sourcecode language=“ruby”] <?xml version=“1.0” encoding=“UTF-8”?>

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)

[sourcecode language=“ruby”] -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.

[sourcecode language=“ruby”] 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.

Flex source code of this example can be found on The Smalltalk source code is available in the Cincom Public Store Repository in the package Glare-Examples-MethodFinder.

comments powered by Disqus