diff --git a/ConcurrentCSharp/ClientServerConfig.json b/ConcurrentCSharp/ClientServerConfig.json index 7de1bbfc..329e0b61 100644 --- a/ConcurrentCSharp/ClientServerConfig.json +++ b/ConcurrentCSharp/ClientServerConfig.json @@ -3,7 +3,7 @@ "serverIPAddress": "127.0.0.1", "clientMinStartingTime": 100, "clientMaxStartingTime": 1000, - "experimentNumberOfClients": 30, + "experimentNumberOfClients": 150, "delayForTermination": 1000, "votingList": "ps :open -a preview :open -a whatsapp :ls -all", "serverListeningQueue": 20, diff --git a/ConcurrentCSharp/ConcurrentCSharp.sln b/ConcurrentCSharp/ConcurrentCSharp.sln index 93f2c7d4..c4d517a7 100644 --- a/ConcurrentCSharp/ConcurrentCSharp.sln +++ b/ConcurrentCSharp/ConcurrentCSharp.sln @@ -38,6 +38,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Examples", "Examples\Exampl EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Asynchronous", "Asynchronous\Asynchronous.csproj", "{CFF4C5D4-01AA-4394-940A-97F1C493D67D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CaseDistribution", "CaseDistribution\CaseDistribution.csproj", "{6604F39B-1D3E-4A18-A4FB-6196AB74B384}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assessment", "Assessment\Assessment.csproj", "{ABE82029-B706-431F-864E-A3952835278A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -108,5 +112,13 @@ Global {CFF4C5D4-01AA-4394-940A-97F1C493D67D}.Debug|Any CPU.Build.0 = Debug|Any CPU {CFF4C5D4-01AA-4394-940A-97F1C493D67D}.Release|Any CPU.ActiveCfg = Release|Any CPU {CFF4C5D4-01AA-4394-940A-97F1C493D67D}.Release|Any CPU.Build.0 = Release|Any CPU + {6604F39B-1D3E-4A18-A4FB-6196AB74B384}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6604F39B-1D3E-4A18-A4FB-6196AB74B384}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6604F39B-1D3E-4A18-A4FB-6196AB74B384}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6604F39B-1D3E-4A18-A4FB-6196AB74B384}.Release|Any CPU.Build.0 = Release|Any CPU + {ABE82029-B706-431F-864E-A3952835278A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ABE82029-B706-431F-864E-A3952835278A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ABE82029-B706-431F-864E-A3952835278A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ABE82029-B706-431F-864E-A3952835278A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/ConcurrentCSharp/DiningPhilosophers/Program.cs b/ConcurrentCSharp/DiningPhilosophers/Program.cs index bcd9f38c..9b5c1a0a 100644 --- a/ConcurrentCSharp/DiningPhilosophers/Program.cs +++ b/ConcurrentCSharp/DiningPhilosophers/Program.cs @@ -1,6 +1,6 @@ using System; -//using Exercise; -using Solution; // will be provided later +using Exercise; +//using Solution; // will be provided later namespace DiningPhilosophers { @@ -12,12 +12,12 @@ static void Main(string[] args) Table table = new Table(numPhilosphers); Console.WriteLine("[Dinining Philospher] Dining with one fork is going to start ..."); - //table.startOneForkDining(iteration); + table.startOneForkDining(iteration); //Console.WriteLine("[Dinining Philospher] Dining with two forks is going to start (press enter)..."); //Console.ReadLine(); //table.startTwoForksDining(iteration); - table.startTwoForksDiningSafe(iteration); + //table.startTwoForksDiningSafe(iteration); } } diff --git a/ConcurrentCSharp/SocketClient/Program.cs b/ConcurrentCSharp/SocketClient/Program.cs index f5417384..8e1866b9 100644 --- a/ConcurrentCSharp/SocketClient/Program.cs +++ b/ConcurrentCSharp/SocketClient/Program.cs @@ -1,7 +1,7 @@ using System; using Sequential; -using Concurrent; -//using Solution; +//using Concurrent; +using Solution; namespace Program { diff --git a/ConcurrentCSharp/SocketClient/Sequential.cs b/ConcurrentCSharp/SocketClient/Sequential.cs index a902a6b6..2aa8bc44 100644 --- a/ConcurrentCSharp/SocketClient/Sequential.cs +++ b/ConcurrentCSharp/SocketClient/Sequential.cs @@ -166,8 +166,8 @@ public class SequentialClientsSimulator { private SimpleClient[] clients; public Setting settings; - public string configFile = "../ClientServerConfig.json"; - //public string configFile = "../../../../ClientServerConfig.json"; // for debugging + //public string configFile = "../ClientServerConfig.json"; + public string configFile = "../../../../ClientServerConfig.json"; // for debugging public SequentialClientsSimulator() diff --git a/ConcurrentCSharp/SocketServer/Concurrent.cs b/ConcurrentCSharp/SocketServer/Concurrent.cs index 23646643..6c13a46d 100644 --- a/ConcurrentCSharp/SocketServer/Concurrent.cs +++ b/ConcurrentCSharp/SocketServer/Concurrent.cs @@ -1,25 +1,25 @@ -using Sequential; -using System; -//todo [Assignment]: add required namespaces +//using Sequential; +//using System; +////todo [Assignment]: add required namespaces -namespace Concurrent -{ - public class ConcurrentServer : SequentialServer - { - // todo [Assignment]: implement required attributes specific for concurrent server +//namespace Concurrent +//{ +// public class ConcurrentServer : SequentialServer +// { +// // todo [Assignment]: implement required attributes specific for concurrent server - public ConcurrentServer(Setting settings) : base(settings) - { - // todo [Assignment]: implement required code - } - public override void prepareServer() - { - // todo [Assignment]: implement required code - } - public override string processMessage(String msg) - { - // todo [Assignment]: implement required code - return ""; - } - } -} \ No newline at end of file +// public ConcurrentServer(Setting settings) : base(settings) +// { +// // todo [Assignment]: implement required code +// } +// public override void prepareServer() +// { +// // todo [Assignment]: implement required code +// } +// public override string processMessage(String msg) +// { +// // todo [Assignment]: implement required code +// return ""; +// } +// } +//} \ No newline at end of file diff --git a/ConcurrentCSharp/SocketServer/Program.cs b/ConcurrentCSharp/SocketServer/Program.cs index b697cd11..74e5e27b 100644 --- a/ConcurrentCSharp/SocketServer/Program.cs +++ b/ConcurrentCSharp/SocketServer/Program.cs @@ -13,7 +13,9 @@ namespace Program public class ServerSimulator { public Setting settings; - public string configFile = "../ClientServerConfig.json"; + //public string configFile = "../ClientServerConfig.json"; + public string configFile = "../../../../ClientServerConfig.json"; // for debugging + public ServerSimulator() { @@ -84,8 +86,7 @@ static void Main(string[] args) Console.WriteLine("\n Invalid input ... "); break; } - } - + } } } } diff --git a/Networking/DistLibrary/ClientServerConfig.json b/Networking/DistLibrary/ClientServerConfig.json index 0c378d98..e52856b1 100644 --- a/Networking/DistLibrary/ClientServerConfig.json +++ b/Networking/DistLibrary/ClientServerConfig.json @@ -1,9 +1,9 @@ { - "serverPortNumber": 11111, - "bookHelperPortNumber": 11112, - "userHelperPortNumber": 11113, - "serverIPAddress": "127.0.0.1", - "bookHelperIPAddress": "127.0.0.1", - "userHelperIPAddress": "127.0.0.1", - "serverListeningQueue": 5 + "ServerPortNumber": 11111, + "BookHelperPortNumber": 11112, + "UserHelperPortNumber": 11113, + "ServerIPAddress": "127.0.0.1", + "BookHelperIPAddress": "127.0.0.1", + "UserHelperIPAddress": "127.0.0.1", + "ServerListeningQueue": 5 } \ No newline at end of file diff --git a/Networking/DistLibrary/LibBookHelper/BookHelper.cs b/Networking/DistLibrary/LibBookHelper/BookHelper.cs index fe3c8f71..d65c2688 100644 --- a/Networking/DistLibrary/LibBookHelper/BookHelper.cs +++ b/Networking/DistLibrary/LibBookHelper/BookHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using System.Text.Json; using System.Net; using System.Net.Sockets; @@ -12,15 +11,16 @@ namespace BookHelper // Note: Do not change this class. public class Setting { - public int serverPortNumber { get; set; } - public int bookHelperPortNumber { get; set; } - public int userHelperPortNumber { get; set; } - public string serverIPAddress { get; set; } - public string bookHelperIPAddress { get; set; } - public string userHelperIPAddress { get; set; } - public int serverListeningQueue { get; set; } + public int ServerPortNumber { get; set; } + public int BookHelperPortNumber { get; set; } + public int UserHelperPortNumber { get; set; } + public string ServerIPAddress { get; set; } + public string BookHelperIPAddress { get; set; } + public string UserHelperIPAddress { get; set; } + public int ServerListeningQueue { get; set; } } + // Note: Complete the implementation of this class. You can adjust the structure of this class. public class SequentialHelper { public SequentialHelper() diff --git a/Networking/DistLibrary/LibBookHelper/Data.cs b/Networking/DistLibrary/LibBookHelper/Data.cs index a78e7102..aeaa70d6 100644 --- a/Networking/DistLibrary/LibBookHelper/Data.cs +++ b/Networking/DistLibrary/LibBookHelper/Data.cs @@ -2,35 +2,36 @@ namespace LibData { + public class Message { - public string type { get; set; } - public string content { get; set; } + public MessageType Type { get; set; } + public string Content { get; set; } } - public class MessageType + public enum MessageType { - public const string hello = "Hello"; - public const string welcome = "Welcome"; - public const string bookInquiry = "BookInquiry"; - public const string userInquiry = "UserInquiry"; - public const string bookInqReply = "BookInquiryReply"; - public const string userInqReply = "UserInquiryReply"; - public const string endComm = "EndCommunication"; - public const string error = "Error"; - public const string notFound = "NotFound"; + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, } public class BookData { // the name of the book - public string title { get; set; } + public string Title { get; set; } // the author of the book - public string author { get; set; } + public string Author { get; set; } // the availability of the book: can be either Available or Borrowed - public string status { get; set; } + public string Status { get; set; } //the user id of the person who borrowed the book, otherwise null if the book is available. - public string borrowedBy { get; set; } + public string BorrowedBy { get; set; } // return date of a book if it is borrowed, otherwise null. public string ReturnDate { get; set; } } @@ -38,10 +39,10 @@ public class BookData public class UserData { // user id: has the format User-[a number] - public string user_id { get; set; } + public string User_id { get; set; } // full name - public string name { get; set; } - public string email { get; set; } - public string phone { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } } } diff --git a/Networking/DistLibrary/LibBookHelper/Program.cs b/Networking/DistLibrary/LibBookHelper/Program.cs index 4882a9e5..2dbce1e0 100644 --- a/Networking/DistLibrary/LibBookHelper/Program.cs +++ b/Networking/DistLibrary/LibBookHelper/Program.cs @@ -1,6 +1,9 @@ -using System; -using BookHelper; -//using BookHelperSolution; + +using System; +// using BookHelper; +using BookHelperSolution; +using System.Text; +using System.Diagnostics.Tracing; // NOTE: THIS FILE MUST NOT CHANGE @@ -26,9 +29,54 @@ class Program /// static void Main(string[] args) { + using var listener = new SocketEventListener(); Console.Clear(); HelperSimulator hs = new HelperSimulator(); hs.sequentialRun(); } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security"|| + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } } } + diff --git a/Networking/DistLibrary/LibClient/Client.cs b/Networking/DistLibrary/LibClient/Client.cs index e8e51670..ac5d8b0b 100644 --- a/Networking/DistLibrary/LibClient/Client.cs +++ b/Networking/DistLibrary/LibClient/Client.cs @@ -3,32 +3,34 @@ using System.Net.Sockets; using System.Net; using System.Text.Json; -using System.Threading; using LibData; namespace LibClient { + // Note: Do not change this class public class Setting { - public int serverPortNumber { get; set; } - public int bookHelperPortNumber { get; set; } - public int userHelperPortNumber { get; set; } - public string serverIPAddress { get; set; } - public string bookHelperIPAddress { get; set; } - public string userHelperIPAddress { get; set; } - public int serverListeningQueue { get; set; } + public int ServerPortNumber { get; set; } + public int BookHelperPortNumber { get; set; } + public int UserHelperPortNumber { get; set; } + public string ServerIPAddress { get; set; } + public string BookHelperIPAddress { get; set; } + public string UserHelperIPAddress { get; set; } + public int ServerListeningQueue { get; set; } } + // Note: Do not change this class public class Output { - public string client_id { get; set; } // the id of the client that requests the book - public string bookName { get; set; } // the name of the book to be reqyested - public string status { get; set; } // final status received from the server - public string borrowerName { get; set; } // the name of the borrower in case the status is borrowed, otherwise null - public string borrowerEmail { get; set; } // the email of the borrower in case the status is borrowed, otherwise null + public string Client_id { get; set; } // the id of the client that requests the book + public string BookName { get; set; } // the name of the book to be reqyested + public string Status { get; set; } // final status received from the server + public string BorrowerName { get; set; } // the name of the borrower in case the status is borrowed, otherwise null + public string BorrowerEmail { get; set; } // the email of the borrower in case the status is borrowed, otherwise null } + // Note: Complete the implementation of this class. You can adjust the structure of this class. public class SimpleClient { // some of the fields are defined. @@ -56,14 +58,14 @@ public SimpleClient(int id, string bookName) this.bookName = bookName; this.client_id = "Client " + id.ToString(); this.result = new Output(); - result.bookName = bookName; - result.client_id = this.client_id; + result.BookName = bookName; + result.Client_id = this.client_id; // read JSON directly from a file try { string configContent = File.ReadAllText(configFile); this.settings = JsonSerializer.Deserialize(configContent); - this.ipAddress = IPAddress.Parse(settings.serverIPAddress); + this.ipAddress = IPAddress.Parse(settings.ServerIPAddress); } catch (Exception e) { @@ -78,7 +80,7 @@ public SimpleClient(int id, string bookName) /// The result of the request public Output start() { - + // todo: implement the body to communicate with the server and requests the book. Return the result as an Output object. // Adding extra methods to the class is permitted. The signature of this method must not change. diff --git a/Networking/DistLibrary/LibClient/Data.cs b/Networking/DistLibrary/LibClient/Data.cs index a78e7102..aeaa70d6 100644 --- a/Networking/DistLibrary/LibClient/Data.cs +++ b/Networking/DistLibrary/LibClient/Data.cs @@ -2,35 +2,36 @@ namespace LibData { + public class Message { - public string type { get; set; } - public string content { get; set; } + public MessageType Type { get; set; } + public string Content { get; set; } } - public class MessageType + public enum MessageType { - public const string hello = "Hello"; - public const string welcome = "Welcome"; - public const string bookInquiry = "BookInquiry"; - public const string userInquiry = "UserInquiry"; - public const string bookInqReply = "BookInquiryReply"; - public const string userInqReply = "UserInquiryReply"; - public const string endComm = "EndCommunication"; - public const string error = "Error"; - public const string notFound = "NotFound"; + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, } public class BookData { // the name of the book - public string title { get; set; } + public string Title { get; set; } // the author of the book - public string author { get; set; } + public string Author { get; set; } // the availability of the book: can be either Available or Borrowed - public string status { get; set; } + public string Status { get; set; } //the user id of the person who borrowed the book, otherwise null if the book is available. - public string borrowedBy { get; set; } + public string BorrowedBy { get; set; } // return date of a book if it is borrowed, otherwise null. public string ReturnDate { get; set; } } @@ -38,10 +39,10 @@ public class BookData public class UserData { // user id: has the format User-[a number] - public string user_id { get; set; } + public string User_id { get; set; } // full name - public string name { get; set; } - public string email { get; set; } - public string phone { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } } } diff --git a/Networking/DistLibrary/LibClient/LibOutput.json b/Networking/DistLibrary/LibClient/LibOutput.json index 2460e43c..4211a7e7 100644 --- a/Networking/DistLibrary/LibClient/LibOutput.json +++ b/Networking/DistLibrary/LibClient/LibOutput.json @@ -1 +1 @@ -[{"client_id":"Client 0","bookName":"War and Peace","status":"Borrowed","borrowerName":"Abdul Barker","borrowerEmail":"in@Pellentesqueultricies.co.uk"},{"client_id":"Client 1","bookName":"Harry Potter","status":"Available","borrowerName":null,"borrowerEmail":null},{"client_id":"Client 2","bookName":"Lord of the Rings","status":"Available","borrowerName":null,"borrowerEmail":null},{"client_id":"Client 3","bookName":"An Unknown Book","status":"NotFound","borrowerName":null,"borrowerEmail":null}] \ No newline at end of file +[{"Client_id":"Client 0","BookName":"War and Peace","Status":"NotFound","BorrowerName":null,"BorrowerEmail":null},{"Client_id":"Client 1","BookName":"Harry Potter","Status":"NotFound","BorrowerName":null,"BorrowerEmail":null},{"Client_id":"Client 2","BookName":"Lord of the Rings","Status":"NotFound","BorrowerName":null,"BorrowerEmail":null},{"Client_id":"Client 3","BookName":"An Unknown Book","Status":"NotFound","BorrowerName":null,"BorrowerEmail":null}] \ No newline at end of file diff --git a/Networking/DistLibrary/LibClient/Program.cs b/Networking/DistLibrary/LibClient/Program.cs index 5b2c7865..99a93d6f 100644 --- a/Networking/DistLibrary/LibClient/Program.cs +++ b/Networking/DistLibrary/LibClient/Program.cs @@ -1,9 +1,12 @@ -using System; +using System.Text; +using System.Diagnostics.Tracing; +using System.Drawing; +using System; using System.Text.Json; using System.IO; using System.Collections.Generic; -using LibClient; -//using LibClientSolution; +// using LibClient; +using LibClientSolution; // NOTE: DO NOT CHANGE THIS FILE @@ -42,10 +45,11 @@ public ClientsSimulator() // each client is initialized by a client id and a book name (from the input file) to request foreach (InputData d in this.inputDataList) { - clients[id] = new SimpleClient(id,d.BookName); + clients[id] = new SimpleClient(id, d.BookName); id++; } - }catch (Exception e) { Console.Out.WriteLine("[ClientSimulator] Exception: {0}", e.Message); } + } + catch (Exception e) { Console.Out.WriteLine("[ClientSimulator] Exception: {0}", e.Message); } } /// /// Starts the book requesting process for each client and collects the result for each request @@ -59,7 +63,7 @@ public void startSimulation() results[i] = clients[i].start(); } //this is the ending client - new SimpleClient(-1,"").start(); + new SimpleClient(-1, "").start(); } /// @@ -70,14 +74,14 @@ public void printOutput() Console.Out.WriteLine("\n *************** Final Output *********** \n"); for (int i = 0; i < results.Length; i++) Console.WriteLine("{0} {1} {2} {3}", - results[i].client_id, - results[i].bookName, - results[i].borrowerName, - results[i].borrowerEmail); + results[i].Client_id, + results[i].BookName, + results[i].BorrowerName, + results[i].BorrowerEmail); string outputContent = JsonSerializer.Serialize(this.results); - Console.WriteLine("Content of the Output file:\n {0}",outputContent); + Console.WriteLine("Content of the Output file:\n {0}", outputContent); - File.WriteAllText(outputFile,outputContent); + File.WriteAllText(outputFile, outputContent); } } @@ -87,12 +91,60 @@ class Program /// Starts the simulation for a set of clients and produces the output results. /// /// + static void Main(string[] args) { + + using var listener = new SocketEventListener(); Console.Clear(); ClientsSimulator simulator = new ClientsSimulator(); simulator.startSimulation(); + simulator.printOutput(); } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security" || + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } } + } diff --git a/Networking/DistLibrary/LibServer/Data.cs b/Networking/DistLibrary/LibServer/Data.cs index a78e7102..2106c130 100644 --- a/Networking/DistLibrary/LibServer/Data.cs +++ b/Networking/DistLibrary/LibServer/Data.cs @@ -2,46 +2,47 @@ namespace LibData { - public class Message + + public class Message { - public string type { get; set; } - public string content { get; set; } + public MessageType Type { get; set; } + public string Content { get; set; } } - public class MessageType + public enum MessageType { - public const string hello = "Hello"; - public const string welcome = "Welcome"; - public const string bookInquiry = "BookInquiry"; - public const string userInquiry = "UserInquiry"; - public const string bookInqReply = "BookInquiryReply"; - public const string userInqReply = "UserInquiryReply"; - public const string endComm = "EndCommunication"; - public const string error = "Error"; - public const string notFound = "NotFound"; + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, } - public class BookData + public class BookData { // the name of the book - public string title { get; set; } + public string Title { get; set; } // the author of the book - public string author { get; set; } + public string Author { get; set; } // the availability of the book: can be either Available or Borrowed - public string status { get; set; } + public string Status { get; set; } //the user id of the person who borrowed the book, otherwise null if the book is available. - public string borrowedBy { get; set; } + public string BorrowedBy { get; set; } // return date of a book if it is borrowed, otherwise null. public string ReturnDate { get; set; } } - public class UserData + public class UserData { // user id: has the format User-[a number] - public string user_id { get; set; } + public string User_id { get; set; } // full name - public string name { get; set; } - public string email { get; set; } - public string phone { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } } } diff --git a/Networking/DistLibrary/LibServer/LibServer.cs b/Networking/DistLibrary/LibServer/LibServer.cs index 2ab57668..724c6b86 100644 --- a/Networking/DistLibrary/LibServer/LibServer.cs +++ b/Networking/DistLibrary/LibServer/LibServer.cs @@ -4,7 +4,7 @@ using System.Net.Sockets; using System.IO; using LibData; -using System.Threading; + namespace LibServer { @@ -20,8 +20,10 @@ public class Setting public int serverListeningQueue { get; set; } } + + // Note: Complete the implementation of this class. You can adjust the structure of this class. public class SequentialServer - { + { public SequentialServer() { //todo: implement the body. Add extra fields and methods to the class if it is needed @@ -33,4 +35,8 @@ public void start() } } + } + + + diff --git a/Networking/DistLibrary/LibServer/Program.cs b/Networking/DistLibrary/LibServer/Program.cs index 3334e7ec..ffdc42bb 100644 --- a/Networking/DistLibrary/LibServer/Program.cs +++ b/Networking/DistLibrary/LibServer/Program.cs @@ -1,6 +1,8 @@ using System; -using LibServer; -//using LibServerSolution; +// using LibServer; +using LibServerSolution; +using System.Text; +using System.Diagnostics.Tracing; // NOTE: THIS FILE MUST NOT CHANGE @@ -11,7 +13,7 @@ public class ServerSimulator public Setting settings; public ServerSimulator() - { } + { } /// /// initiates the server @@ -31,9 +33,54 @@ public class Program /// static void Main(string[] args) { + using var listener = new SocketEventListener(); Console.Clear(); new ServerSimulator().sequentialRun(); } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security" || + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName} {eventData.Task}.{eventData.Message}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } } } + diff --git a/Networking/DistLibrary/LibUserHelper/Data.cs b/Networking/DistLibrary/LibUserHelper/Data.cs index a78e7102..aeaa70d6 100644 --- a/Networking/DistLibrary/LibUserHelper/Data.cs +++ b/Networking/DistLibrary/LibUserHelper/Data.cs @@ -2,35 +2,36 @@ namespace LibData { + public class Message { - public string type { get; set; } - public string content { get; set; } + public MessageType Type { get; set; } + public string Content { get; set; } } - public class MessageType + public enum MessageType { - public const string hello = "Hello"; - public const string welcome = "Welcome"; - public const string bookInquiry = "BookInquiry"; - public const string userInquiry = "UserInquiry"; - public const string bookInqReply = "BookInquiryReply"; - public const string userInqReply = "UserInquiryReply"; - public const string endComm = "EndCommunication"; - public const string error = "Error"; - public const string notFound = "NotFound"; + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, } public class BookData { // the name of the book - public string title { get; set; } + public string Title { get; set; } // the author of the book - public string author { get; set; } + public string Author { get; set; } // the availability of the book: can be either Available or Borrowed - public string status { get; set; } + public string Status { get; set; } //the user id of the person who borrowed the book, otherwise null if the book is available. - public string borrowedBy { get; set; } + public string BorrowedBy { get; set; } // return date of a book if it is borrowed, otherwise null. public string ReturnDate { get; set; } } @@ -38,10 +39,10 @@ public class BookData public class UserData { // user id: has the format User-[a number] - public string user_id { get; set; } + public string User_id { get; set; } // full name - public string name { get; set; } - public string email { get; set; } - public string phone { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } } } diff --git a/Networking/DistLibrary/LibUserHelper/Program.cs b/Networking/DistLibrary/LibUserHelper/Program.cs index 4a22624d..885a7fa1 100644 --- a/Networking/DistLibrary/LibUserHelper/Program.cs +++ b/Networking/DistLibrary/LibUserHelper/Program.cs @@ -1,6 +1,9 @@ -using System; -using UserHelper; -//using UserHelperSolution; +using System.Drawing; +using System; +// using UserHelper; +using UserHelperSolution; +using System.Text; +using System.Diagnostics.Tracing; // NOTE: THIS FILE MUST NOT CHANGE @@ -26,9 +29,56 @@ class Program /// static void Main(string[] args) { + using var listener = new SocketEventListener(); Console.Clear(); HelperSimulator hs = new HelperSimulator(); hs.sequentialRun(); } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security"|| + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } } + + } diff --git a/Networking/DistLibrary/LibUserHelper/UserHelper.cs b/Networking/DistLibrary/LibUserHelper/UserHelper.cs index 122aada6..75dd26bf 100644 --- a/Networking/DistLibrary/LibUserHelper/UserHelper.cs +++ b/Networking/DistLibrary/LibUserHelper/UserHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Threading; using System.Text.Json; using System.Net; using System.Net.Sockets; @@ -12,15 +11,16 @@ namespace UserHelper // Note: Do not change this class. public class Setting { - public int serverPortNumber { get; set; } - public int bookHelperPortNumber { get; set; } - public int userHelperPortNumber { get; set; } - public string serverIPAddress { get; set; } - public string bookHelperIPAddress { get; set; } - public string userHelperIPAddress { get; set; } - public int serverListeningQueue { get; set; } + public int ServerPortNumber { get; set; } + public int BookHelperPortNumber { get; set; } + public int UserHelperPortNumber { get; set; } + public string ServerIPAddress { get; set; } + public string BookHelperIPAddress { get; set; } + public string UserHelperIPAddress { get; set; } + public int ServerListeningQueue { get; set; } } + // Note: Complete the implementation of this class. You can adjust the structure of this class. public class SequentialHelper { public SequentialHelper() diff --git a/Networking/SimpleClientServer/Client/Client.csproj b/Networking/SimpleClientServer/Client/Client.csproj new file mode 100644 index 00000000..1d2d39a9 --- /dev/null +++ b/Networking/SimpleClientServer/Client/Client.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/Networking/SimpleClientServer/Client/Program.cs b/Networking/SimpleClientServer/Client/Program.cs new file mode 100644 index 00000000..5b441b25 --- /dev/null +++ b/Networking/SimpleClientServer/Client/Program.cs @@ -0,0 +1,59 @@ +using System; +using System.Text; +using System.Net; +using System.Net.Sockets; + +namespace socket_prog +{ + class Client + { + static void Main(string[] args) + { + byte[] buffer = new byte[1000]; + byte[] msg = new byte[1000]; + string data = null; + + + IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); + + IPEndPoint serverEndpoint = new IPEndPoint(ipAddress, 32000); + + ConsoleKeyInfo key; + + + Socket sock = new Socket(AddressFamily.InterNetwork, + SocketType.Stream, ProtocolType.Tcp); + sock.Connect(serverEndpoint); + + while (true) + { + Console.WriteLine("\nEnter a message to send to the server"); + data = Console.ReadLine(); + if (data.Length != 0) + { + + msg = Encoding.ASCII.GetBytes(data); + sock.Send(msg); + int b = sock.Receive(buffer); + data = Encoding.ASCII.GetString(buffer, 0, b); + + Console.WriteLine("" + data); + data = null; + } + + Console.WriteLine("\n<< Continue 'y' , Exit 'e'>>\n"); + key = Console.ReadKey(); + if (key.KeyChar == 'e') + { + sock.Send(Encoding.ASCII.GetBytes("Closed")); + Console.WriteLine("\nExiting.. Press any key to continue"); + key = Console.ReadKey(); + sock.Close(); + break; + } + + } + } + } + +} diff --git a/Networking/SimpleClientServer/Server/Program.cs b/Networking/SimpleClientServer/Server/Program.cs new file mode 100644 index 00000000..8f082ac2 --- /dev/null +++ b/Networking/SimpleClientServer/Server/Program.cs @@ -0,0 +1,61 @@ +using System; +using System.Text; +using System.Net; +using System.Net.Sockets; + +namespace socket_prog +{ + class Server + { + static void Main(string[] args) + { + // define required variables + const string ipAdd = "127.0.0.1"; + const int portNum = 32000; + const int maxBuffSize = 1000; + byte[] buffer = new byte[maxBuffSize]; + byte[] msg = Encoding.ASCII.GetBytes("From server: Your message delivered\n"); + string data = null; + + // define parameters equired to set up the network + IPAddress ipAddress = IPAddress.Parse(ipAdd); + IPEndPoint localEndpoint = new IPEndPoint(ipAddress, portNum); + + // establish a listening socket + Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); + + //bind to the socket + sock.Bind(localEndpoint); + // listen with max number of pending incoming requests + sock.Listen(5); + Console.WriteLine("\n Waiting for clients.."); + // wait to accept incoming requests + Socket newSock = sock.Accept(); + + // a loop to receive / process / reply + while (true) + { + int b = newSock.Receive(buffer); // receive + data = Encoding.ASCII.GetString(buffer, 0, b); + // process + if (data == "Closed") + { + newSock.Close(); + Console.WriteLine("Closing the socket.."); + break; + } + else + { + Console.WriteLine("" + data); + data = null; + newSock.Send(msg); // reply + } + + } + // close the socket + sock.Close(); + + } + } + +} diff --git a/Networking/SimpleClientServer/Server/Server.csproj b/Networking/SimpleClientServer/Server/Server.csproj new file mode 100644 index 00000000..1d2d39a9 --- /dev/null +++ b/Networking/SimpleClientServer/Server/Server.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/Networking/SimpleClientServer/SimpleClientServer.sln b/Networking/SimpleClientServer/SimpleClientServer.sln new file mode 100644 index 00000000..4a1e7ed9 --- /dev/null +++ b/Networking/SimpleClientServer/SimpleClientServer.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.810.9 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{1D7EAE9B-A9C2-488E-99F4-7C33D9EFFE5C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{FDF5BA6E-8C4E-48CD-961B-0509175977E3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1D7EAE9B-A9C2-488E-99F4-7C33D9EFFE5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D7EAE9B-A9C2-488E-99F4-7C33D9EFFE5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D7EAE9B-A9C2-488E-99F4-7C33D9EFFE5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D7EAE9B-A9C2-488E-99F4-7C33D9EFFE5C}.Release|Any CPU.Build.0 = Release|Any CPU + {FDF5BA6E-8C4E-48CD-961B-0509175977E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FDF5BA6E-8C4E-48CD-961B-0509175977E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FDF5BA6E-8C4E-48CD-961B-0509175977E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FDF5BA6E-8C4E-48CD-961B-0509175977E3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C0D849A0-6F44-4884-A47B-FFF3ED38E3D4} + EndGlobalSection +EndGlobal diff --git a/basecamp/readme.md b/basecamp/readme.md index b2cd98a1..d1e6ba46 100644 --- a/basecamp/readme.md +++ b/basecamp/readme.md @@ -10,4 +10,3 @@ The learning activities are designed based on these following references: - **Ref-B01**: Book, Bill Lubanovic; "Introducing Python: Modern Computing in Simple Packages"; [Available here](https://www.oreilly.com/library/view/introducing-python-2nd/9781492051374/) - **Ref-O02**: Online Tutorial; Charles Severance; "Python for Everybody"; [Available here](https://books.trinket.io/pfe/index.html) -- diff --git a/basecamp/week02/inf-bc-w02-program.md b/basecamp/week02/inf-bc-w02-program.md index a2d0d656..a128722a 100644 --- a/basecamp/week02/inf-bc-w02-program.md +++ b/basecamp/week02/inf-bc-w02-program.md @@ -2,9 +2,9 @@ **Introduction**: This document presents learning activities for week 02. In Week 02, you will learn basics of Python structures to add decision points to your linear programs. A branching program is a flow of sequential instructions with branching statements. By the end this week, you will be able to implement a program where a user can enter simple input, the program can make choices based on some conditions and after calculation results can be printed as output. -## Day-01: What is a ??? +## Day-01: How to check conditions? -1. Using **Ref-B01: Chapter 03** answer and experiment the following questions: +1. Using **Ref-O02: Chapter 03** answer and experiment the following questions: 1. What is a 2. Using **RefB-01: Appendix B** perform the following tasks: 1. Install Pyt diff --git a/models/Closing.png b/models/Closing.png new file mode 100644 index 00000000..a67c2756 Binary files /dev/null and b/models/Closing.png differ diff --git a/models/Closing.png.bak b/models/Closing.png.bak new file mode 100644 index 00000000..38baece1 Binary files /dev/null and b/models/Closing.png.bak differ diff --git a/models/bookAvailable.png b/models/bookAvailable.png new file mode 100644 index 00000000..8a6e41f4 Binary files /dev/null and b/models/bookAvailable.png differ diff --git a/models/bookAvailable.png.bak b/models/bookAvailable.png.bak new file mode 100644 index 00000000..273198e1 Binary files /dev/null and b/models/bookAvailable.png.bak differ diff --git a/models/bookBorrowed.png b/models/bookBorrowed.png new file mode 100644 index 00000000..8fe37d69 Binary files /dev/null and b/models/bookBorrowed.png differ diff --git a/models/bookBorrowed.png.bak b/models/bookBorrowed.png.bak new file mode 100644 index 00000000..7c4e656a Binary files /dev/null and b/models/bookBorrowed.png.bak differ diff --git a/models/bookNotFound.png b/models/bookNotFound.png new file mode 100644 index 00000000..5985f0ad Binary files /dev/null and b/models/bookNotFound.png differ diff --git a/models/bookNotFound.png.bak b/models/bookNotFound.png.bak new file mode 100644 index 00000000..fdf1912a Binary files /dev/null and b/models/bookNotFound.png.bak differ diff --git a/models/networking-SD.asta b/models/networking-SD.asta new file mode 100644 index 00000000..7f62ac83 Binary files /dev/null and b/models/networking-SD.asta differ diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/ClientServerConfig.json b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/ClientServerConfig.json new file mode 100644 index 00000000..e52856b1 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/ClientServerConfig.json @@ -0,0 +1,9 @@ +{ + "ServerPortNumber": 11111, + "BookHelperPortNumber": 11112, + "UserHelperPortNumber": 11113, + "ServerIPAddress": "127.0.0.1", + "BookHelperIPAddress": "127.0.0.1", + "UserHelperIPAddress": "127.0.0.1", + "ServerListeningQueue": 5 +} \ No newline at end of file diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/DistLibrary.sln b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/DistLibrary.sln new file mode 100644 index 00000000..470ea7b0 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/DistLibrary.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.810.9 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibClient", "LibClient\LibClient.csproj", "{D01312F2-3D27-4A7E-A0AB-B8E0D208C7C1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UserHelper", "LibUserHelper\UserHelper.csproj", "{50A9474A-A777-41A8-A028-D6428516773F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BookHelper", "LibBookHelper\BookHelper.csproj", "{5E3DF90F-B75D-4C89-85EE-BA49D4446F46}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibServer", "LibServer\LibServer.csproj", "{4C716E6E-165B-44EB-90CE-07A864B371A8}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E8D2FBCB-D24D-4A3C-BB87-54C4344272F9}" + ProjectSection(SolutionItems) = preProject + ClientServerConfig.json = ClientServerConfig.json + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D01312F2-3D27-4A7E-A0AB-B8E0D208C7C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D01312F2-3D27-4A7E-A0AB-B8E0D208C7C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D01312F2-3D27-4A7E-A0AB-B8E0D208C7C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D01312F2-3D27-4A7E-A0AB-B8E0D208C7C1}.Release|Any CPU.Build.0 = Release|Any CPU + {50A9474A-A777-41A8-A028-D6428516773F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {50A9474A-A777-41A8-A028-D6428516773F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {50A9474A-A777-41A8-A028-D6428516773F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {50A9474A-A777-41A8-A028-D6428516773F}.Release|Any CPU.Build.0 = Release|Any CPU + {5E3DF90F-B75D-4C89-85EE-BA49D4446F46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5E3DF90F-B75D-4C89-85EE-BA49D4446F46}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5E3DF90F-B75D-4C89-85EE-BA49D4446F46}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5E3DF90F-B75D-4C89-85EE-BA49D4446F46}.Release|Any CPU.Build.0 = Release|Any CPU + {4C716E6E-165B-44EB-90CE-07A864B371A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C716E6E-165B-44EB-90CE-07A864B371A8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C716E6E-165B-44EB-90CE-07A864B371A8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C716E6E-165B-44EB-90CE-07A864B371A8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {8DA0EFBE-C809-4848-BE1F-ACEF770B2252} + EndGlobalSection +EndGlobal diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/BookHelper.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/BookHelper.cs new file mode 100644 index 00000000..d65c2688 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/BookHelper.cs @@ -0,0 +1,36 @@ +using System; +using System.Text.Json; +using System.Net; +using System.Net.Sockets; +using System.IO; +using System.Collections.Generic; +using LibData; + +namespace BookHelper +{ + // Note: Do not change this class. + public class Setting + { + public int ServerPortNumber { get; set; } + public int BookHelperPortNumber { get; set; } + public int UserHelperPortNumber { get; set; } + public string ServerIPAddress { get; set; } + public string BookHelperIPAddress { get; set; } + public string UserHelperIPAddress { get; set; } + public int ServerListeningQueue { get; set; } + } + + // Note: Complete the implementation of this class. You can adjust the structure of this class. + public class SequentialHelper + { + public SequentialHelper() + { + //todo: implement the body. Add extra fields and methods to the class if needed + } + + public void start() + { + //todo: implement the body. Add extra fields and methods to the class if needed + } + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/BookHelper.csproj b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/BookHelper.csproj new file mode 100644 index 00000000..1d2d39a9 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/BookHelper.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Books.json b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Books.json new file mode 100644 index 00000000..5f90765d --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Books.json @@ -0,0 +1 @@ +[{"Title":"War and Peace","Author":"Tolstoy, Leo","Status":"Borrowed","BorrowedBy":"user-2","ReturnDate":"20-09-2021"},{"Title":"Anna Karenina","Author":"Tolstoy, Leo","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Mrs. Dalloway","Author":"Woolf, Virginia","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"The Hours","Author":"Cunnningham, Michael","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Huckleberry Finn","Author":"Twain, Mark","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Bleak House","Author":"Dickens, Charles","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Tom Sawyer","Author":"Twain, Mark","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"A Room of One\u0027s Own","Author":"Woolf, Virginia","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Harry Potter","Author":"Rowling, J.K.","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"One Hundred Years of Solitude","Author":"Marquez","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Hamlet, Prince of Denmark","Author":"Shakespeare","Status":"Available","BorrowedBy":null,"ReturnDate":null},{"Title":"Lord of the Rings","Author":"Tolkien, J.R.","Status":"Available","BorrowedBy":null,"ReturnDate":null}] \ No newline at end of file diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Data.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Data.cs new file mode 100644 index 00000000..aeaa70d6 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Data.cs @@ -0,0 +1,48 @@ +// NOTE: THIS FILE MUST NOT CHANGE + +namespace LibData +{ + + public class Message + { + public MessageType Type { get; set; } + public string Content { get; set; } + } + + public enum MessageType + { + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, + } + + public class BookData + { + // the name of the book + public string Title { get; set; } + // the author of the book + public string Author { get; set; } + // the availability of the book: can be either Available or Borrowed + public string Status { get; set; } + //the user id of the person who borrowed the book, otherwise null if the book is available. + public string BorrowedBy { get; set; } + // return date of a book if it is borrowed, otherwise null. + public string ReturnDate { get; set; } + } + + public class UserData + { + // user id: has the format User-[a number] + public string User_id { get; set; } + // full name + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Program.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Program.cs new file mode 100644 index 00000000..2dbce1e0 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibBookHelper/Program.cs @@ -0,0 +1,82 @@ + +using System; +// using BookHelper; +using BookHelperSolution; +using System.Text; +using System.Diagnostics.Tracing; + +// NOTE: THIS FILE MUST NOT CHANGE + +namespace LibHelper +{ + public class HelperSimulator + { + /// + /// initiates the helper + /// + public void sequentialRun() + { + SequentialHelper server = new SequentialHelper(); + server.start(); + } + } + + class Program + { + /// + /// Starts the simulation for a set of clients and produces the output results. + /// + /// + static void Main(string[] args) + { + using var listener = new SocketEventListener(); + Console.Clear(); + HelperSimulator hs = new HelperSimulator(); + hs.sequentialRun(); + } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security"|| + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } + } +} + diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Client.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Client.cs new file mode 100644 index 00000000..ac5d8b0b --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Client.cs @@ -0,0 +1,91 @@ +using System; +using System.IO; +using System.Net.Sockets; +using System.Net; +using System.Text.Json; +using LibData; + + +namespace LibClient +{ + // Note: Do not change this class + public class Setting + { + public int ServerPortNumber { get; set; } + public int BookHelperPortNumber { get; set; } + public int UserHelperPortNumber { get; set; } + public string ServerIPAddress { get; set; } + public string BookHelperIPAddress { get; set; } + public string UserHelperIPAddress { get; set; } + public int ServerListeningQueue { get; set; } + } + + // Note: Do not change this class + public class Output + { + public string Client_id { get; set; } // the id of the client that requests the book + public string BookName { get; set; } // the name of the book to be reqyested + public string Status { get; set; } // final status received from the server + public string BorrowerName { get; set; } // the name of the borrower in case the status is borrowed, otherwise null + public string BorrowerEmail { get; set; } // the email of the borrower in case the status is borrowed, otherwise null + } + + // Note: Complete the implementation of this class. You can adjust the structure of this class. + public class SimpleClient + { + // some of the fields are defined. + public Output result; + public Socket clientSocket; + public IPEndPoint serverEndPoint; + public IPAddress ipAddress; + public Setting settings; + public string client_id; + private string bookName; + // all the required settings are provided in this file + public string configFile = @"../ClientServerConfig.json"; + //public string configFile = @"../../../../ClientServerConfig.json"; // for debugging + + // todo: add extra fields here in case needed + + /// + /// Initializes the client based on the given parameters and seeting file. + /// + /// id of the clients provided by the simulator + /// name of the book to be requested from the server, provided by the simulator + public SimpleClient(int id, string bookName) + { + //todo: extend the body if needed. + this.bookName = bookName; + this.client_id = "Client " + id.ToString(); + this.result = new Output(); + result.BookName = bookName; + result.Client_id = this.client_id; + // read JSON directly from a file + try + { + string configContent = File.ReadAllText(configFile); + this.settings = JsonSerializer.Deserialize(configContent); + this.ipAddress = IPAddress.Parse(settings.ServerIPAddress); + } + catch (Exception e) + { + Console.Out.WriteLine("[Client Exception] {0}", e.Message); + } + } + + /// + /// Establishes the connection with the server and requests the book according to the specified protocol. + /// Note: The signature of this method must not change. + /// + /// The result of the request + public Output start() + { + + // todo: implement the body to communicate with the server and requests the book. Return the result as an Output object. + // Adding extra methods to the class is permitted. The signature of this method must not change. + + return result; + } + + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Data.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Data.cs new file mode 100644 index 00000000..aeaa70d6 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Data.cs @@ -0,0 +1,48 @@ +// NOTE: THIS FILE MUST NOT CHANGE + +namespace LibData +{ + + public class Message + { + public MessageType Type { get; set; } + public string Content { get; set; } + } + + public enum MessageType + { + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, + } + + public class BookData + { + // the name of the book + public string Title { get; set; } + // the author of the book + public string Author { get; set; } + // the availability of the book: can be either Available or Borrowed + public string Status { get; set; } + //the user id of the person who borrowed the book, otherwise null if the book is available. + public string BorrowedBy { get; set; } + // return date of a book if it is borrowed, otherwise null. + public string ReturnDate { get; set; } + } + + public class UserData + { + // user id: has the format User-[a number] + public string User_id { get; set; } + // full name + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibClient.csproj b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibClient.csproj new file mode 100644 index 00000000..c5cb60fa --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibClient.csproj @@ -0,0 +1,7 @@ + + + + Exe + net5.0 + + \ No newline at end of file diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibInput.json b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibInput.json new file mode 100644 index 00000000..069b87b8 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibInput.json @@ -0,0 +1,14 @@ +[ + { + "BookName": "War and Peace" + }, + { + "BookName": "Harry Potter" + }, + { + "BookName": "Lord of the Rings" + }, + { + "BookName": "An Unknown Book" + } +] diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibOutput.json b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibOutput.json new file mode 100644 index 00000000..4bbc625c --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/LibOutput.json @@ -0,0 +1 @@ +[{"Client_id":"Client 0","BookName":"War and Peace","Status":"Borrowed","BorrowerName":"Abdul Barker","BorrowerEmail":"in@Pellentesqueultricies.co.uk"},{"Client_id":"Client 1","BookName":"Harry Potter","Status":"Available","BorrowerName":null,"BorrowerEmail":null},{"Client_id":"Client 2","BookName":"Lord of the Rings","Status":"Available","BorrowerName":null,"BorrowerEmail":null},{"Client_id":"Client 3","BookName":"An Unknown Book","Status":"NotFound","BorrowerName":null,"BorrowerEmail":null}] \ No newline at end of file diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Program.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Program.cs new file mode 100644 index 00000000..99a93d6f --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibClient/Program.cs @@ -0,0 +1,150 @@ +using System.Text; +using System.Diagnostics.Tracing; +using System.Drawing; +using System; +using System.Text.Json; +using System.IO; +using System.Collections.Generic; +// using LibClient; +using LibClientSolution; + + +// NOTE: DO NOT CHANGE THIS FILE +namespace LibClientSimulator +{ + public class InputData + { + public string BookName { get; set; } + } + public class ClientsSimulator + { + private SimpleClient client; + private List inputDataList; + private SimpleClient[] clients; + private Output[] results; + private string inputFile = @"LibInput.json"; + private string outputFile = @"LibOutput.json"; + + // paths for debugging + //private string inputFile = @"../../../LibInput.json"; + //private string outputFile = @"../../../LibOutput.json"; + + /// + /// Reads the input file and creats the clients and output objects accordingly + /// + public ClientsSimulator() + { + int id = 0; + try + { + string inputContent = File.ReadAllText(inputFile); + this.inputDataList = JsonSerializer.Deserialize>(inputContent); + clients = new SimpleClient[this.inputDataList.Count]; + results = new Output[this.inputDataList.Count]; + + // each client is initialized by a client id and a book name (from the input file) to request + foreach (InputData d in this.inputDataList) + { + clients[id] = new SimpleClient(id, d.BookName); + id++; + } + } + catch (Exception e) { Console.Out.WriteLine("[ClientSimulator] Exception: {0}", e.Message); } + } + /// + /// Starts the book requesting process for each client and collects the result for each request + /// + public void startSimulation() + { + int numCases = clients.Length; + for (int i = 0; i < numCases; i++) + { + Console.Out.WriteLine("\n *********** \n"); + results[i] = clients[i].start(); + } + //this is the ending client + new SimpleClient(-1, "").start(); + } + + /// + /// Prints all the results in console and produces the output file + /// + public void printOutput() + { + Console.Out.WriteLine("\n *************** Final Output *********** \n"); + for (int i = 0; i < results.Length; i++) + Console.WriteLine("{0} {1} {2} {3}", + results[i].Client_id, + results[i].BookName, + results[i].BorrowerName, + results[i].BorrowerEmail); + string outputContent = JsonSerializer.Serialize(this.results); + Console.WriteLine("Content of the Output file:\n {0}", outputContent); + + File.WriteAllText(outputFile, outputContent); + } + } + + class Program + { + /// + /// Starts the simulation for a set of clients and produces the output results. + /// + /// + + static void Main(string[] args) + { + + using var listener = new SocketEventListener(); + Console.Clear(); + ClientsSimulator simulator = new ClientsSimulator(); + simulator.startSimulation(); + + simulator.printOutput(); + } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security" || + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } + } + +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/Data.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/Data.cs new file mode 100644 index 00000000..2106c130 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/Data.cs @@ -0,0 +1,48 @@ +// NOTE: THIS FILE MUST NOT CHANGE + +namespace LibData +{ + + public class Message + { + public MessageType Type { get; set; } + public string Content { get; set; } + } + + public enum MessageType + { + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, + } + + public class BookData + { + // the name of the book + public string Title { get; set; } + // the author of the book + public string Author { get; set; } + // the availability of the book: can be either Available or Borrowed + public string Status { get; set; } + //the user id of the person who borrowed the book, otherwise null if the book is available. + public string BorrowedBy { get; set; } + // return date of a book if it is borrowed, otherwise null. + public string ReturnDate { get; set; } + } + + public class UserData + { + // user id: has the format User-[a number] + public string User_id { get; set; } + // full name + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/LibServer.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/LibServer.cs new file mode 100644 index 00000000..724c6b86 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/LibServer.cs @@ -0,0 +1,42 @@ +using System; +using System.Text.Json; +using System.Net; +using System.Net.Sockets; +using System.IO; +using LibData; + + +namespace LibServer +{ + // Note: Do not change this class. + public class Setting + { + public int serverPortNumber { get; set; } + public int bookHelperPortNumber { get; set; } + public int userHelperPortNumber { get; set; } + public string serverIPAddress { get; set; } + public string bookHelperIPAddress { get; set; } + public string userHelperIPAddress { get; set; } + public int serverListeningQueue { get; set; } + } + + + // Note: Complete the implementation of this class. You can adjust the structure of this class. + public class SequentialServer + { + public SequentialServer() + { + //todo: implement the body. Add extra fields and methods to the class if it is needed + } + + public void start() + { + //todo: implement the body. Add extra fields and methods to the class if it is needed + + } + } + +} + + + diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/LibServer.csproj b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/LibServer.csproj new file mode 100644 index 00000000..1d2d39a9 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/LibServer.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/Program.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/Program.cs new file mode 100644 index 00000000..ffdc42bb --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibServer/Program.cs @@ -0,0 +1,86 @@ +using System; +// using LibServer; +using LibServerSolution; +using System.Text; +using System.Diagnostics.Tracing; + +// NOTE: THIS FILE MUST NOT CHANGE + +namespace Server +{ + public class ServerSimulator + { + public Setting settings; + + public ServerSimulator() + { } + + /// + /// initiates the server + /// + public void sequentialRun() + { + SequentialServer server = new SequentialServer(); + server.start(); + } + } + + public class Program + { + /// + /// Starts the simulation for a set of clients and produces the output results. + /// + /// + static void Main(string[] args) + { + using var listener = new SocketEventListener(); + Console.Clear(); + new ServerSimulator().sequentialRun(); + + } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security" || + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName} {eventData.Task}.{eventData.Message}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } + } +} + diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Data.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Data.cs new file mode 100644 index 00000000..aeaa70d6 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Data.cs @@ -0,0 +1,48 @@ +// NOTE: THIS FILE MUST NOT CHANGE + +namespace LibData +{ + + public class Message + { + public MessageType Type { get; set; } + public string Content { get; set; } + } + + public enum MessageType + { + Hello, + Welcome, + BookInquiry, + UserInquiry, + BookInquiryReply, + UserInquiryReply, + EndCommunication, + Error, + NotFound, + } + + public class BookData + { + // the name of the book + public string Title { get; set; } + // the author of the book + public string Author { get; set; } + // the availability of the book: can be either Available or Borrowed + public string Status { get; set; } + //the user id of the person who borrowed the book, otherwise null if the book is available. + public string BorrowedBy { get; set; } + // return date of a book if it is borrowed, otherwise null. + public string ReturnDate { get; set; } + } + + public class UserData + { + // user id: has the format User-[a number] + public string User_id { get; set; } + // full name + public string Name { get; set; } + public string Email { get; set; } + public string Phone { get; set; } + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Program.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Program.cs new file mode 100644 index 00000000..885a7fa1 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Program.cs @@ -0,0 +1,84 @@ +using System.Drawing; +using System; +// using UserHelper; +using UserHelperSolution; +using System.Text; +using System.Diagnostics.Tracing; + +// NOTE: THIS FILE MUST NOT CHANGE + +namespace LibHelper +{ + public class HelperSimulator + { + /// + /// initiates the helper + /// + public void sequentialRun() + { + SequentialHelper server = new SequentialHelper(); + server.start(); + } + } + + class Program + { + /// + /// Starts the simulation for a set of clients and produces the output results. + /// + /// + static void Main(string[] args) + { + using var listener = new SocketEventListener(); + Console.Clear(); + HelperSimulator hs = new HelperSimulator(); + hs.sequentialRun(); + } + + internal sealed class SocketEventListener : EventListener + { + // Constant necessary for attaching ActivityId to the events. + public const EventKeywords TasksFlowActivityIds = (EventKeywords)0x80; + + protected override void OnEventSourceCreated(EventSource eventSource) + { + // List of event source names provided by networking in .NET 5. + if (eventSource.Name == "System.Net.Sockets" || + eventSource.Name == "System.Net.Security"|| + eventSource.Name == "System.Net.NameResolution") + { + EnableEvents(eventSource, EventLevel.LogAlways); + } + // Turn on ActivityId. + else if (eventSource.Name == "System.Threading.Tasks.TplEventSource") + { + // Attach ActivityId to the events. + EnableEvents(eventSource, EventLevel.LogAlways, TasksFlowActivityIds); + } + } + + protected override void OnEventWritten(EventWrittenEventArgs eventData) + { + Console.ResetColor(); + + var sb = new StringBuilder().Append($"{eventData.TimeStamp:HH:mm:ss.ff} {eventData.ActivityId}.{eventData.RelatedActivityId} {eventData.EventSource.Name}.{eventData.EventName}("); + for (int i = 0; i < eventData.Payload?.Count; i++) + { + + sb.Append(eventData.PayloadNames?[i]).Append(": ").Append(eventData.Payload[i]); + if (i < eventData.Payload?.Count - 1) + { + sb.Append(", "); + } + } + + sb.Append(")"); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine(sb.ToString()); + Console.ResetColor(); + } + } + } + + +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/UserHelper.cs b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/UserHelper.cs new file mode 100644 index 00000000..75dd26bf --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/UserHelper.cs @@ -0,0 +1,36 @@ +using System; +using System.Text.Json; +using System.Net; +using System.Net.Sockets; +using System.IO; +using System.Collections.Generic; +using LibData; + +namespace UserHelper +{ + // Note: Do not change this class. + public class Setting + { + public int ServerPortNumber { get; set; } + public int BookHelperPortNumber { get; set; } + public int UserHelperPortNumber { get; set; } + public string ServerIPAddress { get; set; } + public string BookHelperIPAddress { get; set; } + public string UserHelperIPAddress { get; set; } + public int ServerListeningQueue { get; set; } + } + + // Note: Complete the implementation of this class. You can adjust the structure of this class. + public class SequentialHelper + { + public SequentialHelper() + { + //todo: implement the body. Add extra fields and methods to the class if needed + } + + public void start() + { + //todo: implement the body. Add extra fields and methods to the class if needed + } + } +} diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/UserHelper.csproj b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/UserHelper.csproj new file mode 100644 index 00000000..1d2d39a9 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/UserHelper.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Users.json b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Users.json new file mode 100644 index 00000000..2eb58898 --- /dev/null +++ b/temp/DistLibraryFinalTeachers/DistLibraryFinalTeachers/LibUserHelper/Users.json @@ -0,0 +1 @@ +[{"User_id":"user-1","Name":"Lawrence Cote","Email":"libero@Maecenas.edu","Phone":"0685-603094"},{"User_id":"user-2","Name":"Abdul Barker","Email":"in@Pellentesqueultricies.co.uk","Phone":"0219-483408"},{"User_id":"user-3","Name":"Zeus Aguirre","Email":"ut.aliquam.iaculis@euismodmauriseu.ca","Phone":"0855-091224"},{"User_id":"user-4","Name":"Octavius Hardin","Email":"Nunc@eudolor.org","Phone":"0383-935269"},{"User_id":"user-5","Name":"Kirk Burch","Email":"tempor.est@montesnascetur.co.uk","Phone":"0972-899277"},{"User_id":"user-6","Name":"Raphael Cruz","Email":"purus@risusIn.net","Phone":"0353-696808"},{"User_id":"user-7","Name":"Charles Hughes","Email":"turpis.egestas@ac.com","Phone":"0655-024058"},{"User_id":"user-8","Name":"William Hoffman","Email":"Nullam@pedenec.com","Phone":"0717-814111"},{"User_id":"user-9","Name":"Cyrus Frye","Email":"Sed.dictum.Proin@nonbibendumsed.edu","Phone":"0317-074024"},{"User_id":"user-10","Name":"Lars Morin","Email":"consequat.lectus@liberoettristique.com","Phone":"0477-714268"},{"User_id":"user-11","Name":"Zephania Higgins","Email":"magna@ullamcorperDuis.net","Phone":"0843-150941"},{"User_id":"user-12","Name":"Kirk Spence","Email":"aliquam.eros@malesuadaaugue.net","Phone":"0337-574531"},{"User_id":"user-13","Name":"Chadwick Mosley","Email":"mauris.erat@non.ca","Phone":"0273-278583"},{"User_id":"user-14","Name":"Kato Suarez","Email":"rutrum.lorem.ac@Suspendisse.ca","Phone":"0384-756094"},{"User_id":"user-15","Name":"Wesley Nielsen","Email":"magna@Vivamusrhoncus.co.uk","Phone":"0138-243319"},{"User_id":"user-16","Name":"Ezra Flores","Email":"ut@nonloremvitae.ca","Phone":"0645-369359"},{"User_id":"user-17","Name":"Drew Sears","Email":"sem.vitae.aliquam@quamafelis.org","Phone":"0709-018874"},{"User_id":"user-18","Name":"David Newman","Email":"enim.Suspendisse@enim.org","Phone":"0750-521546"},{"User_id":"user-19","Name":"Lyle Contreras","Email":"et.pede.Nunc@at.edu","Phone":"0326-411776"},{"User_id":"user-20","Name":"Fritz Barry","Email":"Nullam.feugiat.placerat@ornare.ca","Phone":"0319-371410"},{"User_id":"user-21","Name":"Ferdinand Chaney","Email":"at.lacus.Quisque@id.ca","Phone":"0416-431601"},{"User_id":"user-22","Name":"Cullen Benjamin","Email":"et.arcu.imperdiet@sapienCras.com","Phone":"0441-560126"},{"User_id":"user-23","Name":"Dean Nielsen","Email":"augue.eu.tellus@ultricies.co.uk","Phone":"0964-908558"},{"User_id":"user-24","Name":"Harding Beach","Email":"pede.Nunc@eleifendnec.co.uk","Phone":"0901-635151"},{"User_id":"user-25","Name":"Zahir Byrd","Email":"ut.quam.vel@ornarelectus.ca","Phone":"0963-399167"},{"User_id":"user-26","Name":"Zeus Fischer","Email":"odio.sagittis.semper@necquam.com","Phone":"0227-120259"},{"User_id":"user-27","Name":"Bruno Berg","Email":"nulla.at.sem@adipiscingelitCurabitur.org","Phone":"0892-563043"},{"User_id":"user-28","Name":"Victor May","Email":"Vivamus@sedfacilisisvitae.ca","Phone":"0297-033411"},{"User_id":"user-29","Name":"Calvin Christian","Email":"sapien@dis.com","Phone":"0932-647318"},{"User_id":"user-30","Name":"Beau Randall","Email":"a.neque@enimCurabiturmassa.ca","Phone":"0480-572528"},{"User_id":"user-31","Name":"Drake Hester","Email":"Proin.dolor.Nulla@sed.net","Phone":"0646-239983"},{"User_id":"user-32","Name":"Ulric Reynolds","Email":"neque.Nullam@utcursusluctus.ca","Phone":"0714-727819"},{"User_id":"user-33","Name":"Kato Maldonado","Email":"urna@lectus.org","Phone":"0560-928761"},{"User_id":"user-34","Name":"Quinlan Carney","Email":"id.ante@aliquamadipiscing.com","Phone":"0450-643838"},{"User_id":"user-35","Name":"Finn Vaughn","Email":"tempus@nasceturridiculus.org","Phone":"0287-050131"},{"User_id":"user-36","Name":"Clayton Cohen","Email":"risus@idenim.net","Phone":"0406-884561"},{"User_id":"user-37","Name":"Benjamin Forbes","Email":"Quisque.ornare.tortor@anteblandit.net","Phone":"0873-962366"},{"User_id":"user-38","Name":"Barry Williams","Email":"ornare.elit.elit@nec.com","Phone":"0639-145364"},{"User_id":"user-39","Name":"Lev Peterson","Email":"a.enim@Quisque.org","Phone":"0989-736392"},{"User_id":"user-40","Name":"Alfonso Chase","Email":"In.faucibus@velitCraslorem.ca","Phone":"0680-959374"},{"User_id":"user-41","Name":"Forrest Bray","Email":"nunc.sed.libero@nec.com","Phone":"0137-869128"},{"User_id":"user-42","Name":"Leo Pitts","Email":"ut@tristiquenequevenenatis.org","Phone":"0770-852522"},{"User_id":"user-43","Name":"Darius Whitaker","Email":"Aliquam.vulputate@Duisami.co.uk","Phone":"0293-169307"},{"User_id":"user-44","Name":"Silas Jacobson","Email":"lorem.auctor.quis@scelerisque.org","Phone":"0561-348451"},{"User_id":"user-45","Name":"Callum Poole","Email":"et.commodo.at@Suspendisse.org","Phone":"0198-273496"},{"User_id":"user-46","Name":"Garrison Diaz","Email":"nunc.Quisque.ornare@a.edu","Phone":"0285-048090"},{"User_id":"user-47","Name":"Cedric Ewing","Email":"at@fringillami.org","Phone":"0804-931149"},{"User_id":"user-48","Name":"Nicholas Foreman","Email":"velit.dui@diam.edu","Phone":"0486-834702"},{"User_id":"user-49","Name":"Ashton Pitts","Email":"auctor@dictumeleifendnunc.ca","Phone":"0771-872541"},{"User_id":"user-50","Name":"Brady Orr","Email":"sem.magna.nec@loremac.co.uk","Phone":"0387-182017"},{"User_id":"user-51","Name":"Steven Gentry","Email":"consequat.lectus@at.edu","Phone":"0667-887815"},{"User_id":"user-52","Name":"Clarke Salas","Email":"mauris@lobortisnisinibh.com","Phone":"0132-328522"},{"User_id":"user-53","Name":"Colin Morales","Email":"mauris.ut.mi@IncondimentumDonec.com","Phone":"0231-667196"},{"User_id":"user-54","Name":"Drew Fitzgerald","Email":"et.magnis.dis@Phasellusliberomauris.ca","Phone":"0111-215211"},{"User_id":"user-55","Name":"Gabriel Navarro","Email":"et.rutrum.eu@eget.edu","Phone":"0533-619585"},{"User_id":"user-56","Name":"Driscoll Heath","Email":"posuere.at.velit@Phasellus.ca","Phone":"0572-369168"},{"User_id":"user-57","Name":"Nash Vinson","Email":"metus.sit.amet@Fusce.net","Phone":"0222-090547"},{"User_id":"user-58","Name":"Omar Gonzalez","Email":"eget@Etiam.edu","Phone":"0266-826040"},{"User_id":"user-59","Name":"Cadman Hunt","Email":"conubia@semperet.com","Phone":"0323-643550"},{"User_id":"user-60","Name":"Samson Baxter","Email":"ac.mattis.velit@ipsum.edu","Phone":"0468-975433"},{"User_id":"user-61","Name":"Connor Ball","Email":"ligula.Aenean@semegestasblandit.org","Phone":"0346-113151"},{"User_id":"user-62","Name":"Ulysses Hahn","Email":"cursus.purus@ac.org","Phone":"0792-099607"},{"User_id":"user-63","Name":"Clinton Hancock","Email":"egestas@sapienAeneanmassa.co.uk","Phone":"0741-605720"},{"User_id":"user-64","Name":"Gage Snow","Email":"Aenean.euismod@loremsemperauctor.ca","Phone":"0899-485786"},{"User_id":"user-65","Name":"Logan Sexton","Email":"cursus.Nunc@loremfringillaornare.edu","Phone":"0559-202978"},{"User_id":"user-66","Name":"Demetrius Wade","Email":"tincidunt.neque@Fuscefeugiat.edu","Phone":"0756-225542"},{"User_id":"user-67","Name":"Brock Griffin","Email":"ultrices.posuere.cubilia@hendreritidante.edu","Phone":"0386-685810"},{"User_id":"user-68","Name":"Hedley Kramer","Email":"id@risus.org","Phone":"0979-044631"},{"User_id":"user-69","Name":"Damian Sullivan","Email":"non.sollicitudin.a@gravidanunc.ca","Phone":"0635-912048"},{"User_id":"user-70","Name":"Zachary Wolf","Email":"velit@interdumenimnon.org","Phone":"0403-287732"},{"User_id":"user-71","Name":"Wylie Buck","Email":"erat@lorem.org","Phone":"0863-375851"},{"User_id":"user-72","Name":"Hilel Roberson","Email":"nisi@velfaucibus.co.uk","Phone":"0502-434070"},{"User_id":"user-73","Name":"Kadeem Chavez","Email":"nec@Nullamfeugiat.com","Phone":"0892-585725"},{"User_id":"user-74","Name":"Rooney Welch","Email":"ultricies.dignissim.lacus@Maecenasmalesuadafringilla.org","Phone":"0120-431647"},{"User_id":"user-75","Name":"Cody Woodward","Email":"cubilia@ac.edu","Phone":"0219-368773"},{"User_id":"user-76","Name":"Jackson Terrell","Email":"mollis@sedpedeCum.ca","Phone":"0406-456026"},{"User_id":"user-77","Name":"Merrill Benton","Email":"Quisque.varius@libero.org","Phone":"0869-700827"},{"User_id":"user-78","Name":"Xander Sutton","Email":"Integer.vulputate.risus@nonummyultriciesornare.net","Phone":"0742-274863"},{"User_id":"user-79","Name":"Carson Guerrero","Email":"fringilla.ornare.placerat@utsemNulla.co.uk","Phone":"0200-027298"},{"User_id":"user-80","Name":"Keegan Dodson","Email":"a.tortor@Sed.ca","Phone":"0119-720334"},{"User_id":"user-81","Name":"Laith Aguirre","Email":"blandit.viverra.Donec@Nuncacsem.ca","Phone":"0764-013374"},{"User_id":"user-82","Name":"Burton Garrett","Email":"facilisis.non@felisorciadipiscing.net","Phone":"0358-606518"},{"User_id":"user-83","Name":"Quinlan Santiago","Email":"arcu.Vivamus@etrutrumnon.ca","Phone":"0490-902034"},{"User_id":"user-84","Name":"Joseph Mclean","Email":"litora@congue.org","Phone":"0518-537486"},{"User_id":"user-85","Name":"Francis Campos","Email":"libero@molestietortor.org","Phone":"0574-740285"},{"User_id":"user-86","Name":"Oren Bender","Email":"risus.Donec@Seddiamlorem.com","Phone":"0320-973456"},{"User_id":"user-87","Name":"Rogan Watson","Email":"dolor.dolor.tempus@pellentesquea.org","Phone":"0120-075623"},{"User_id":"user-88","Name":"Cade Schwartz","Email":"id@Craseutellus.org","Phone":"0254-018877"},{"User_id":"user-89","Name":"Joshua Farley","Email":"non.egestas.a@nasceturridiculus.com","Phone":"0412-514901"},{"User_id":"user-90","Name":"John Williams","Email":"nec.tellus@anteipsumprimis.co.uk","Phone":"0689-499424"},{"User_id":"user-91","Name":"Ishmael Santos","Email":"aliquam.arcu.Aliquam@nonsapien.com","Phone":"0401-468173"},{"User_id":"user-92","Name":"Adam Sanford","Email":"laoreet.posuere.enim@sollicitudincommodo.co.uk","Phone":"0954-483461"},{"User_id":"user-93","Name":"Graham Valenzuela","Email":"elementum.dui@Quisquelibero.ca","Phone":"0371-799230"},{"User_id":"user-94","Name":"Solomon Hodges","Email":"dictum.cursus@varius.ca","Phone":"0299-558815"},{"User_id":"user-95","Name":"Cain Jacobson","Email":"lobortis@etmalesuada.edu","Phone":"0732-793443"},{"User_id":"user-96","Name":"Rafael Russo","Email":"adipiscing.ligula@penatibuset.org","Phone":"0458-001240"},{"User_id":"user-97","Name":"Barclay Dennis","Email":"magna@elit.net","Phone":"0240-122074"},{"User_id":"user-98","Name":"Joshua Barton","Email":"nec.mauris.blandit@feugiatmetus.co.uk","Phone":"0568-468707"},{"User_id":"user-99","Name":"Paul Thompson","Email":"Sed.eu.nibh@Sedneque.edu","Phone":"0481-532470"},{"User_id":"user-100","Name":"Akeem Nicholson","Email":"dapibus@enimmitempor.co.uk","Phone":"0818-909694"}] \ No newline at end of file