/* Client.cs * Version: Dec. 5, 2014 * * Changelog: * Dec. 5, 2014: Histogram server reply update (typo * Dec. 4, 2014: Exercise 20 corrected * Dec. 3, 2014: Initial version */ using System; using System.IO; using System.Text; using System.Collections; using System.Net.Sockets; using System.Drawing; namespace RIPS { class Client { Socket s; Bencode codec; static public void Main (string [] args) { Run (args); } static public void Run (string [] args) { Client c; ClientOptions opt; foreach (string s in args) Console.WriteLine ("arg {0}", s); if (args.Length == 0) { ClientOptions.Usage (); return; } opt = new ClientOptions (args); c = new Client (); c.Connect (); if (opt.Request == "histogram") { c.Histogram (opt); } else if (opt.Request == "blur") { c.Blur (opt); } else if (opt.Request == "merge") { c.Merge (opt); } else { throw new Exception ("Internal problem"); } c.Disconnect (); Console.WriteLine ("Have a good day"); } public Client () { /* Exercise 12 * =========== * * Create a TCP socket (a Socket, not a TcpClient object) and * store it in the filed "s" */ s = null; // NOTE - these two lines are here just to avoid a compiler // warning, remove them !! Console.WriteLine (s); Console.WriteLine (codec); } IPEndPoint LocateServer () { /* Exercise 13 * =========== * * Using an instance of the class TcpClient, make a very simple * HTTP request to download the file * * http://lipn.univ-paris13.fr/~rodriguez/teach/dotnet/2014-15/rips/servers.php * * You will retrieve a plain-text file. Locate the line associated * to your server and return an object of type IPEndPoint storing * the IP and port of your server. * */ return null; } void Connect () { /* Exercise 16 * =========== * * 1. Call LocateServer to retrieve the ip and port of the server * to which we want to connect * 2. Using the socket "s", open a TCP connection to the server * 3. Construct an instance of the Bencode class to read and write * to the connection, set it up in the field "codec" */ codec = new Bencode (RecvByte, SendByte); } void Disconnect () { /* Exercise 17 * =========== * * Close the socket */ } void Histogram (ClientOptions opt) { /* Exercise 20 * =========== * * This methods sends a request to the server to compute the * histogram of an image. It then parses the reply, handles * possible errors and saves the histogram to disk. * * When this method is called, * - the socket is already connected to the server * - the Bencode codec is attached to the socket * - the parameter "opt" contains the filepath to the image and * any other necessary information * * The server will first convert the image to grayscale, with each * pixel storing an 8-bit level of gray (levels from 0, completely * black, to 255, completely white). Then, for each of 256 * possible levels of gray, it will count the number of pixels in * the image and return an array with 256 positions, each * contining the count of pixels for the index associated to that * position. * * The request will be a SortedList associative table with the * following key/value paris (keys are always strings): * * "request" => "histogram" * "image" => The byte array returned by LoadImage * "width" => The number of columns in the image * "height" => The number of lines in the image * "nthreads" => Number of worker threads that the server will use * to carry out the computation * * The server will reply with another associative table with the * following key/value paris: * * "response" => either the string "ok" or an string starting * with "error, " (comma, space) * "histogram" => an ArrayList with 256 positions, where position * n contains the number of pixels of graylevel n * * * Your task is as follows: * * 1. Build a SortedList as described above, send it to the codec * 2. Read, from the codec, the answer from the server * 3. Analyze the response, if it contains errors, handle them * appropriately * 4. Write the histogram in a human-readable format (plain text) * to the disk, on file opt.Outfile */ } void Blur (ClientOptions opt) { /* Exercise xx * =========== * * This methods sends a request to the server to blur an image. * It parses the reply, handles possible errors and saves the * resulting image to disk. * * When this method is called, * - the socket is already connected to the server * - the Bencode codec is attached to the socket * - the parameter "opt" contains the filepath to the image and * any other necessary information * * To blur an image we will substitute the RGB (red, green, blue) * levels of a pixel by the average levels of the surrounding * pixels forming a "ring" (in fact, a square) centered at the * pixel. How many of those concentric rings we include in the * average will be controlled by the "contour" parameter. * * The request will be a SortedList associative table with the * same key/value paris as in the Histogram method, except that * the "request" key will be now as follows: * * "request" => "blur" * * Additionally, we will include the pair * * "contour" => the number of concentric "rings" to include when * averaging a pixel * * The server will reply with another associative table with the * following key/value paris: * * "response" => either the string "ok" or an string starting * with "error, " (comma, space) * "image" => the blurred image, encoded as before * * * Your task is as follows: * * 1. Build a SortedList as described above, send it to the codec * 2. Read, from the codec, the answer from the server * 3. Analyze the response, if it contains errors, handle them * appropriately * 4. Write the blurred image to disk, in .bmp format, on the file * indicated in opt.Outfile */ } void Merge (ClientOptions opt) { /* Exercise xx * =========== * * This methods sends a request to the server to merge two images. * It parses the reply, handles possible errors and saves the * resulting image to disk. * * When this method is called, * - the socket is already connected to the server * - the Bencode codec is attached to the socket * - the parameter "opt" contains the filepath to the image and * any other necessary information * * To merge image2 into image1 we will define a transparency * ratio r, from 0 to 100, and will define each pixel in the * result as a weighted sum of the two original pixels. * * The request will be, as usual, an associative array as in the * Histogram method, except that * the "request" key will be now as follows: * * "request" => "merge" * * Additionally, we will include the pair * * "transp" => a number from 0 to 100 indicating the * transparency of image2 (this number is stored in * opt.Alpha). * * The server will reply with another associative table with the * following key/value paris: * * "response" => either the string "ok" or an string starting * with "error, " (comma, space) * "image" => the resulting image, encoded as usual * * * Your task is as follows: * * 1. Build a SortedList as described above, send it to the codec * 2. Read, from the codec, the answer from the server * 3. Analyze the response, if it contains errors, handle them * appropriately * 4. Write the output image to disk, in .bmp format, on the file * indicated in opt.Outfile */ } void LoadImage (string filepath, out byte [] data, out int width, out int height) { /* Exercise 18 * =========== * * This method opens the image file "filepath" and reads into the * array "data" the data of the image. Assume the file you are * loading is a BMP file. The format of "data" is as follows. * * The red, green, and blue values of each pixel will be stored in * three consecutive bytes. The pixels will be stored in the * array line by line, starting from the first line. For each * line, the pixel at column 0 will come first, then the pixel at * column 1, etc. * * For instance, if the pixels at coordinates (x,y) * * (0,0) is red 4, green 99, blue 12, and * (0,1) is red 82, green 190, blue 78, * * then the first 6 bytes of the array will be: * * 4, 99, 12, 82, 190, 78 * * Reference: * System.Drawing.Bitmap */ data = null; width = height = 0; } void SaveImage (string filepath, byte [] data, int width, int height) { /* Exercise 19 * =========== * * Assuming that the "data" array is in the same format as in the * LoadImage method, write to the file "filepath" the image whose * height and width is given to you on the arguments and whose * pixel data is given to you by the "data" array. * */ } private byte RecvByte () { /* Exercise 14 * =========== * * Read a byte from the socket and return it; if you reach the * "end of file" (i.e., the connection has been closed), throw an * exception. */ return 0; } private void SendByte (byte b) { /* Exercise 15 * =========== * * Write the byte b to the socket. */ } static void Test1 () { /* Exercise 08: simple TCP client * ============================== * * (Notice below the "host" and "port" variables) * * Using the socket interface, open a TCP connection to the host * and port identified by the variables below. * * Then, in a loop: * - read input to send from the user * - send it through the connection * - read from the connection and print it to screen, together * with the number of bytes read * * Do this until the user types 'exit'. * * As an experiment, send the string httprequest to the web server * at www.univ-paris13.fr and print the result. */ //string host = "www.univ-paris13.fr"; //int port = 80; //string httprequest = "GET /robots.txt HTTP/1.0\r\nHost: www.univ-paris13.fr\r\n\r\n"; } static void Test2 () { /* Exercise 09: even simpler TCP client * ==================================== * * Repeat the previous exercise using the TcpClient class */ } } class ClientOptions { public string Request { get; private set; } public string Image1 { get; private set; } public string Image2 { get; private set; } public string Outfile { get; private set; } public int Threadnum { get; private set; } public int Contour { get; private set; } public int Alpha { get; private set; } public ClientOptions (string [] args) { int i; // parse the REQUEST and IMAGE1 if (args.Length < 2) throw new Exception ("Invalid number of arguments, invoke without arguments for more info."); Request = args[0]; if (Request != "histogram" && Request != "blur" && Request != "merge") throw new Exception ("Invalid REQUEST, call without arguments for more info."); Image1 = args[1]; // IMAGE2 required if we have merge if (Request == "merge") { if (args.Length < 3) throw new Exception ("Merge requires two images, call without arguments for more info."); Image2 = args[2]; i = 3; } else { i = 2; } // default values for options Outfile = "out.bmp"; if (Request == "histogram") Outfile = "out.txt"; Threadnum = 2; Contour = 3; Alpha = 50; // options, of the form /option:value for (; i < args.Length; i++) { string op, val; int j; j = args[i].IndexOf (':'); if (args[i][0] != '/' || j == -1) throw new Exception ("Ill-formed option, call without arguments for more info."); op = args[i].Substring (1, j - 1); val = args[i].Substring (j + 1); if (op == "out") { Outfile = val; } else if (op == "nthreads") { Threadnum = Convert.ToInt32 (val); } else if (op == "contour") { Contour = Convert.ToInt32 (val); } else if (op == "alpha") { Alpha = Convert.ToInt32 (val); } else throw new Exception ("Unexpected option, call without arguments for more info."); } } static public void Usage () { string s = @" Usage: Client REQUEST IMAGE1 [IMAGE2] [OPTIONS] Where REQUEST can be one of the following histogram - Outputs the histogram of IMAGE, by default to 'out.txt' blur - Blurs the image and stores the result, by default, to 'out.bmp' merge - Fuses IMAGE2 into IMAGE1, output to 'out.bmp' and where [OPTIONS] are zero or more options from the following list: /out:FILE - Saves the result image or histogram in FILE instead of the default file indicated above /nthreads:N - The server will use 2^N threads (default 2, ie, 4 threads used) /contour:N - (only for 'blur') Compute pixel averages using N concentring 'rings' (default 3) /alpha:N - (only for 'merge') Set the transparency of IMAGE2 to N percent, where 0% means IMAGE2 is completely opaque and 100% is completely transparent (default 50) "; Console.Write (s); } } } // vim: set efm=%f(%l\\,%c)\:\ %m: