Indy 10 TIdTCPServer: Server-side message push example

ServerPushExample

This example uses a Delphi 2009 VCL application with a main form, which contains only one visual component, a TMemo named “MemoLog”.

Client and server are both started in the FormCreate event.

procedure TServerPushExampleForm.FormCreate(Sender: TObject);
begin
  ExampleServer := TMyPushServer.Create;
  ExampleServer.DefaultPort := 8088;
  ExampleServer.Active := True;

  ExampleClient := TMyPushClientThread.Create('localhost', 8088,
    MemoLog.Lines);
end;

Server

The server code uses a TIdTCPServer subclass which sends the current time every second.

procedure TMyPushServer.MyOnExecute(AContext: TIdContext);
begin
  Sleep(1000);
  AContext.Connection.IOHandler.WriteLn('Server-Zeit ist: ' +
    TimeToStr(Now), IndyTextEncoding_UTF8);
end;

Client

The client code uses a TThread subclass to run asynchronously without blocking the main VCL thread. It contains a private TIdTCPClient instance, and periodically tries to receive a string from the connection.

...
  S := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);
...

Full Delphi Form Code

Below is the full code for the example main form.

unit Unit1;

interface

uses
  IdTCPServer, IdTCPClient, IdContext,
  SysUtils, Classes, Forms, StdCtrls, Controls;

type
  TMyPushClientThread = class(TThread)
  private
    TCPClient: TIdTCPClient;
    FLog: TStrings;
  public
    constructor Create(AHost: string; APort: Word; ALog:
      TStrings);
    destructor Destroy; override;
    procedure Execute; override;
  end;

  TMyPushServer = class (TIdTCPServer)
  public
    procedure InitComponent; override;
    procedure MyOnExecute(AContext: TIdContext);
  end;

  TServerPushExampleForm = class(TForm)
    MemoLog: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    ExampleClient: TMyPushClientThread;
    ExampleServer: TMyPushServer;
  end;

var
  ServerPushExampleForm: TServerPushExampleForm;

implementation

uses
  IdGlobal;

{$R *.dfm}

procedure TServerPushExampleForm.FormCreate(Sender: TObject);
begin
  IsConsole := True;

  ExampleServer := TMyPushServer.Create;
  ExampleServer.DefaultPort := 8088;
  ExampleServer.Active := True;

  ExampleClient := TMyPushClientThread.Create('localhost',
    8088, MemoLog.Lines);
end;

procedure TServerPushExampleForm.FormDestroy(Sender: TObject);
begin
  ExampleServer.Free;
  ExampleClient.Terminate;
  ExampleClient.WaitFor;
  ExampleClient.Free;
end;

{ TMyPushServer }

procedure TMyPushServer.InitComponent;
begin
  inherited;

  OnExecute := Self.MyOnExecute;
end;

procedure TMyPushServer.MyOnExecute(AContext: TIdContext);
begin
  Sleep(1000);
  AContext.Connection.IOHandler.WriteLn('Server-Zeit ist: ' +
    TimeToStr(Now), IndyTextEncoding_UTF8);
end;

{ TMyPushClientThread }

constructor TMyPushClientThread.Create(AHost: string;
  APort: Word; ALog: TStrings);
begin
  inherited Create(False);

  FLog := ALog;

  TCPClient := TIdTCPClient.Create;
  TCPClient.Host := AHost;
  TCPClient.Port := APort;
  TCPClient.ReadTimeout := 500;
end;

destructor TMyPushClientThread.Destroy;
begin
  TCPClient.Free;
  inherited;
end;

procedure TMyPushClientThread.Execute;
var
  S: string;
begin
  TCPClient.Connect;

  while not Terminated do
  begin
    S := TCPClient.IOHandler.ReadLn(IndyTextEncoding_UTF8);

    if not TCPClient.IOHandler.ReadLnTimedout then
    begin
      TThread.Queue(nil,
        procedure
        begin
          FLog.Append(S);
        end);
    end;

  end;

  TCPClient.Disconnect;
