public void AddDeviceConnection(Connection c, IDevice2 dev) { _connections.Add(c); dev.AddConnection(c); _connectionDeviceMap[c] = dev; c.MessageReceivedEvent += new PUC.Communication.Connection.MessageReceivedHandler(DeviceMessageHandler); c.ConnectionLostEvent += new PUC.Communication.Connection.ConnectionLostHandler(ConnectionLostHandler); }
private void ConnectionLostHandler(ConnectionEventArgs args) { Connection c = args.GetConnection(); IDevice2 dev = (IDevice2)_connectionDeviceMap[c]; c.Stop(); _connections.Remove(c); _connectionDeviceMap.Remove(c); if (dev != null) { dev.RemoveConnection(c); } }
protected void serverStateChanged(bool added, IDevice2 dev) { if (_discoveryServiceRunning) { Message msg = new ServerInformation(Name, _devices.GetEnumerator()); for (int i = 0; i < _connections.Count; i++) { Connection c = (Connection)_connections[i]; if (!c.IsConnected()) { _connections.RemoveAt(i); i--; continue; } if (c.LocalPort == SERVER_INFO_PORT) { c.Send(msg); } } } else { if (dev == null) { return; } Message msg; if (added) { msg = new RegisterDevice(dev.Name, dev.Port); } else { msg = new UnregisterDevice(dev.Port); } try { _discoveryConnection.Send(msg); } catch (Exception) { // problem sending message to discovery service } } }
public void StopListener(IDevice2 dev) { Listener l = findListener(dev.Port); if (l == null) { return; } _listeners.Remove(l); l.Stop(); serverStateChanged(false, dev); }
public void RemoveDevice(IDevice2 device) { if (device.IsRunning()) { StopListener(device); IEnumerator e = _connections.GetEnumerator(); while (e.MoveNext()) { Connection c = (Connection)e.Current; if (c.LocalPort == device.Port) { c.Stop(); } } } _devices.Remove(device); }
public void StartListener(IDevice2 dev) { if (findListener(dev.Port) != null) { return; } try { Listener l = new DeviceListener(this, dev); l.Start(); _listeners.Add(l); serverStateChanged(true, dev); } catch (Exception e) { MessageBox.Show("Another appliance is already running on this port.", e.GetType().Name); } }
private void _discoveryConnection_ConnectionRegainedEvent(ConnectionEventArgs args) { if (!_discoveryServiceRunning) { try { IEnumerator e = _devices.GetEnumerator(); while (e.MoveNext()) { IDevice2 dev = (IDevice2)e.Current; Message msg = new RegisterDevice(dev.Name, dev.Port); _discoveryConnection.Send(msg); } } catch (Exception) { // do nothing here...wait for another opportunity // to re-connect } } }
private void DeviceMessageHandler(ConnectionEventArgs args) { Message msg = args.GetMessage(); if (msg == null) { return; } Connection c = args.GetConnection(); IDevice2 dev = (IDevice2)_connectionDeviceMap[c]; Debug.Assert(dev != null); if (msg is ServerInformationRequest) { c.Send(new ServerInformation(Name, _devices.GetEnumerator())); } else { dev.HandleMessage(c, msg); } }
public static IDevice2 CreateRef(this IDevice2 objectRef) => ((IDevice2)objectRef.CreateRef(typeof(IDevice2)));
/* * Constructor */ public DeviceListener(PUCServer server, IDevice2 device) : base(server, device.Port) { _device = device; }
/* * Member Methods */ public void AddDevice(IDevice2 device) { _devices.Add(device); }
private void _applianceTable_ColumnChanging(object sender, DataColumnChangeEventArgs e) { if (e.Column.ColumnName == "DeviceObject") { return; } IDevice2 dev = (IDevice2)e.Row["DeviceObject"]; switch (e.Column.ColumnName) { case "Port": if (dev.Port != (int)e.ProposedValue) { if (dev.IsRunning()) { if (_server.IsListenerOnPort((int)e.ProposedValue)) { e.Row.RejectChanges(); } else { dev.Stop(); _server.StopListener(dev); dev.Port = (int)e.ProposedValue; dev.Start(); _server.StartListener(dev); } } else { dev.Port = (int)e.ProposedValue; } } break; case "Active": if ((bool)e.ProposedValue) { try { dev.Start(); if (dev.IsRunning()) { _server.StartListener(dev); } else { e.ProposedValue = false; } } catch (Exception) { dev.Stop(); _server.StopListener(dev); } } else { dev.Stop(); _server.StopListener(dev); } break; case "Visible": ((DebugServerFrame)dev).Visible = (bool)e.ProposedValue; break; } }