dnxProtocol.c

Go to the documentation of this file.
00001 /*--------------------------------------------------------------------------
00002 
00003    Copyright (c) 2006-2007, 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 "dnxProtocol.h"
00029 
00030 #include "dnxError.h"
00031 #include "dnxDebug.h"
00032 #include "dnxTransport.h"
00033 #include "dnxXml.h"
00034 #include "dnxLogging.h"
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <string.h>
00040 #include <syslog.h>
00041 #include <assert.h>
00042 
00043 //----------------------------------------------------------------------------
00044 
00054 int dnxMakeXID(DnxXID * pxid, DnxObjType xType, unsigned long xSerial,
00055       unsigned long xSlot)
00056 {
00057    assert(pxid && xType >= 0 && xType < DNX_OBJ_MAX);
00058 
00059    pxid->objType   = xType;
00060    pxid->objSerial = xSerial;
00061    pxid->objSlot   = xSlot;
00062 
00063    return DNX_OK;
00064 }
00065 
00066 //----------------------------------------------------------------------------
00067 
00076 int dnxEqualXIDs(DnxXID * pxa, DnxXID * pxb)
00077 {
00078    return pxa->objType == pxb->objType
00079          && pxa->objSerial == pxb->objSerial
00080          && pxa->objSlot == pxb->objSlot;
00081 }
00082 
00083 //--------------------------------------------------------------------------*/
00084 
00096 int dnxSendMgmtRequest(DnxChannel * channel, DnxMgmtRequest * pRequest)
00097 {
00098    DnxXmlBuf xbuf;
00099 
00100    assert(channel && pRequest);
00101 
00102    // create the XML message
00103    dnxXmlOpen (&xbuf, "MgmtRequest");
00104    dnxXmlAdd  (&xbuf, "XID",    DNX_XML_XID, &pRequest->xid);
00105    dnxXmlAdd  (&xbuf, "GUID",   DNX_XML_XID, &pRequest->xid);  // old format - for bc
00106    dnxXmlAdd  (&xbuf, "Action", DNX_XML_STR,  pRequest->action);
00107    dnxXmlClose(&xbuf);
00108 
00109    dnxDebug(3, "dnxSendMgmtRequest: XML msg(%d bytes)=%s.", 
00110          xbuf.size, xbuf.buf);
00111 
00112    // send it on the specified channel
00113    return dnxPut(channel, xbuf.buf, xbuf.size, 0, 0);
00114 }
00115 
00116 //----------------------------------------------------------------------------
00117 
00126 int dnxSendMgmtReply(DnxChannel * channel, DnxMgmtReply * pReply, 
00127       char * address)
00128 {
00129    DnxXmlBuf xbuf;
00130    char addrstr[DNX_MAX_ADDRSTR];
00131 
00132    assert(channel && pReply);
00133 
00134    // create the XML message
00135    dnxXmlOpen (&xbuf, "MgmtReply");
00136    dnxXmlAdd  (&xbuf, "XID",    DNX_XML_XID, &pReply->xid);
00137    dnxXmlAdd  (&xbuf, "Status", DNX_XML_INT, &pReply->status);
00138    dnxXmlAdd  (&xbuf, "Result", DNX_XML_STR,  pReply->reply);
00139    dnxXmlClose(&xbuf);
00140 
00141    dnxDebug(3, "dnxSendMgmtReply: XML msg(%d bytes) = %s to %s.", 
00142          xbuf.size, xbuf.buf, dnxNtop(address, addrstr, sizeof addrstr));
00143    
00144    // send it on the specified channel
00145    return dnxPut(channel, xbuf.buf, xbuf.size, 0, address);
00146 }
00147 
00148 //----------------------------------------------------------------------------
00149 
00164 int dnxWaitForMgmtReply(DnxChannel * channel, DnxMgmtReply * pReply, int timeout)
00165 {
00166    DnxXmlBuf xbuf;
00167    int ret;
00168 
00169    assert(channel && pReply);
00170 
00171    memset(pReply, 0, sizeof *pReply);
00172 
00173    // await a message from the specified channel
00174    xbuf.size = sizeof xbuf.buf - 1;
00175    if ((ret = dnxGet(channel, xbuf.buf, &xbuf.size, timeout, 0)) != DNX_OK)
00176       return ret;
00177 
00178    // decode the XML message
00179    xbuf.buf[xbuf.size] = 0;
00180    dnxDebug(3, "dnxWaitForMgmtReply: XML msg(%d bytes)=%s.", xbuf.size, xbuf.buf);
00181 
00182    // verify this is a "MgmtRequest" message
00183    if ((ret = dnxXmlCmpStr(&xbuf, "Request", "MgmtReply")) != DNX_OK)
00184       return ret;
00185 
00186    // decode the Manager's XID.
00187    if ((ret = dnxXmlGet(&xbuf, "XID", DNX_XML_XID, &pReply->xid)) != DNX_OK)
00188       return ret;
00189 
00190    // decode the Manager's XID.
00191    if ((ret = dnxXmlGet(&xbuf, "Status", DNX_XML_INT, &pReply->status)) != DNX_OK)
00192       return ret;
00193 
00194    // decode the management request
00195    return dnxXmlGet(&xbuf, "Result", DNX_XML_STR, &pReply->reply);
00196 }
00197 
00198 //---------------------------------------------------------------------
00199 
00213 int dnxWaitForMgmtRequest(DnxChannel * channel, DnxMgmtRequest * pRequest,
00214       char * address, int timeout)
00215 {
00216    DnxXmlBuf xbuf;
00217    int ret;
00218    char addrstr[DNX_MAX_ADDRSTR];
00219    assert(channel && pRequest);
00220 
00221    memset(pRequest, 0, sizeof *pRequest);
00222 
00223    // await a message from the specified channel
00224    xbuf.size = sizeof xbuf.buf - 1;
00225    if ((ret = dnxGet(channel, xbuf.buf, &xbuf.size, timeout, address)) != DNX_OK)
00226       return ret;
00227 
00228    // decode the XML message
00229    xbuf.buf[xbuf.size] = 0;
00230    dnxNtop(address, addrstr, sizeof addrstr);
00231    dnxDebug(3, "dnxWaitForMgmtRequest: XML msg(%d bytes)=%s. from %s", 
00232          xbuf.size, xbuf.buf, addrstr);
00233 
00234    // verify this is a "MgmtRequest" message
00235    if ((ret = dnxXmlCmpStr(&xbuf, "Request", "MgmtRequest")) != DNX_OK)
00236       return ret;
00237 
00238    // decode the Manager's XID (support older GUID format as well).
00239    if ((ret = dnxXmlGet(&xbuf, "XID", DNX_XML_XID, &pRequest->xid)) != DNX_OK
00240          && (ret = dnxXmlGet(&xbuf, "GUID", DNX_XML_XID, &pRequest->xid)) != DNX_OK)
00241       return ret;
00242 
00243    // decode the management request
00244    return dnxXmlGet(&xbuf, "Action", DNX_XML_STR, &pRequest->action);
00245 }
00246 
00247 //----------------------------------------------------------------------------

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