1 #include "FileDescriptor.h"
4 namespace remote
{ namespace mcs
{
6 bool FileDescriptor::serviceLoop()
8 unsigned int i
, instanceCount
;
10 filedescriptorsbyfd_t::iterator fileDescriptorIterator
;
11 // register the timeout handler
12 signal( SIGALRM
, timeoutHandler
);
13 installSignalHandlers();
17 instanceCount
= instances
.size();
18 pollfd fdmap
[instanceCount
];
19 FileDescriptor::buildPollMap(fdmap
);
21 if ( poll(fdmap
,instanceCount
,-1) < 0)
25 __THROW__("Error in poll!");
30 // look for activity on any sockets
31 for ( i
= 0; i
< instanceCount
; i
++ )
33 if ( fdmap
[i
].revents
)
36 fileDescriptorIterator
= instances
.find(f
);
37 if (fileDescriptorIterator
!= instances
.end())
39 fileDescriptorIterator
->second
->handleEvent(fdmap
[i
].revents
);
44 catch (remote::protocols::MMSException e
)
47 log("Exception: %s\n",e
.what());
48 fileDescriptorIterator
->second
->clearTimeout();
49 fileDescriptorIterator
->second
->destroy(false);
54 return exitSignal
== 0;
57 FileDescriptor::FileDescriptor(int p_fd
)
61 log("Opened fd %i\n",fd
);
65 void FileDescriptor::buildPollMap(pollfd
* map
)
68 filedescriptorsbyfd_t::iterator i
;
69 for (i
= instances
.begin(); i
!= instances
.end(); i
++)
71 map
[j
].fd
= i
->second
->fd
;
72 map
[j
].events
= POLLMSG
|POLLRDBAND
|POLLRDNORM
|POLLIN
|POLLPRI
|POLLERR
|POLLHUP
|POLLNVAL
;
79 FileDescriptor::~FileDescriptor()
82 log("close(%i) - %s\n", fd
, strerror(errno
));
84 log("Closed fd %i\n", fd
);
88 void FileDescriptor::destroy(bool silent
)
94 void FileDescriptor::setTimeout(int secs
)
98 timeout
.it_interval
.tv_sec
= 0;
99 timeout
.it_interval
.tv_usec
= 0;
100 timeout
.it_value
.tv_sec
= secs
;
101 timeout
.it_value
.tv_usec
= 0;
102 if (setitimer(ITIMER_REAL
, &timeout
, NULL
))
104 __THROW__ ("Timer set error\n");
109 void FileDescriptor::clearTimeout()
113 timeout
.it_interval
.tv_sec
= 0;
114 timeout
.it_interval
.tv_usec
= 0;
115 timeout
.it_value
.tv_sec
= 0;
116 timeout
.it_value
.tv_usec
= 0;
117 if (setitimer(ITIMER_REAL
, &timeout
, NULL
))
119 __THROW__ ("Timer clear error\n");
125 RETSIGTYPE
FileDescriptor::timeoutHandler(int sig
)
127 log( "Timeout occured!\n");
128 close(currentFd
); // this should generate an exception
131 void FileDescriptor::exitSignalHandler(int signo
)
136 void FileDescriptor::installSignalHandlers()
138 struct sigaction act
;
140 memset(&act
, 0, sizeof(act
));
141 act
.sa_handler
= SIG_IGN
;
144 sigaction(SIGPIPE
, &act
, NULL
);
146 memset(&act
, 0, sizeof(act
));
147 act
.sa_handler
= exitSignalHandler
;
148 sigfillset(&act
.sa_mask
);
149 sigaction(SIGINT
, &act
, NULL
);
150 sigaction(SIGTERM
, &act
, NULL
);
151 sigaction(SIGSEGV
, &act
, NULL
);
152 sigaction(SIGKILL
, &act
, NULL
);
156 filedescriptorsbyfd_t
FileDescriptor::instances
;
157 int FileDescriptor::currentFd
= -1;
158 int FileDescriptor::exitSignal
= 0;