Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmstcpserverthread.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmstools/mmstcpserverthread.h"
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <string.h>
00037 #include <mmstools/tools.h>
00038 
00039 MMSTCPServerThread::MMSTCPServerThread(MMSServerInterface *interface, int s, string identity) : MMSThread(identity) {
00040     this->interface = interface;
00041     this->s = s;
00042     this->request_buffer = "";
00043     this->answer_buffer = "";
00044 }
00045 
00046 bool MMSTCPServerThread::setSocket(int s) {
00047     this->s = s;
00048     return true;
00049 }
00050 
00051 void MMSTCPServerThread::threadMain() {
00052     char    mybuf[4096+1];
00053     int     len, from;
00054 
00055     DEBUGMSG("MMSTCPServerThread", "process TCP Request");
00056     
00057     /* check somthing */
00058     if (!this->s) return;
00059     if (!this->interface) {
00060         close(this->s);
00061         this->s=-1;
00062         return;
00063     }
00064 
00065     /* receive request */
00066     this->request_buffer = "";
00067     do {
00068         if ((len = recv(this->s, mybuf, sizeof(mybuf)-1, 0))<0) {
00069             close(this->s);
00070             this->s=-1;
00071             return;
00072         }
00073         if (len>0) {
00074             mybuf[len]=0;
00075             this->request_buffer+= mybuf;
00076         }
00077     } while ((len>0)&&(mybuf[len-1]!=0));
00078 
00079     /* process request */
00080     this->answer_buffer = "";
00081     if (!this->interface->processRequest(&this->request_buffer, &this->answer_buffer)) {
00082         close(this->s);
00083         this->s=-1;
00084         return;
00085     }
00086 
00087     /* send answer */
00088     from = 0;
00089     do {
00090         strcpy(mybuf, (this->answer_buffer.substr(from, sizeof(mybuf)-1)).c_str());
00091         if (!*mybuf) break;
00092         if ((len = send(this->s, mybuf, strlen(mybuf), MSG_NOSIGNAL))<0) {
00093             close(this->s);
00094             this->s=-1;
00095             return;
00096         }
00097         from+=len;
00098     } while (len>0);
00099     send(this->s, "\0", 1, 0);
00100 
00101     close(this->s);
00102     this->s=-1;
00103 }

Generated by doxygen