Skip to content

Commit

Permalink
distlibrary is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
afshinamighi committed Sep 21, 2021
1 parent a2bf2fe commit 0543b97
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 37 deletions.
7 changes: 3 additions & 4 deletions Networking/DistLibrary/LibBookHelper/BookHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace BookHelper
{
// Note: Do not change this class.
public class Setting
{
public int serverPortNumber { get; set; }
Expand All @@ -22,16 +23,14 @@ public class Setting

public class SequentialHelper
{

public SequentialHelper()
{
//todo: implement the body. Add extra fields and methods to the class if it is needed
//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 it is needed
//todo: implement the body. Add extra fields and methods to the class if needed
}

}
}
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibBookHelper/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public class MessageType

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; }
Expand Down
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibBookHelper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace LibHelper
{
public class HelperSimulator
{
/// <summary>
/// initiates the helper
/// </summary>
public void sequentialRun()
{
SequentialHelper server = new SequentialHelper();
Expand All @@ -17,6 +20,10 @@ public void sequentialRun()

class Program
{
/// <summary>
/// Starts the simulation for a set of clients and produces the output results.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.Clear();
Expand Down
55 changes: 48 additions & 7 deletions Networking/DistLibrary/LibClient/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text.Json;
using System.Threading;
using LibData;
//todo: implement full client such that it follows the protocol discussed in the assignment.


namespace LibClient
{
Expand All @@ -20,28 +20,69 @@ public class Setting
public int serverListeningQueue { get; set; }
}

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 class SimpleClient
{
public string outcome;
// some of the fields are defined.
public Output result;
public Socket clientSocket;
public IPEndPoint serverEndPoint;
public IPAddress ipAddress;
public Setting settings;
public string user_id;
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

/// <summary>
/// Initializes the client based on the given parameters and seeting file.
/// </summary>
/// <param name="id">id of the clients provided by the simulator</param>
/// <param name="bookName">name of the book to be requested from the server, provided by the simulator</param>
public SimpleClient(int id, string bookName)
{
//todo: implement the body
//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<Setting>(configContent);
this.ipAddress = IPAddress.Parse(settings.serverIPAddress);
}
catch (Exception e)
{
Console.Out.WriteLine("[Client Exception] {0}", e.Message);
}
}

public string start()
/// <summary>
/// Establishes the connection with the server and requests the book according to the specified protocol.
/// Note: The signature of this method must not change.
/// </summary>
/// <returns>The result of the request</returns>
public Output start()
{
//todo: implement the body

// 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 outcome;
return result;
}

}
Expand Down
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibClient/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public class MessageType

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; }
Expand Down
58 changes: 38 additions & 20 deletions Networking/DistLibrary/LibClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,87 @@
//using LibClientSolution;


// NOTE: THIS FILE MUST NOT CHANGE

// NOTE: DO NOT CHANGE THIS FILE
namespace LibClientSimulator
{
public class InputData
{
public string BookName { get; set; }
}
public class TestCases
{
public SimpleClient client;
public string outcome;
}
public class ClientsSimulator
{
private SimpleClient client;
private List<InputData> inputDataList;
private TestCases[] tests;
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";

/// <summary>
/// Reads the input file and creats the clients and output objects accordingly
/// </summary>
public ClientsSimulator()
{
int id = 0;
try
{
string inputContent = File.ReadAllText(inputFile);
this.inputDataList = JsonSerializer.Deserialize<List<InputData>>(inputContent);
tests = new TestCases[this.inputDataList.Count];
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)
{
//each client has an id and a book to request
tests[id] = new TestCases();
tests[id].client = new SimpleClient(id, d.BookName);
clients[id] = new SimpleClient(id,d.BookName);
id++;
}
}catch (Exception e) { Console.Out.WriteLine("[ClientSimulator] Exception: {0}", e.Message); }
}

/// <summary>
/// Starts the book requesting process for each client and collects the result for each request
/// </summary>
public void startSimulation()
{
int numCases = tests.Length;
int numCases = clients.Length;
for (int i = 0; i < numCases; i++)
{
Console.Out.WriteLine("\n *********** \n");
tests[i].outcome = tests[i].client.start();
tests[i].client.delay();
results[i] = clients[i].start();
}
//this is the ending user
new SimpleClient(-1, "").start();
//this is the ending client
new SimpleClient(-1,"").start();
}

/// <summary>
/// Prints all the results in console and produces the output file
/// </summary>
public void printOutput()
{
Console.Out.WriteLine("\n *************** Final Output *********** \n");
for (int i = 0; i < tests.Length; i++)
Console.WriteLine("{0} : {1}", tests[i].client.user_id,tests[i].outcome);
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<Output[]>(this.results);
Console.WriteLine("Content of the Output file:\n {0}",outputContent);

File.WriteAllText(outputFile,outputContent);
}
}

class Program
{
/// <summary>
/// Starts the simulation for a set of clients and produces the output results.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.Clear();
Expand Down
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibServer/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public class MessageType

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; }
Expand Down
4 changes: 2 additions & 2 deletions Networking/DistLibrary/LibServer/LibServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace LibServer
{
// Note: Do not change this class.
public class Setting
{
public int serverPortNumber { get; set; }
Expand All @@ -20,8 +21,7 @@ public class Setting
}

public class SequentialServer
{

{
public SequentialServer()
{
//todo: implement the body. Add extra fields and methods to the class if it is needed
Expand Down
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public class ServerSimulator
public ServerSimulator()
{ }

/// <summary>
/// initiates the server
/// </summary>
public void sequentialRun()
{
SequentialServer server = new SequentialServer();
Expand All @@ -22,6 +25,10 @@ public void sequentialRun()

public class Program
{
/// <summary>
/// Starts the simulation for a set of clients and produces the output results.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.Clear();
Expand Down
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibUserHelper/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public class MessageType

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; }
Expand Down
7 changes: 7 additions & 0 deletions Networking/DistLibrary/LibUserHelper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace LibHelper
{
public class HelperSimulator
{
/// <summary>
/// initiates the helper
/// </summary>
public void sequentialRun()
{
SequentialHelper server = new SequentialHelper();
Expand All @@ -17,6 +20,10 @@ public void sequentialRun()

class Program
{
/// <summary>
/// Starts the simulation for a set of clients and produces the output results.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.Clear();
Expand Down
Loading

0 comments on commit 0543b97

Please sign in to comment.