Example #1
0
        public static DaemonKey GetDaemonKey(this IFuseLauncher fuseLauncher)
        {
            //var p = _fuseLauncher.Start("daemon", new []{ "--get-key" });

            // Temporary workaround for slow startup.
            return(DaemonKey.GetDaemonKey());
        }
Example #2
0
 /// <exception cref="RequestFailed"></exception>
 /// <exception cref="TimeoutException"></exception>
 static void SendHelloRequest(
     this IMessagingService client,
     string identifier,
     TimeSpan timeout,
     DaemonKey daemonKey)
 {
     client
     .SendHelloRequestAsync(identifier, daemonKey)
     .GetResultAndUnpackExceptions(timeout)
     .OrThrow(new TimeoutException("Daemon did not respond to handshake (Timeout = " + timeout + ")"));
 }
Example #3
0
 /// <exception cref="RequestFailed"></exception>
 /// <exception cref="TimeoutException"></exception>
 static async Task <HelloResponse> SendHelloRequestAsync(
     this IMessagingService client,
     string identifier,
     DaemonKey daemonKey)
 {
     return(await client.Request(
                new HelloRequest
     {
         Identifier = identifier,
         DaemonKey = daemonKey.Serialize()
     }));
 }
        static void SendHello(string clientName, NetworkStream stream)
        {
            var helloPayload =
                "{" +
                "\"Name\": \"Hello\", " +
                "\"Id\": 0, " +
                "\"Arguments\": "
                + "{"
                + "\"Identifier\": \"" + clientName + "\","
                + "\"DaemonKey\": \"" + DaemonKey.GetDaemonKey() + "\","
                + "\"EventFilter\": \"Strict\""
                + "}" +
                "}";

            var helloPayloadBytes = Encoding.UTF8.GetBytes(helloPayload);
            var helloRequestHead  = Encoding.UTF8.GetBytes("Request\n" + helloPayloadBytes.Length + "\n");
            var helloRequest      = helloRequestHead.Concat(helloPayloadBytes).ToArray();

            stream.Write(helloRequest, 0, helloRequest.Length);

            while (true)
            {
                var reader      = new BinaryReader(stream);
                var messageType = Encoding.UTF8.GetString(reader.ReadLine());
                var lengthBytes = Encoding.UTF8.GetString(reader.ReadLine());

                int length;
                if (!int.TryParse(lengthBytes, out length))
                {
                    throw new ExitWithError("Failed to parse message length of expected Hello response.");
                }

                // We need to read all data, even if we don't care about other things than hello response.
                reader.ReadBytes(length);

                if (messageType == "Response")
                {
                    // TODO: We should probably verify that the response is a hello response.
                    break;
                }
            }
        }
Example #5
0
 public DaemonRunner(
     EnsureSingleUser ensureSingleUser,
     LocalSocketServer localSocketServer,
     bool isDebug,
     bool startServices,
     IFuseLauncher fuse,
     ServiceRunnerFactory serviceRunnerFactory,
     IReport report)
 {
     _ensureSingleUser      = ensureSingleUser;
     _runServices           = startServices;
     _fuse                  = fuse;
     _localServer           = localSocketServer;
     _isDebug               = isDebug;
     _daemonKey             = ensureSingleUser.GetDaemonKey();
     _broadcastEvents       = new Subject <IEventMessage <IEventData> >();
     _replayBroadcastEvents = _broadcastEvents.Replay(TimeSpan.FromMinutes(5));
     _pluginClients         = new PluginClients();
     _serviceRunnerFactory  = serviceRunnerFactory;
     _report                = report;
 }
Example #6
0
        void HandleHello(
            HelloRequest helloReq,
            IMessageConnection c,
            ConcurrentStack <IDisposable> disposables,
            RequestReceiver requestReceiver,
            IObservable <IMessage> messagesIn,
            IObserver <IMessage> messagesOut)
        {
            if (helloReq == null)
            {
                throw new FuseRequestErrorException(ErrorCode.InvalidData, "Expected data to not be empty.");
            }

            // TODO: Enforce daemonkey to be present in the future
            if (!string.IsNullOrWhiteSpace(helloReq.DaemonKey) &&
                DaemonKey.Deserialize(helloReq.DaemonKey) != _daemonKey)
            {
                throw new FuseRequestErrorException(
                          ErrorCode.WrongDaemonKey,
                          "Daemon key was not right, maybe because daemon possesses wrong local user.");
            }

#pragma warning disable 0618
            var pluginClient = new PluginClient(
                messagesOut,
                helloReq.Identifier,
                helloReq.Implements,
                helloReq.EventFilter);
#pragma warning restore 0618

            _pluginClients.Add(c, pluginClient);

            _report.Info("Client connected: " + helloReq.Identifier, ReportTo.LogAndUser);
            c.Disconnected.Subscribe(d => _report.Info("Client disconnected: " + helloReq.Identifier, ReportTo.LogAndUser));

            // Broadcast events to clients that wants the events.
            disposables.Push(messagesIn.OfType <IEventMessage <IEventData> >().Subscribe(_broadcastEvents.OnNext));
            disposables.Push(_broadcastEvents
                             .Subscribe(pluginClient.HandleEvent));

            // Handle Subscribe to event request.
            disposables.Push(
                messagesIn
                .OfType <IRequestMessage <UnresolvedMessagePayload> >()
                .Deserialize <SubscribeRequest>(_report)
                .Subscribe(
                    r => disposables.Push(
                        pluginClient.SubscribeToEvent(
                            r,
                            hotMessages: _broadcastEvents,
                            replayMessages: _replayBroadcastEvents))
                    ));

            disposables.Push(
                requestReceiver.SubscribeToRequest <PublishServiceRequest, PublishServiceResponse>(pluginClient.AppendSupportedRequests));

            // Send requests to clients that implements the request.
            disposables.Push(
                messagesIn.OfType <IRequestMessage <IRequestData> >()
                .Where(r => r.Name != "Subscribe" && r.Name != "Fuse.KillDaemon" && r.Name != "PublishService")
                .Subscribe(r => Task.Run(() => _pluginClients.PassRequestToAnImplementor(pluginClient, r))));

            // Handle the response from the one that implemented the request
            disposables.Push(
                messagesIn.OfType <IResponseMessage <IResponseData> >().Subscribe(pluginClient.HandleResponse));
        }
Example #7
0
 public DaemonKey GetDaemonKey()
 {
     return(DaemonKey.GetDaemonKey());
 }