00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 # include <errno.h>
00025 # include <stdio.h>
00026 # include <string.h>
00027 # include <QFile>
00028 # include "photogrotto.h"
00029
00030 FILE *logfp;
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 PhotoGrotto::PhotoGrotto(QString confName, QObject *parent) :
00052 QTcpServer(parent)
00053 {
00054 int line, col;
00055
00056 logfp = stderr;
00057
00058
00059
00060
00061 QFile file( confName );
00062 if ( !file.open( IO_ReadOnly ) )
00063 {
00064 error_str = "Could not open file: " + confName;
00065 return;
00066 }
00067
00068
00069
00070
00071
00072 if ( !doc.setContent( &file, &err, &line, &col ) ) {
00073 file.close();
00074 error_str = "Error parsing xml doc: " + confName + "The error reported as: " + err + " found on line: " + QString::number(line) + ", column: " + QString::number(col);
00075 fprintf(stderr, "PhotoGrotto::PhotoGrotto(): Error in setContent file\n%s\n", qPrintable(error_str));
00076 return;
00077 }
00078
00079
00080
00081
00082
00083 getTcpParms();
00084 if ( ! listen(address, port) )
00085 {
00086 fprintf(logfp, "Error listening\n");
00087 return;
00088 }
00089 }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void PhotoGrotto::incomingConnection(int sock)
00102 {
00103 fprintf(logfp, "PhotoGrotto::incomingConnection(): Enter, sock = %d\n", sock);
00104 fflush(logfp);
00105
00106
00107
00108
00109
00110
00111 DbPhotoService *p = new DbPhotoService(sock, &doc, 120);
00112 connect(p, SIGNAL(send(QTcpSocket*, QByteArray)), this, SLOT(sendData(QTcpSocket*, QByteArray)));
00113 connect(p, SIGNAL(closeSocket(QTcpSocket*)), this, SLOT(haveCloseSocket(QTcpSocket*)));
00114
00115 p->setMaxCache(2000);
00116
00117
00118
00119
00120 p->start();
00121 fprintf(logfp, "PhotoGrotto::incomingConnection(): exit\n");
00122 fflush(logfp);
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 void PhotoGrotto::getTcpParms()
00134 {
00135 port = 80;
00136 address.setAddress(QHostAddress::Any);
00137
00138 QDomElement docElem = doc.documentElement();
00139 QDomNode n = docElem.firstChild();
00140 while ( !n.isNull() )
00141 {
00142 QDomElement e = n.toElement();
00143
00144
00145
00146
00147
00148 if ( e.tagName() == "logfile" )
00149 {
00150 if ( e.attribute("name").size() > 0 )
00151 {
00152 FILE *fp = fopen(qPrintable(e.attribute("name")), "a");
00153 if ( fp != 0 )
00154 {
00155 logfp = fp;
00156 stdout = fp;
00157 stderr = fp;
00158 }
00159 else
00160 fprintf(logfp, "Error opening logfile %s: %s\n", qPrintable(e.attribute("name")), strerror(errno));
00161 }
00162 }
00163
00164
00165
00166
00167
00168 else if ( e.tagName() == "listen" )
00169 {
00170 if ( e.attribute("addr").size() > 0 )
00171 {
00172 fprintf(logfp, "Listening on address: %s\n", qPrintable(e.attribute("addr")));
00173 address.setAddress(e.attribute("addr"));
00174 }
00175 else if ( e.attribute("address").size() > 0 )
00176 {
00177 fprintf(logfp, "Listening on address: %s\n", qPrintable(e.attribute("address")));
00178 address.setAddress(e.attribute("address"));
00179 }
00180
00181 if ( e.attribute("port").size() > 0 )
00182 port = e.attribute("port").toInt();
00183 break;
00184 }
00185 n = n.nextSibling();
00186 }
00187 fprintf(logfp, "Listening on port: %d\n", port);
00188 fflush(logfp);
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198 void PhotoGrotto::sendData(QTcpSocket *sock, QByteArray buf)
00199 {
00200 fprintf(logfp, "PhotoGrotto::sendData(): Enter, sending %d bytes to sock %d\n", buf.count(), sock->socketDescriptor());
00201 fflush(logfp);
00202 sock->write(buf);
00203 fprintf(logfp, "PhotoGrotto::sendData(): Exit\n");
00204 fflush(logfp);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213 void PhotoGrotto::haveCloseSocket(QTcpSocket *sock)
00214 {
00215 fprintf(logfp, "PhotoGrotto::haveCloseSocket(): Enter, sock %d\n", sock->socketDescriptor());
00216 fflush(logfp);
00217 sock->disconnectFromHost();
00218 fprintf(logfp, "PhotoGrotto::haveClocseSocket(): Exit\n");
00219 fflush(logfp);
00220 }
00221