Here's an example of how to unmarshall a result string from a STAF service 
request using the STAF C Unmarshall API.  This example submits a "STAF 
local MISC SETTINGS" service request which returns a marshalled result 
whose root object is a map and unmarshalls the result and accesses the 
value for one of the keys in the map.  We plan to add an unmarshalling 
example using the STAF C APIs to the STAF documentation (probably based on 
this example).

Note that we think that using the STAF C++ APIs to unmarshall data is 
easier than using the STAF C APIs (if you can use C++ instead of C).  The 
STAF services use the C++ unmarshall APIs, so there are also lots of C++ 
examples in the STAF source code.  When using the STAF C APIs to 
unmarshall data, you'll probably find it helpful to look at STAF source 
code like stafif/STAFDataTypesInlImpl.cpp.

#include "STAF.h"
#include "STAFDataTypes.h"
#include "STAF_iostream.h"

int main(int argc, char **argv)
{
    STAFRC_t rc = 0;
    char *handleName = "MyProgram";
    STAFHandle_t myHandle = 0;

    if ((rc = STAFRegister(handleName, &myHandle)) != 0)
    {
        printf("Error registering with STAF, RC: %d\n", rc);
        return rc;
    }

    printf("\nUsing STAF handle number:  %d\n", myHandle);

    // Submit a STAF local MISC LIST SETTINGS service request

    char *someMachine = "local";
    char *service = "MISC";
    char *request = "LIST SETTINGS";
    unsigned int requestLength = strlen(request);
    char *result = 0;
    unsigned int resultLength = 0;

    printf("\nSTAF %s %s %s\n", someMachine, service, request);

    rc = STAFSubmit(myHandle, someMachine, service, request, 
requestLength,
                    &result, &resultLength);

    printf("\nRC: %d\n", rc);
    printf("\nMarshalled Result String:\n%s\n", result);

    // Unmarshall the marshalled result string

    STAFObject_t context = 0;
    unsigned int flags = 0;
    STAFString_t *marshalledString;
    STAFStringConstruct(marshalledString, result, resultLength, NULL);
    STAFObjectUnmarshallFromString(&context, *marshalledString, 0, flags);

    // Print the formatted output for the result's marshalling context

    STAFString_t outString = 0;
    STAFObjectGetFormattedStringValue(context, &outString, 0);
    cout << "\nFormatted output for the result's marshalling context:\n"
         << STAFString(outString, STAFString::kShallow) << endl;

    // Get the root object from the marshalling context (a map object)

    STAFObject_t mapObj = 0;
    STAFObjectMarshallingContextGetRootObject(context, &mapObj);

    // Get the string value of the "connectAttempts" key from the map 
object

    const STAFString key("connectAttempts");
    STAFObject_t valueObj = 0;
    STAFObjectMapGet(mapObj, key.getImpl(), &valueObj);

    STAFString_t connectAttempts = 0;
    STAFObjectGetStringValue(valueObj, &connectAttempts);
    cout << "\nconnectAttempts: "
         << STAFString(connectAttempts, STAFString::kShallow) << endl;

    STAFFree(myHandle, result);

    if ((rc = STAFUnRegister(myHandle)) != 0)
    {
        printf("\nError unregistering with STAF, RC: %d\n", rc);
        return rc;
    }

    printf("\nEnd of Test\n");

    return 0;
}

Here's the output from running this TestC program:

[r...@staf1b TestC]# g++ TestC.c -o TestC -lSTAF -L/usr/local/staf/lib 
-I/usr/local/staf/include
[r...@staf1b TestC]# ./TestC

Using STAF handle number:  90

STAF local MISC LIST SETTINGS

RC: 0

Marshalled Result String:
@SDT/*:1334:@SDT/{:1110::13:map-class-...@sdt/{:1081::26:STAF/Service/Misc/Setti
n...@sdt/{:1039::4:k...@sdt/[11:975:@SDT/{:78::12:display-n...@sdt/$S:19:Connecti
on 
Attempts:3:k...@sdt/$S:15:connectattem...@sdt/{:80::12:display-n...@sdt/$S:19:
Connect Retry 
Delay:3:k...@sdt/$S:17:connectretryde...@sdt/{:77::12:display-name@
SDT/$S:17:Interface 
Cycling:3:k...@sdt/$S:16:interfacecycl...@sdt/{:74::12:displa
y-n...@sdt/$S:18:Maximum Queue 
Size:3:k...@sdt/$S:12:maxqueues...@sdt/{:85::12:di
splay-n...@sdt/$S:24:Maximum Return File 
Size:3:k...@sdt/$S:17:maxReturnFileSize@
SDT/{:73::12:display-n...@sdt/$S:15:Initial 
Threads:3:k...@sdt/$S:14:initialThrea
d...@sdt/{:80::12:display-n...@sdt/$S:19:Thread Growth 
Delta:3:k...@sdt/$S:17:threa
dgrowthde...@sdt/{:64::12:display-n...@sdt/$S:14:Data 
Directory:3:k...@sdt/$S:7:d
ata...@sdt/{:77::12:display-n...@sdt/$S:17:Default 
Interface:3:k...@sdt/$S:16:def
aultinterf...@sdt/{:85::12:display-n...@sdt/$S:21:Default 
Authenticator:3:k...@sd
T/$S:20:defaultauthentica...@sdt/{:92::12:display-n...@sdt/$S:25:Result 
Compatib
ility 
Mode:3:k...@sdt/$S:23:resultCompatibilityMode:4:n...@sdt/$S:26:STAF/Service
/Misc/setti...@sdt/%:201::26:STAF/Service/Misc/setti...@sdt/$S:1:2...@sdt/$S:4:1000
@SDT/$S:7:enab...@sdt/$S:5:50...@sdt/$S:1:0...@sdt/$S:2:1...@sdt/$S:1:1...@sdt/$S:25:/us
r/local/staf/data/s...@sdt/$S:3:s...@sdt/$S:4:n...@sdt/$S:7:Verbose

Formatted output for the result's marshalling context:
{
  Connection Attempts      : 2
  Connect Retry Delay      : 1000
  Interface Cycling        : Enabled
  Maximum Queue Size       : 50000
  Maximum Return File Size : 0
  Initial Threads          : 10
  Thread Growth Delta      : 1
  Data Directory           : /usr/local/staf/data/STAF
  Default Interface        : ssl
  Default Authenticator    : none
  Result Compatibility Mode: Verbose
}

connectAttempts: 2

End of Test
[r...@staf1b TestC]#

--------------------------------------------------------------
Sharon Lucas
IBM Austin,   luc...@us.ibm.com
(512) 286-7313 or Tieline 363-7313




Phil Rumble <prum...@au1.ibm.com> 
01/13/2010 09:03 PM

To
staf-users@lists.sourceforge.net
cc

Subject
[staf-users] C Example for handling marshalled data







Can anyone provide a C example of handling marshalled data being returned
from a STAFSubmit call.

Thanks


Rumble


------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for 
Conference
attendees to learn about information security's most important issues 
through
interactions with peers, luminaries and emerging and established 
companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

------------------------------------------------------------------------------
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Conference
attendees to learn about information security's most important issues through
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users

Reply via email to