Esempio n. 1
0
        public async Task <SendMessageResponse <TResponse> > SendAsync <TResponse>(
            IEnumerable <string> destinations,
            string text,
            SendOptions options = null)
        {
            options = options ?? new SendOptions();

            var readyClientIds = this.GetReadyClientIds();

            destinations = await this.DefaultClient.ProcessDestinationManyAsync(destinations);

            try
            {
                var responseChannel = Channel.CreateBounded <TResponse>(1);
                var timeoutChannel  = Channel.CreateBounded <TResponse>(1);

                var resultTasks = readyClientIds.Select(clientId => this.SendWithClientAsync(
                                                            clientId,
                                                            destinations,
                                                            text,
                                                            options,
                                                            responseChannel,
                                                            timeoutChannel));

                return(await HandleSendMessageTasksAsync(resultTasks, options, responseChannel, timeoutChannel));
            }
            catch (Exception e)
            {
                return(new SendMessageResponse <TResponse> {
                    ErrorMessage = "failed to send with any client"
                });
            }
        }
Esempio n. 2
0
        public SendMessageResponse <TResponse> SendAsync <TResponse>(
            string destination,
            byte[] data,
            SendOptions options = null)
        {
            options = options ?? new SendOptions();

            var readyClientIds = this.GetReadyClientIds();

            destination = this.DefaultClient.ProcessDestinationAsync(destination);

            try
            {
                var responseChannel = Channel.CreateBounded <TResponse>(1);
                var timeoutChannel  = Channel.CreateBounded <TResponse>(1);

                var resultTasks = readyClientIds.Select(clientId => this.SendWithClientAsync(
                                                            clientId,
                                                            destination,
                                                            data,
                                                            options,
                                                            responseChannel,
                                                            timeoutChannel));

                return(HandleSendMessageTasksAsync(resultTasks, options, responseChannel, timeoutChannel));
            }
            catch (Exception e)
            {
                return(new SendMessageResponse <TResponse> {
                    ErrorMessage = "failed to send with any client"
                });
            }
        }
Esempio n. 3
0
        public Task <string> SubscribeAsync(
            string topic,
            int duration,
            string identifier,
            string meta,
            TransactionOptions options = null)
        {
            options = options ?? new TransactionOptions();

            return(RpcClient.Subscribe(topic, duration, identifier, meta, this, options));
        }
Esempio n. 4
0
        public async Task <SendMessageResponse <byte[]> > PublishAsync(
            string topic,
            byte[] data,
            PublishOptions options = null)
        {
            options         = options ?? new PublishOptions();
            options.NoReply = true;

            var subscribers = await this.GetAllSubscribersAsync(topic, options);

            return(await this.SendAsync <byte[]>(subscribers.ToList(), data));
        }
Esempio n. 5
0
        public SendMessageResponse <byte[]> PublishAsync(
            string topic,
            string text,
            PublishOptions options = null)
        {
            options         = options ?? new PublishOptions();
            options.NoReply = true;

            var subscribers = this.GetAllSubscribersAsync(topic, options);

            return(this.SendAsync <byte[]>(subscribers.ToList(), text));
        }
Esempio n. 6
0
        /// <summary>
        /// Dial a session to a remote NKN address.
        /// </summary>
        /// <param name="remoteAddress">The address to open session with</param>
        /// <param name="options">Session configuration options</param>
        /// <returns>The session object</returns>
        public async Task <Session> DialAsync(string remoteAddress, SessionOptions options = null)
        {
            options = options ?? new SessionOptions();

            var dialTimeout = Ncp.Constants.DefaultInitialRetransmissionTimeout;

            var sessionId = PseudoRandom.RandomBytesAsHexString(Constants.SessionIdLength);

            var session = this.MakeSession(remoteAddress, sessionId, options);

            var sessionKey = Session.MakeKey(remoteAddress, sessionId);

            this.sessions.Add(sessionKey, session);

            await session.DialAsync(dialTimeout);

            return(session);
        }
Esempio n. 7
0
        public MultiClient(MultiClientOptions options = null)
        {
            options = options ?? new MultiClientOptions();

            options.Identifier = options.Identifier ?? "";

            this.options = options;

            this.InitializeClients();

            this.key     = this.DefaultClient.Key;
            this.Address = (string.IsNullOrWhiteSpace(options.Identifier) ? "" : options.Identifier + ".") + this.key.PublicKey;

            this.messageHandlers   = new List <Func <MessageHandlerRequest, Task <object> > >();
            this.sessionHandlers   = new List <Func <Session, Task> >();
            this.acceptedAddresses = new List <Regex>();
            this.sessions          = new ConcurrentDictionary <string, Session>();
            this.messageCache      = new MemoryCache("messageCache");

            this.isReady  = false;
            this.isClosed = false;
        }
Esempio n. 8
0
        public async Task <GetSubscribersResult> GetSubscribersAsync(string topic, PublishOptions options = null)
        {
            options = options ?? new PublishOptions();

            foreach (var clientId in this.Clients.Keys)
            {
                if (string.IsNullOrEmpty(this.Clients[clientId].Wallet.Options.RpcServerAddress) == false)
                {
                    try
                    {
                        var mergedOptions = this.Clients[clientId].Wallet.Options.MergeWith(options);
                        return(await Wallet.Wallet.GetSubscribersAsync(topic, mergedOptions));
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            var walletOptions = WalletOptions.NewFrom(this.options).AssignFrom(options);

            return(await Wallet.Wallet.GetSubscribersAsync(topic, walletOptions));
        }
Esempio n. 9
0
        public Task <string> UnsubscribeAsync(string topic, string identifier, TransactionOptions options = null)
        {
            options = options ?? new TransactionOptions();

            return(RpcClient.Unsubscribe(topic, identifier, this, options));
        }
Esempio n. 10
0
        public Task <string> DeleteNameAsync(string name, TransactionOptions options = null)
        {
            options = options ?? new TransactionOptions();

            return(RpcClient.DeleteName(name, this, options));
        }
Esempio n. 11
0
        public Task <string> TransferToAsync(string toAddress, decimal amount, TransactionOptions options = null)
        {
            options = options ?? new TransactionOptions();

            return(RpcClient.TransferTo(toAddress, new Amount(amount), this, options));
        }
Esempio n. 12
0
        public string RegisterNameAsync(string name, TransactionOptions options = null)
        {
            options = options ?? new TransactionOptions();

            return(RpcClient.RegisterName(name, this, options));
        }