Back to index

nordugrid-arc-nox  1.1.0~rc6
Classes | Public Member Functions | Private Attributes
CommFIFO Class Reference

#include <commfifo.h>

Collaboration diagram for CommFIFO:
Collaboration graph
[legend]

List of all members.

Classes

class  elem_t

Public Member Functions

 CommFIFO (void)
 ~CommFIFO (void)
bool add (JobUser &user)
JobUserwait (int timeout)
JobUserwait (void)
void timeout (int t)

Private Attributes

std::list< elem_tfds
int kick_in
int kick_out
Glib::Mutex lock
int timeout_

Detailed Description

Definition at line 8 of file commfifo.h.


Constructor & Destructor Documentation

CommFIFO::CommFIFO ( void  )

Definition at line 16 of file commfifo.cpp.

                       {
  timeout_=-1;
  lock.lock();
  int filedes[2];
  kick_in=-1; kick_out=-1;
  if(pipe(filedes) == 0) {
    kick_in=filedes[1];
    kick_out=filedes[0];
    long arg;
    arg=fcntl(kick_in,F_GETFL);
    if(arg != -1) { arg|=O_NONBLOCK; fcntl(kick_in,F_SETFL,&arg); };
    arg=fcntl(kick_out,F_GETFL);
    if(arg != -1) { arg|=O_NONBLOCK; fcntl(kick_out,F_SETFL,&arg); };
  };
  lock.unlock();
}
CommFIFO::~CommFIFO ( void  )

Definition at line 33 of file commfifo.cpp.

                        {
}

Member Function Documentation

bool CommFIFO::add ( JobUser user)

Definition at line 90 of file commfifo.cpp.

                                {
  std::string path = user.ControlDir() + "/gm." + user.UnixName() + ".fifo";
  if(mkfifo(path.c_str(),S_IRUSR | S_IWUSR) != 0) {
    if(errno != EEXIST) {
      return false; 
    };
  };
  (void)chmod(path.c_str(),S_IRUSR | S_IWUSR);
  uid_t uid = user.get_uid();
  gid_t gid = user.get_gid();
  (lchown(path.c_str(),uid,gid) != 0);
  int fd = open(path.c_str(),O_RDONLY | O_NONBLOCK);
  if(fd == -1) return false;
  int fd_keep = open(path.c_str(),O_WRONLY | O_NONBLOCK);
  if(fd_keep == -1) { close(fd); return false; };
  elem_t el; el.user=&user; el.fd=fd; el.fd_keep=fd_keep;
  lock.lock();
  fds.push_back(el);
  lock.unlock();
  if(kick_in >= 0) {
    char c = 0;
    (write(kick_in,&c,1) != -1);
  };
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void CommFIFO::timeout ( int  t) [inline]

Definition at line 28 of file commfifo.h.

{ timeout_=t; };

Here is the caller graph for this function:

JobUser * CommFIFO::wait ( int  timeout)

Definition at line 36 of file commfifo.cpp.

                                   {
  time_t start_time = time(NULL);
  time_t end_time = start_time + timeout;
  for(;;) {
    fd_set fin,fout,fexc;
    FD_ZERO(&fin); FD_ZERO(&fout); FD_ZERO(&fexc);
    int maxfd=-1;
    if(kick_out >= 0) { maxfd=kick_out; FD_SET(kick_out,&fin); };
    lock.lock();
    for(std::list<elem_t>::iterator i = fds.begin();i!=fds.end();++i) {
      if(i->fd < 0) continue;
      if(i->fd>maxfd) maxfd=i->fd;
      FD_SET(i->fd,&fin);
    };
    lock.unlock();
    int n;
    maxfd++;
    if(timeout >= 0) {
      struct timeval t;
      if(((int)(end_time-start_time)) < 0) return NULL;
      t.tv_sec=end_time-start_time;
      t.tv_usec=0;
      n = select(maxfd,&fin,&fout,&fexc,&t);
      start_time = time(NULL);
    } else {
      n = select(maxfd,&fin,&fout,&fexc,NULL);
    };
    if(n == 0) return NULL;
    if(n == -1) {
      // One of fifos must be broken ?

    };
    if(kick_out >= 0) {
      if(FD_ISSET(kick_out,&fin)) {
        char buf[256]; (read(kick_out,buf,256) != -1);
        continue;
      };
    };
    lock.lock();
    for(std::list<elem_t>::iterator i = fds.begin();i!=fds.end();++i) {
      if(i->fd < 0) continue;
      if(FD_ISSET(i->fd,&fin)) {
        lock.unlock();
        char buf[256];
        ssize_t l = read(i->fd,buf,sizeof(buf));
        // -1 ???
        // 0 means kick, 1 - ping, rest undefined yet
        if(l > 0) if(memchr(buf,0,sizeof(buf))) return i->user;
      };
    };
    lock.unlock();
  };
}

Here is the call graph for this function:

Here is the caller graph for this function:

JobUser* CommFIFO::wait ( void  ) [inline]

Definition at line 27 of file commfifo.h.

{ return wait(timeout_); };

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

std::list<elem_t> CommFIFO::fds [private]

Definition at line 17 of file commfifo.h.

int CommFIFO::kick_in [private]

Definition at line 18 of file commfifo.h.

int CommFIFO::kick_out [private]

Definition at line 19 of file commfifo.h.

Glib::Mutex CommFIFO::lock [private]

Definition at line 20 of file commfifo.h.

int CommFIFO::timeout_ [private]

Definition at line 21 of file commfifo.h.


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