end;

end.

Apache ActiveMQ 5.9.1 released

Apache ActiveMQ 5.9.1 resolves more than 120 issues, mostly bug fixes and improvements. It has the following new features:

  • Replicated LevelDB Store for shared nothing Master/Slave.
  • Runtime Configuration
  • Hawtio web console
  • Generic JMS XA connection pool in activemq-jms-pool module (has no ActiveMQ dependency)
  • MQTT over WebSockets support
  • broker Apache Camel component
  • Broker auto-restart upon losing master status
  • AMQP Hardening
  • LevelDB Hardening

Source: http://activemq.apache.org/activemq-591-release.html

A Delphi and Free Pascal client library is available from Habarisoft.

 

Free Pascal is Sourceforge Community Choice Project of the Month

The Community Choice April Project of the Month is Free Pascal, an advanced open source compiler for Pascal and Object Pascal. Read our interview with the project’s founder. Congratulations to the Free Pascal project on being voted as the April 2014 Community Choice POTM.

AMQP, MQTT, and STOMP Messaging Protocols compared

Andy Piper / vFabric Team published an article in 2013 about three popular protocols for message broker client/server communication: Choosing Your Messaging Protocol: AMQP, MQTT, or STOMP. The STOMP specification (currently at 1.2) can be found here. For Delphi and Free Pascal developers, Habarisoft offers STOMP client libraries designed for and tested with popular open source message brokers.

Habari Client Libraries

 

 

ActiveMQ Apollo 1.7 open source message broker released

The Apache ActiveMQ Project announced the availability of Apollo 1.7, a new open source message broker.

ActiveMQ Apollo is a faster, more reliable, easier to maintain messaging broker built from the foundations of the original ActiveMQ. It accomplishes this using a radically different threading and message dispatching architecture. Like ActiveMQ, Apollo is a multi-protocol broker and supports STOMP, AMQP, MQTT, Openwire, SSL, and WebSockets.

 

Client libraries are available for many programming languages. Habarisoft offers client libraries for Delphi and Free Pascal.

Habari Client for Apollo

NetBeans IDE 8.0 released

The new release of the NetBeans IDE is out. The Release notes list many new features including Java 8 support, Java SE Embedded support, PrimeFaces code generators, TomEE support, updated JavaServer Faces Versions and PrimeFaces 4.0 library, Profiler enhancements, Support for Subversion 1.8 working copies, various enhancements to Git and Mercurial support.

NetBeans 8: JavaDoc tooltips

NetBeans IDE 8.0 Information

NetBeans IDE 8.0 provides out-of-the-box code analyzers and editors for working with the latest Java 8 technologies–Java SE 8, Java SE Embedded 8, and Java ME Embedded 8. The IDE also has a range of new enhancements that further improve its support for Maven and Java EE with PrimeFaces; new tools for HTML5, in particular for AngularJS; and improvements to PHP and C/C++ support.

NetBeans IDE 8.0 is available in English, Brazilian Portuguese, Japanese, Russian, and Simplified Chinese.

Habari Client libraries update 2014.03.u1

Habari Client libraries are Delphi and Free Pascal libraries for application integration with five popular open source message brokers. Updated versions of these libraries are now available for download by registered users.

Tested with current message broker versions

The new release has been tested with the latest message broker versions: Apache ActiveMQ 5.9.0, Apollo 1.6, HornetQ 2.4.0.Final, Open Message Queue 5.0.1 and RabbitMQ 3.2.4

Bug fixes and improvements

  • Negotiation of heart-beat negotiation according STOMP spec (bug id 988)
  • Log CONNECT frame heart-beat intervals (bug id 984)
  • TBinaryMessageTestCase is registered in wrong test suite (bug id 983)
  • Log4D crashes with Free Pascal (bug id 977)
  • Use Id_SOL_* constants in Indy communication adapter (bug id 973)
  • Producer flow control for throughput demo application (bug id 925)
  • Equal signs in message property keys and values fail (bug id 924)

Habari Client Libraries