Ejemplo n.º 1
0
        public static async Task <SaveWorkResult> SaveWork(SaveWork work)
        {
            await FindServer();

            if (Server == null)
            {
                return(null);
            }

            SaveWorkResult result = null;

            NetworkComms.AppendGlobalIncomingPacketHandler <SaveWorkResult>(SaveWorkResult.GetHeader(),
                                                                            (h, c, i) =>
            {
                NetworkComms.RemoveGlobalIncomingPacketHandler(SaveWorkResult.GetHeader());
                result = i;
                if (!i.Success)
                {
                    Encoder = null;
                }
            });

            await work.Send(new IPEndPoint(IPAddress.Parse(Server.IP), Server.Port));

            var start = DateTime.Now;

            while ((DateTime.Now - start).TotalSeconds < 17)
            {
                if (result != null)
                {
                    return(result);
                }
                await TaskEx.Delay(TimeSpan.FromSeconds(1));
            }

            Server = null;
            NetworkComms.RemoveGlobalIncomingPacketHandler(SaveWorkResult.GetHeader());
            return(null);
        }
Ejemplo n.º 2
0
        private static async void SaveWorkHandler(PacketHeader packetheader, Connection connection, SaveWork i)
        {
            var ip     = ((IPEndPoint)connection.ConnectionInfo.RemoteEndPoint).Address.ToString();
            var client = Client.Cache.FirstOrDefault(x => x.IP == ip);

            if (!(client?.IsEnabled ?? false))
            {
                return;
            }

            client.IsOnline      = true;
            client.LastHeartBeat = DateTime.Now;

            if (client.Encoder == null)
            {
                var res = new SaveWorkResult();
                res.Success      = false;
                res.ErrorMessage = "Request Denied";
                await res.Send(new IPEndPoint(IPAddress.Parse(client.IP), client.Port));

                return;
            }

            TerminalLog.Add(client.Id, "Enrollment item completed.");

            var req = Request.Cache.FirstOrDefault(x => string.Equals(x.StudentId, i.StudentId, StringComparison.CurrentCultureIgnoreCase));

            if (req == null)
            {
                return;
            }

            foreach (var sched in i.ClassSchedules)
            {
                var s = RequestDetail.Cache.FirstOrDefault(x => x.RequestId == req.Id && x.ScheduleId == sched.ClassId);
                if (s == null)
                {
                    continue;
                }
                var stat = Request.Statuses.Pending;
                switch (sched.EnrollmentStatus)
                {
                case ScheduleStatuses.Accepted:
                    stat = Request.Statuses.Accepted;
                    break;

                case ScheduleStatuses.Conflict:
                    stat = Request.Statuses.Conflict;
                    break;

                case ScheduleStatuses.Closed:
                    stat = Request.Statuses.Closed;
                    break;
                }
                if (stat > req.Status)
                {
                    req.Status = stat;
                }
                s.Update(nameof(s.Status), stat);
            }
            req.EncoderId = client.Encoder.Id;
            req.Save();

            client.Encoder.EndWork();

            var result = new SaveWorkResult
            {
                Success     = true,
                WorkCount   = Request.Cache.Count(x => x.Status > Request.Statuses.Proccessing && x.EncoderId == client.Encoder.Id),
                BestTime    = TimeSpan.FromSeconds(client.Encoder.BestTime).ToString(),
                AverageTime = TimeSpan.FromSeconds(client.Encoder.AverageTime).ToString()
            };

            await result.Send(new IPEndPoint(IPAddress.Parse(client.IP), client.Port));

            await SendEncoderUpdates(Client.Cache.ToList());
        }