// Adds a rule to the mandatory rules (upload to the server) public AddRuleAdmin(ServerConnection server, int housenumber, MandatoryRules rules) { _server = server; _houseNumber = housenumber; _rules = rules; InitializeComponent(); }
// Constructor to make the adminform connect to the server // Sees if the server sends "updated", if so: updates public AdminForm(ServerConnection server, int houseNumber, UserInfo user) { InitializeComponent(); Task.Run(() => { udpClient.Client.Bind(new IPEndPoint(0, server.GetAvailableUdpPort())); int attempts = 0; string message = ""; while (true) { Thread.Sleep(200); int count = udpClient.Client.Available; byte[] msg = new byte[count]; if (msg.Length == 0 && attempts < 100) { continue; } else if (attempts >= 100) { } else if (msg.Length == 0) { continue; } if (attempts < 100) { udpClient.Client.Receive(msg); message = Encoding.Default.GetString(msg, 0, msg.Length); } if (message.Contains("Updated") || attempts >= 100) { _user.StudentsInfo = server.GetUsersInfo(_user.HouseNumber); _mandatoryRules = server.GetMandatoryRules(_user.HouseNumber); _houseRules = server.GetHouseRules(_user.HouseNumber); _complaints = server.GetComplaints(_user.HouseNumber); _messages = server.GetMessages(_user.HouseNumber); } ; attempts = 0; } }); _houseNumber = houseNumber; _server = server; _mandatoryRules = server.GetMandatoryRules(houseNumber); _houseRules = server.GetHouseRules(houseNumber); _complaints = server.GetComplaints(houseNumber); _messages = server.GetMessages(houseNumber); _user = user; UpdateTick(false); }
public void UpdateMandatoryRules(MandatoryRules mandatoryRules) { Wait(25); string json = JsonConvert.SerializeObject(mandatoryRules, Formatting.Indented); string message = GetResponce(PackageType.UPDATE_MANDATORY_RULES, PackageType.RECEIVED, json); }
public AddComplainStudent(HouseRules houseRules, MandatoryRules mandatoryRules, UserInfo user, Form form) { InitializeComponent(); this.parent = (StudentForm)form; this.houseRules = houseRules; this.mandatoryRules = mandatoryRules; this.user = user; cbAnon.Checked = false; }
// Updates the MandatoryRules private void UpdateMandatoryRulesLayout(MandatoryRules mr, bool showUpdate) { pnlMandatoryRules.SuspendLayout(); pnlMandatoryRules.Controls.Clear(); foreach (MandatoryRule rule in mr.AllRules) { AddMandatoryRule(rule, mr.AllRules.IndexOf(rule)); } pnlMandatoryRules.ResumeLayout(); }
// Constructor to make the student form connect to the server // Sees if the server sends "updated", if so: updates public StudentForm(ServerConnection server, UserInfo student) { InitializeComponent(); ThreadStart threadStart = new ThreadStart(updateTemperature); Thread t1 = new Thread(threadStart); t1.Start(); Task.Run(() => { udpClient.Client.Bind(new IPEndPoint(0, server.GetAvailableUdpPort())); int attempts = 0; string message = ""; while (true) { Thread.Sleep(100); attempts++; byte[] msg = new byte[udpClient.Client.Available]; if (msg.Length == 0 && attempts < 100) { continue; } else if (attempts >= 100) { } else if (msg.Length == 0) { continue; } if (attempts < 100) { udpClient.Client.Receive(msg); message = Encoding.Default.GetString(msg, 0, msg.Length); } if (message.Contains("Updated") || attempts >= 100) { student.StudentsInfo = server.GetUsersInfo(student.HouseNumber); mandatoryRules = server.GetMandatoryRules(student.HouseNumber); houseRules = server.GetHouseRules(student.HouseNumber); complaints = server.GetComplaints(student.HouseNumber); _messages = server.GetMessages(student.HouseNumber); if (InvokeRequired) { Invoke((MethodInvoker) delegate { RulesUpdateTick(); UpdatesTick(); ScheduleUpdate(); }); } else { RulesUpdateTick(); UpdatesTick(); ScheduleUpdate(); } } ; attempts = 0; } }); this.student = student; this.server = server; lblHello.Text = "Hello, " + this.student.Name; mandatoryRules = server.GetMandatoryRules(student.HouseNumber); houseRules = server.GetHouseRules(student.HouseNumber); complaints = server.GetComplaints(student.HouseNumber); _messages = server.GetMessages(student.HouseNumber); ScheduleUpdate(); timerUpdate.Start(); RulesUpdateTick(false); UpdatesTick(); }
//Checks if needed files/directories exist //Creates new files if it is needed private void CheckExistedFiles(int houseNumber) { //Checks if directory for this house exists if (!Directory.Exists(@$ "data/house-{houseNumber}")) { //Creates new directory Directory.CreateDirectory(@$ "data/house-{houseNumber}"); } //Checks if file for house rules exists if (!File.Exists(@$ "data/house-{houseNumber}/house-rules.json")) { //Creates a new object with zero rules HouseRules houseRules = new HouseRules { HouseNumber = houseNumber, AllRules = new List <HouseRule>() }; //Saves new file File.WriteAllText(@$ "data/house-{houseNumber}/house-rules.json", JsonConvert.SerializeObject(houseRules, Formatting.Indented)); } //Checks if file for mandatory rules exists if (!File.Exists(@$ "data/house-{houseNumber}/mandatory-rules.json")) { //Creates a new object with zero rules MandatoryRules houseRules = new MandatoryRules { HouseNumber = houseNumber, AllRules = new List <MandatoryRule>() }; //Saves new file File.WriteAllText(@$ "data/house-{houseNumber}/mandatory-rules.json", JsonConvert.SerializeObject(houseRules, Formatting.Indented)); } //Checks if file for users exists if (!File.Exists(@$ "data/house-{houseNumber}/students.json")) { //Creates a new object with zero users Users users = new Users { TotalStudentNumber = 0, AllUsers = new List <ServerUser>() }; //Saves new file File.WriteAllText(@$ "data/house-{houseNumber}/students.json", JsonConvert.SerializeObject(users, Formatting.Indented)); } //Checks if file for complaints rules exists if (!File.Exists(@$ "data/house-{houseNumber}/complaints.json")) { //Creates a new object with zero complaints Complaints complaints = new Complaints { HouseNumber = houseNumber, AllComplaints = new List <Complaint>() }; //Saves new file File.WriteAllText(@$ "data/house-{houseNumber}/complaints.json", JsonConvert.SerializeObject(complaints, Formatting.Indented)); } if (!File.Exists(@$ "data/house-{houseNumber}/messages.json")) { //Creates a new object with zero complaints ChatHistory messages = new ChatHistory { HouseNumber = houseNumber, AllMessages = new List <ChatMessage>() }; //Saves new file File.WriteAllText(@$ "data/house-{houseNumber}/messages.json", JsonConvert.SerializeObject(messages, Formatting.Indented)); } }
//Handles received packages based on their types private void HandleDataQuery(TcpClient client, ServerPackage package) { //Based on the package type returns needed information switch (package.Type) { case PackageType.USER_CHECK: { CheckUdp(package.UdpServerPort); UserCheck userCheck = JsonConvert.DeserializeObject <UserCheck>(package.Message); ServerUser user = CheckUserInfo(client, userCheck); //Checks if user with given credentials exists if (user == null) { //Sends "invalid data" error if user doesn't exist SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.INVALID_DATA, "", -1))); } else { Users users = JsonConvert.DeserializeObject <Users>(File.ReadAllText(@$ "data/house-{userCheck.HouseNumber}/students.json")); Dictionary <int, string> usersInfo = new Dictionary <int, string>(); //Adds all student's Name and ID in a dictionary foreach (var u in users.AllUsers) { usersInfo.Add(u.ID, u.Name); } //Generates new object with all needed info as a responce UserInfo userInfo = new UserInfo() { Type = user.Type, ID = user.ID, Name = user.Name, LastName = user.LastName, HouseNumber = user.HouseNumber, Room = user.Room, TotalStudentNumber = users.TotalStudentNumber, StudentsInfo = usersInfo }; string json = JsonConvert.SerializeObject(userInfo); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.USER_INFO, json, -1))); } break; } case PackageType.GET_ALL_USERS: { CheckUdp(package.UdpServerPort); Users users = JsonConvert.DeserializeObject <Users>(File.ReadAllText(@$ "data/house-{package.Message}/students.json")); Dictionary <int, string> usersInfo = new Dictionary <int, string>(); //Adds all student's Name and ID in a dictionary foreach (var u in users.AllUsers) { usersInfo.Add(u.ID, u.Name); } string json = JsonConvert.SerializeObject(usersInfo); SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.ALL_USERS, json, -1))); break; } case PackageType.GET_HOUSE_RULES: { //Checks if needed files/directories exist CheckExistedFiles(Convert.ToInt32(package.Message)); CheckUdp(package.UdpServerPort); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.HOUSE_RULES, File.ReadAllText(@$ "data/house-{package.Message}/house-rules.json"), -1))); break; } case PackageType.UPDATE_HOUSE_RULES: { HouseRules houseRules = JsonConvert.DeserializeObject <HouseRules>(package.Message); //Checks if needed files/directories exist CheckExistedFiles(Convert.ToInt32(houseRules.HouseNumber)); CheckUdp(package.UdpServerPort); //Update file File.WriteAllText(@$ "data/house-{houseRules.HouseNumber}/house-rules.json", package.Message); SendUpdated(package.UdpServerPort); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.RECEIVED, "", -1))); break; } case PackageType.GET_MANDATORY_RULES: { //Checks if needed files/directories exist CheckExistedFiles(Convert.ToInt32(package.Message)); CheckUdp(package.UdpServerPort); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.MANDATORY_RULES, File.ReadAllText(@$ "data/house-{package.Message}/mandatory-rules.json"), -1))); break; } case PackageType.UPDATE_MANDATORY_RULES: { MandatoryRules mandatoryRules = JsonConvert.DeserializeObject <MandatoryRules>(package.Message); //Checks if needed files/directories exist CheckExistedFiles(Convert.ToInt32(mandatoryRules.HouseNumber)); CheckUdp(package.UdpServerPort); SendUpdated(package.UdpServerPort); //Update file File.WriteAllText(@$ "data/house-{mandatoryRules.HouseNumber}/mandatory-rules.json", package.Message); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.RECEIVED, "", -1))); break; } case PackageType.GET_COMPLAINTS: { //Checks if needed files/directories exist CheckExistedFiles(Convert.ToInt32(package.Message)); CheckUdp(package.UdpServerPort); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.COMPLAINTS, File.ReadAllText(@$ "data/house-{package.Message}/complaints.json"), -1))); break; } case PackageType.UPDATE_COMPLAINTS: { MandatoryRules mandatoryRules = JsonConvert.DeserializeObject <MandatoryRules>(package.Message); //Checks if needed files/directories CheckExistedFiles(Convert.ToInt32(mandatoryRules.HouseNumber)); CheckUdp(package.UdpServerPort); SendUpdated(package.UdpServerPort); //Update file File.WriteAllText(@$ "data/house-{mandatoryRules.HouseNumber}/complaints.json", package.Message); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.RECEIVED, "", -1))); break; } case PackageType.GET_MESSAGES: { //Checks if needed files/directories exist CheckExistedFiles(Convert.ToInt32(package.Message)); CheckUdp(package.UdpServerPort); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.MESSAGES, File.ReadAllText(@$ "data/house-{package.Message}/messages.json"), -1))); break; } case PackageType.UPDATE_MESSAGES: { ChatHistory chathistory = JsonConvert.DeserializeObject <ChatHistory>(package.Message); //Checks if needed files/directories CheckExistedFiles(Convert.ToInt32(chathistory.HouseNumber)); CheckUdp(package.UdpServerPort); //Update file File.WriteAllText(@$ "data/house-{chathistory.HouseNumber}/messages.json", package.Message); SendUpdated(package.UdpServerPort); //Sends responce SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.RECEIVED, "", -1))); break; } case PackageType.UPDATE_PASSWORD: { CheckUdp(package.UdpServerPort); PasswordChange passwordChange = JsonConvert.DeserializeObject <PasswordChange>(package.Message); Users users = JsonConvert.DeserializeObject <Users>(File.ReadAllText(@$ "data/house-{passwordChange.HouseNumber}/students.json")); //Searches for login of the user based on his ID string login = users.AllUsers.Find(x => x.ID == passwordChange.ID).Login; UserCheck userCheck = new UserCheck(login, passwordChange.CurrentPassword, passwordChange.HouseNumber); ServerUser user = CheckUserInfo(client, userCheck); //Checks if user with given credentials exists if (user == null) { //Sends "invalid data" error if user doesn't exist SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.INVALID_DATA, "", -1))); } else { foreach (var u in users.AllUsers) { //Searches needed user and changes his password if (u.ID == user.ID) { u.Password = passwordChange.NewPassword; break; } } string json = JsonConvert.SerializeObject(users, Formatting.Indented); File.WriteAllText(@$ "data/house-{userCheck.HouseNumber}/students.json", json); SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.PASSWORD_CHANGED, "", -1))); } break; } case PackageType.CREATE_NEW_USER: { CheckUdp(package.UdpServerPort); ServerUser newUser = JsonConvert.DeserializeObject <ServerUser>(package.Message); Users users = JsonConvert.DeserializeObject <Users>(File.ReadAllText(@$ "data/house-{newUser.HouseNumber}/students.json")); int lastId = users.AllUsers.Max(x => x.ID); lastId++; newUser.ID = lastId; //If there are no users with the same login - proceed if (users.AllUsers.Any(x => x.Login == newUser.Login)) { SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.USER_EXISTS, "", -1))); } else { users.AllUsers.Add(newUser); users.TotalStudentNumber++; string json = JsonConvert.SerializeObject(users, Formatting.Indented); File.WriteAllText(@$ "data/house-{newUser.HouseNumber}/students.json", json); HouseRules houseRules = JsonConvert.DeserializeObject <HouseRules>( File.ReadAllText(@$ "data/house-{newUser.HouseNumber}/house-rules.json")); foreach (var rule in houseRules.AllRules) { if (rule.ApprovalState) { rule.StudentsApproval.Add(newUser.ID, true); } else { rule.StudentsApproval.Add(newUser.ID, false); } rule.OrderOfStudents.Add(newUser.ID); } json = JsonConvert.SerializeObject(houseRules, Formatting.Indented); File.WriteAllText(@$ "data/house-{newUser.HouseNumber}/house-rules.json", json); SendMessage(package.Type, client, JsonConvert.SerializeObject(new ServerPackage(PackageType.USER_CREATED, "", -1))); } SendUpdated(package.UdpServerPort); break; } } }