Advertisement
IvanNikolov2217

rad 6

May 19th, 2022
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BusinessLayer;
  7.  
  8. namespace Server
  9. {
  10.     class Program
  11.     {
  12.         static Dictionary<Type, object> recievedOperation = new Dictionary<Type, object>();
  13.         static Dictionary<Type, object> recievedData = new Dictionary<Type, object>(); static BinaryMessage operationToSend = new BinaryMessage();
  14.         static BinaryMessage dataToSend = new BinaryMessage();
  15.         static List<Student> students = new List<Student>();
  16.         static List<Classroom> classrooms = new List<Classroom>();
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             try
  21.             {
  22.                 ServerManager.InitializeServer();
  23.  
  24.                 while (true)
  25.                 {
  26.                     ServerManager.ListenForNewConnections();
  27.  
  28.                     do
  29.                     {
  30.                         recievedOperation = ServerManager.WaitForMessage();
  31.                         recievedData = ServerManager.WaitForMessage();
  32.  
  33.                         ProcessClientOperation();
  34.  
  35.                         if (!ServerManager.CommunicationIsActive)
  36.                         {
  37.                             break;
  38.                         }
  39.                     } while (true);
  40.  
  41.                     if (!ServerManager.ContinueListening())
  42.                     {
  43.                         break;
  44.                     }
  45.                 }
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 Console.WriteLine(ex.Message);
  50.                 Console.WriteLine("Press any key to close the program");
  51.                 Console.ReadKey();
  52.             }
  53.             finally
  54.             {
  55.                 ServerManager.CloseConnection();
  56.             }
  57.         }
  58.  
  59.         private static void ProcessClientOperation()
  60.         {
  61.             Classroom classroom = null;
  62.             Student student= null;
  63.             int? index;
  64.  
  65.             int? operation = recievedOperation[typeof(Int32)] as int?;
  66.  
  67.             switch (operation)
  68.             {
  69.                 case 1:
  70.                     student = recievedData[typeof(Student)] as Student;
  71.                     students.Add(student);
  72.                     Console.WriteLine("Student added successfully!");
  73.                     break;
  74.  
  75.                 case 2:
  76.                     classroom = recievedData[typeof(Classroom)] as Classroom;
  77.                     classrooms.Add(classroom);
  78.                     Console.WriteLine("Classroom added successfully!");
  79.                     break;
  80.  
  81.                 case 3:
  82.                     dataToSend = TransformDataManager.Serialize(students);
  83.                     ServerManager.SendMessage(dataToSend);
  84.                     break;
  85.  
  86.                 case 4:
  87.                     dataToSend = TransformDataManager.Serialize(classrooms);
  88.                     ServerManager.SendMessage(dataToSend);
  89.                     break;
  90.  
  91.                 case 5:
  92.                     ServerManager.CommunicationIsActive = false;
  93.                     break;
  94.  
  95.                 default:
  96.                     break;
  97.             }
  98.  
  99.         }
  100.  
  101.         public static void EndCommunication()
  102.         {
  103.             operationToSend = TransformDataManager.Serialize(6);
  104.             dataToSend = TransformDataManager.Serialize(string.Empty);
  105.  
  106.             ServerManager.SendMessage(operationToSend);
  107.             ServerManager.SendMessage(dataToSend);
  108.         }
  109.  
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement