예제 #1
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
            {
                Orleans.GrainClient.Initialize();

                IChirperAccount account = ChirperAccountFactory.GetGrain(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 ChirperViewerFactory.CreateObjectReference(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);
        }
예제 #2
0
        private async Task AddChirperUser(ChirperUserInfo userData)
        {
            // Create Chirper account grain for this user
            long            userId    = userData.UserId;
            string          userAlias = userData.UserAlias;
            IChirperAccount grain     = ChirperAccountFactory.GetGrain(userId);

            this.Users[userId] = grain;
            try
            {
                // Force creation of the grain by sending it a first message to set user alias
                ChirperUserInfo userInfo = ChirperUserInfo.GetUserInfo(userId, userAlias);
                await grain.SetUserDetails(userInfo);

                Interlocked.Increment(ref numUsers);
            }
            catch (Exception exc)
            {
                ReportError("Error creating user id " + userId, exc);
                throw exc.GetBaseException();
            }
        }