public PuppetInstruction ParseInstruction(string line) { PuppetInstruction instruction = new PuppetInstruction (); string[] type = line.Split (' '); if (type[0].Equals ("connect")) { //if (type[1].StartsWith ("central")) //{ // return null; //} instruction.Type = PuppetInstructionType.CONNECT; instruction.ApplyToUser = type[1].Trim (); } else if (type[0].Equals ("disconnect")) { instruction.Type = PuppetInstructionType.DISCONNECT; instruction.ApplyToUser = type[1].Trim (); } else if (type[0].Equals ("readCalendar")) { instruction.Type = PuppetInstructionType.READ_CALENDAR; instruction.ApplyToUser = type[1].Trim (); } else if (type[0].Equals ("reservation")) { instruction.Type = PuppetInstructionType.RESERVATION; if (PuppetConfigurationReader.AttachReservationData(instruction, line) == false) { instruction = null; } } else if (type[0].Equals ("wait")) { instruction.Type = PuppetInstructionType.WAIT; instruction.ApplyToUser = type[1].Trim (); } else { instruction = null; } return instruction; }
public bool CommandClient(PuppetInstruction instruction) { if (!Clients.ContainsKey(instruction.ApplyToUser)) { if (instruction.Type == PuppetInstructionType.CONNECT) { if (instruction.ApplyToUser.Contains ("central")) { DebugLogic ("Starting new server: {0}", instruction.ApplyToUser); SpawnServer(instruction.ApplyToUser); // server will self-register, safe to return return true; } else { DebugLogic ("Starting a new client: {0}", instruction.ApplyToUser); SpawnClient (instruction.ApplyToUser); // client will register with puppet master when initing, safe to return return true; } } DebugLogic ("No such user is connected: {0}", instruction.ApplyToUser); return false; } else if (instruction.Type == PuppetInstructionType.DISCONNECT && instruction.ApplyToUser.Contains ("central")) { Clients.Remove (instruction.ApplyToUser); try { SpawnedClients[instruction.ApplyToUser].Kill (); SpawnedClients.Remove (instruction.ApplyToUser); return true; } catch (InvalidOperationException) { DebugLogic ("Couldn't kill server: {0}", instruction.ApplyToUser); } } Message m = new Message (); m.SetSourceUserName ("puppetmaster"); m.SetDestinationUsers (instruction.ApplyToUser); m.SetMessageType ("puppetmaster"); switch (instruction.Type) { case PuppetInstructionType.RESERVATION: m.PushString (BuildStringList (instruction.Slots)); m.PushString (BuildStringList (instruction.Users)); m.PushString (instruction.Description); m.PushString (instruction.Type.ToString ()); break; default: m.PushString (instruction.Type.ToString ()); break; } m_puppetMaster.m_sendReceiveMiddleLayer.Send (m); return true; }
private static bool AttachReservationData(PuppetInstruction instruction, string line) { // parse: "reservation {GroupMeeting; user1, user2; 13, 25 }" try { // description string[] r = line.Substring (10).Split (';'); instruction.Description = r[0].Substring(3).Trim (); // apply to user string[] data = r[1].Split (','); instruction.ApplyToUser = data[0].Trim (); // users instruction.Users = new List<string>(); instruction.Users.Add (instruction.ApplyToUser); for (int i = 1; i < data.Length; i++) { instruction.Users.Add (data[i].Trim ()); // all other users } // slots string[] slots = r[2].Substring (0,r[2].Length-1).Split (','); instruction.Slots = new List<string>(); for (int i = 0; i < slots.Length; i++) { instruction.Slots.Add (slots[i].Trim ()); } } catch (Exception) { return false; } return true; }
public void Step() { if (InstructionSet.Count > 0) { instruction = InstructionSet.Dequeue (); NotifySubscribers (String.Format ("{0} {1}", instruction.Type, instruction.ApplyToUser)); if (instruction.Type == PuppetInstructionType.WAIT) { Thread.Sleep (Int32.Parse (instruction.ApplyToUser)); return; } else { m_puppetMasterService.CommandClient (instruction); } } else { NotifySubscribers ("Instruction queue is empty."); } }