Skip to content

LiveConnectService Component

gstaas edited this page Oct 27, 2014 · 2 revisions

Table of Contents

LiveConnectService provides a simple zero-configuration communications technology between computers on a network subnet or between programs on a single computer. The underlying technology is multicast DNS (mDNS). Apple popularized this network protocol with their Bonjour product, and Bonjour is currently required to be installed in order for LiveConnectService to work. For more information about mDNS and Bonjour, see the Wikipedia article Multicast DNS. To install Bonjour, the easiest way is to download and install iTunes. Sony developers with a SHIP account can get the Bonjour installer from \NoDistro\ThirdParty\Wws.LiveConnect in the wws_atf directory.

Bonjour Overview

Bonjour allows you to send messages to a single named client or to broadcast messages to a set of clients who have subscribed to a message group, also known as a channel. During its initialization, each Bonjour instance must advertise the groups that can be broadcast to and also subscribe to groups. Advertising allows Bonjour instances to discover each other's groups. Subscribing associates a group with a callback that processes messages to that group.

LiveConnectService Component

Initialization

The LiveConnectService component's IInitializable.Initialize() method calls CommonInit() to initialize Bonjour:

protected void CommonInit()
{
    string[] groups = new string[] { AtfGlobalChannel };
    Errors error = (Errors)Client.Init(null, groups);
    if (error == Errors.LIVECONNECT_SUCCESS)
    {
        Client.Subscribe(AtfGlobalChannel, GotMessage, null);
    }
    ...

In this code segment, Client is Wws.LiveConnect.Client, which is defined in a private library and is a simple wrapper around Bonjour. The Init() method advertises the single channel AtfGlobalChannel, available to all LiveConnectService users.

Assuming Init() succeeds, it also calls Subscribe() to subscribe to that channel. LiveConnectService uses the GotMessage() callback to trigger a message received event.

LiveConnectService Methods

LiveConnectService offers a simple interface to send and receive messages:

  • void Send(string message): Broadcast a string message.
  • Send(byte[] bytes): Broadcast a byte array message.
  • SendTo(uint senderId, string message): Send a string message to a particular process that previously broadcast a message. The sender ID is from a previous message's LiveConnectMessageArgs.
  • SendTo(uint senderId, byte[] bytes): Send a byte array message to a particular process that previously broadcast a message. The sender ID is from a previous message's LiveConnectMessageArgs.
  • event EventHandler<LiveConnectMessageArgs> MessageReceived: Event for receiving broadcasted messages. This event is triggered by the GotMessage() callback that was associated with receiving messages broadcast to the channel AtfGlobalChannel. Message information is placed in a LiveConnectMessageArgs object.
The message arguments class LiveConnectMessageArgs provides the following properties and method to access message information:
  • string MessageString: Get the message as a string. It is null if the payload was not sent as a string using LiveConnectService.
  • byte[] MessageBytes: Get the message as a byte array.
  • uint SenderId: Get the sender's ID. This can be used to respond directly to this sender by calling SentTo(). This ID is typically the hash code, using FNV1Hash(), of the sender's name.
  • string SenderName: Get the computer name and port number that sent this message.
  • bool CheckMessageId(string id): Return whether or not this message's ID matches the given id parameter. This may be useful when receiving messages from non-ATF applications that are using Bonjour directly.
FNV1Hash() is one of the Fowler-Noll-Vo hash functions, which are fast to compute and have a low collision rate.

LiveConnectService Usage Examples

The AutomationService component sends and receives messages to any other LiveConnectService clients, for the purpose of testing LiveConnectService. The AutomationService component provides facilities to run an automated script. For more information, see AutomationService Component.

To send a text message, AutomationService simply calls the LiveConnectService.Send() method:

{
    s_liveConnectService.Send(msg);
}
The field s_liveConnectService contains the imported LiveConnectService object.

AutomationService subscribes to the LiveConnectService.MessageReceived event, and its handler gets the message from the LiveConnectMessageArgs, which GetLastMessage() retrieves:

s_liveConnectService = m_liveConnectService;
if (s_liveConnectService != null)
    s_liveConnectService.MessageReceived += new EventHandler<LiveConnectService.LiveConnectMessageArgs>(s_liveConnectService_MessageReceived);
...
void s_liveConnectService_MessageReceived(object sender, LiveConnectService.LiveConnectMessageArgs e)
{
    //For testing purposes we ignore messages from other machines so we don't have to worry
    //about messages from other machines interfering with a test
    if (e.SenderName.ToLower().Contains(Environment.MachineName.ToLower()))
        s_lastMessage = e.MessageString;
}
...
public string GetLastMessage()
{
    return s_lastMessage;
}
Note that the event handler s_liveConnectService_MessageReceived() filters the messages, eliminating those not originating on this computer, so that the test isn't accidentally broken by other network activity.

The ATF Timeline Editor Sample uses LiveConnectService in its TimelineEditor component to send and receive messages in a very similar way to AutomationService.

Clone this wiki locally