| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// File: CommObj.hh |
| 3 |
// Author: Bala Swaminathan 5/12/94 |
| 4 |
// This file contains interface of the Network Communication Object class. |
| 5 |
// |
| 6 |
// Copyright 1994, Washington University in St. Louis |
| 7 |
//---------------------------------------------------------------------------- |
| 8 |
|
| 9 |
// The following CommObj is modeled using the sockets. |
| 10 |
// Each PRO creates a CommObj of type CommObj::PRIMARY and this becomes |
| 11 |
// its main socket. Later whenever it needs a "new" socket it either |
| 12 |
// (1) creates a socket of type CommObj::SECONDARY and then "connects" |
| 13 |
// to another PRO's primary socket, or (2) the PRO can "accept" a connection |
| 14 |
// from another PRO's secondary socket. This "accepting" a connection, if |
| 15 |
// succesful, results in the creation of a socket of type CommObj::SECONDARY. |
| 16 |
|
| 17 |
#ifndef CommObj_hh |
| 18 |
#define CommObj_hh |
| 19 |
|
| 20 |
#include "sockinet.h" |
| 21 |
|
| 22 |
class CommObj : public sockinetbuf { |
| 23 |
public: |
| 24 |
enum type { PRIMARY, SECONDARY }; |
| 25 |
public: |
| 26 |
CommObj (const type ty = SECONDARY, // "do-nothing" constructor |
| 27 |
const int portID=0) // can specify a port (for PRIMARY) |
| 28 |
: sockinetbuf(sockbuf::sock_stream) // TCP Sockets |
| 29 |
{ |
| 30 |
if (ty == PRIMARY) { |
| 31 |
sockinetaddr addr (INADDR_ANY, portID); |
| 32 |
// this creates addr of INADDR_ANY and port=0 |
| 33 |
// i.e. sockets bound to any address can "connect" to us |
| 34 |
bind (addr); // bind to the above address |
| 35 |
listen (); // now listen (default 5) for new connections |
| 36 |
} |
| 37 |
} |
| 38 |
CommObj (const sockbuf& sb): sockinetbuf (sb) {} |
| 39 |
|
| 40 |
const sockinetbuf sbuf () { return *this; } // for DEUBUGGING purposes |
| 41 |
}; |
| 42 |
|
| 43 |
// The CommController manages all the CommObj instances in the PROTOCOL. |
| 44 |
// (* the list of CommObj* maintained by CommController are not shown *) |
| 45 |
|
| 46 |
class CommController { |
| 47 |
public: |
| 48 |
CommController (int portID=0) // creates the PERIMARY CommObj |
| 49 |
{ |
| 50 |
c_server = new CommObj (CommObj::PRIMARY, portID); |
| 51 |
} |
| 52 |
~CommController () // close all sockets and delete list |
| 53 |
{ delete c_server; } |
| 54 |
CommObj* server () // return "main" socket |
| 55 |
{ return c_server; } |
| 56 |
CommObj accept () // accept connection on the SERVER Commobj |
| 57 |
{ |
| 58 |
// return c_server->accept(); |
| 59 |
sockinetaddr sa; |
| 60 |
sockinetbuf si = c_server->accept(sa); |
| 61 |
cout << " accept(): peer.host = " << sa.gethostname() << endl; |
| 62 |
cout << " accept(): peer.port = " << sa.getport() << endl; |
| 63 |
cout << " acc: local.host = " << (si).localhost() << endl; |
| 64 |
cout << " acc: local.port = " << (si).localport() << endl; |
| 65 |
cout << " acc: peer.host = " << (si).peerhost() << endl; |
| 66 |
cout << " acc: peer.port = " << (si).peerport() << endl; |
| 67 |
return si; |
| 68 |
} |
| 69 |
private: |
| 70 |
CommObj* c_server; // the server or the main Object |
| 71 |
}; |
| 72 |
|
| 73 |
#endif CommObj_hh |
| 74 |
|