dnxSrvProt.c

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------------
00002 
00003    Copyright (c) 2006-2010, Intellectual Reserve, Inc. All rights reserved.
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 version 2 as
00007    published by the Free Software Foundation.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012    GNU General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; if not, write to the Free Software
00016    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018   --------------------------------------------------------------------------*/
00019 
00028 #include "dnxSrvProt.h"
00029 #include "dnxXml.h"
00030 #include "dnxError.h"
00031 #include "dnxDebug.h"
00032 
00033 #include <assert.h>
00034 #include <string.h>
00035 
00036 //----------------------------------------------------------------------------
00037 
00048 int dnxSendJob(DnxChannel * channel, DnxJob * pJob, char * address)
00049 {
00050    DnxXmlBuf xbuf;
00051 
00052    assert(channel && pJob && pJob->cmd && *pJob->cmd);
00053 
00054    // create the XML message
00055    dnxXmlOpen (&xbuf, "Job");
00056    dnxXmlAdd  (&xbuf, "XID",      DNX_XML_XID,  &pJob->xid);
00057    dnxXmlAdd  (&xbuf, "GUID",     DNX_XML_XID,  &pJob->xid); // old format - for bc
00058    dnxXmlAdd  (&xbuf, "State",    DNX_XML_INT,  &pJob->state);
00059    dnxXmlAdd  (&xbuf, "Priority", DNX_XML_INT,  &pJob->priority);
00060    dnxXmlAdd  (&xbuf, "Timeout",  DNX_XML_INT,  &pJob->timeout);
00061    dnxXmlAdd  (&xbuf, "Command",  DNX_XML_STR,   pJob->cmd);
00062    dnxXmlClose(&xbuf);
00063 
00064    dnxDebug(3, "dnxSendJob: XML msg(%d bytes)=%s.", xbuf.size, xbuf.buf);
00065 
00066    // send it on the specified channel
00067    return dnxPut(channel, xbuf.buf, xbuf.size, 0, address);
00068 }
00069 
00070 //----------------------------------------------------------------------------
00071 
00085 int dnxWaitForNodeRequest(DnxChannel * channel, DnxNodeRequest * pReg, 
00086       char * address, int timeout)
00087 {
00088    DnxXmlBuf xbuf;
00089    int ret;
00090 
00091    assert(channel && pReg);
00092 
00093    memset(pReg, 0, sizeof *pReg);
00094 
00095    // await a message from the specified channel
00096    xbuf.size = sizeof xbuf.buf - 1;
00097    if ((ret = dnxGet(channel, xbuf.buf, &xbuf.size, timeout, address)) != DNX_OK)
00098       return ret;
00099 
00100    // decode the XML message:
00101    xbuf.buf[xbuf.size] = 0;
00102    dnxDebug(3, "dnxWaitForNodeRequest: XML msg(%d bytes)=%s.", xbuf.size, xbuf.buf);
00103 
00104    // verify this is a "NodeRequest" message
00105    if ((ret = dnxXmlCmpStr(&xbuf, "Request", "NodeRequest")) != DNX_OK)
00106       return ret;
00107 
00108    // decode the worker node's XID (support older GUID format as well)
00109    if ((ret = dnxXmlGet(&xbuf, "XID", DNX_XML_XID, &pReg->xid)) != DNX_OK
00110          && (ret = dnxXmlGet(&xbuf, "GUID", DNX_XML_XID, &pReg->xid)) != DNX_OK)
00111       return ret;
00112 
00113    // decode request type
00114    if ((ret = dnxXmlGet(&xbuf, "ReqType", DNX_XML_INT, &pReg->reqType)) != DNX_OK)
00115       return ret;
00116 
00117    // decode job capacity (support strange mixture of JobCap and Capacity)
00118    if ((ret = dnxXmlGet(&xbuf, "JobCap", DNX_XML_INT, &pReg->jobCap)) != DNX_OK
00119          && (ret = dnxXmlGet(&xbuf, "Capacity", DNX_XML_INT, &pReg->jobCap)) != DNX_OK)
00120       return ret;
00121 
00122    // decode job expiration (Time-To-Live in seconds)
00123    if ((ret = dnxXmlGet(&xbuf, "TTL", DNX_XML_INT, &pReg->ttl)) != DNX_OK)
00124       return ret;
00125 
00126    // msg okay - translate address now to save time in logging later
00127    dnxNtop(address, pReg->addrstr, sizeof pReg->addrstr);
00128 
00129    return DNX_OK;
00130 }
00131 
00132 //----------------------------------------------------------------------------
00133 
00147 int dnxWaitForResult(DnxChannel * channel, DnxResult * pResult, 
00148       char * address, int timeout)
00149 {
00150    DnxXmlBuf xbuf;
00151    int ret;
00152 
00153    assert(channel && pResult);
00154 
00155    memset(pResult, 0, sizeof *pResult);
00156 
00157    // await a message from the specified channel
00158    xbuf.size = sizeof xbuf.buf - 1;
00159    if ((ret = dnxGet(channel, xbuf.buf, &xbuf.size, timeout, address)) != DNX_OK)
00160       return ret;
00161 
00162    // decode the XML message
00163    xbuf.buf[xbuf.size] = 0;
00164    dnxDebug(3, "dnxWaitForResult: XML msg(%d bytes)=%s.", xbuf.size, xbuf.buf);
00165 
00166    // verify this is a "Result" message
00167    if ((ret = dnxXmlCmpStr(&xbuf, "Request", "Result")) == DNX_OK)
00168    {
00169 
00170        // decode the result's XID (support older GUID format as well)
00171        if ((ret = dnxXmlGet(&xbuf, "XID", DNX_XML_XID, &pResult->xid)) != DNX_OK
00172              && (ret = dnxXmlGet(&xbuf, "GUID", DNX_XML_XID, &pResult->xid)) != DNX_OK)
00173           return ret;
00174 
00175        // decode the result's state
00176        if ((ret = dnxXmlGet(&xbuf, "State", DNX_XML_INT, &pResult->state)) != DNX_OK)
00177           return ret;
00178 
00179        // decode the result's execution time delta
00180        if ((ret = dnxXmlGet(&xbuf, "Delta", DNX_XML_UINT, &pResult->delta)) != DNX_OK)
00181           return ret;
00182 
00183        // decode the result's result code
00184        if ((ret = dnxXmlGet(&xbuf, "ResultCode", DNX_XML_INT, &pResult->resCode)) != DNX_OK)
00185           return ret;
00186 
00187        // decode the result's result data
00188        return dnxXmlGet(&xbuf, "ResultData", DNX_XML_STR, &pResult->resData);
00189    }
00190 
00191 }
00192 
00193 //----------------------------------------------------------------------------
00194 

Generated on Tue Apr 13 15:48:07 2010 for DNX by  doxygen 1.5.6