From 8f27e65bddd7d4b8515ce620fb485fdd78fcdf89 Mon Sep 17 00:00:00 2001 From: Constantin Jucovschi Date: Fri, 24 Apr 2009 07:20:22 -0400 Subject: Initial commit --- rascontrol/rasmgr_utils_comm.cc | 359 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 rascontrol/rasmgr_utils_comm.cc (limited to 'rascontrol/rasmgr_utils_comm.cc') diff --git a/rascontrol/rasmgr_utils_comm.cc b/rascontrol/rasmgr_utils_comm.cc new file mode 100644 index 0000000..01b504c --- /dev/null +++ b/rascontrol/rasmgr_utils_comm.cc @@ -0,0 +1,359 @@ +/* +* This file is part of rasdaman community. +* +* Rasdaman community is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* Rasdaman community is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with rasdaman community. If not, see . +* +* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann / +rasdaman GmbH. +* +* For more information please see +* or contact Peter Baumann via . +/ +/** + * SOURCE: rasmgr_utils_comm.cc + * + * MODULE: rascontrol + * CLASS: RasMgrClientComm, UserLogin + * + * PURPOSE: + * rasmgr-Client communication and login classes + * + * COMMENTS: + * + * +*/ + +// trace macros +#include "debug-clt.hh" + +#include "globals.hh" + +#include "rasmgr_utils_comm.hh" +#include "rascontrol.hh" + + +RasMgrClientComm::RasMgrClientComm() + { + rasmgrSocket=-1; + userName[0]= EOS_CHAR; + encrPass[0]= EOS_CHAR; + rasmgrHost[0]= EOS_CHAR; + answerBody=answerMessage; + answerMessage[0]= EOS_CHAR; + } + +RasMgrClientComm::~RasMgrClientComm() + { + } +void RasMgrClientComm::setRasMgrHost(const char *rasmgrHost, int rasmgrPort) + { + strcpy(this->rasmgrHost,rasmgrHost); + this->rasmgrPort=rasmgrPort; + } + +const char* RasMgrClientComm::getRasMgrHost() + { return rasmgrHost; + } + +void RasMgrClientComm::setUserIdentification(const char *userName, const char *encrPass) + { + strcpy(this->userName,userName); + strcpy(this->encrPass,encrPass); + } + +int RasMgrClientComm::openSocket() + { + ENTER("RasMgrClientComm::openSocket: enter." ); + + // if open already, close beforehand + if(rasmgrSocket!=-1) + { + TALK ("RasMgrClientComm::openSocket: socket was open, closing it." ); + closeSocket(); + } + + struct protoent *getprotoptr = getprotoptr=getprotobyname("tcp"); // FIXME: what is this??? + struct hostent *hostinfo = gethostbyname(rasmgrHost); + + if(hostinfo==NULL) + { + TALK ("RasMgrClientComm::openSocket: leave. unknown host " << rasmgrHost ); + return -1; + } + + sockaddr_in internetAddress; + internetAddress.sin_family=AF_INET; + internetAddress.sin_port=htons(rasmgrPort); + internetAddress.sin_addr=*(struct in_addr*)hostinfo->h_addr; + + rasmgrSocket=socket(PF_INET,SOCK_STREAM,getprotoptr->p_proto); + + if(rasmgrSocket<0) + { + int tempErrno = errno; + TALK ("RasMgrClientComm::openSocket: leave. error opening socket: " << strerror(tempErrno ) ); + return -1; + } + + if(0>connect(rasmgrSocket,(struct sockaddr*)&internetAddress,sizeof(internetAddress))) + { + int tempErrno = errno; + TALK ("RasMgrClientComm::openSocket: leave. error connecting socket: " << strerror(tempErrno ) ); + return -1; + } + + LEAVE( "RasMgrClientComm::openSocket: leave. ok." ); + return 0; + } + +void RasMgrClientComm::closeSocket() + { + ENTER( "RasMgrClientComm::closeSocket. enter." ); + if(rasmgrSocket >0) + { + TALK( "RasMgrClientComm::closeSocket. closing, socket=" << rasmgrSocket ); + close(rasmgrSocket); + rasmgrSocket=-1; + } + LEAVE( "RasMgrClientComm::closeSocket. leave." ); + } + +int RasMgrClientComm::sendMessage(const char *message) + { + char request[MAXMSG]; + int result = COMM_CONT; // was: 0, but this is same value + + ENTER( "RasMgrClientComm::sendMessage: enter. message=" << message ); + + sprintf(request, "POST rascontrol HTTP/1.1\r\nAccept: text/plain\r\nUserAgent: rascontrol/2.0"); + sprintf(request+strlen(request),"\r\nAuthorization: ras %s:%s",userName,encrPass);//"rasadmin","d293a15562d3e70b6fdc5ee452eaed40"); + sprintf(request+strlen(request),"\r\nContent length: %d\r\n\r\n%s ",strlen(message)+1,message); + + int reqLen=strlen(request)+1; // including final '\0'!! + if(writeWholeMessage(rasmgrSocket,request,reqLen)