/**
 *  SAMP_GETMETADATA -- Get the metadata for a specified app.
 *
 *  @brief      Get the metadata for a specified app.
 *  @fn         map = samp_GetMetadata (Samp *samp, String pubId)
 *
 *  @param  samp        samp struct handle
 *  @param  pubId       App public-id
 *  @return             Map to message response
 */
Map
samp_GetMetadata (handle_t samp, String pubId)
{
    Samp *sampP = samp_H2P(samp);
    Hub *hub = sampP->hub;
    Map   resp  = (Map) 0;
/*
    Map   err = (Map) 0, snum = (Map) 0, res = (Map) 0;
*/


    if (!hub)
	return ((Map) 0);

    xr_initParam (hub->id);             /* set calling parameters       */
    xr_setStringInParam (hub->id, hub->privateKey);
    xr_setStringInParam (hub->id, pubId);

    if (xr_callSync (hub->id, "samp.hub.getMetadata") == OK) {
        xr_getStructFromResult (hub->id, &resp);
/*
        char  rstr[SZ_RESSTR], *sres = rstr;
        resp = samp_newMap ();
            xr_getStringFromStruct (snum, "samp.status", &sres);
	    if (strcasecmp (sres, "samp.ok") != 0) {
        	xr_freeStruct (snum);
    		return ( (Map) 0 );
	    } else {
                xr_getStructFromStruct (snum, "samp.error", &err);
                xr_getStringFromStruct (snum, "samp.errortxt", &sres);
		memset (sampP->errortxt, 0, SZ_LINE);
		strcpy (sampP->errortxt, sres);

                xr_getStructFromStruct (snum, "samp.result", &res);
	    }
        xr_freeStruct (snum);
*/
        return (resp);
    } else {
	memset (sampP->errortxt, 0, SZ_LINE);
	strcpy (sampP->errortxt, xr_getErrMsg (hub->id));
 
	fprintf (stderr, "Error: '%s'\n", sampP->errortxt);
    }

    return ( (Map) 0 );
}
/**
 *  SAMP_STARTUP -- Startup the SAMP interface to send/receive messages.
 *
 *  @brief  	Startup the SAMP interface to send/receive messages.
 *  @fn		sampStartup (handle_t handle)
 * 
 *  @param handle	user handle to samp struct
 *  @return		nothing
 */
int 
sampStartup (handle_t handle)
{
    Samp  *sampP = samp_H2P (handle);	/* get struct pointer	*/
    int    i, stat = 0, appnum = 0, done = 0;


    stat = SAMP_OK;
    if (sampP->active == 0) {
	/*  Startup the application server so we can handle the responses.
	 */
        samp_initServer (sampP);

	if (sampP->trace)
	    samp_printSubs (sampP);
	    
	/*  Open the Hub connection.
	 */
	if (sampP->hubHandle < 0)
            sampP->hubHandle = samp_hubOpen (sampP);

	/*  We *still* can't open the Hub, a first attempt failed in sampInit()
  	 */
        if (sampP->hubHandle < 0) {
            sampP->hub = (Hub *) NULL;
	    if (sampP->verbose)
	        fprintf (stderr, "Error: No Hub available on startup\n");
    	    sampP->active = 0;
	    stat = SAMP_ERR;
	} else {
            sampP->hub = (Hub *) samp_H2P (sampP->hubHandle);
    	    sampP->active = 1;
	}
    }


    /*  Do the client startup declarations to the hub.
     */
    if (sampP->hub) {
	char  base[SZ_LINE];

	if (sampP->mapClients) {
            /*  Final step is to gather the metadata from the other attached
             *  clients.  This allows us to send messages to the appName and not
             *  just the public ID.
             */
            samp_mapClients (handle);

	    /* First get the list of currently registered clients.  If we find
 	     * one already registered using our name, add a number to the name
	     * and try agin.
	     */
	    memset (base, 0, SZ_LINE);
	    strcpy (base, sampP->appName);
	    while (!done) {
	        done = 1;
                for (i=0; i < sampP->nclients; i++) {
                    if (strcasecmp (sampP->appName,sampP->clients[i].name)==0) {
		        sprintf (sampP->appName, "%s%d", base, ++appnum);
    		        strcpy (hub->appName, sampP->appName);
		        done = 0;
                        break;
                    }
                }
	    }
	}

        if (samp_hubDeclareMetadata (hub) != SAMP_OK) {
            fprintf (stderr, "Error: Metadata declaration fails: '%s'\n", 
                xr_getErrMsg (hub->id));
            return ((int) 0);
        }

        if (samp_hubDeclareSubscriptions (hub) != SAMP_OK) {
            fprintf (stderr, "Error: Subscription declaration fails: '%s'\n", 
                xr_getErrMsg (hub->id));
            return ((int) 0);
        }

    }

    return (stat);
}
Beispiel #3
0
/**
 *  SAMP_PARAMGET -- Get a parameter variable in a remote application.
 *
 *  @brief      Get a parameter variable in a remote application.
 *  @fn         val = samp_paramGet (handle_t handle, String recip, String name)
 *
 *  @param  handle      samp struct handle
 *  @param  recip       name of recipient (or 'all')
 *  @param  name        Name of variable in receiver's environment
 *  @return        	NULL, Value of variable, or list of values
 */
