Personal tools
You are here: Home Tutorials Announcer v1.0 Tutorial #2: Service monitoring and IPC
Document Actions

Announcer v1.0 Tutorial #2: Service monitoring and IPC

Add to MyNewgie

In this tutorial, we will create a simple example of interprocess communication using the Announcer component to create an application to monitor services running in separate machines on a local server.

Click here to download the sample project files

You may download a 15-day trial version of the Announcer v1.0 component here, to test this project.

The Announcer component simplifies network communications by automatically handling all aspects of communication, including connection, disconnection, and data integrity, while providing an event-driven communications model: events are raised by the component when it receives data, and you can respond right away, without the need to constantly poll for incoming data. The Announcer component also removes the need to know addresses of hosts, just requiring that Announcer Clients know a Service Identifier string, which they will use to automatically detect an Announcer Server instance on the local network.

The Announcer component is particularly suited to Windows Services, because of its small memory footprint and low CPU utilization, enabling you to interconnect your services, without putting a high load on your host computer.

Let's start by network-enabling our service:

Protected Overrides Sub OnStart(ByVal args() As String)
' Create instance of the Announcer Client
serviceconn = New foxtrot.xray.Announcer.Client("ServiceMonitor1.0")
' Start service process thread
process = New System.Threading.Thread(AddressOf DoProcess)
process.Start()
End Sub

We created an instance of the Announcer Client, and provided a Service Identifier string, which the Client will use to look for an available Announcer Server. Connection, and reconnection is automatic past this point.

Our test service does only one thing: it adds random numbers to two counters, which we call TransactionCountA and TransactionCountB. If the Announcer Client is connected, the service sends the counters, along with the hostname and assembly version to the Announcer Server.

Private Sub DoProcess()
Try
While (True)
' Simulate processing being done, and increment
' counters with random values
System.Threading.Thread.Sleep(4000)
intTransactionCountA += RandomNumber(10)
intTransactionCountB += RandomNumber(25)
' If the Service Client is connected, send counter data
If (serviceconn.Connected = True) Then
' Create a Hasthable to populate with data
Dim servdata As New Hashtable
servdata("hostname") = host
servdata("version") = version
servdata("TransactionA") = intTransactionCountA
servdata("TransactionB") = intTransactionCountB
' Send the Hashtable
serviceconn.Send(servdata)
End If
End While
Catch ex As System.Threading.ThreadAbortException
End Try
End Sub

Our service is now sending its counters to the monitor! Now, on to the monitor application:

Private Sub Monitor_Load(ByVal sender As System.Object, _ 
ByVal e As System.EventArgs) Handles MyBase.Load
' Create instance of the Announcer Server
monitorconn = New foxtrot.xray.Announcer.Server("ServiceMonitor1.0", 5678, 5)
End Sub

We created our Announcer Server instance, assigned it a Service Identifier, a port, and a TTL value. Now, we will add a handler for the data reception event, and receive all monitor data sent by running services, which will be displayed by the Monitor application. This handler will update the screen with the data from every message sent by the clients.

Private Sub monitorconn_DataReceived(ByVal message As foxtrot.xray.Announcer.Server.ClientMessage) _
Handles monitorconn.DataReceived
Dim recvdata As New Hashtable
' Display the amount of connected services
lblTotalServers.Text = monitorconn.ClientCount
message.GetData(recvdata)
' Display service information from the Hashtable
End Sub

In this particular case, we do not need to send any data back to the client, but if we did, we could use the Send() method of the message object. We will now add handlers for the disconnection event, to update the screen whenever clients disconnect from the server, and we will add a handler for the Closing event of our monitor form, to close all current connections and release all resources.

Private Sub monitorconn_ClientDisconnected(ByVal client As _
foxtrot.xray.Announcer.Server.ClientConnection) _
Handles monitorconn.ClientDisconnected
' Update the amount of connected services
lblTotalServers.Text = monitorconn.ClientCount
' services is a Hashtable that contains the list of services with their
' information, to calculate totals. Note the use of the ClientID property,
' which contains a unique identifier for an Announcer Client connection
If (services.ContainsKey(client.ClientID) = True) Then
' Update the monitor screen
End If
End Sub

Private Sub Monitor_Closing(ByVal sender As Object, ByVal e _
As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
' Close all connections, and release all resources
monitorconn.Close()
End Sub

The Announcer component can quickly interface your applications on a local network, with few lines of code. Use it wherever you need a reliable message-passing infrastructure that can manage itself without requiring too much attention from your code.


 

2Checkout.com, Inc. is an authorized retailer of goods and services provided by foxtrot_xray:software.

Powered by Plone, the Open Source Content Management System