Skip to content

Instantly share code, notes, and snippets.

@chrisforbes
Created October 21, 2009 02:08
Show Gist options
  • Save chrisforbes/214787 to your computer and use it in GitHub Desktop.
Save chrisforbes/214787 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Farmworks.Utils;
using Farmworks.Gateway.Client;
namespace farmnet_soil
{
class DualMaster : Device
{
enum State { Unlocked, Locking, Locked, Unlocking };
int passiveIO;
State state;
Action onLock;
List<IDevice> devs = new List<IDevice>();
public IEnumerable<IDevice> Devices { get { return devs; } }
public IDisposable AcquireWithIO(Action a, int ioForLease)
{
if (state != State.Unlocked) return null;
onLock = a;
desiredIo = ioForLease;
state = State.Locking;
Console.WriteLine("{0}: Lock started, waiting for IO to reach active state...", Name);
return new Lease(
() => {
state = State.Unlocking;
desiredIo = passiveIO;
onLock = null;
Console.WriteLine("{0} Unlock started, waiting for IO to return to passive state...", Name);
});
}
public DualMaster(string name, int passiveIO, IniFile f)
: base(name, 0, (3<<6), 0, f)
{
this.passiveIO = passiveIO;
this.desiredIo = passiveIO;
ShouldDumpIo = false;
}
public void AddSlave(IDevice d) { devs.Add(d); }
public override void Joined(FarmnetClientApp app)
{
base.Joined(app);
foreach (var d in devs) d.Joined(app);
}
public override void OnMessage(FarmnetClientApp app, MessageType type, byte[] content)
{
base.OnMessage(app, type, content);
foreach (var d in devs) d.OnMessage(app, type, content);
}
public override void Tick(FarmnetClientApp app)
{
base.Tick(app);
if (state == State.Locking && !IsIoSynced())
{
state = State.Locked;
Console.WriteLine("{0} Active IO state reached. Lock complete.", Name);
onLock();
}
if (state == State.Unlocking && !IsIoSynced())
{
state = State.Unlocked;
Console.WriteLine("{0} Passive IO state reached. Unlock complete.", Name);
}
foreach (var d in devs) d.Tick(app);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment