src/network/NetworkPacket.cxx

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Prabakaran Thirumalai   *
00003  *   praba_tuty@yahoo.com   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <CSql.h>
00021 #include <Network.h>
00022 #include <DataType.h>
00023 #include <SqlLogStatement.h>
00024 
00025 DbRetVal PacketPrepare::marshall()
00026 {
00027     printDebug(DM_Network, "PacketPrepare::marshall called\n");
00028     bufferSize  = sizeof(int) * 4 + strlen(stmtString) + 1;
00029     printDebug(DM_Network, "NOOFPARAMS %d buffer size %d\n", noParams, bufferSize);
00030     printDebug(DM_Network, "stmt %s size %d\n", stmtString, strlen(stmtString));
00031     printDebug(DM_Network, "noParams is %d\n", noParams);
00032     if (noParams >0)
00033         bufferSize = bufferSize + 2 * sizeof(int) * noParams;
00034     buffer = (char*) malloc(bufferSize);
00035     *(int*)buffer = stmtID;
00036     char *bufIter = buffer + sizeof(int);
00037     *(int*)bufIter = syncMode;
00038     bufIter = bufIter + sizeof(int); 
00039     *(int*)bufIter = strlen(stmtString);
00040     bufIter = bufIter + sizeof(int);
00041     *(int*)bufIter = noParams;
00042     bufIter = bufIter + sizeof(int); 
00043     if (noParams >0) { 
00044        memcpy(bufIter, type, sizeof(int) * noParams);
00045        bufIter = bufIter + sizeof(int)* noParams; 
00046        memcpy(bufIter, length, sizeof(int) * noParams);
00047        bufIter = bufIter + sizeof(int)* noParams; 
00048     }
00049     strcpy(bufIter, stmtString);
00050     printDebug(DM_Network, "PacketPrepare::marshall ended\n");
00051     return OK;
00052 }
00053 DbRetVal PacketPrepare::unmarshall()
00054 {
00055     printDebug(DM_Network, "PacketPrepare::unmarshall called\n");
00056     stmtID = *(int*)buffer;
00057     printDebug(DM_Network, "start of the buffer is %x\n", buffer);
00058     char *bufIter = buffer + sizeof (int);
00059     syncMode = *(int*)bufIter;
00060     bufIter = bufIter + sizeof(int);
00061     stmtLength = *(int*)bufIter;
00062     bufIter = bufIter + sizeof(int);
00063     noParams = *(int*)bufIter;
00064     bufIter = bufIter + sizeof(int);
00065     if (noParams >0) { 
00066         type = (int*) bufIter;
00067         bufIter = bufIter + sizeof(int) * noParams;
00068         length = (int*) bufIter;
00069         bufIter = bufIter + sizeof(int) * noParams;
00070     }
00071     stmtString = bufIter;
00072     printDebug(DM_Network, "stmtString ptr is %x\n", stmtString);
00073     stmtString[stmtLength+1] = '\0';
00074     printDebug(DM_Network, "PacketPrepare::unmarshall ended\n");
00075     return OK;
00076 }
00077 DbRetVal PacketFree::marshall()
00078 {
00079     bufferSize  = sizeof(int);
00080     buffer = (char*) malloc(bufferSize);
00081     *(int*)buffer = stmtID;
00082     return OK;
00083 }
00084 DbRetVal PacketFree::unmarshall()
00085 {
00086     stmtID = *(int*)buffer;
00087     return OK;
00088 }
00089 void PacketExecute::setParams(List list)
00090 {
00091     paramList = list;
00092     noParams = list.size();
00093     paramValues = new char*[noParams];
00094     BindSqlField* bindField = NULL;
00095     for (int i = 0 ; i < noParams; i++)
00096     {
00097         bindField = (BindSqlField*) paramList.get(i+1);
00098         paramValues[i] = (char*) bindField->value;
00099     }
00100     return; 
00101 }
00102 void PacketExecute::setStatementList(List stmtlist)
00103 {
00104     stmtList = stmtlist;
00105     return; 
00106 }
00107 DbRetVal PacketExecute::marshall()
00108 {
00109     bufferSize  = sizeof(int)+ sizeof(int);
00110     BindSqlField* bindField = NULL;
00111     for (int i = 0 ; i < noParams; i++)
00112     {
00113         bindField = (BindSqlField*) paramList.get(i+1);
00114         bufferSize = bufferSize + AllDataType::size(bindField->type, bindField->length);
00115     }
00116     buffer = (char*) malloc(bufferSize);
00117     *(int*)buffer = stmtID;
00118     char* bufIter = (char*) buffer + sizeof(int);
00119     *(int*)bufIter = noParams;
00120     bufIter = (char*) bufIter + sizeof(int);
00121     for (int i = 0 ; i < noParams; i++)
00122     {
00123         bindField = (BindSqlField*) paramList.get(i+1);
00124         AllDataType::copyVal(bufIter, bindField->value, bindField->type,bindField->length);
00125         bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
00126     }
00127     return OK;
00128 }
00129 DbRetVal PacketExecute::unmarshall()
00130 {
00131     stmtID = *(int*)buffer;
00132     char *bufIter = buffer + sizeof(int);
00133     noParams = *(int*)bufIter;
00134     bufIter = bufIter +sizeof(int);
00135     ListIterator stmtIter = stmtList.getIterator();
00136     NetworkStmt *stmt;
00137     while (stmtIter.hasElement())
00138     {
00139        stmt = (NetworkStmt*) stmtIter.nextElement();
00140        //TODO::Also check teh srcNetworkID
00141        if (stmt->stmtID == stmtID ) break;
00142     } 
00143     if (noParams == 0) return OK;
00144     paramValues = new char*[noParams];
00145     ListIterator paramIter = stmt->paramList.getIterator();
00146     BindSqlField *bindField = NULL;
00147     for (int i=0; i <noParams; i++)
00148     { 
00149         paramValues[i] = bufIter;
00150         bindField = (BindSqlField*) stmt->paramList.get(i+1);
00151         bufIter = bufIter + AllDataType::size(bindField->type, bindField->length);
00152     }
00153     return OK;
00154 }
00155 
00156 void PacketCommit::setExecPackets(int tid, List list)
00157 {
00158     txnID = tid;
00159     noOfStmts = list.size();
00160     stmtBufSize = new int[noOfStmts];
00161     stmtBuffer = new char*[noOfStmts];
00162     PacketExecute* pkt = NULL;
00163     int totalSize =0;
00164     for (int i = 0 ; i < noOfStmts; i++)
00165     {
00166         pkt = (PacketExecute*) list.get(i+1);
00167         if (pkt == NULL) printError(ErrSysFatal, "pkt is null.should never happen\n");
00168         stmtBufSize[i] = pkt->getBufferSize();
00169         stmtBuffer[i] = pkt->getMarshalledBuffer();
00170         //                             *(int*)(((char*)stmtBuffer[i]) + 4));
00171         totalSize = totalSize + stmtBufSize[i];
00172     }
00173     totalSize = sizeof(int) + sizeof(int) + noOfStmts * sizeof(int) +
00174                totalSize;
00175     bufferSize = totalSize;
00176     return;
00177 }
00178 DbRetVal PacketCommit::marshall()
00179 {
00180     buffer = (char*) malloc(bufferSize);
00181     *(int*)buffer = txnID;
00182     char* bufIter = (char*) buffer + sizeof(int);
00183     *(int*)bufIter = noOfStmts;
00184     bufIter = (char*) bufIter + sizeof(int);
00185     memcpy(bufIter, stmtBufSize, noOfStmts*sizeof(int));
00186     bufIter = (char*) bufIter + noOfStmts* sizeof(int);
00187     for (int i=0; i < noOfStmts; i++)
00188     {
00189         memcpy(bufIter, stmtBuffer[i], stmtBufSize[i]);
00190         bufIter = bufIter + stmtBufSize[i];
00191     }
00192     return OK;
00193 }
00194 DbRetVal PacketCommit::unmarshall()
00195 {
00196     txnID = *(int*)buffer;
00197     char *bufIter = buffer + sizeof(int);
00198     noOfStmts = *(int*)bufIter;
00199     bufIter = bufIter + sizeof(int);
00200     stmtBufSize = new int[noOfStmts];
00201     memcpy(stmtBufSize, bufIter, noOfStmts*sizeof(int));
00202     bufIter = bufIter + noOfStmts * sizeof(int);
00203     stmtBuffer = new char*[noOfStmts];
00204     for (int i = 0 ; i  <noOfStmts; i++)
00205     {
00206        stmtBuffer[i] = bufIter;
00207        bufIter = bufIter + stmtBufSize[i];
00208     }
00209 }
00210 //call unmarshall  before calling this
00211 void PacketCommit::getExecPacketList(List stmtList, List &list)
00212 {
00213     PacketExecute* pkt = NULL;
00214     for (int i = 0 ; i < noOfStmts; i++)
00215     {
00216         pkt = new PacketExecute();
00217         pkt->setBuffer(stmtBuffer[i]);
00218         pkt->setBufferSize(stmtBufSize[i]);
00219         pkt->setStatementList(stmtList);
00220         pkt->unmarshall();
00221         list.append(pkt);
00222     }
00223 
00224 }

Generated on Mon Jun 9 22:37:14 2008 for csql by  doxygen 1.4.7