Gorilla.Arena.Manager Unit Reference

Overview

The Gorilla.Arena.Manager unit provides TGorillaArenaManager, the component every Arena application rests on. It wraps a TTetheringManager and a TTetheringAppProfile, pairs the devices, connects their profiles, keeps the list of participants, and mirrors entities between them.

Users, Items, Myself, the user and item events and the logging come from its base class TGorillaArenaCustomManager (Gorilla.Arena.Base), which has no tethering in it. The manager is the App Tethering part and does not exist on Linux; there TGorillaArenaSession derives from the base class directly.

The tethering manager and the app profile are created in the constructor and left disabled until Join(), so dropping the component on a form starts nothing.

Class Hierarchy

TComponent
  └── TGorillaArenaCustomManager       (Gorilla.Arena.Base)
        └── TGorillaArenaManager      (not on Linux)
              └── TGorillaArenaSession

Event Types

Type Signature
TOnArenaStatusChanged procedure(const ASender : TObject; const AState : TArenaUserState) of object
TOnArenaUserEvent procedure(const ASender : TObject; AUser : IGorillaArenaUser; const AAction : TArenaUserAction) of object
TOnArenaMessage procedure(const ASender : TObject; const AMessage : String) of object
TOnArenaError procedure(const ASender : TObject; const AMessage : String) of object
TOnArenaItemEvent procedure(const ASender : TObject; AItem : IGorillaArenaEntity; const AAction : TArenaUserAction) of object

The TOnArena* types are declared in Gorilla.Arena.Base and repeated here as aliases, so code that only uses this unit keeps compiling.

Type Signature
TRemoteResourceRequestProc reference to procedure(const AManager : TGorillaArenaManager; const AProfile : TTetheringProfileInfo; const ARes : TRemoteResource)

TArenaUserState and TArenaUserAction are declared in Gorilla.Arena.User.

IArenaLifetime

GUID {2F3C8E51-6A44-4C0B-9E2D-7B15A0C4D8E9}

Tells a background task whether the manager that started it is still alive. The asynchronous resource requests hand their result back through TThread.Queue, which can run after the manager has been destroyed; the task holds a reference to this token, so it outlives the manager and can be asked whether calling back is still safe.

Member Description
Alive : Boolean The owner is still there
Terminate() Marks the owner as gone. Called from the destructor

TGorillaArenaManager

Published Properties

Property Type Description
Id String Stable identity in the group. Defaults to a fresh GUID; set it before Join(). Not what a user reads
DisplayName String The name other participants see. Free text
Target String The group to join. Changing it while joined disconnects and reconnects
AllowedAdapters String Which tethering adapters may be used, comma separated. Defaults to Network_V4
BindToAddress String The local IPv4 address to advertise. Empty lets Join() choose
OnStatusChanged TOnArenaStatusChanged None, Pending or Joined
OnUserEvent TOnArenaUserEvent Somebody was added, removed or updated
OnMessage TOnArenaMessage Log output
OnError TOnArenaError Problems in plain text; also goes to OnMessage

Public Properties

Property Type Description
Myself IGorillaArenaUser Our own user. Only assigned between Join and Leave
Tethering TTetheringManager The internal tethering manager
Profile TTetheringAppProfile The internal app profile
Joined Boolean Join() brought the manager up — with or without anybody else
Active Boolean The same, under the name that says what it means
HasPeers Boolean At least one remote manager is paired
Users TGorillaArenaUserList The participants, keyed by GUID
Items TGorillaArenaItemList The known entities

Public Methods

Method Description
Join() Connects to the configured group, creates our user, reports Pending then Joined
Leave() Leaves the group and reports None
Broadcast(const AResource, AData : String) Sends AData to the named resource on every connected profile
Greeting(const AMessage : String) A broadcast to the Greeting resource
UpdateUsers() Asks every remote profile for its user list and raises OnUserEvent with Update for each
CleanupUsers() Drops users whose profile is gone, raising OnUserEvent with Remove
SetUserClass(AUserClass : TGorillaArenaUserClass) Use a user class of your own. Call before joining
Log(const AMessage : String) Produces a log line through DoOutputMessage

Protected Methods (Extension Points)

Method Description
SetupDefaultProfile() Declare resources of your own. Called on the first Join(), never from the constructor
EnsureDefaultProfile() Runs SetupDefaultProfile once
ConnectRemoteProfiles() Connects our profile to every remote profile of our group
PickBindAddress(const ASubnets : TArray<TIPv4Subnet>) : String Picks the address to advertise on a multi-homed machine
EnsureBindAddress() Sets BindToAddress when the machine has two or more IPv4 subnets
LocalIPv4Subnets() : TArray<TIPv4Subnet> This machine's IPv4 addresses
DoOutputMessage(const AMessage : String) Where log output goes
DoErrorMessage(const AMessage : String) Reports an error as a log line and through OnError
DoStatusChanged, DoUserEvent Raise the matching events; both return at once while FDestroying
CreateMyself() : IGorillaArenaUser Builds our own user
AddRemoteUser(const AGUID : TGUID; const AProfile) : IGorillaArenaUser Builds an empty remote user
RequestRemoteResources(const AResource : String; ACallback) Blocking round trip to every connected profile
RequestRemoteResourcesAsync(const AResource : String; ACallback) The same in a background task; the callback runs on the main thread

Protected Fields

Field Type Description
FTetheringManager TTetheringManager
FAppProfile TTetheringAppProfile
FUsers TGorillaArenaUserList
FItems TGorillaArenaItemList
FProfileReady Boolean SetupDefaultProfile has run
FDestroying Boolean Set while the destructor runs; suppresses all outgoing events
FLifetime IArenaLifetime Handed to background tasks

Built-in Resources

Resource Description
UserList The participant list, requested from remote profiles
Greeting Plain string messages, see Greeting()
Item A complete entity
Update A single changed property of an entity

DispatchToResource works around the way the RTL delivers SendString and SendStream: both wrap what arrived in a temporary resource with a freshly generated name and put the sender's name in its Hint, raising only the profile-wide OnResourceReceived. Without that dispatch, every per-resource handler would be dead code for anything sent rather than mirrored.

The MULTIHOMED Defect

On a machine with two or more IPv4 subnets — and only then — the RTL multicast adapter puts the literal placeholder MULTIHOMED into its connection string instead of an address. The UDP adapter substitutes a real address per interface before a datagram leaves; the multicast adapter's BroadcastCommand does not, so the placeholder goes out on the wire. Every manager that answers then opens a TCP connection to the host MULTIHOMED, which resolves nowhere, and gives up with "no connections available". Nobody discovers anybody and no error is visible from the outside.

EnsureBindAddress sets BindToAddress, which makes the adapter leave its subnet table empty and put its real address on the wire. A machine with a single subnet never had the problem and is left alone.

Note that TGorillaArenaSession overrides PickBindAddress to return an empty string — for a broadcast based session that is the better answer. See Gorilla.Arena.Session.

Threading

The manager is not synchronized. It relies on the tethering manager delivering its events on the main thread, which is what TTetheringManager.SynchronizeEvents does and defaults to. With that switched off, every handler runs on a worker thread and guarding the user and item lists is the caller's job. The only work the manager does off the main thread by itself is RequestRemoteResourcesAsync.

See Also