private void MakeChannel() { if (_connectedChannel == null) { _listener.Start(); var tcpClient = _listener.AcceptTcpClient(); _connectedChannel = new BinaryChannel(tcpClient); _listener.Stop(); _listener = null; } }
public void Connect() { var debuggerUri = Binder.GetDebuggerUri(_port); SessionLog.WriteLine("Creating commands tcp channel"); _commandsChannel = new BinaryChannel(new TcpClient(debuggerUri.Host, debuggerUri.Port)); SessionLog.WriteLine("connected"); RunEventsListener(_commandsChannel); }
static void Main(string[] args) { log4net.Config.BasicConfigurator.Configure(); Tracer.Initialize(new TracerManager()); AppDomain.CurrentDomain.UnhandledException += (s, e) => tracer.Error(e.ExceptionObject); TaskScheduler.UnobservedTaskException += (s, e) => tracer.Error(e.Exception); try { short port = 1055; if (args.Length > 0) port = short.Parse(args[0]); ServiceRegistration.Start(port); var devices = new DeviceRegistry(); var topics = new Dictionary<string, TopicType>(); var stream = new EventStream(); var state = new SystemState(); var impulseStore = new FileImpulseStore("Store\\Impulses"); var commandStore = new FileCommandStore("Store\\Commands"); SetupTracing(stream); // Hook up event stream consumers that perform orthogonal operations. new ClockImpulses(Clock.Default).Connect(stream); new CommandToBytes().Connect(stream); new SensedToImpulse(Sensorium.Clock.Default, topics).Connect(stream); new SetSystemState(state).Connect(stream); // Hook up stores new StoreCommands(commandStore).Connect(stream); new StoreImpulses(impulseStore).Connect(stream); var brain = new Brain(stream, devices, topics, state, Clock.Default); if (File.Exists("Server.cfg")) { var setup = Setup.Read("Server.cfg", File.ReadAllText("Server.cfg")); Console.WriteLine("Applying configuration file:"); Console.WriteLine(setup.ToString(true)); foreach (var topic in setup.Topics) { topics[topic.Key] = topic.Value; } foreach (var device in setup.DeviceTypes) { devices.Register(device.Type, device.Commands.ToArray()); } foreach (var behavior in setup.Behaviors) { brain.Behave(behavior); } } var server = new ReactiveListener(port); server.Connections.Subscribe(socket => { Console.WriteLine("New socket connected {0}", socket.GetHashCode()); var binary = new BinaryChannel(socket); var message = new MessageChannel(binary); var device = new TcpDevice(brain, message, Clock.Default); connectedDevices.Add(device); device.Disconnected += (sender, e) => socket.Dispose(); socket.Disconnected += (sender, e) => { Console.WriteLine("Socket disconnected {0}", sender.GetHashCode()); connectedDevices.Remove(device); device.Dispose(); }; socket.Disposed += (sender, e) => { Console.WriteLine("Socket disposed {0}", sender.GetHashCode()); connectedDevices.Remove(device); device.Dispose(); }; }); server.Start(); Console.WriteLine("Define topic:"); Console.WriteLine(" topic [void|bool|number|string] [name]"); Console.WriteLine("Define device:"); Console.WriteLine(" device [type] [comma-separated list of topic commands the device can receive]"); Console.WriteLine("Define behavior:"); Console.WriteLine(" behave [when then expression]"); Console.WriteLine("Press Enter to exit"); string line = null; while ((line = Console.ReadLine()) != "") { if (line == Environment.NewLine) return; if (line.StartsWith("topic")) { var topic = TopicParser.Parse(line); topics[topic.Item1] = topic.Item2; Console.WriteLine("Registered topic '{0}' of type {1}", topic.Item1, topic.Item2); } else if (line.StartsWith("device")) { var device = DeviceParser.Parse(line); devices.Register(device.Item1, device.Item2.ToArray()); Console.WriteLine("Registered device type '{0}' to receive commands {1}", device.Item1, string.Join(", ", device.Item2.Select(s => "'" + s + "'"))); } else if (line.StartsWith("behave ")) { try { brain.Behave(line.Substring(7)); } catch (Exception e) { Console.WriteLine(e.Message); } } } } catch (Exception e) { Console.WriteLine("Failed: {0}", e); } }
public void Setup() { _subscriber = A.Fake <ISubscriber>(); _binaryChannel = new BinaryChannel(_subscriber); _message = new Message(); }
static void Main(string[] args) { log4net.Config.BasicConfigurator.Configure(); Tracer.Initialize(new TracerManager()); AppDomain.CurrentDomain.UnhandledException += (s, e) => Tracer.Get<Client>().Error(e.ExceptionObject); TaskScheduler.UnobservedTaskException += (s, e) => Tracer.Get<Client>().Error(e.Exception); try { var ipPort = ServiceDiscovery.DiscoverBroker(); var host = ipPort.Ip; var port = ipPort.Port; //var host = "localhost"; //var port = 1055; //if (args.Length > 0) // host = args[0]; //if (args.Length > 1) // port = int.Parse(args[1]); var client = new ReactiveClient(host, port); var binary = new BinaryChannel(client); var channel = new MessageChannel(binary); channel.Receiver.SubscribeOn(TaskPoolScheduler.Default).Subscribe( s => { Console.WriteLine("Received: {0}", s); // Report state change immediately. var topic = s as Topic; if (topic != null) { Console.WriteLine("Sending back to cause state change: {0}", topic); channel.SendAsync(topic); } }, e => Console.WriteLine(e), () => Console.WriteLine("Socket receiver completed")); Console.WriteLine("To connect, enter: connect [device id] [device type]"); Console.WriteLine("To send message once connected: [topic] = [value], where [value] can be:"); Console.WriteLine(" * a boolean (words 'true' or 'false' without quotes)"); Console.WriteLine(" * a number (a numberic value followed by the suffic 'f' denoting a floating point number)"); Console.WriteLine(" * a string (without quotes)"); Console.WriteLine("If no value is provided after the topic, then it's assumed to be a void topic (no payload needed)"); string line = null; while ((line = Console.ReadLine()) != "") { if (line.StartsWith("connect ")) { var connectInfo = line.Substring(8).Split(' '); var deviceId = connectInfo[0]; var deviceType = connectInfo[1]; Console.WriteLine("Connecting..."); try { client.ConnectAsync().Wait(); channel.SendAsync(new Connect(deviceId, deviceType)).Wait(); Console.WriteLine("Connected!"); } catch (Exception e) { Console.WriteLine("Failed to connect: {0}", e); } } else if (line == "disconnect") { if (client.IsConnected) { Console.WriteLine("Disconnecting..."); try { channel.SendAsync(new Disconnect()).Wait(); client.Disconnect(); Console.WriteLine("Disconnected!"); } catch (Exception e) { Console.WriteLine("Failed to disconnect: {0}", e); } } else { Console.WriteLine("Client was already disconnected."); } } else if (line == "r") { Console.WriteLine("Reconnecting..."); client.Disconnect(); client.ConnectAsync().Wait(); Console.WriteLine("IsConnected = {0}. Re-send Connect message.", client.IsConnected); } else { Console.WriteLine("Sending..."); if (line.IndexOf('=') == -1) { channel.SendAsync(new Topic(line.Trim())); } else { var parts = line.Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()).ToArray(); if (parts[1].Equals("true", StringComparison.OrdinalIgnoreCase)) channel.SendAsync(new Topic(parts[0], Payload.ToBytes(true))); else if (parts[1].Equals("false", StringComparison.OrdinalIgnoreCase)) channel.SendAsync(new Topic(parts[0], Payload.ToBytes(false))); else if (parts[1].EndsWith("f")) channel.SendAsync(new Topic(parts[0], Payload.ToBytes(float.Parse(parts[1].Substring(0, parts[1].Length - 1))))); else channel.SendAsync(new Topic(parts[0], Payload.ToBytes(parts[1]))); } } } } catch (Exception e) { Console.WriteLine("Failed: {0}", e); } }