예제 #1
0
        private void registerWithPool(PoolMember peerListSource)
        {
            HttpResponseMessage peerListResponse;

            try {
                peerListResponse = client.GetAsync(
                    $"http://{peerListSource.uri.toStringWithPort()}/pool/peerlist",
                    peerListCancellationToken.Token).Result;
            } catch (Exception e) {
                if (e is HttpRequestException || e is AggregateException)
                {
                    Logger.logError($"Failed to connect to pool. Assuming role of seed.");
                    return;
                }
                throw;
            }

            string peerListJson = peerListResponse.Content.ReadAsStringAsync().Result;

            members = JsonConvert.DeserializeObject <List <PoolMember> >(peerListJson);
            registerNewUser(peerListSource);

            Logger.log($"{members.Count} members in pool");
            foreach (PoolMember poolMember in members)
            {
                Logger.log($"{poolMember}");

                StringContent form = new StringContent(JsonConvert.SerializeObject(memberInfo));

                HttpResponseMessage registerResponse = client.PostAsync($"http://{peerListSource.uri.toStringWithPort()}/pool/register", form).Result;
            }
        }
예제 #2
0
        public override bool Equals(object obj)
        {
            if (!(obj is PoolMember))
            {
                return(false);
            }
            PoolMember comparison = (PoolMember)obj;

            return(comparison.uri.toStringWithPort() == uri.toStringWithPort());
        }
예제 #3
0
        public Pool(Uri localUri, PoolMember poolSeed)
        {
            memberInfo = new PoolMember()
            {
                uri = localUri
            };

            if (poolSeed != null)
            {
                registerWithPool(poolSeed);
            }
        }
예제 #4
0
 public bool registerNewUser(PoolMember newMember)
 {
     if (members.Contains(newMember))
     {
         Logger.log($"{newMember} reconnected");
     }
     else
     {
         Logger.log($"{newMember} connected");
         members.Add(newMember);
     }
     return(true);
 }
예제 #5
0
        public Page requestPage(PageRequestMessage pageRequest)
        {
            pageRequest.ignorantPeerUris.Add(memberInfo.uri.toStringWithPort());

            PoolMember nonIgnorantPoolMember = members.Where(m => !pageRequest.ignorantPeerUris.Contains(m.uri.toStringWithPort())).getRandomElement();

            Page page = null;

            if (nonIgnorantPoolMember != null)
            {
                StringContent form = new StringContent(JsonConvert.SerializeObject(pageRequest.ignorantPeerUris));
                Logger.log($"Asking {nonIgnorantPoolMember.uri.toStringWithPort()} for the {pageRequest.title} page");
                HttpResponseMessage pageResponse = client.PostAsync($"http://{nonIgnorantPoolMember.uri.toStringWithPort()}/data/{pageRequest.title}", form).Result;

                string responseString = pageResponse.Content.ReadAsStringAsync().Result;
                page = JsonConvert.DeserializeObject <Page>(responseString);
            }

            return(page);
        }
예제 #6
0
        static void Main(string[] args)
        {
            ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();

            configMap.ExeConfigFilename = @"App.config";
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

            if (!config.HasFile)
            {
                Logger.logError("No App.config detected. Exiting.");
                Console.ReadLine();
                Environment.Exit(1);
            }

            DirectoryInfo localStorageDirectory = new DirectoryInfo(config.AppSettings.Settings["LocalStorageDirectory"].Value);
            Uri           localUri = new UriBuilder(config.AppSettings.Settings["LocalUri"].Value).Uri;

            Logger.log($"Local Uri: {localUri.toStringWithPort()}");

            PoolMember poolSeed = null;

            try {
                string poolSeedUriString = config.AppSettings.Settings["PoolSeed"].Value;
                Uri    poolSeedUri       = new UriBuilder(poolSeedUriString).Uri;
                Logger.log($"Pool Seed Uri: {poolSeedUri.toStringWithPort()}");
                poolSeed = new PoolMember()
                {
                    uri = poolSeedUri
                };
            } catch (Exception e) {
                Logger.log("No PoolSeed set, assuming role of pool seed");
            }


            OriginStorage originStorage = new OriginStorage();

            Pool        pool        = new Pool(localUri, poolSeed);
            PoolStorage poolStorage = new PoolStorage(pool)
            {
                backup = originStorage
            };

            DiskStorage diskStorage = new DiskStorage(localStorageDirectory)
            {
                backup = poolStorage
            };

            MemoryStorage memoryStorage = new MemoryStorage()
            {
                backup = diskStorage
            };



            Host host = new Host(
                localUri,
                new WikipediaTemplate(),
                memoryStorage
                )
            {
                pool = pool
            };


            host.run();
        }