Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  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 <stdlib.h>
  12. #include <stdio.h>
  13. #include <netinet/in.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include <unistd.h>
  17. #include <math.h>
  18.  
  19. #define SIMPLE_SERVER_VERSION 1.0
  20. #define REQUEST_BUFFER_SIZE 1000
  21. #define DEFAULT_PORT 1917
  22. #define NUMBER_OF_PAGES_TO_SERVE 1000
  23. #define BYTES_PER_PIXEL 3
  24. #define BITS_PER_PIXEL (BYTES_PER_PIXEL*8)
  25. #define NUMBER_PLANES 1
  26. #define PIX_PER_METRE 2835
  27. #define MAGIC_NUMBER 0x4d42
  28. #define NO_COMPRESSION 0
  29. #define OFFSET 54
  30. #define DIB_HEADER_SIZE 40
  31. #define NUM_COLORS 0
  32.  
  33. #define SIZE 512
  34. #define MAXITERATIONS 256
  35. #define BMP_FILE "mandelbrot.bmp"
  36. #define TRUE 1
  37. #define FALSE 0
  38.  
  39. int waitForConnection (int serverSocket);
  40. int makeServerSocket (int portno);
  41. void serveBMP (int socket, double x, double y, double z);
  42. int escapeSteps (double x, double y);
  43. static void serveHTML (int socket)
  44.  
  45. // after serving this many pages the server will halt
  46.  
  47. int main (int argc, char *argv[]) {
  48. double xRequest;
  49. double yRequest;
  50. int zRequest;
  51.  
  52. char xCheck;
  53. char yCheck;
  54. char zCheck;
  55.  
  56. printf ("************************************\n");
  57. printf ("Starting simple server %f\n", SIMPLE_SERVER_VERSION);
  58. printf ("Serving bmps since 2012\n");
  59.  
  60. int serverSocket = makeServerSocket (DEFAULT_PORT);
  61. printf ("Access this server at http://localhost:%d/\n", DEFAULT_PORT);
  62. printf ("************************************\n");
  63.  
  64. char request[REQUEST_BUFFER_SIZE];
  65.  
  66. int numberServed = 0;
  67. while (numberServed < NUMBER_OF_PAGES_TO_SERVE) {
  68.  
  69. printf ("*** So far served %d pages ***\n", numberServed);
  70.  
  71. int connectionSocket = waitForConnection (serverSocket);
  72. // wait for a request to be sent from a web browser, open a new
  73. // connection for this conversation
  74.  
  75. // read the first line of the request sent by the browser
  76. int bytesRead;
  77. bytesRead = read (connectionSocket, request, (sizeof request)-1);
  78. assert (bytesRead >= 0);
  79. // were we able to read any data from the connection?
  80.  
  81. // print entire request to the console
  82. printf (" *** Received http request ***\n %s\n", request);
  83.  
  84. //send the browser a simple html page using http
  85. printf (" *** Sending http response ***\n");
  86.  
  87. sscanf(request, "GET /tile_%c%lf_%c%lf_%c%d.bmp", &xCheck, xRequest, &yCheck, &yRequest, &zCheck, zRequest);
  88. if ((xCheck == 'x') && (yCheck == 'y') && (zCheck == 'z')
  89. serveBMP (connectionSocket, xRequest, yRequest, zRequest);
  90. } else {
  91. serveHTML (socket);
  92. }
  93. // close the connection after sending the page- keep aust beautiful
  94. close(connectionSocket);
  95.  
  96. numberServed++;
  97.  
  98. // close the server connection after we are done- keep aust beautiful
  99. printf ("** shutting down the server **\n");
  100. close (serverSocket);
  101.  
  102. return EXIT_SUCCESS;
  103. }
  104.  
  105. void serveBMP (int socket) {
  106. char* message;
  107.  
  108. // first send the http response header
  109.  
  110. // (if you write stings one after another like this on separate
  111. // lines the c compiler kindly joins them togther for you into
  112. // one long string)
  113. message = "HTTP/1.0 200 OK\r\n"
  114. "Content-Type: image/bmp\r\n"
  115. "\r\n";
  116. printf ("about to send=> %s\n", message);
  117. write (socket, message, strlen (message));
  118.  
  119. // now send the BMP
  120. unsigned char bmpheader[54] = {
  121. 0x42,0x4d,0x36,0x00,0x0c,0x00,0x00,0x00,
  122. 0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00,
  123. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,
  124. 0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00,
  125. 0x00,0x00,0x00,0x00,0x0c,0x00,0x13,0x0b,
  126. 0x00,0x00,0x13,0x0b,0x00,0x00,0x00,0x00,
  127. 0x00,0x00,0x00,0x00,0x00,0x00};
  128.  
  129. write (socket, bmpheader, sizeof(bmpheader));
  130.  
  131. int x = 0;
  132. int y = 0;
  133. while (y < SIZE) {
  134. x = 0;
  135. while (x < SIZE) {
  136. if (escapeSteps ((-2 + (double) x * 0.005), (1.25 - (double) y * 0.005)) == MAXITERATIONS) {
  137. int intensity = 0;
  138. char pixel[BYTES_PER_PIXEL] = {intensity, intensity, intensity};
  139. write (socket, pixel, sizeof(pixel));
  140. } else {
  141. int intensity = 255;
  142. char pixel[BYTES_PER_PIXEL] = {intensity, intensity, intensity};
  143. write (socket, pixel, sizeof(pixel));
  144. }
  145. x++;
  146. }
  147. y++;
  148. }
  149. }
  150.  
  151. // start the server listening on the specified port number
  152. int makeServerSocket (int portNumber) {
  153.  
  154. // create socket
  155. int serverSocket = socket (AF_INET, SOCK_STREAM, 0);
  156. assert (serverSocket >= 0);
  157. // error opening socket
  158.  
  159. // bind socket to listening port
  160. struct sockaddr_in serverAddress;
  161. bzero ((char *) &serverAddress, sizeof (serverAddress));
  162.  
  163. serverAddress.sin_family = AF_INET;
  164. serverAddress.sin_addr.s_addr = INADDR_ANY;
  165. serverAddress.sin_port = htons (portNumber);
  166.  
  167. // let the server start immediately after a previous shutdown
  168. int optionValue = 1;
  169. setsockopt (
  170. serverSocket,
  171. SOL_SOCKET,
  172. SO_REUSEADDR,
  173. &optionValue,
  174. sizeof(int)
  175. );
  176.  
  177. int bindSuccess =
  178. bind (
  179. serverSocket,
  180. (struct sockaddr *) &serverAddress,
  181. sizeof (serverAddress)
  182. );
  183.  
  184. assert (bindSuccess >= 0);
  185. // if this assert fails wait a short while to let the operating
  186. // system clear the port before trying again
  187.  
  188. return serverSocket;
  189. }
  190.  
  191. // wait for a browser to request a connection,
  192. // returns the socket on which the conversation will take place
  193. int waitForConnection (int serverSocket) {
  194. // listen for a connection
  195. const int serverMaxBacklog = 10;
  196. listen (serverSocket, serverMaxBacklog);
  197.  
  198. // accept the connection
  199. struct sockaddr_in clientAddress;
  200. socklen_t clientLen = sizeof (clientAddress);
  201. int connectionSocket =
  202. accept (
  203. serverSocket,
  204. (struct sockaddr *) &clientAddress,
  205. &clientLen
  206. );
  207.  
  208. assert (connectionSocket >= 0);
  209. // error on accept
  210.  
  211. return (connectionSocket);
  212. }
  213.  
  214. int escapeSteps (double x, double y) {
  215. int iterations = 0;
  216. double zReal = 0;
  217. double zImaginary = 0;
  218. double tempX = 0;
  219. double tempY = 0;
  220. double distance = 0;
  221.  
  222. while ((iterations < MAXITERATIONS) && (distance < 4)) {
  223. tempX = zReal;
  224. tempY = zImaginary;
  225. zImaginary = (2 * tempX * tempY) + y;
  226. zReal = (tempX * tempX) - (tempY * tempY) + x;
  227. distance = (zReal * zReal) + (zImaginary * zImaginary);
  228. iterations++;
  229. }
  230. return iterations;
  231. }
  232.  
  233. static void serveHTML (int socket) {
  234. char* message;
  235.  
  236. // first send the http response header
  237. message =
  238. "HTTP/1.0 200 Found\n"
  239. "Content-Type: text/html\n"
  240. "\n";
  241. printf ("about to send=> %s\n", message);
  242. write (socket, message, strlen (message));
  243.  
  244. message =
  245. "<!DOCTYPE html>\n"
  246. "<script src=\"http://almondbread.cse.unsw.edu.au/tiles.js\"></script>"
  247. "\n";
  248. write (socket, message, strlen (message));
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement