summaryrefslogtreecommitdiff
authorzecke <zecke>2002-10-12 01:04:40 (UTC)
committer zecke <zecke>2002-10-12 01:04:40 (UTC)
commit78eddaa8057462b0b8e94f10f8673bb9f555061b (patch) (unidiff)
treeec73d894164866c349fd0bc28e441f6006269069
parent39b88e5809e50a4951869434b8015c55265fc495 (diff)
downloadopie-78eddaa8057462b0b8e94f10f8673bb9f555061b.zip
opie-78eddaa8057462b0b8e94f10f8673bb9f555061b.tar.gz
opie-78eddaa8057462b0b8e94f10f8673bb9f555061b.tar.bz2
SWITCH to the ProcCtl interface
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-console/filetransfer.cpp55
-rw-r--r--noncore/apps/opie-console/test/console.pro4
2 files changed, 27 insertions, 32 deletions
diff --git a/noncore/apps/opie-console/filetransfer.cpp b/noncore/apps/opie-console/filetransfer.cpp
index 7fd9f37..97552fe 100644
--- a/noncore/apps/opie-console/filetransfer.cpp
+++ b/noncore/apps/opie-console/filetransfer.cpp
@@ -1,242 +1,237 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <errno.h> 3#include <errno.h>
4#include <fcntl.h> 4#include <fcntl.h>
5#include <unistd.h> 5#include <unistd.h>
6 6
7#include <qcstring.h> 7#include <qcstring.h>
8#include <qsocketnotifier.h> 8#include <qsocketnotifier.h>
9 9
10#include <opie/oprocess.h> 10#include <opie/oprocess.h>
11 11
12#include "procctl.h"
12#include "filetransfer.h" 13#include "filetransfer.h"
13 14
14/**
15 *
16 *
17class FileTransferControl {
18public:
19 FileTransferControl();
20 ~FileTransferControl();
21
22
23};
24*/
25
26bool FileTransfer::terminate = false;
27pid_t FileTransfer::m_pid;
28 15
29FileTransfer::FileTransfer( Type t, IOLayer* lay ) 16FileTransfer::FileTransfer( Type t, IOLayer* lay )
30 : FileTransferLayer( lay ), m_type( t ) { 17 : FileTransferLayer( lay ), m_type( t ) {
31 signal(SIGPIPE, SIG_IGN ); 18 signal(SIGPIPE, SIG_IGN );
32 signal( SIGCHLD, signal_handler ); 19
33 m_not = 0l; 20 m_not = 0l;
21 m_proc = 0l;
34} 22}
35FileTransfer::~FileTransfer() { 23FileTransfer::~FileTransfer() {
36} 24}
37 25
38/** 26/**
39 * now we will send the file. 27 * now we will send the file.
40 * 28 *
41 * we request an fd. The IOLayer should be closed 29 * we request an fd. The IOLayer should be closed
42 * then we will setup a pipe for progress communication 30 * then we will setup a pipe for progress communication
43 * then we will dup2 the m_fd in the forked process 31 * then we will dup2 the m_fd in the forked process
44 * to do direct IO from and to the fd 32 * to do direct IO from and to the fd
45 */ 33 */
46void FileTransfer::sendFile( const QString& file ) { 34void FileTransfer::sendFile( const QString& file ) {
47 m_prog =-1; 35 m_prog =-1;
48 m_fd = layer()->rawIO(); 36 m_fd = layer()->rawIO();
49// 37//
50// m_fd = ::open("/dev/ttyS0", O_RDWR); 38// m_fd = ::open("/dev/ttyS0", O_RDWR);
51 39
52 m_file = file; 40 m_file = file;
53 if ( pipe( m_comm ) < 0 ) 41 if ( pipe( m_comm ) < 0 )
54 m_comm[0] = m_comm[1] = 0; 42 m_comm[0] = m_comm[1] = 0;
55 if ( pipe( m_info ) < 0 ) 43 if ( pipe( m_info ) < 0 )
56 m_info[0] = m_info[1] = 0; 44 m_info[0] = m_info[1] = 0;
57 45
58 46
59 m_pid = fork(); 47 m_pid = fork();
60 switch( m_pid ) { 48 switch( m_pid ) {
61 case -1: 49 case -1:
62 emit error( StartError, tr("Was not able to fork") ); 50 emit error( StartError, tr("Was not able to fork") );
63 break; 51 break;
64 case 0:{ 52 case 0:{
65 setupChild(); 53 setupChild();
66 qWarning("output:"+file ); 54 qWarning("output:"+file );
67 /* exec */ 55 /* exec */
68 char* verbose = "-vv"; 56 char* verbose = "-vv";
69 char* binray = "-b"; 57 char* binray = "-b";
70 58
71 59
72 /* we should never return from here */ 60 /* we should never return from here */
73 execlp("sz", "sz", verbose, binray, file.latin1(), NULL ); 61 execlp("sz", "sz", verbose, binray, file.latin1(), NULL );
74 62
75 /* communication for error!*/ 63 /* communication for error!*/
76 char resultByte =1; 64 char resultByte =1;
77 if (m_info[1] ) 65 if (m_info[1] )
78 write(m_info[1], &resultByte, 1 ); 66 write(m_info[1], &resultByte, 1 );
79 _exit( -1 ); 67 _exit( -1 );
80 break; 68 break;
81 } 69 }
82 default:{ 70 default:{
83 if ( m_info[1] ) 71 if ( m_info[1] )
84 close( m_info[1] ); 72 close( m_info[1] );
85 if ( m_info[0] ) for (;;) { 73 if ( m_info[0] ) for (;;) {
86 char resultByte; int len; 74 char resultByte; int len;
87 len = read(m_info[0], &resultByte, 1 ); 75 len = read(m_info[0], &resultByte, 1 );
88 /* len == 1 start up failed */ 76 /* len == 1 start up failed */
89 if ( len == 1 ) { 77 if ( len == 1 ) {
90 emit error( StartError, tr("Could not start") ); 78 emit error( StartError, tr("Could not start") );
91 return; 79 return;
92 } 80 }
93 if ( len == -1 ) 81 if ( len == -1 )
94 if ( (errno == ECHILD ) || (errno == EINTR ) ) 82 if ( (errno == ECHILD ) || (errno == EINTR ) )
95 continue; 83 continue;
96 84
97 // len == 0 or something like this 85 // len == 0 or something like this
98 break; 86 break;
99 } 87 }
100 if ( m_info[0] ) 88 if ( m_info[0] )
101 close( m_info[0] ); 89 close( m_info[0] );
102 90
103 terminate = false; 91
104 fd_set fds;
105 struct timeval timeout;
106 int sel;
107 92
108 /* replace by QSocketNotifier!!! */ 93 /* replace by QSocketNotifier!!! */
109 m_not = new QSocketNotifier(m_comm[0], QSocketNotifier::Read ); 94 m_not = new QSocketNotifier(m_comm[0], QSocketNotifier::Read );
110 connect(m_not, SIGNAL(activated(int) ), 95 connect(m_not, SIGNAL(activated(int) ),
111 this, SLOT(slotRead() ) ); 96 this, SLOT(slotRead() ) );
97 if ( pipe(m_term) < 0 )
98 m_term[0] = m_term[1] = 0;
99
100 ProcCtl::self()->add(m_pid, m_term[1] );
101 m_proc = new QSocketNotifier(m_term[0], QSocketNotifier::Read );
102 connect(m_proc, SIGNAL(activated(int) ),
103 this, SLOT(slotExec() ) );
104
112 } 105 }
113 break; 106 break;
114 } 107 }
115} 108}
116/* 109/*
117 * let's call the one with the filename 110 * let's call the one with the filename
118 */ 111 */
119void FileTransfer::sendFile( const QFile& file ) { 112void FileTransfer::sendFile( const QFile& file ) {
120 sendFile( file.name() ); 113 sendFile( file.name() );
121} 114}
122/*
123 * our signal handler to be replaced by
124 * a procctl thingie
125 */
126void FileTransfer::signal_handler(int ) {
127 qWarning("Terminated");
128 int status;
129 signal( SIGCHLD, signal_handler );
130 waitpid( m_pid, &status, WNOHANG );
131 terminate = true;
132}
133 115
134/* 116/*
135 * setting up communication 117 * setting up communication
136 * between parent child and ioLayer 118 * between parent child and ioLayer
137 */ 119 */
138void FileTransfer::setupChild() { 120void FileTransfer::setupChild() {
139 /* 121 /*
140 * we do not want to read from our 122 * we do not want to read from our
141 * information channel 123 * information channel
142 */ 124 */
143 if (m_info[0] ) 125 if (m_info[0] )
144 close(m_info[0] ); 126 close(m_info[0] );
145 /* 127 /*
146 * FD_CLOEXEC will close the 128 * FD_CLOEXEC will close the
147 * fd on successfull exec 129 * fd on successfull exec
148 */ 130 */
149 if (m_info[1] ) 131 if (m_info[1] )
150 fcntl(m_info[1], F_SETFD, FD_CLOEXEC ); 132 fcntl(m_info[1], F_SETFD, FD_CLOEXEC );
151 133
152 if (m_comm[0] ) 134 if (m_comm[0] )
153 close( m_comm[0] ); 135 close( m_comm[0] );
154 /* 136 /*
155 * now set the communication 137 * now set the communication
156 * m_fd STDIN_FILENO 138 * m_fd STDIN_FILENO
157 * STDOUT_FILENO 139 * STDOUT_FILENO
158 * STDERR_FILENO 140 * STDERR_FILENO
159 */ 141 */
160 dup2( m_fd, STDIN_FILENO ); 142 dup2( m_fd, STDIN_FILENO );
161 dup2( m_fd, STDOUT_FILENO ); 143 dup2( m_fd, STDOUT_FILENO );
162 dup2( m_comm[1], STDERR_FILENO ); 144 dup2( m_comm[1], STDERR_FILENO );
163} 145}
164 146
165/* 147/*
166 * read from the stderr of the child 148 * read from the stderr of the child
167 * process 149 * process
168 */ 150 */
169void FileTransfer::slotRead() { 151void FileTransfer::slotRead() {
170 QByteArray ar(4096); 152 QByteArray ar(4096);
171 int len = read(m_comm[0], ar.data(), 4096 ); 153 int len = read(m_comm[0], ar.data(), 4096 );
172 qWarning("slot read %d", len); 154 qWarning("slot read %d", len);
173 for (int i = 0; i < len; i++ ) { 155 for (int i = 0; i < len; i++ ) {
174 // printf("%c", ar[i] ); 156 // printf("%c", ar[i] );
175 } 157 }
176 ar.resize( len ); 158 ar.resize( len );
177 QString str( ar ); 159 QString str( ar );
160 qWarning(str.simplifyWhiteSpace() );
178 QStringList lis = QStringList::split(' ', str ); 161 QStringList lis = QStringList::split(' ', str );
179 /* 162 /*
180 * Transfer finished.. either complete or incomplete 163 * Transfer finished.. either complete or incomplete
181 */ 164 */
182 if ( lis[0].simplifyWhiteSpace() == "Transfer" ) { 165 if ( lis[0].simplifyWhiteSpace() == "Transfer" ) {
183 qWarning("sent!!!!"); 166 qWarning("sent!!!!");
184 emit sent(); 167 emit sent();
185 return; 168 return;
186 } 169 }
187 /* 170 /*
188 * do progress reading 171 * do progress reading
189 */ 172 */
190 slotProgress( lis ); 173 slotProgress( lis );
191 174
192 175
193} 176}
194/* 177/*
195 * find the progress 178 * find the progress
196 */ 179 */
197void FileTransfer::slotProgress( const QStringList& list ) { 180void FileTransfer::slotProgress( const QStringList& list ) {
198 bool complete = true; 181 bool complete = true;
199 int min, sec; 182 int min, sec;
200 int bps; 183 int bps;
201 unsigned long sent, total; 184 unsigned long sent, total;
202 185
203 min = sec = bps = -1; 186 min = sec = bps = -1;
204 sent = total = 0; 187 sent = total = 0;
205 188
206 // Data looks like this 189 // Data looks like this
207 // 0 1 2 3 4 5 190 // 0 1 2 3 4 5
208 // Bytes Sent 65536/11534336 BPS:7784 ETA 24:33 191 // Bytes Sent 65536/11534336 BPS:7784 ETA 24:33
209 QStringList progi = QStringList::split('/', list[2].simplifyWhiteSpace() ); 192 QStringList progi = QStringList::split('/', list[2].simplifyWhiteSpace() );
210 sent = progi[0].toULong(&complete ); 193 sent = progi[0].toULong(&complete );
211 if (!complete ) return; 194 if (!complete ) return;
212 195
213 total = progi[1].toULong(&complete ); 196 total = progi[1].toULong(&complete );
214 if (!complete || total == 0) { 197 if (!complete || total == 0) {
215 return; 198 return;
216 } 199 }
217 200
218 qWarning("%s, %d, %d", progi.join("/").latin1(), sent, total ); 201 qWarning("%s, %d, %d", progi.join("/").latin1(), sent, total );
219 202
220 double pro = (double)sent/total; 203 double pro = (double)sent/total;
221 int prog = pro * 100; 204 int prog = pro * 100;
222 205
223 // speed 206 // speed
224 progi = QStringList::split(':', list[3].simplifyWhiteSpace() ); 207 progi = QStringList::split(':', list[3].simplifyWhiteSpace() );
225 bps = progi[1].toInt(); 208 bps = progi[1].toInt();
226 209
227 // time 210 // time
228 progi = QStringList::split(':', list[5].simplifyWhiteSpace() ); 211 progi = QStringList::split(':', list[5].simplifyWhiteSpace() );
229 min = progi[0].toInt(); 212 min = progi[0].toInt();
230 sec = progi[1].toInt(); 213 sec = progi[1].toInt();
231 214
232 215
233 if ( prog > m_prog ) { 216 if ( prog > m_prog ) {
234 m_prog = prog; 217 m_prog = prog;
235 emit progress(m_file, m_prog, bps, -1, min , sec ); 218 emit progress(m_file, m_prog, bps, -1, min , sec );
236 } 219 }
237 220
238} 221}
239void FileTransfer::cancel() { 222void FileTransfer::cancel() {
240 ::kill(m_pid,9 ); 223 ::kill(m_pid,9 );
241 delete m_not; 224 delete m_not;
242} 225}
226void FileTransfer::slotExec() {
227 qWarning("exited!");
228 char buf[2];
229 ::read(m_term[0], buf, 1 );
230 delete m_proc;
231 delete m_not;
232 close( m_term[0] );
233 close( m_term[1] );
234 close( m_comm[0] );
235 close( m_comm[1] );
236 emit sent();
237}
diff --git a/noncore/apps/opie-console/test/console.pro b/noncore/apps/opie-console/test/console.pro
index 9fe8e13..af0e9f7 100644
--- a/noncore/apps/opie-console/test/console.pro
+++ b/noncore/apps/opie-console/test/console.pro
@@ -1,15 +1,15 @@
1TEMPLATE = app 1TEMPLATE = app
2#CONFIG = qt warn_on release 2#CONFIG = qt warn_on release
3 CONFIG = qt debug 3 CONFIG = qt debug
4#DESTDIR = $(OPIEDIR)/bin 4#DESTDIR = $(OPIEDIR)/bin
5HEADERS = ../io_layer.h ../io_serial.h ../sz_transfer.h ../file_layer.h\ 5HEADERS = ../io_layer.h ../io_serial.h ../sz_transfer.h ../file_layer.h\
6 senderui.h ../profile.h ../filetransfer.h 6 senderui.h ../profile.h ../filetransfer.h ../procctl.h
7SOURCES = ../io_layer.cpp ../io_serial.cpp \ 7SOURCES = ../io_layer.cpp ../io_serial.cpp \
8 ../profile.cpp ../sz_transfer.cpp ../file_layer.cpp\ 8 ../profile.cpp ../sz_transfer.cpp ../file_layer.cpp\
9 main.cpp senderui.cpp ../filetransfer.cpp 9 main.cpp senderui.cpp ../filetransfer.cpp ../procctl.cpp
10INTERFACES = sender.ui 10INTERFACES = sender.ui
11INCLUDEPATH += $(OPIEDIR)/include 11INCLUDEPATH += $(OPIEDIR)/include
12DEPENDPATH += $(OPIEDIR)/include 12DEPENDPATH += $(OPIEDIR)/include
13LIBS += -lqpe -lopie 13LIBS += -lqpe -lopie
14TARGET = test 14TARGET = test
15 15