/* --------CONNECTION FUNCTIONS---------------------*/ private void feedback(ref Protocol.Request response) { setThreadedStatusLabel("Managing response"); setThreadedButton(true); if (response.header.req_type == Protocol.reqType.req_res) { //some error responses Protocol.RData_Response ans_data = (Protocol.RData_Response)Marshal.PtrToStructure(response.data, typeof(Protocol.RData_Response)); if (ans_data.res_type == Protocol.resType.res_ok) { if (ans_data.id == -1) { setThreadedStatusLabel("Connected succesfully"); setThreadedSendButton(true); } else //file accepted { client.tasksProgress.Add(ans_data.id); setThreadedTasksProgress(); setThreadedStatusLabel("File accepted"); setThreadedReceiveButton(true); } } else if (ans_data.res_type == Protocol.resType.res_empty) { setThreadedStatusLabel("No files to download"); } else if (ans_data.res_type == Protocol.resType.res_fail) { setThreadedStatusLabel("Operation failed on server"); } else if (ans_data.res_type == Protocol.resType.res_full) { setThreadedStatusLabel("Server is busy. Try again later"); } else { //other srver response setThreadedStatusLabel("Unsupported request type"); } } else if (response.header.req_type == Protocol.reqType.req_snd) { //there is a file Protocol.RData_File ans_data = (Protocol.RData_File)Marshal.PtrToStructure(response.data, typeof(Protocol.RData_File)); taskDone(ref ans_data); } else { //other srver response setThreadedStatusLabel("Unsupported request type"); } }
private void taskDone(ref Protocol.RData_File datafile) { if (!client.tasksProgress.Contains(datafile.id)) { setThreadedStatusLabel("Incorrect received file id"); return; } else { //delete from in progress client.tasksProgress.Remove(datafile.id); setThreadedTasksProgress(); string filename = datafile.id.ToString() + "_res.txt"; string path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + @"\results\" + filename; setThreadedTasksDone(datafile.id.ToString() + ": " + path); try { if (File.Exists(path)) { File.Delete(path); } // Create the file. using (FileStream fs = File.Create(path)) { // Add text to the file. Byte[] info = Protocol.StringToByteArray(datafile.data, (int)datafile.size); fs.Write(info, 0, info.Length); } } catch (Exception exc) { MessageBox.Show("Exception:\t\n" + exc.Message.ToString()); } } }
/* Send button */ private void buttonSend_Click(object sender, EventArgs e) { if ((radioButtonP2.Checked || radioButtonP3.Checked) && textBoxFile.Text.Length > 0) { string filename = textBoxFile.Text; try { setThreadedSendButton(false); setThreadedReceiveButton(false); using (StreamReader sr = new StreamReader(filename)) { String line = sr.ReadToEnd(); Protocol.Request req; req = new Protocol.Request(); //creating data Protocol.RData_File data_file = new Protocol.RData_File(); data_file.file_type = radioButtonP2.Checked ? Protocol.fileType.file_py2_script : Protocol.fileType.file_py3_script; data_file.size = (ulong)line.Length; data_file.data = line; req.data = Marshal.AllocHGlobal(Marshal.SizeOf(data_file)); Marshal.StructureToPtr(data_file, req.data, false); //creating header req.header = new Protocol.Header(); req.header.req_type = Protocol.reqType.req_snd; req.header.size = (ulong)Marshal.SizeOf(data_file.GetType()); req.header.key = "12345678"; sendRequest(ref req); } } catch (Exception exc) { setThreadedStatusLabel("Exception:\t\n" + exc.Message.ToString()); } } else { setThreadedStatusLabel("No file or file type!"); } }