Thursday, December 10, 2009

A Basic Delphi RemObjects DataAbstract ROZeroConf tutorial

Yesterday I did a short tutorial on getting up and running with the base RemObjects SDK. Today's short tutorial will be on how to get a basic DataAbstract server started with ROZeroConf capabilities.
  1. Run the DataAbstract login server wizard. First I named the project "DelphiDAZeroConfTest". I then went into the advanced section and named the service "DAZeroConfTestService" and the library "DAZeroConfTestLibrary".
  2. We'll connect to the PCTrade database. I'm using the one in my SQL Server 2005 install. If you have that, fine, or perhaps you can use the SQLite PCTrade database that comes with the product.
  3. Add TDAZeroConfRegistration to your fServerDataModule.
  4. Attach it to the Server component on the same DataModule.
  5. Go to the "RemObjects SDK" menu item in Delphi and select "Regenerate Units from RODL".
  6. Uncomment the RegisterForZeroConf() function in your _Impl file. It's named DAZeroConfTestService_Impl.pas in my test project.
  7. Copy the second parameter for the RegisterForZeroConf function, which is a string, to the clipboard. In this sample it's _DAZeroConfTestService_rosdk._tcp.
  8. Open the client form and add a TROZeroConfBrowser component to it.
  9. Paste the string you copied in step 6 into the "ServiceType" property.
  10. Set the Active property to True.
  11. Set the Domain property to "local."
  12. Add a TMemo to the form and align it where you want to. It's aligned to the top in this test.
  13. Select the TROZeroConfBrowser component again and go to the events tab in the Object Inspector.
  14. We're going to create two event handlers. One for OnServiceAdded and one for OnServiceRemoved
  15. Add code to OnServiceAdded event handler
    if aRecord.TryResolve  then  
    begin
      Memo1.Lines.Add(Format('Service resolved. Host name: %s Port number: %d', [aRecord.HostTarget, aRecord.Port]));
    end;
    
  16. Add code to OnServiceRemoved event handler
    Memo1.Lines.Add(Format('Service has gone down: %s (at host %s:%d)',
                            [aRecord.ServiceName,
                             aRecord.HostTarget,
                             aRecord.Port]));
    
  17. Compile the server and launch it
  18. When the client comes up login with the same username and password
  19. After it logs in you should see something liek the following in your memo field:
    "Service resolved. Host name: vmware-devel.local. Port number: 8099"
  20. If you close the server application the memo field should have something like the following added:
    Service has gone down: vmware-devel (at host vmware-devel.local.:8099)
  21. If you restart the server the memo field should acknoledge it resolved, and visa versa.
Now we'll get some data from the server
  1. Add a button and call it "Load Emps" and name it btnLoadEmployees
  2. Double click the button to add an event handler and add the code:
    ClientDataModule.tbl_Employees.Active := True;
  3. This isn't what I'd do in a real app of course, but for this demo we're going to have the DBGrid clear when ROZeroConf notifies us the server has gone down. To do that I'm going deactivate tbl_Employees in the OnServiceRemoved event handler like this:
    ClientDataModule.tbl_Employees.Active := False;
  4. Now we'll drop a DBGrid onto the client form and set its DataSource to ClientDataModule.ds_Employees. I've set it to align to the bottom of the form for this test.
  5. Launch the server and then launch the client. You should be promted to login of course. Once you're logged in you should see the TMemo notify you that it's connected to the server with a message like this:
    "Service resolved. Host name: vmware-devel.local. Port number: 8099"
  6. Go ahead and click the "Load Emps" button. We should see data.
  7. Close the server and you should be notified that the server is down with a message like this:
    "Service has gone down: vmware-devel (at host vmware-devel.local.:8099)"
  8. Restart the server and try to load the data. It should blow up with an EROSessionNotFound Exception.

Of course we'll want to catch that and prompt for the user to login again. Let's change the btnLoadEmployees event handler to the following:
try
  ClientDataModule.tbl_Employees.Active := True;
except
  on EROSessionNotFound do begin
    if self.Login then
      ClientDataModule.tbl_Employees.Active := True;
  end;
end;

Recompile the client and try 1-8 again. This time we handle the exception and it prompts us to login again. Once we do that the data comes back. You'll probably want to catch other exceptions here as well like EROExceptions that are raised when the server isn't available at all. But this should get you started down the right path.

The Delphi 2010 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