public Task ViewerDisconnect(IChirperViewer viewer)
 {
     if (viewers.IsSubscribed(viewer))
     {
         viewers.Unsubscribe(viewer);
     }
     return(TaskDone.Done);
 }
Beispiel #2
0
        public async Task <bool> Run()
        {
            if (UserId == 0)
            {
                throw new ArgumentNullException("UserId", "No user UserId provided");
            }

            Console.WriteLine("ChirperClient UserId={0}", UserId);
            bool ok = true;

            try
            {
                var config = ClientConfiguration.LocalhostSilo();
                GrainClient.Initialize(config);

                IChirperAccount account = GrainClient.GrainFactory.GetGrain <IChirperAccount>(UserId);
                publisher = account;

                List <ChirperMessage> chirps = await account.GetReceivedMessages(10);

                // Process the most recent chirps received
                foreach (ChirperMessage c in chirps)
                {
                    this.NewChirpArrived(c);
                }

                if (Snapshot)
                {
                    Console.WriteLine("--- Press any key to exit ---");
                    Console.ReadKey();
                }
                else
                {
                    // ... and then subscribe to receive any new chirps
                    viewer = await GrainClient.GrainFactory.CreateObjectReference <IChirperViewer>(this);

                    if (!this.IsPublisher)
                    {
                        Console.WriteLine("Listening for new chirps...");
                    }
                    await account.ViewerConnect(viewer);

                    // Sleeps forwever, so Ctrl-C to exit
                    Thread.Sleep(-1);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("Error connecting Chirper client for user={0}. Exception:{1}", UserId, exc);
                ok = false;
            }

            return(ok);
        }
Beispiel #3
0
        private async Task Unobserve()
        {
            if (this.viewer != null)
            {
                await this.account.UnsubscribeAsync(this.viewer);

                this.viewer = null;

                Console.WriteLine($"No longer observing [{this.account.GetPrimaryKeyString()}]");
            }
        }
Beispiel #4
0
        public async Task<bool> Run()
        {
            if (UserId == 0) throw new ArgumentNullException("UserId", "No user UserId provided");

            Console.WriteLine("ChirperClient UserId={0}", UserId);
            bool ok = true;

            try 
            {
                var config = ClientConfiguration.LocalhostSilo();
                GrainClient.Initialize(config);

                IChirperAccount account = GrainClient.GrainFactory.GetGrain<IChirperAccount>(UserId);
                publisher = account;

                List<ChirperMessage> chirps = await account.GetReceivedMessages(10);

                // Process the most recent chirps received
                foreach (ChirperMessage c in chirps)
                {
                    this.NewChirpArrived(c);
                }

                if (Snapshot)
                {
                    Console.WriteLine("--- Press any key to exit ---");
                    Console.ReadKey();
                }
                else
                {
                    // ... and then subscribe to receive any new chirps
                    viewer = await GrainClient.GrainFactory.CreateObjectReference<IChirperViewer>(this);
                    if (!this.IsPublisher) Console.WriteLine("Listening for new chirps...");
                    await account.ViewerConnect(viewer);
                    // Sleeps forwever, so Ctrl-C to exit
                    Thread.Sleep(-1);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("Error connecting Chirper client for user={0}. Exception:{1}", UserId, exc);
                ok = false;
            }

            return ok;
        }
Beispiel #5
0
 public Task UnsubscribeAsync(IChirperViewer viewer)
 {
     this.viewers.Remove(viewer);
     return(Task.CompletedTask);
 }
Beispiel #6
0
 public Task SubscribeAsync(IChirperViewer viewer)
 {
     this.viewers.Add(viewer);
     return(Task.CompletedTask);
 }
Beispiel #7
0
 public static System.Threading.Tasks.Task DeleteObjectReference(IChirperViewer reference)
 {
     return global::Orleans.Runtime.GrainReference.DeleteObjectReference(reference);
 }
Beispiel #8
0
 public async static System.Threading.Tasks.Task<IChirperViewer> CreateObjectReference(IChirperViewer obj)
 {
     if (methodInvoker == null) methodInvoker = new ChirperViewerMethodInvoker();
     return ChirperViewerFactory.Cast(await global::Orleans.Runtime.GrainReference.CreateObjectReference(obj, methodInvoker));
 }
Beispiel #9
0
 public Task ViewerConnect(IChirperViewer viewer)
 {
     viewers.Subscribe(viewer);
     return(TaskDone.Done);
 }
Beispiel #10
0
        public async Task RunAsync(IClusterClient client)
        {
            this.ShowHelp(true);

            while (true)
            {
                var command = Console.ReadLine();
                if (command == "/help")
                {
                    this.ShowHelp();
                }
                else if (command == "/quit")
                {
                    return;
                }
                else if (command.StartsWith("/user "))
                {
                    var match = Regex.Match(command, @"/user (?<username>\w{1,100})");
                    if (match.Success)
                    {
                        await this.Unobserve();

                        var username = match.Groups["username"].Value;
                        this.account = client.GetGrain <IChirperAccount>(username);

                        Console.WriteLine($"The current user is now [{username}]");
                    }
                    else
                    {
                        Console.WriteLine("Invalid username. Try again or type /help for a list of commands.");
                    }
                }
                else if (command.StartsWith("/follow "))
                {
                    if (this.EnsureActiveAccount())
                    {
                        var match = Regex.Match(command, @"/follow (?<username>\w{1,100})");
                        if (match.Success)
                        {
                            var targetName = match.Groups["username"].Value;
                            await this.account.FollowUserIdAsync(targetName);

                            Console.WriteLine($"[{this.account.GetPrimaryKeyString()}] is now following [{targetName}]");
                        }
                        else
                        {
                            Console.WriteLine("Invalid target username. Try again or type /help for a list of commands.");
                        }
                    }
                }
                else if (command == "/following")
                {
                    if (this.EnsureActiveAccount())
                    {
                        (await this.account.GetFollowingListAsync())
                        .ForEach(_ => Console.WriteLine(_));
                    }
                }
                else if (command == "/followers")
                {
                    if (this.EnsureActiveAccount())
                    {
                        (await this.account.GetFollowersListAsync())
                        .ForEach(_ => Console.WriteLine(_));
                    }
                }
                else if (command == "/observe")
                {
                    if (this.EnsureActiveAccount())
                    {
                        if (this.viewer == null)
                        {
                            this.viewer = await client.CreateObjectReference <IChirperViewer>(new ChirperConsoleViewer(this.account.GetPrimaryKeyString()));
                        }

                        await this.account.SubscribeAsync(this.viewer);

                        Console.WriteLine($"Now observing [{this.account.GetPrimaryKeyString()}]");
                    }
                }
                else if (command == "/unobserve")
                {
                    if (this.EnsureActiveAccount())
                    {
                        await this.Unobserve();
                    }
                }
                else if (command.StartsWith("/unfollow "))
                {
                    if (this.EnsureActiveAccount())
                    {
                        var match = Regex.Match(command, @"/unfollow (?<username>\w{1,100})");
                        if (match.Success)
                        {
                            var targetName = match.Groups["username"].Value;
                            await this.account.UnfollowUserIdAsync(targetName);

                            Console.WriteLine($"[{this.account.GetPrimaryKeyString()}] is no longer following [{targetName}]");
                        }
                        else
                        {
                            Console.WriteLine("Invalid target username. Try again or type /help for a list of commands.");
                        }
                    }
                }
                else if (command.StartsWith("/chirp "))
                {
                    if (this.EnsureActiveAccount())
                    {
                        var match = Regex.Match(command, @"/chirp (?<message>.+)");
                        if (match.Success)
                        {
                            var message = match.Groups["message"].Value;
                            await this.account.PublishMessageAsync(message);

                            Console.WriteLine("Published the new message!");
                        }
                        else
                        {
                            Console.WriteLine("Invalid chirp. Try again or type /help for a list of commands.");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Unknown command. Type /help for list of commands.");
                }
            }
        }