forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
PhotoServer0613.cpp
1 #include "Np4d.h"
2 #include "NetBuffer.h"
3 #include <map>
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <string>
8 #include <pthread.h>
9 
10 #define CHUNK 1024
11 using namespace forest;
12 
13 void handleClient(int);
14 void* handler(void*);
15 
16 int main() {
17  // open stream socket and bind to port 30124
18 
19  int listenSock = Np4d::streamSocket();
20  if (listenSock < 0) fatal("can't create socket");
21  ipa_t myIP = Np4d::myIpAddress();
22  if (!Np4d::bind4d(listenSock, myIP, 30124)) fatal("can't bind socket");
23 
24  // prepare to accept connections
25  if (!Np4d::listen4d(listenSock)) fatal("error on listen");
26  while (true) {
27  // wait for incoming connection request and create new socket
28  int connSock = Np4d::accept4d(listenSock); //
29  std::cout << connSock << "connected" << std::endl;
30  if (connSock < 0) fatal("error on accept"); //include? for fatal
31 
32  // start a separate thread to handle this socket
33  handleClient(connSock);
34  }
35 }
36 
37 void handleClient(int sock) {
38  pthread_t thisThread;
39  int* sockp = new int;
40  *sockp = sock;
41 
42  pthread_attr_t attr; pthread_attr_init(&attr);
43  pthread_attr_setstacksize(&attr,4*PTHREAD_STACK_MIN);
44  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
45  if (pthread_create(&thisThread, &attr, &(handler), (void *) sockp) != 0)
46  fatal("cannot create thread");
47 }
48 
49 void* handler(void* sockp) {
50 
51  int sock = *((int *) sockp); delete ((int *) sockp);
52  string fileName;
53  NetBuffer buf(sock, 1024);
54  string s1;
55 
56  if (!buf.readAlphas(s1) || s1 != "getPhoto") {
57  Np4d::sendString(sock,"1unrecognized input\noverAndOut\n");
58  close(sock); return NULL;
59  }
60 
61  if (buf.verify(':')) {
62  if ( !buf.readAlphas(s1)) {
63  Np4d::sendString(sock,"2unrecognized input\n"
64  "overAndOut\n");
65  close(sock); return NULL;
66  } else {
67 <<<<<<< .mine
68  fileName = string("clientPhotos/") + s1 + string(".jpg");
69 =======
70  fileName = string("clientPhotos/") + s1 +string(".jpg");
71 >>>>>>> .r415
72  //int str_size = fileName.size() + 1;
73  //char pFileName[str_size];
74  //pFileName = fileName.c_str();
75  ifstream pFile (fileName.c_str(), ios::in | ios::binary | ios::ate);
76 
77  ifstream::pos_type size;
78  char *memblock;
79 
80  if(pFile.is_open())
81  {
82  size = pFile.tellg();
83  memblock = new char [size];
84  pFile.seekg (0, ios::beg);
85  pFile.read(memblock, size);
86  //complete file in its binary form is now in memory
87  pFile.close();
88  int bufSize = (int)size;
89  char * ap = &memblock[0];
90 
91  //send from memblock 1024 bytes at a time
92  while(bufSize >= CHUNK)
93  {
94  Np4d::sendBufBlock(sock, ap, CHUNK);
95  ap += CHUNK;
96  bufSize -= CHUNK;
97  }
98  // if last chunk less than CHUNK bytes (1024 for now)
99  if(bufSize > 0)
100  {
101  Np4d::sendBufBlock(sock, ap, bufSize);
102  }
103 
104  //de-allocate memory
105  delete[] memblock;
106  }
107  else{
108  Np4d::sendString(sock,"failed to locate file\n"
109  "overAndOut\n");
110  close(sock); return NULL;
111  }
112  }
113  }
114  else{
115  Np4d::sendString(sock,"3unrecognized input\n"
116  "overAndOut\n");
117  close(sock); return NULL;
118  }
119 
120  close(sock); return NULL;
121 }