Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 KB | None | 0 0
  1. // Daniel DePaolo
  2. // 10-25-10
  3. // CS 450
  4. // Server.cpp
  5.  
  6. // C++ libs
  7. #include <string>
  8. #include <iostream>
  9. #include <ctime>
  10. #include <cerrno>
  11. #include <cstdlib>
  12. #include <cstdio>
  13.  
  14. // C libs
  15. #include "sys/types.h"
  16. #include "sys/socket.h"
  17. #include "netinet/in.h"
  18. #include "arpa/inet.h"
  19. #include "strings.h"
  20.  
  21. // My classes
  22. #include "ChatRoom.h"
  23. #include "Collections.h"
  24. #include "Client.h"
  25.  
  26. using namespace std;
  27.  
  28. #define PORT_NUMBER            5081
  29. #define MAXLINE                200
  30. #define MAXSIZE                512
  31.  
  32. #define ACK                    2
  33. #define NACK                   3
  34. #define REQUESTMSGTRANSFER     100
  35.  
  36. int writen(int sd, char *ptr, int size);
  37. int readn(int sd, char *ptr, int size);
  38. void *commandThread(void *sID);
  39. void *readerThread( void * clientObject);
  40. void *writerThread( void * clientObject);
  41.  
  42. ClientCollection cliCol;
  43. ChatRoomCollection crCol;
  44.  
  45. int main()
  46. {
  47.     int listen_sd, conn_sd;
  48.     struct sockaddr_in serv_addr, client_addr;
  49.     socklen_t clilen = sizeof( client_addr );
  50.     ChatRoom * Lobby = new ChatRoom("Lobby", "Lobby");
  51.     crCol.addChatRoom(Lobby);
  52.      
  53.     cout<< "server: creating socket to listen for client"<< endl;
  54.     if ((listen_sd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  55.     {
  56.         cout<< "server: socket error : "<< errno<< endl;
  57.         exit(0);
  58.     }
  59.  
  60.     cout<< "server: binding local socket"<< endl;
  61.     bzero((char *) &serv_addr, sizeof(serv_addr));
  62.     serv_addr.sin_family = AF_INET; // Comm. using internet domain "AF_INET"
  63.     serv_addr.sin_port = htons(PORT_NUMBER); // Which port does server listen on?
  64.     serv_addr.sin_addr.s_addr = htons(INADDR_ANY); // Who can connect to server?
  65.     if (bind (listen_sd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  66.     {
  67.         perror( "Bind error" );
  68.         exit(0);
  69.     }
  70.  
  71.     cout<< "server: start listening"<< endl;
  72.     if (listen (listen_sd, 6) < 0)
  73.     {
  74.         perror( "Listen error" );
  75.         exit(0);
  76.     }
  77.  
  78.     for ( ; ; )
  79.     {
  80.         // Accept a connection from a client
  81.        
  82.         cout<< "server: starting accept"<< endl;
  83.         if ((conn_sd = accept(listen_sd, (struct sockaddr *) &client_addr, &clilen)) < 0)
  84.         {
  85.             perror( "Failed to accept" );
  86.             exit(0);
  87.         }
  88.  
  89.         // START COMMAND THREAD
  90.         // -----------------------------
  91.         commandThread((int*)listen_sd);
  92.     }
  93.  
  94. }
  95.  
  96. void *commandThread( void *sID ) {
  97.  
  98. //    1. Negotiate with the client to establish a name for the client.
  99.   //  2. Create the client-object and add it to the list of clients.
  100. //    3. Block until both the reader and writer processes have been created.
  101.   //  4. while( the client is active ) {
  102.     //       read a command from the client's socket.
  103.       //     carry out the command.
  104.         //   send a status back to the client.
  105.       // }
  106.  
  107.     const int COMMAND_LEN = 4;
  108.     int sockID = *(reinterpret_cast<int *>(sID));
  109.     int msg_ok = 0;
  110.  
  111.     // negotiate with client to establish unique name
  112.     string cliName;
  113.     while (msg_ok == 0) {
  114.         // get client name from the client
  115.         cliName = " ";
  116.         if (readn(sockID, (char *)cliName.c_str(), cliName.length()) < 0)
  117.         {
  118.             perror( "Read error" );
  119.             exit(0);
  120.         }
  121.         // found client ID in list, negotiate for a new name
  122.         if (cliCol.findClient(cliName) == false)
  123.             msg_ok = 1;
  124.  
  125.         if (writen(sockID, (char *)&msg_ok, sizeof(msg_ok)) < 0)
  126.         {
  127.             perror( "Write error" );
  128.             exit(0);
  129.         }
  130.     }
  131.     // by here, unique ID has been established.
  132.     // create client obj, add it to collection list
  133.     Client * clientObj = new Client(cliName, crCol.getChatRoom("Lobby"));
  134.     cliCol.addClient(clientObj);
  135.  
  136.     string command;
  137.     int n;
  138.     while( (n = readn( sockID, (char *)command.c_str(), COMMAND_LEN ) ) == COMMAND_LEN ) {
  139.         command[ COMMAND_LEN ] = '\0';
  140.         // Act on it.
  141.     }
  142.     // clean up!
  143. }
  144.  
  145. /*
  146. // client's reader thread. The server writes to its socket!
  147. void *readerThread( void * clientObject) {
  148.     Client *client = reinterpret_cast<Client *>(clientObject);
  149.    
  150.     int wDesc = client->getWriteSockDesc();
  151.     while( client->isActive() ) {
  152.         std::string msg = client->readMessage();
  153.         // send this message to the client.
  154.         flush( wDesc );
  155.     }
  156.     client.closeReader();
  157. }
  158.  
  159. // client's writer thread. The server reads from it.
  160. void *writerThread( void * clientObject ) {
  161.     Client *client = reinterpret_cast<Client *>(clientObject);
  162.    
  163.     while( client->isActive() ) {
  164.         // read msg from the client's writer process
  165.         client->postMessage( msg );
  166.     }
  167.     client->closeWriter();
  168. };
  169. */
  170.  
  171. int readn(int sd, char *ptr, int size)
  172. {
  173.     int no_left, no_read;
  174.     no_left = size;
  175.     while (no_left > 0)
  176.       {
  177.     no_read = read(sd, ptr, no_left);
  178.     if (no_read < 0)  return (no_read);
  179.     if (no_read == 0) break;
  180.     no_left -= no_read;
  181.     ptr += no_read;
  182.       }
  183.     return (size - no_left);
  184. }
  185.  
  186. int writen(int sd, char *ptr, int size)
  187. {
  188.   int no_left, no_written;
  189.   no_left = size;
  190.   while (no_left > 0)
  191.     {
  192.       no_written = write(sd, ptr, no_left);
  193.       if (no_written <= 0) return (no_written);
  194.       no_left -= no_written;
  195.       ptr += no_written;
  196.     }
  197.   return (size - no_left);
  198. }
  199.  
  200.  
  201. ///////////////////////////////////////////////////////
  202.  
  203. // Daniel DePaolo
  204. // 10-25-10
  205. // CS 450
  206. // Client.h
  207.  
  208. #ifndef CLIENT_H
  209. #define CLIENT_H
  210.  
  211. #include <string>
  212. #include "BoundedBuffer.hpp"
  213. class ChatRoom;
  214.  
  215. class Client {
  216. public:
  217.     Client( std::string name, ChatRoom * defaultCR );
  218.  
  219.     std::string getID();
  220.     bool isActive();
  221.     void closeWriter();
  222.     void closeReader();
  223.     void setWriteSockDesc( int wsd );
  224.     void setReadSockDesc( int rsd );
  225.     void getWriteSockDesc();
  226.     void getReadSockDesc();
  227.     void setDefaultChatRoom( ChatRoom *chatRoom );
  228.     ChatRoom *getDefaultChatRoom();
  229.  
  230.     // consumer thread:
  231.     // Gets a message from the msgBox of the client to which this
  232.     // object belongs and sends it to the client's reader
  233.     // process. Notice that no reading takes place here. Instead, the
  234.     // messages that are deposited into this client's "message box",
  235.     // are taken out, one at a time, by this function and transmitted
  236.     // to the reader process of this client.
  237.     std::string readChatMessage();  
  238.  
  239.     // producer thread:
  240.     // adds a message to the msgBox of this client so that it can
  241.     // ultimately be sent to the client through a call, by the thread
  242.     // that represent the read process of the client, to the
  243.     // "readChatMessage()".
  244.     void writeChatMessage( std::string msg );
  245.  
  246.     // Post this message to the default chat-room of this client.  The
  247.     // thread that represents the writer process of the client that
  248.     // this object serves, after having received a message from it,
  249.     // calls this function to deliver the message for posting. These
  250.     // messages are posted to the default chat-room of the client that
  251.     // owns this object and not into the "message box" of this
  252.     // client. However, the chat-room, after it receives the message,
  253.     // ultimately calls the "writeChatMessage" of every client who has
  254.     // subscribed to this chat-room, including this client.
  255.     void postMessageToDefaultChatRoom( std::string msg );
  256.  
  257. private:
  258.     BoundedBuffer *msgBox;
  259.  
  260.     std::string clientID;
  261.     int rSock, wSock;
  262.     ChatRoom *defaultChatRoom;
  263. };
  264.  
  265. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement