Skip to content

Instantly share code, notes, and snippets.

@yallie
Last active December 15, 2015 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yallie/5300068 to your computer and use it in GitHub Desktop.
Save yallie/5300068 to your computer and use it in GitHub Desktop.
Master/slave example using Zyan.
// Compile using: csc MasterSlaveExample.cs /r:Zyan.Communication.dll
// Run master server: MasterSlaveExample.exe
// Run slave server: MasterSlaveExample.exe slave
// Run client: MasterSlaveExample.exe client
using System;
using System.Linq;
using Zyan.Communication;
public static class Program
{
// Simple master-slave example for Zyan
static void Main(string[] args)
{
foreach (var arg in args)
{
switch (arg.ToLower())
{
case "client":
Client.Start();
return;
case "slave":
SlaveServer.Start();
return;
}
}
// no arguments given, start master server by default
MasterServer.Start();
}
}
//----------------------------
// Shared code
public interface IBroadcastService
{
event Action<string, string> Broadcast;
void BroadcastMessage(string title, string message);
}
//----------------------------
// Master server
internal class BroadcastService : IBroadcastService
{
public event Action<string, string> Broadcast;
public void BroadcastMessage(string title, string message)
{
var broadcast = Broadcast;
if (broadcast != null)
{
broadcast(title, message);
}
}
}
static class MasterServer
{
public static void Start()
{
var host = new ZyanComponentHost("Master", 8765);
host.RegisterComponent<IBroadcastService, BroadcastService>();
Console.WriteLine("Master server started. Press ENTER to quit.");
Console.ReadLine();
}
}
//----------------------------
// Slave server
internal class RebroadcastService : IBroadcastService
{
public RebroadcastService(IBroadcastService masterServer)
{
MasterServer = masterServer;
MasterServer.Broadcast += (title, message) =>
BroadcastMessageToLocalSubscribers(title, message);
}
private IBroadcastService MasterServer { get; set; }
public event Action<string, string> Broadcast;
public void BroadcastMessage(string title, string message)
{
MasterServer.BroadcastMessage(title, message);
}
private void BroadcastMessageToLocalSubscribers(string title, string message)
{
var broadcast = Broadcast;
if (broadcast != null)
{
broadcast(title, message);
}
}
}
static class SlaveServer
{
public static void Start()
{
// connect to master server
var conn = new ZyanConnection("tcp://localhost:8765/Master");
var masterServer = conn.CreateProxy<IBroadcastService>();
Console.WriteLine("Connected to master server: {0}.", conn.ServerUrl);
// start slave server
var host = new ZyanComponentHost("Slave", 7654);
host.RegisterComponent<IBroadcastService, RebroadcastService>(new RebroadcastService(masterServer));
Console.WriteLine("Slave server started. Press ENTER to quit.");
Console.ReadLine();
}
}
//----------------------------
// Client
static class Client
{
public static void Start()
{
// connect to slave server
var conn = new ZyanConnection("tcp://localhost:7654/Slave");
var slaveServer = conn.CreateProxy<IBroadcastService>();
Console.WriteLine("Connected to slave server: {0}.", conn.ServerUrl);
// subscribe
slaveServer.Broadcast += (title, message) =>
{
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Received: {0}, message: {1}", title, message);
Console.ForegroundColor = oldColor;
};
// command loop
while (true)
{
Console.WriteLine("Enter a message to broadcast (empty line to quit):");
var message = Console.ReadLine();
if (string.IsNullOrEmpty(message))
{
break;
}
Console.WriteLine("Enter a title (optional):");
var title = Console.ReadLine();
if (string.IsNullOrEmpty(title))
{
title = "Untitled";
}
slaveServer.BroadcastMessage(title, message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment