Delphi Labs: DataSnap XE - Testing Servers in Data Explorer

By: Pawel Glowacki

Abstract: In this article we are going to use Delphi XE to build a DataSnap standalone server listening on multiple TCP/IP and HTTP ports. Instead of building a client for it, we are going to test its functionality in Data Explorer that is part of the RAD Studio IDE.

    Introduction

The beauty of rapid application development is that you can build complex systems by assembling components. This approach to building software applications shines when applied to Delphi DataSnap programming. In a matter of minutes you can create a multithreaded server application, capable of servicing many concurrent clients and listening on multiple communication ports simultaneously.

In this lab exercise we are going to use Delphi XE to build a simple DataSnap server with multiple transport components and test its functionality inside the Data Explorer.

    Building the Server

Click on the “File – New – Other” menu and inside the “New Items” dialog double-click on the “DataSnap Server” wizard in “Delphi Projects – DataSnap Server” category to create a standalone Delphi DataSnap server application.

Hide image
DataSnapServerWiz

In the first tab of the wizard keep the default project type “VCL Forms Application” and on the second tab make sure to check “HTTP” as the communication protocol and keep all other default selections.

Hide image
Click to see full-sized image

On the next screen make sure that the selected TCP/IP port 212 is open and HTTP port 8080 is open as well.

Hide image
Click to see full-sized image

On the last screen of the “DataSnap Server” wizard dialog keep the default server methods ancestor class and click on “Finish”.

Select “File – Save All” to save all the files in the new project generated by the wizard. In my case I am going to create a new “C:\DataSnapLabs\TestingServerInDataExplorer\” folder, give the main unit of the project “FormServerUnit” name, keep default names for other units (typically “ServerMethodsUnit1” and “ServerContainerUnit1”) and name project “TestingServer”.

You can optionally modify the implementation of “Echo” and “ReverseString” server methods generated by the wizard.

I want my echoing implementation the enterprise strength, so I am going to return the provided string not once, but twice!

Here is my implementation of the “Echo” method inside the server methods unit.

function TServerMethods2.EchoString(Value: string): string;
begin
  Result := 'Echoing ' + Value + ' ... ' + Value;
end;

In the Project Manager double-click on the server container unit. The wizard generated one “DSServer1” component, one “DSServerClass1” component and two transport components: “DSTCPServerTransport1” and “DSHTTPService1”.

In DataSnap server architecture there is always one central “TDSServer” instance, a number of “TDSServerClass” components representing different server methods classes available for clients and a number of transport components representing connectivity on different protocols and ports. The wizard has generated one component for listening on the TCP port 212 and one for listening on the HTTP port 8080.

In this demo we are going to add to the server an additional port for listening on client requests.

Press “F6” to display “IDE Insight”, start typing “TDSTCPServerTransport” and add this component to the form.

Hide image
DataSnapDelphiXETestingServer1ServerContainer

Select the “DSTCPServerTransport2” component and modify its properties inside the Object Inspector.

Set the “Port” value to “212” and set “Server” property to point to “DSServer1” component.

Hide image
TCPStarterProps

Save All.

Our server is now ready for testing!

Select “Run – Run Without Debugging” and minimize the server when it is running. The server has to be running in order to test its functionality.

    Testing in Data Explorer

Locate “Data Explorer” tab in the bottom of the “Project Manager” window in the default layout or select “View – Data Explorer” from the menu.

Right-click on “DATASNAP” node in the “Data Explorer” and select “Add New Connection”.

Right-click on the new connection node and select “Rename Connection”.

Enter “TEST_TCP_211” as the name of the new connection.

Right-click on the connection and select “Modify Connection”.

In the dialog select “tcp/ip” as the protocol, enter “127.0.0.1” as host and “211” as port.

Click on the “Test Connection” button to verify that connection works.

Hide image
Click to see full-sized image

Close the “Modify Connection” dialog and add two more DataSnap connections in Data Explorer. One named “TEST_TCP_212” and one “TEST_HTTP_8080”. Modify connection properties accordingly and test connections.

Hide image
threetestconns

Expand one of the connection nodes.

Note that from the client perspective a connection to a DataSnap server looks very much like a connection to a database, and calling DataSnap server methods looks very much like calling database stored procedures!

Trying to expand “Tables” and “Views” nodes will result in an error messages, because they do not make sense in the context of a DataSnap server.

When you expand “Procedures” nodes you should see a long list of server methods belonging to different server classes. Note that there are two built-in server classes – “DSAdmin” and “DSMetadata” – that contain many server methods.

Hide image
Click to see full-sized image

At the bottom of the list there are two methods implemented by our server methods class: “Echo” and “EchoString”.

Right-click on the “Echo” method and select “View Parameters”.

In the “Value” field enter any string you want to echo. I have entered “Delphi”. In the top left corner of the form press “Execute” button and in the bottom of the form you should see the return value: “Echoing Delphi … Delphi”.

Hide image
viewparams

That’s it!

    Summary

In this “Delphi Labs” episode we have looked into testing DataSnap servers in “Data Explorer”, which is part of Delphi and RAD Studio.

DataSnap servers are inherently multithreaded and they can listen simultaneously on different ports using different communication protocols.

In this example we have built a test server that listens on two TCP/IP and one HTTP port.

That’s the power of rapid application development (“RAD”) and Delphi DataSnap. Take few components, connect them together, adjust few properties and you are ready with the complex serverside architecture in a matter of minutes!

The source code for this article is available at the Code Central (http://cc.embarcadero.com/item/28213) and the video version of this demo is available on YouTube (http://www.youtube.com/watch?v=KbRjTb8NHQ8).

More information about "Delphi Labs" is available at http://blogs.embarcadero.com/pawelglowacki

Server Response from: ETNASC04