- factor out the filepusher from ts recorder
[enigma2.git] / lib / base / filepush.cpp
1 #include <lib/base/filepush.h>
2 #include <lib/base/eerror.h>
3 #include <errno.h>
4
5 eFilePushThread::eFilePushThread()
6 {
7         m_stop = 0;
8         m_buf_start = m_buf_end = 0;
9 }
10
11 static void signal_handler(int x)
12 {
13 }
14
15 void eFilePushThread::thread()
16 {
17         eDebug("FILEPUSH THREAD START");
18                 // this is a race. FIXME.
19         
20                 /* we set the signal to not restart syscalls, so we can detect our signal. */
21         struct sigaction act;
22         act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
23         act.sa_flags = 0;
24         sigaction(SIGUSR1, &act, 0);
25         
26                 /* m_stop must be evaluated after each syscall. */
27         while (!m_stop)
28         {
29                         /* first try flushing the bufptr */
30                 if (m_buf_start != m_buf_end)
31                 {
32                                 // TODO: take care of boundaries.
33                         int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
34                         if (w <= 0)
35                         {
36                                 if (errno == -EINTR)
37                                         continue;
38                                 eDebug("eFilePushThread *write error* - not yet handled");
39                                 // ... we would stop the thread
40                         }
41                         printf("FILEPUSH: wrote %d bytes\n", w);
42                         m_buf_start += w;
43                         continue;
44                 }
45                         
46                         /* now fill our buffer. */
47                 m_buf_start = 0;
48                 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
49                 if (m_buf_end < 0)
50                 {
51                         m_buf_end = 0;
52                         if (errno == EINTR)
53                                 continue;
54                         eDebug("eFilePushThread *read error* - not yet handled");
55                 }
56                 printf("FILEPUSH: read %d bytes\n", m_buf_end);
57         }
58         
59         eDebug("FILEPUSH THREAD STOP");
60 }
61
62 void eFilePushThread::start(int fd_source, int fd_dest)
63 {
64         m_fd_source = fd_source;
65         m_fd_dest = fd_dest;
66         m_stop = 0;
67         run();
68 }
69
70 void eFilePushThread::stop()
71 {
72         m_stop = 1;
73         sendSignal(SIGUSR1);
74         kill();
75 }