[fpc-pascal] Pascal Actor Model

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Mon Jul 25 01:51:17 CEST 2011


V2 Release of Pascal Actor Model is done.

I am now im the process of adding UDP message streaming support.

This will allow multithreading to include threads running in other
computers on the same broadcast domain.

Actor is a thread of sorts (it IS a thread but with some specific
semantic rules that comprises the actor model :
http://en.wikipedia.org/wiki/Actor_model)

Messages are objects sent from an actor to another.

In a program hosted in a single computer the messages are objects sent
to/from actors.

When the message needs to jump from a computer to another, the message
object is streamed into a string and sent via UDP, destreamed at
target and injected in the correct actor message queue.

The main program can send/receive messages too (via the mainthreadqueue).

Example program :

Uses
	{$IFDEF UNIX}
	CThreads,
	{$ENDIF}
	Classes,
	SysUtils,
	Actors,
	ActorMessages,
	ActorLogger,
	CustomActors;

Type
	TScreenMessage = Class(TCustomStringActorMessage);

	TScreenWriterActor = Class(TActorThread)
	Public
		Procedure ScreenWrite(Var aMessage); Message 'tscreenmessage';
	End;

Procedure TScreenWriterActor.ScreenWrite(Var aMessage);
Var
	lMessage : TScreenMessage;
Begin
	lMessage := UnbundleMessage(aMessage) As TScreenMessage;
	WriteLn(ActorName, ': ', lMessage.Data);
End;

Var
	gBuffer : String;
	gScreenMessage : TScreenMessage;

Begin
	DefaultActorMessageTTL := 1000000; // things are rough at startup,
lets make messages float around forever
	Actors.Init('localhost', 'switchboard');
	ActorLogger.Init;
	CustomActors.Init;
	ActorMessageClassFactory.RegisterMessage(TScreenMessage);
	RegisterActorClass(TScreenWriterActor);
	StartActorInstance('TScreenWriterActor', 'screen1');
	StartActorInstance('TScreenWriterActor', 'screen2');
	StartActorInstance('TScreenWriterActor', 'screen3');
	StartActorInstance('TLoadBalancerActor', 'screen');
	AddTargetToActor('screen', 'screen1');
	AddTargetToActor('screen', 'screen2');
	AddTargetToActor('screen', 'screen3');
	DefaultActorMessageTTL := 5; // everything should be quick from now on.
	Repeat
		Write('Input something : '); ReadLn(gBuffer);
		If gBuffer <> 'quit' Then
		Begin
			gScreenMessage := TScreenMessage.Create('localhost', 'screen');
			gScreenMessage.Data := gBuffer;
			Switchboard.Mailbox.Push(gScreenMessage);
		End;
	Until gBuffer = 'quit';
	CustomActors.Fini;
	ActorLogger.Fini;
	Actors.Fini;
End.

Project is stored at :

https://code.google.com/p/pascal-actor-model/



More information about the fpc-pascal mailing list