CDomainSocket Class Reference

#include <CDomainSocket.h>

Inheritance diagram for CDomainSocket:

CSocket

List of all members.

Public Member Functions

 CDomainSocket ()
 CDomainSocket (const string &inPath)
 ~CDomainSocket ()
bool listenToPath (const string &inPath)
string getPath () const
virtual void serveStream (int)
virtual bool acceptAndServe ()
 operator bool () const

Protected Attributes

bool mIsInitialized
string mPath
struct sockaddr_un mSocketStructure


Detailed Description

A structure that opens/closes a domain socket for a given path and sets the file permissions to 0600.

This class will be used with select to be the entry to the local search engine.

See also:
: CMultiServer

Definition at line 56 of file CDomainSocket.h.


Constructor & Destructor Documentation

CDomainSocket::CDomainSocket (  ) 

constructor

Definition at line 2 of file CDomainSocket.cc.

00002                             :
00003   mIsInitialized(false){
00004 };

CDomainSocket::CDomainSocket ( const string &  inPath  ) 

constructor: copy the path, otherwise do nothing

Definition at line 7 of file CDomainSocket.cc.

References listenToPath().

00007                                                 :
00008   mIsInitialized(false){
00009   this->listenToPath(inPath);
00010 };

CDomainSocket::~CDomainSocket (  ) 

destroy this: close the socket

Definition at line 13 of file CDomainSocket.cc.

References mIsInitialized, and CSocket::mSocketDescriptor.

00013                              {
00014   if(mIsInitialized){
00015     close(mSocketDescriptor);
00016   }
00017 };


Member Function Documentation

bool CDomainSocket::listenToPath ( const string &  inPath  ) 

set the path of the domain socket and listen to it

set the path of the domain socket

Definition at line 19 of file CDomainSocket.cc.

References mIsInitialized, mPath, CSocket::mSocketDescriptor, and mSocketStructure.

Referenced by CDomainSocket().

00019                                                     {
00020   mPath=inPath;
00021   
00022   if (( mSocketDescriptor= socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
00023     perror("socket");
00024     return false;//no exit
00025   }
00026   
00027   mSocketStructure.sun_family = AF_UNIX;
00028   
00029   cout << "Making socket for path " << mPath << endl;
00030   strcpy(mSocketStructure.sun_path, mPath.c_str());
00031   unlink(mSocketStructure.sun_path);
00032   
00033   int len = strlen(mSocketStructure.sun_path) + sizeof(mSocketStructure.sun_family);
00034   if (bind(mSocketDescriptor, (struct sockaddr *)&mSocketStructure, len) == -1) {
00035     perror("bind");
00036     return 0;
00037   }
00038   if (listen(mSocketDescriptor, 5) == -1) {
00039     perror("listen");
00040     return 0;
00041   }
00042   
00043   if(chmod(mPath.c_str(), 
00044      S_IRUSR|S_IWUSR)){
00045     perror("chmod");
00046     return 0;
00047   }
00048   mIsInitialized=1;
00049   return 1;
00050 };

string CDomainSocket::getPath (  )  const

get the path of the socket

Definition at line 56 of file CDomainSocket.cc.

References mPath.

Referenced by acceptAndServe().

00056                                   {
00057   return mPath;
00058 }

void CDomainSocket::serveStream ( int  inSocket  )  [virtual]

Serve from the socket that accepted

Reimplemented from CSocket.

Definition at line 60 of file CDomainSocket.cc.

Referenced by acceptAndServe().

00060                                            {
00061   int done = 0;
00062   char str[2];
00063   string lMessage;
00064   int lRead(0);
00065   do {
00066     lRead = recv(inSocket, 
00067      str, 1, 0);
00068     if (lRead <= 0) {
00069       cerr << "here?" << endl;
00070       if (lRead < 0) perror("recv");
00071       done = 1;
00072     }else{
00073       str[lRead]=char(0);
00074       lMessage+=str;
00075     }
00076     
00077   } while (!done);
00078 
00079   cout << " after while" << endl;
00080   
00081   if (send(inSocket, lMessage.c_str(), lMessage.size(), 0) < 0) {
00082     perror("send");
00083   }
00084 }

bool CDomainSocket::acceptAndServe (  )  [virtual]

if we are here, this means there is data for one socket

Implements CSocket.

Definition at line 86 of file CDomainSocket.cc.

References getPath(), CSocket::getSocketDescriptor(), and serveStream().

00086                                   {
00087   struct sockaddr_un lAcceptedSocket;
00091   unsigned int t = sizeof(lAcceptedSocket);
00092   int s2(0);
00093   if ((s2 = accept(this->getSocketDescriptor(), (struct sockaddr *)&lAcceptedSocket, &t)) == -1) {
00094     perror("accept");
00095     return false;
00096   }
00097   {
00098     struct ucred lCredentials;
00099     socklen_t lSize = sizeof(lCredentials);
00100     if (getsockopt(s2, 
00101        SOL_SOCKET, 
00102        SO_PEERCRED, 
00103        &lCredentials, 
00104        &lSize) 
00105   == 0){
00106       // cred.uid now contains UID of the connecting process. 
00107       cout << "Socket credentials:" << endl
00108      << "Process ID: " << lCredentials.pid << endl
00109      << "User ID:    " << lCredentials.uid << endl
00110      << "Group ID:   " << lCredentials.gid << endl;
00111     }
00112   }
00113   cout << "connected: " << this->getPath() << endl;
00114 
00115   this->serveStream(s2);
00116   //close(s2); I cannot close sockets here, otherwise multithreading goes down the drain.
00117   return true;
00118 }

CDomainSocket::operator bool (  )  const [virtual]

get the path of the socket

Reimplemented from CSocket.

Definition at line 52 of file CDomainSocket.cc.

References mIsInitialized.

00052                                  {
00053   return mIsInitialized;
00054 }


Member Data Documentation

has this been initialized ?

Reimplemented from CSocket.

Definition at line 59 of file CDomainSocket.h.

Referenced by listenToPath(), operator bool(), and ~CDomainSocket().

string CDomainSocket::mPath [protected]

the path to the socket (visible in the file system)

Definition at line 61 of file CDomainSocket.h.

Referenced by getPath(), and listenToPath().

struct sockaddr_un CDomainSocket::mSocketStructure [read, protected]

the socket structure of the local socket

Definition at line 63 of file CDomainSocket.h.

Referenced by listenToPath().


The documentation for this class was generated from the following files:

Generated on Tue Jan 6 00:31:10 2009 for Gift by  doxygen 1.5.6