Пример #1
0
        public HttpResponseMessage GetClients()
        {
            DataModel      db     = DataModel.get();
            ClientListData output = new ClientListData(db.getClients());

            return(Request.CreateResponse(HttpStatusCode.OK, output));
        }
Пример #2
0
        public void DoWork(uint clientPort, ref string solution)
        {
            RestClient webServer;

            shouldStop = false;

            //Connect to web server
            ClientListData updatedClients = null;

            webServer = new RestClient("https://localhost:44370/");


            while (!shouldStop)
            {
                //Attempt to grab/update client list from web server
                RestRequest   req  = new RestRequest("api/clients");
                IRestResponse resp = webServer.Get(req);
                if (resp.IsSuccessful)
                {
                    //Update client list.
                    updatedClients = JsonConvert.DeserializeObject <ClientListData>(resp.Content);
                    clients        = new List <ClientData>(updatedClients.clients);

                    //Look for a job to do.
                    IEnumerator <ClientData> enumerator = clients.GetEnumerator();
                    enumerator.MoveNext();
                    ClientData curr = enumerator.Current;

                    bool keepSearching = true;
                    while (enumerator.Current != null && keepSearching)
                    {
                        //Ignore the current client if it is the server's owning client
                        if (curr.port != clientPort)
                        {
                            try
                            {
                                //Connect to that client's server
                                NetTcpBinding tcp = new NetTcpBinding();
                                string        URL = String.Concat("net.tcp://localhost:", curr.port.ToString(), "/ClientServer");
                                ChannelFactory <JobServerInterface> jobServerFactory;
                                jobServerFactory = new ChannelFactory <JobServerInterface>(tcp, URL);
                                JobServerInterface clientServer = jobServerFactory.CreateChannel();

                                //Check list of available jobs
                                List <Job> jobs = clientServer.GetJobs();

                                //Do job if found, then stop searching.
                                Job foundJob = jobs.Find(x => x.solution == null);
                                if (foundJob != null)
                                {
                                    //do job
                                    clientServer.SubmitSolution(foundJob.localID, "testSolution");

                                    keepSearching = false;
                                }


                                /*DEBUG: Attempt to do all jobs.
                                 * for(int ii=0; ii < jobs.Count; ii++)
                                 * {
                                 *  Job foundJob = jobs.ElementAt(ii);
                                 *
                                 *  //Do job if found, then stop searching.
                                 *  if (foundJob != null && foundJob.solution == null)
                                 *  {
                                 *      //do job
                                 *      clientServer.SubmitSolution(foundJob.localID, "testSolution");
                                 *
                                 *      keepSearching = false;
                                 *  }
                                 * }
                                 */
                            }
                            catch (EndpointNotFoundException e)
                            {
                                //Server may have been closed unexpectedly, skip this client.
                            } //end try catch
                        }     //end if

                        //Progress client enumerator
                        enumerator.MoveNext();
                        curr = enumerator.Current;
                    } //end while
                }
                else  //If some problem occurs...
                {
                    //Don't update list.

                    if (clients == null) //List will be empty on first request. If an error occurred, close thread and log error.
                    {
                        shouldStop = true;
                        Console.WriteLine("ERROR: Clients list could not be retrieved after connecting to the web server.");
                    }
                }

                //Wait half a second before updating the list again and looking for a job to do.
                Thread.Sleep(500);
            }//Loop runs until Stop() is called by another thread
        }