OpenLearning

Edward Lin

edward.lin
  1. /*
  2. * bmpServer.c
  3. * 1917 serve that 3x3 bmp from lab3 Image activity
  4. *
  5. * Created by Tim Lambert on 02/04/12.
  6. * Containing code created by Richard Buckland on 28/01/11.
  7. * Copyright 2012 Licensed under Creative Commons SA-BY-NC 3.0.
  8. *
  9. */
  10.  
  11. #include "pixelColor.h"
  12. #include "mandelBrot.h"
  13.  
  14. // after serving this many pages the server will halt
  15. int main(int argc, char *argv[]) {
  16. printf("************************************\n");
  17. printf("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
  18. printf("Serving bmps since 2012\n");
  19. int serverSocket = makeServerSocket (DEFAULT_PORT);
  20. printf("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
  21. printf("************************************\n");
  22. char request[REQUEST_BUFFER_SIZE] = {0};
  23. int numberServed = 0;
  24.  
  25. while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
  26. printf("*** So far served %d pages ***\n", numberServed);
  27. int connectionSocket = waitForConnection(serverSocket);
  28. // wait for a request to be sent from a web browser, open a new
  29. // connection for this conversation
  30. // read the first line of the request sent by the browser
  31. int bytesRead;
  32. bytesRead = read(connectionSocket, request, (sizeof request)-1);
  33. assert(bytesRead >= 0);
  34. // were we able to read any data from the connection?
  35. double x1;
  36. double y1;
  37. double zoom1;
  38. char header[REQUEST_BUFFER_SIZE];
  39.  
  40. int position = 0;
  41. while (request[position] != 'b'){
  42. header[position] = request[position+5];
  43. position++;
  44. }
  45.  
  46. // print entire request to the console
  47. printf (" *** Received http request ***\n %s\n", request);
  48.  
  49. if (request[5] == ' ') { //if string size = 0
  50. serveHTML(connectionSocket);
  51. x1 = 0;
  52. y1 = 0;
  53. zoom1 = 0;
  54. } else {
  55. int counter1 = 1;
  56. char part_x[strlen(header)];
  57. while (header[counter1] != '_') {
  58. part_x[counter1-1] = header[counter1];
  59. counter1++;
  60. }
  61. part_x[counter1-1] = '\0'; //end string
  62.  
  63. counter1 = counter1 +2; // jump two to start of Y
  64.  
  65. int counter2 = 0;
  66.  
  67. char part_y[strlen(header)];
  68. while(header[counter1] != '_') {
  69. part_y[counter2] = header[counter1];
  70. counter1++;
  71. counter2++;
  72. }
  73. part_y[counter2] = '\0'; //end string
  74.  
  75. counter1 = counter1+2; //jump two to start of Z
  76.  
  77. char part_z[strlen(header)];
  78.  
  79. int count3 =0;
  80.  
  81. while (header[counter1] != '.') {
  82. part_z[count3] = header[counter1];
  83. count3++;
  84. counter1++;
  85. }
  86.  
  87. part_z[count3] = '\0'; //end of string
  88. char * pointer_x = (part_x);
  89. char * pointer_y = (part_y);
  90. char * pointer_z = (part_z);
  91. x1 = run_strtod(pointer_x);
  92. y1 = run_strtod(pointer_y);
  93. zoom1 = run_strtod(pointer_z);
  94.  
  95. //send the browser a simple html page using http
  96. printf (" *** Sending http response ***\n");
  97. serveBMP(connectionSocket, x1, y1, zoom1);
  98. }
  99. // close the connection after sending the page- keep aust beautiful
  100. close(connectionSocket);
  101. numberServed++;
  102.  
  103. }
  104. // close the server connection after we are done- keep aust beautiful
  105. printf("** shutting down the server **\n");
  106. close(serverSocket);
  107. return EXIT_SUCCESS;
  108. }
  109.  
  110.  
  111. void serveBMP(int socket, double x1, double y1, double zoom1) {
  112. char* message;
  113.  
  114. // (if you write stings one after another like this on separate
  115. // lines the c compiler kindly joins them togther for you into
  116. // one long string)
  117.  
  118. message = "HTTP/1.0 200 OK\r\n"
  119. "Content-Type: image/bmp\r\n"
  120. "\r\n";
  121. // first send the http response header
  122. printf ("about to send=> %s\n", message);
  123. write (socket, message, strlen (message));
  124. // now send the BMP
  125. unsigned char bmp[BMP_FILE_SIZE] = {0};
  126. createBMP(bmp, x1, y1, zoom1);
  127. write (socket, bmp, sizeof(bmp));
  128.  
  129. }
  130. // -------------------------------------------------------
  131. // start the server listening on the specified port number
  132. // -------------------------------------------------------
  133.  
  134. int makeServerSocket(int portNumber) {
  135. // create socket
  136. int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
  137. assert(serverSocket >= 0);
  138. // error opening socket
  139. // bind socket to listening port
  140. struct sockaddr_in serverAddress;
  141. bzero((char *) &serverAddress, sizeof(serverAddress));
  142. serverAddress.sin_family = AF_INET;
  143. serverAddress.sin_addr.s_addr = INADDR_ANY;
  144. serverAddress.sin_port = htons(portNumber);
  145. // let the server start immediately after a previous shutdown
  146. int optionValue = 1;
  147. setsockopt(
  148. serverSocket,
  149. SOL_SOCKET,
  150. SO_REUSEADDR,
  151. &optionValue,
  152. sizeof(int)
  153. );
  154. int bindSuccess =
  155. bind(
  156. serverSocket,
  157. (struct sockaddr *) &serverAddress,
  158. sizeof (serverAddress)
  159. );
  160. assert(bindSuccess >= 0);
  161. // if this assert fails wait a short while to let the operating
  162. // system clear the port before trying again
  163. return serverSocket;
  164. }
  165.  
  166. // ------------------------------------------------------------
  167. // wait for a browser to request a connection,
  168. // returns the socket on which the conversation will take place
  169. // ------------------------------------------------------------
  170.  
  171. int waitForConnection(int serverSocket) {
  172. // listen for a connection
  173. const int serverMaxBacklog = 10;
  174. listen(serverSocket, serverMaxBacklog);
  175. // accept the connection
  176. struct sockaddr_in clientAddress;
  177. socklen_t clientLen = sizeof (clientAddress);
  178. int connectionSocket =
  179. accept(
  180. serverSocket,
  181. (struct sockaddr *) &clientAddress,
  182. &clientLen
  183. );
  184.  
  185. assert(connectionSocket >= 0);
  186. // error on accept
  187. return (connectionSocket);
  188. }
  189.  
  190. int createBMP(unsigned char bmp[BMP_FILE_SIZE], double x1, double y1, double zoom1) {
  191.  
  192. double y = y1;
  193. double x = x1;
  194. double zoom = zoom1;
  195.  
  196. zoom = configureZoom(zoom);
  197.  
  198. unsigned char header[] =
  199. {0x42,0x4d,0x5a,0x00,0x00,0x00,0x00,0x00,
  200. 0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
  201. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,
  202. 0x00,0x00,
  203. 0x01,0x00,0x18,0x00,0x00,0x00,0x00,0x00,
  204. 0x24,0x00,0x00,0x00,0x13,0x0b,0x00,0x00,
  205. 0x13,0x0b,0x00,0x00,0x00,0x00,
  206. 0x00,0x00,0x00,0x00,0x00,0x00,};
  207.  
  208. unsigned char body[BMP_BODY_SIZE] = {0};
  209. int pixel_array = 0;
  210. double yposition = 0;
  211.  
  212. while (yposition< bmpSize) {
  213.  
  214. double xposition = 0;
  215. while (xposition < bmpSize) {
  216. int iterations = setCheck(yposition, xposition, x, y, zoom);
  217. if (setCheck(yposition, xposition, x, y, zoom) > minIterations &&
  218. setCheck(yposition, xposition, x, y, zoom) < maxIterations) {
  219. body[pixel_array] = stepsToBlue(iterations);
  220. body[pixel_array+1] = stepsToGreen(iterations);
  221. body[pixel_array+2] = stepsToRed(iterations);
  222. pixel_array = pixel_array+3;
  223. } else {
  224. body[pixel_array] = 0x00;
  225. body[pixel_array+1] = 0x00;
  226. body[pixel_array+2] = 0x00;
  227. pixel_array = pixel_array+3;
  228. }
  229. xposition++;
  230. }
  231. yposition++;
  232. }
  233.  
  234. int count1 = 0;
  235. while (count1 <BMP_HEADER_SIZE) {
  236. bmp[count1] = header[count1];
  237. count1++;
  238. }
  239.  
  240. int count2 = 0;
  241. while (count2 < BMP_BODY_SIZE) {
  242. bmp[count2+BMP_HEADER_SIZE] = body[count2]; //these two loops just put
  243. count2++; //the header and the body together
  244. }
  245.  
  246. return EXIT_SUCCESS;
  247. }
  248.  
  249.  
  250. int setCheck(double yposition, double xposition, double x, double y, double zoom) {
  251.  
  252. double point_x = (xposition - maxIterations)/(zoom) +x; // the -2 just orientates it , and adding
  253. // x makes the new co ordinate the origin
  254. double point_y = (yposition - maxIterations)/(zoom) +y;
  255. int count = 0;
  256. double Re = 0;
  257. double Im = 0;
  258. double tempx = 0;
  259. while ((count < maxIterations) && ((Re*Re + Im*Im) <= 4)) {
  260. tempx = Re;
  261. Re = (Re)*(Re) - (Im)*(Im) + point_x;
  262. Im = (2*tempx*Im) + point_y;
  263. count++;
  264. }
  265.  
  266. return count;
  267. }
  268.  
  269. double configureZoom(double zoom) {
  270.  
  271. double number = 2;
  272. int counter = 0;
  273. while (counter != zoom) {
  274. number = 2*number;
  275. counter++;
  276. }
  277. number = number/2;
  278.  
  279. return number;
  280. }
  281.  
  282.  
  283. void serveHTML(int socket) {
  284. char* message;
  285. // first send the http response header
  286. message =
  287. "HTTP/1.0 200 Found\n"
  288. "Content-Type: text/html\n"
  289. "\n";
  290. printf("about to send=> %s\n", message);
  291. write(socket, message, strlen(message));
  292. message =
  293. "<!DOCTYPE html>\n"
  294. "<html>\n"
  295. "<script src=\"https://almondbread.openlearning.com/tileviewer.js\"></script>\n"
  296. "</html>\n";
  297. write(socket, message, strlen(message));
  298. }
  299.  
  300.  
  301. double run_strtod(char * input) {
  302. double output;
  303. char * end;
  304.  
  305. output = strtod(input, & end);
  306. return output;
  307. }

Download file: mandelBrot.c (9.1 KB)

Comments

Loading
Error: Loading CSS chunk web_containers_LearnersConsentModal_connector_ts failed. (error: https://assets.openlearning.com/chunks_live/web_containers_LearnersConsentModal_connector_ts.fdabdb72241075de.css)
Error: Loading CSS chunk web_components_AcceptCustomTnc_component_tsx-node_modules_shallowequal_index_js-node_modules_-ccf26b failed. (error: https://assets.openlearning.com/chunks_live/web_components_AcceptCustomTnc_component_tsx-node_modules_shallowequal_index_js-node_modules_-ccf26b.169ec8446de2a217.css)

Chat