ZSO-SPNFS_server.cpp

Go to the documentation of this file.
00001 
00090 #include <arpa/inet.h>
00091 #include <cstdio>      /* standard I/O routines.            */
00092 #include <cstdlib>     /* standard library definitions      */
00093 #include <cstring>     /* C++ wrapper for string.h - string operations*/
00094 #include <errno.h>     /* system error numbers */
00095 #include <fcntl.h>     /* file control options */
00096 #include <map>         /* STL -> map */
00097 #include <netdb.h>
00098 #include <netinet/in.h>
00099 #include <string>      /* STL -> string */
00100 #include <sys/socket.h> /* guess for what is that ? ;) */
00101 #include <sys/stat.h>  /* data returned by the stat() function */
00102 #include <sys/types.h> /* standard system data types.       */
00103 #include <unistd.h>    /* standard symbolic constants and types */
00104 #include "ZSO-SPNFS_hooks.hpp" /* functions that handles each hook */
00105 #include "ZSO-SPNFS_network.hpp" /*network functions */
00106 
00107 void service_client(int client_socket);
00108 
00109 int main (int argc, char **argv) {
00110     int sock;
00111     int status;
00112     int i;
00113     struct sockaddr_in clientname;
00114     socklen_t socklen;
00115     size_t size;
00116     int portnum;
00117 
00118     if(argc<=1){
00119         printf("Server usage:\n%s port\n",
00120                 "\n"
00121                 "When server runs, you can use client, telnet and webbrowser as well\n",
00122                 argv[0]);
00123         return 0;
00124     };
00125 
00126     //INIT
00127 
00128     portnum = atoi(argv[1]);
00129     printf("Port = %d\n", portnum);
00130 
00131     /* Create the socket and set it up to accept connections.  */
00132     sock = make_socket (portnum);
00133     if (listen (sock, 1) < 0) {
00134         perror ("listen");
00135         exit (EXIT_FAILURE);
00136     }
00137 
00138     set_TCP_NODELAY(sock);
00139 
00140     // MAIN LOOP
00141     while (1) {
00142         if ( (status = accept (sock, (struct sockaddr *) &clientname, &socklen)) < 0) {
00143             perror ("accept");
00144             exit (EXIT_FAILURE);
00145         }
00146 //#ifdef DEB
00147         fprintf (stderr, "Server: connect from host %s, port %hd.\n", inet_ntoa (clientname.sin_addr), ntohs (clientname.sin_port));
00148 //#endif
00149         if( fork()==0 ){
00150             service_client(status);
00151             return 0;
00152         };
00153         // close client socket on parent thread side. To give thread child posiibility to close it permamently.
00154         close(status); 
00155     };
00156     return 0; //never reached
00157 }
00158     
00159 int logining(int);
00160 
00162 void service_client(int client_socket){
00163     char ch;
00164     hooks_container hc; 
00165     //init
00166     if(logining(client_socket)<0){
00167         write(client_socket,make_csv_string(-1)+make_csv_string("login failed"));
00168         close(client_socket);
00169         return;
00170     };
00171     
00172     init_hooks(hc);
00173     
00174     //main loop
00175     while(1){
00176         //printf("%s\n", (read_csv_string(client_socket)).c_str() );
00177         hc << client_socket; //**< process one command
00178         write(client_socket, "\n-  -  -  -  -  -  -  -  -  -  -  -  -\n"); //flushing buffer
00179     };
00180     close(client_socket); 
00181 };
00182 
00183 int logining(int fd){
00184     if(read_csv_string(fd)=="login" && 
00185             read_csv_string(fd)=="ala" &&
00186             read_csv_string(fd)=="kota" ){
00187         write(fd,make_csv_string(0)+make_csv_string("logged in successfully"));
00188         return 0;
00189     };
00190     return -1;
00191 };
00192 

Generated on Mon Jan 21 12:09:51 2008 for ZSO - Simple Prototype of Network File System by  doxygen 1.5.4