Wednesday, December 9, 2009

A basic RemObjects SDK ROZeroConf tutorial

I was recently asked about the RemObjects SDK ROZeroConf capabilities in Delphi. I hadn't actually played with it yet, particularly in Delphi since I've mostly been doing .NET work of late. They do have the BonourDiscovery sample project, but if you you're like me you may prefer to read a little before trying to follow code, particularly if you're new to the component suite and already have the usual learning curve to follow. You also don't know what was added when, and you may miss important component properties if they aren't highlighted or done in code. The wiki article is a little slim and not always easy to follow since it includes both Delphi and .NET instructions. It's a new feature so I'm sure documentation will improve in time, but here's a start to finish barebones sample from scratch:
  1. Create a new RemObjects SDK VCL Server project using the wizard. I named my project ROZeroConfTest. Make sure you do create the client project as well. It's checked by default. I went into the advanced options of the wizard and renamed NewLibrary to ROZeroConfLibrary. I also renamed NewService to ROZeroConfService. Finish the wizard and your base project will be created.
  2. On your client application's form (which comes up first by default) add a ROZeroConfBrowser component from the RemObjects SDK component group.
  3. In the properties list set Domain to local. and set the Active property to True.
  4. Add a TMemo component to the client form and align it to the client.
  5. Open your server form and add a ROZeroConfRegistration component to it.
  6. In the Properties list set Server to your server component. It's probably named ROServer by default, and if you took the default configuration in the wizard it's probably of type TROIndyHttpServer.
  7. In the Project Manager make the server project active. Then go to the RemObject SDK menu in Delphi and select Edit Service Library. You'll see it has the usual Sum and GetServerTime test methods that all default project have. Add more if you like, but for this sample we'll just leave the defaults.
  8. Go to the RemObjects SDK menu and select Regenerate Units from RODL. This will launch a wizard. Just say OK to the default selection of Remobjects SDK Remote Datamodule.
  9. Open the code of your _Impl file, in the case of this sample it's named ROZeroConfService_Impl, and uncomment the RegisterForZeroConf function in your Initialization section.
  10. The second parameter to the RegisterForZeroConf function is a string containing your services name. In this sample it's _ROZeroConfService_rosdk._tcp.. Copy that string and open your client form. Select the ROZeroConfBrowser component and paste that string into the ServiceType property.
  11. While you still have the ROZeroConfBrowser component selected go to the Events tab in the Object Inspector. We're going to add two event handlers. One for OnServiceAdded and one for OnServiceRemoved
  12. Add the following line to your OnServiceAdded event:
    Memo1.Lines.Add(Format('Service resolved. Host name: %s Port number: %d', [aRecord.HostTarget, aRecord.Port]));
  13. And we'll add this line to the OnServiceRemoved event:
    Memo1.Lines.Add(Format('Service has gone down: %s (at host %s:%d)', [aRecord.ServiceName, aRecord.HostTarget, aRecord.Port]));

Assuming I got all the steps in there properly you should be able to compile and start your server application. Then start your client application. The client should register and add a line to your memo field saying something like this:

"Service resolved. Host name: vmware-devel.local. Port number: 8099"

If you close the server down, the client should add a line to the memo field that says something like this:

"Service has gone down: vmware-devel (at host vmware-devel.local.:8099)"

Hope that helps someone, at least until theres more information about these new features on the RemObjects wiki. The Delphi 2007 project can be downloaded here.


About The Author

Ron Grove draws on over ten years of training, network administration and development experience. He loves to work with new technology and see how that technology can be best utilized by his clients. You can find him through his company Evanoah, LLC or through his LinkedIn profile.

0 comments:

Post a Comment