public static async Task <GetWorkResult> GetNextWork(string username = "") { await FindServer(); if (Server == null) { return(new GetWorkResult(ResultCodes.Offline)); } var request = new GetWork(username); GetWorkResult result = null; NetworkComms.AppendGlobalIncomingPacketHandler <GetWorkResult>(GetWorkResult.GetHeader(), (h, c, i) => { NetworkComms.RemoveGlobalIncomingPacketHandler(GetWorkResult.GetHeader()); result = i; }); await request.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(GetWorkResult.GetHeader()); return(new GetWorkResult(ResultCodes.Timeout)); }
public static async void GetWorkHandler(PacketHeader packetheader, Connection connection, GetWork req) { var ip = ((IPEndPoint)connection.ConnectionInfo.RemoteEndPoint).Address.ToString(); var client = Client.Cache.FirstOrDefault(x => x.IP == ip); if (!(client?.IsEnabled ?? false)) { Activity.Log( Activity.Categories.Network, Activity.Types.Warning, $"Work item requested at an unauthorized terminal ({ip})."); return; } if (client.Encoder == null) { return; } client.IsOnline = true; client.LastHeartBeat = DateTime.Now; TerminalLog.Add(client.Id, "Work item requested."); var work = Request.GetNextRequest(); if (work == null) { await new GetWorkResult(ResultCodes.NotFound).Send(new IPEndPoint(IPAddress.Parse(client.IP), client.Port)); return; } //work.Update(nameof(work.Status),Request.Statuses.Proccessing); work.Status = Request.Statuses.Proccessing; var student = Models.Student.Cache.FirstOrDefault(x => x.StudentId == work.StudentId); var result = new GetWorkResult(ResultCodes.Success) { RequestId = work.Id, StudentId = work.StudentId?.ToUpper(), StudentName = $"{student?.FirstName} {student?.LastName}", Student = new Student() { Address = student.Address, BirthDate = student.BirthDate, Course = student.Course.Acronym, CourseId = student.CourseId, FirstName = student.FirstName, LastName = student.LastName, Major = student.Major, Minor = student.Minor, Male = student.Sex == Sexes.Male, Id = student.Id, Scholarship = student.Scholarship, StudentId = student.StudentId, } }; var items = RequestDetail.Cache.Where(x => x.RequestId == work.Id).ToList(); foreach (var item in items) { var sched = Models.ClassSchedule.Cache.FirstOrDefault(x => x.Id == item.ScheduleId); result.ClassSchedules.Add(new ClassSchedule() { ClassId = item.ScheduleId, SubjectCode = item.Schedule.Subject.Code, Instructor = sched?.Instructor, Room = sched?.Room, Schedule = sched?.Description, }); } await result.Send(new IPEndPoint(IPAddress.Parse(client.IP), client.Port)); await SendEncoderUpdates(Client.Cache.ToList()); client.Encoder.StartWork(); }