forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
PhotoServer.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, INADDR_ANY, 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  if (buf.verify(':')) {
61  string s2;
62  if (buf.readAlphas(s2) && s2 != ""){
63 
64  fileName = string("clientPhotos/") + s2 + string(".jpg");
65  std::cout << fileName << std::endl;
66 
67  //std::cout << "opening" << s2 << std::endl;
68  ifstream pFile (fileName.c_str(), ios::in | ios::binary | ios::ate);
69 
70  ifstream::pos_type size;
71  char *memblock;
72 
73  if(pFile.is_open())
74  {
75  std::cout << "file opened" << std::endl;
76  size = pFile.tellg();
77  memblock = new char [size];
78  pFile.seekg (0, ios::beg);
79  pFile.read(memblock, size);
80  //complete file in its binary form is now in memory
81  pFile.close();
82  int bufSize = (int)size;
83  //std::cout << bufSize << std::endl;
84  stringstream ss;
85  ss << "success:" << bufSize;
86  string echo = ss.str();
87  while (echo.size() < 14)
88  {
89  echo += " ";
90  }
91  echo += '/n';
92  Np4d::sendString(sock, echo);
93  char * ap = &memblock[0];
94 
95  //send from memblock 1024 bytes at a time
96  while(bufSize >= CHUNK)
97  {
98  Np4d::sendBufBlock(sock, ap, CHUNK);
99  ap += CHUNK;
100  bufSize -= CHUNK;
101  }
102  // if last chunk less than CHUNK bytes (1024 for now)
103  if(bufSize > 0)
104  {
105  Np4d::sendBufBlock(sock, ap, bufSize);
106  }
107 
108  //de-allocate memory
109  delete[] memblock;
110  }
111  else{
112  std::cout << "didn't open" << std::endl;
113  Np4d::sendString(sock,"failure:00404\n");
114  close(sock); return NULL;
115  }
116  }
117  else{
118  std::cout << "hmmmm" << std::endl;
119  Np4d::sendString(sock,"2unrecognized input\n"
120  "overAndOut\n");
121  close(sock); return NULL;
122  }
123  }
124  else{
125  Np4d::sendString(sock,"3unrecognized input\n"
126  "overAndOut\n");
127  close(sock); return NULL;
128  }
129 
130  close(sock); return NULL;
131 }