Esempio n. 1
0
        private static void CheckHosts()
        {
            bool done = false;

            while (!done)
            {
                int pendingCount = 0;
                lock (PendingAddresses)
                    pendingCount = PendingAddresses.Count;

                while (ActiveScanHosts.Count < MaxInteregations && pendingCount > 0)
                {
                    ActiveScanHosts.Add(new LANHost(PopAddress()));
                }

                foreach (var host in ActiveScanHosts.ToArray())
                {
                    if (host.Done)
                    {
                        ActiveScanHosts.Remove(host);

                        if (!host.Error)
                        {
                            ServiceList.AddRemoteService(host.ServiceResult);
                        }
                    }
                }

                Thread.Sleep(100);

                pendingCount = 0;
                lock (PendingAddresses)
                    pendingCount = PendingAddresses.Count;

                done = ActiveScanHosts.Count == 0 && pendingCount == 0;
            }

            WorkerThread = null;
        }
Esempio n. 2
0
        private static void CheckOnline()
        {
            if (CurrentWebClient == null)
            {
                CurrentWebClient = new WebClient();
            }


            PendingActions action = PopPending();

            while (action != PendingActions.Nothing)
            {
                switch (action)
                {
                case PendingActions.Pull:
                {
                    DataContractJsonSerializer pullSerialzier = new DataContractJsonSerializer(typeof(HostedServicesList));

                    try
                    {
                        HostedServicesList list = pullSerialzier.ReadObject(CurrentWebClient.OpenRead(DefaultServiceURL + "action=get")) as HostedServicesList;
                        if (list != null)
                        {
                            if (list.StructureVersion == 1)
                            {
                                foreach (var host in list.Services)
                                {
                                    ServiceList.AddRemoteService(host);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // ignore web exceptions, we'll get it on the next update I guess
                    }

                    ServiceList.CallUpdateNotifiation();
                }
                break;

                case PendingActions.Push:
                    try
                    {
                        DataContractJsonSerializer updateSerializer = new DataContractJsonSerializer(typeof(HostedService));

                        MemoryStream ms = new MemoryStream();
                        updateSerializer.WriteObject(ms, ServiceToUpdate);
                        ms.Close();
                        byte[] responce = CurrentWebClient.UploadData(DefaultServiceURL + "?action=update", ms.ToArray());
                        ServiceToUpdate.GlobalAccessKey = Encoding.UTF8.GetString(responce);
                    }
                    catch (Exception)
                    {
                        // ignore web exceptions, we'll get it on the next update I guess
                    }
                    break;

                case PendingActions.Remove:
                    if (ServiceToRemove == null)
                    {
                        break;
                    }
                    try
                    {
                        CurrentWebClient.UploadString(DefaultServiceURL + "?action=remove", ServiceToRemove.GlobalAccessKey);
                    }
                    catch (Exception)
                    {
                        // ignore web exceptions, it'll time out
                    }

                    lock (Pending)
                        ServiceToRemove = null;
                    break;

                case PendingActions.Nothing:
                default:
                    // should never be hit
                    break;
                }
                action = PopPending();
            }

            WorkerThread = null;
        }