static void CheckQueue() { if (queue.Count < 1) { Console.WriteLine("Queue empty"); return; } List <Slave> idleSlaves = slaves.Where(s => s.Status == SlaveStatus.Idle).ToList(); int count = idleSlaves.Count; if (count < 1) { Console.WriteLine("No idle slaves left. Queued tasks: {0}", queue.Count); return; } while (count > 0) { if (queue.Count == 0) { Console.WriteLine("Queue emptied, idle slaves: {0}", count); break; } Slave idle = idleSlaves[0]; Task task = queue[0]; if (!task.CanRun()) { Console.WriteLine("Can't run task #{0} in \"{1}\" yet, skipping", task.Index(), task.ParentProgram.Name); queue.Remove(task); queue.Insert(1, task); continue; } Console.WriteLine("Sending task #{0} in \"{1}\" to idle slave: {2} ({3})", task.Index(), task.ParentProgram.Name, idle.Name, idle.Connection.RemoteEndPoint); idle.SendTask(task); runningTasks.Add(task); idleSlaves.Remove(idle); queue.Remove(task); count -= 1; } }
static void MessageReceived(object peerObj) { NetPeer peer = (NetPeer)peerObj; NetIncomingMessage inc = peer.ReadMessage(); string data; switch (inc.MessageType) { case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine("Lidgren: {0}", inc.ReadString()); break; case NetIncomingMessageType.Data: //Console.WriteLine("Message received"); data = inc.ReadString(); //Console.WriteLine(data); if (data == "result") { Task task = runningTasks.Find(t => t.Assignee.Connection == inc.SenderConnection); task.Assignee.Free(); runningTasks.Remove(task); string result = inc.ReadString(); string type = inc.ReadString(); if (result == "error") { string reason = inc.ReadString(); Console.WriteLine("{0}: Task #{1} in \"{2}\" failed with reason: {3}", task.Assignee.Name, task.Index(), task.ParentProgram.Name, reason); } else { Console.WriteLine("{0}: Result for task #{1} in \"{2}\": {3}", task.Assignee.Name, task.Index(), task.ParentProgram.Name, result); Task wantTask = queue.Find(t => t.ParentProgram == task.ParentProgram && t.WantedTaskOutputs.Contains(task.Index())); if (wantTask != null) { Console.WriteLine("Passing result to task #{0} in \"{1}\"", wantTask.Index(), task.ParentProgram.Name); if (type == "number") { wantTask.Arguments.Add(task.Index(), new Value <int>(Int32.Parse(result))); } else if (type == "string") { wantTask.Arguments.Add(task.Index(), new Value <string>(result)); } else if (type == "bool") { wantTask.Arguments.Add(task.Index(), new Value <bool>(Boolean.Parse(result))); } else { Console.WriteLine("Invalid result type: {0} ({1})", type, result); } } } CheckQueue(); } break; case NetIncomingMessageType.DiscoveryRequest: Console.WriteLine("Discovery request from {0}", inc.SenderEndPoint.ToString()); NetOutgoingMessage outMsg = server.CreateMessage("master"); server.SendDiscoveryResponse(outMsg, inc.SenderEndPoint); break; case NetIncomingMessageType.StatusChanged: Console.WriteLine("Status of {0}: {1}", inc.SenderEndPoint.ToString(), ((NetConnectionStatus)inc.ReadByte()).ToString()); string addit = inc.ReadString(); if (!String.IsNullOrEmpty(addit)) { Console.WriteLine("Addit: {0}", addit); } break; case NetIncomingMessageType.ConnectionApproval: Console.WriteLine("Connection approval: {0}", inc.SenderEndPoint); data = inc.ReadString(); if (data == "slave") { string slaveName = inc.ReadString(); //Console.WriteLine(slaveName); Slave slave = new Slave(inc.SenderConnection, slaveName, server); slaves.Add(slave); Console.WriteLine("Slave registered: {0} ({1})", slaveName, inc.SenderEndPoint.ToString()); //inc.SenderConnection.Approve(); } else { inc.SenderConnection.Deny("Not a slave"); } inc.SenderConnection.Approve(); break; } }