char *
samp_paramGet (handle_t handle, String recip, String name)
{
    register int  i, nfound = 0;
    Samp *sampP = samp_H2P (handle);
    Hub    *hub = sampP->hub;
    Map    resp = (Map) 0;
    Map    sres = (Map) 0;
    static char val[SZ_LINE], *value = val;

    Msg     msg = samp_newMsg ();
    Param param = samp_newParam ();


    if (!hub)
	return (NULL);

    /*  Create message map.
     */
    samp_msgMType (msg, "client.param.get");
    samp_msgParam (msg, param);
	samp_addStringParam (msg, "name", name);


    /*  Send the message using the synch pattern, don't provide a choice.
     */
    memset (value, 0, SZ_LINE);
    if (strcmp ("all", recip) == 0) {
	char result[SZ_LINE], *res = result;

	for (i=0; i < sampP->nclients; i++) {
	    if (strcasecmp (sampP->appName, sampP->clients[i].name) == 0)
		continue;
	    if (strcasecmp ("hub", sampP->clients[i].name) == 0)
		continue;
	    if (!sampP->clients[i].name[0])
		continue;

            xr_initParam (hub->id);         /* set calling parameters       */
            xr_setStringInParam (hub->id, hub->privateKey);
            xr_setStringInParam (hub->id, sampP->clients[i].pubId);
            xr_setStructInParam (hub->id, msg);
            xr_setStringInParam (hub->id, hub->timeout);

            if (xr_callSync (hub->id, "samp.hub.callAndWait") == OK ) {
                /*  Save the response to a new map so we can free the result.
                 */
		memset (res, 0, SZ_LINE);
                xr_getStructFromResult (hub->id, &resp);
        	xr_getStructFromStruct (resp, "samp.result", &sres);
        	xr_getStringFromStruct (sres, "value", &res);
		nfound++;

            } else {
		strcpy (res, "Error");
            }
	    if (i && nfound > 1)
	        strcat (value, ",");

	    strcat (value, sampP->clients[i].name);
	    if (strchr (res, (int) ' ') || strchr (res, (int) ',')) {
	        strcat (value, ":\"");
	        strcat (value, res);
	        strcat (value, "\"");
	    } else {
	        strcat (value, ":");
	        strcat (value, res);
	    }
	}

    } else {
        xr_initParam (hub->id);             /* set calling parameters       */
        xr_setStringInParam (hub->id, hub->privateKey);
        xr_setStringInParam (hub->id, samp_app2id(handle, recip));
        xr_setStructInParam (hub->id, msg);
        xr_setStringInParam (hub->id, hub->timeout);

        if (xr_callSync (hub->id, "samp.hub.callAndWait") == OK ) {
            /*  Save the response to a new map so we can free the msg result.
             */
            xr_getStructFromResult (hub->id, &resp);
            xr_getStructFromStruct (resp, "samp.result", &sres);
            xr_getStringFromStruct (sres, "value", &value);

        } else {
            strcpy (sampP->errortxt, xr_getErrMsg (hub->id));
            return (NULL);
        }
    }

    samp_freeMsg (msg);
    return ( value );
}