Esempio n. 1
0
/**
 *  SAMP_MAPCLIENTS -- Map the public-ids of registered clients to the 
 *  appName.
 */
int
samp_mapClients (handle_t handle)
{
    Samp  *sampP = samp_H2P (handle);	/* get struct pointer	*/
    register int  i = 0;
    char  *pub, rstr[SZ_RESSTR], *sres = rstr;
    List clients;
    Map  resp;


    /*  Get the list of registered clients and their metadata.
     */        
    sampP->nclients = 0;
    clients = samp_GetRegisteredClients (handle);
    for (i=0; i < samp_listLen (clients); i++) {
	pub = samp_getStringFromList (clients, i);
        resp = samp_GetMetadata (handle, pub);
        xr_getStringFromStruct (resp, "samp.name", &sres);

	memset (sampP->clients[i].name, 0, SZ_NAME);
	memset (sampP->clients[i].pubId, 0, SZ_NAME);

	sampP->nclients++;
	strcpy (sampP->clients[i].name, sres);
	strcpy (sampP->clients[i].pubId, pub);
	/*
	if (resp) 
	    xr_freeStruct (resp);
	*/
    }
    samp_freeList (clients);

    return (SAMP_OK);
}
/**
 *  SAMP_REGISTER -- Register with the Hub.  We use the currently stored
 *  metadata.
 *
 *  @brief      ...
 *  @fn         handle = samp_hubOpen (Samp *samp)
 *
 *  @param  samp        ...
 *  @return             ...
 */
int
samp_Register (handle_t samp)
{
    char *privateKey, *hubId, *selfId;
    Samp *sampP = samp_H2P(samp);
    Hub  *hub 	= sampP->hub;
    int   reg;


    if (!hub)
	return (SAMP_ERR);

    privateKey = hub->privateKey;
    hubId      = hub->hubId;
    selfId     = hub->selfId;

    xr_initParam (hub->id);
    xr_setStringInParam (hub->id, hub->secret);

    xr_callSync (hub->id, "samp.hub.register");

    /* Check for an error response.
    */
    if (samp_replyStatus (hub->id) != SAMP_OK) {
	fprintf (stderr, "Hub registration failed.\n");
	return (SAMP_ERR);
    }
	

    xr_getStructFromResult (hub->id, &reg);
        xr_getStringFromStruct (reg, "samp.private-key", &privateKey);
        xr_getStringFromStruct (reg, "samp.hub-id", &hubId);
        xr_getStringFromStruct (reg, "samp.self-id", &selfId);

    /* Save the private key we get back.  Note there is a difference in
    ** behavior between the hubs as to whether we strip any prefix on the
    ** key returned in the response.
    */
    strcpy (hub->privateKey, &hub->privateKey[7]);

    return (SAMP_OK);
}
Esempio n. 3
0
/* PROC_CMD -- Process a single command.
 */
static int
proc_cmd (int samp, char *method, 
	char *arg1, char *arg2, char *arg3, char *arg4)
{
    register int   i = 0, stat = 0;
    char  *appName = NULL;
    char   rstr[SZ_RESSTR], *sres = rstr;
    Map    resp;


    if (debug)
	fprintf (stderr, "Proc_Cmd Method '%s'  Args: '%s' '%s' '%s'\n", 
				method,arg1,arg2,arg3);

    if (strcasecmp (method, "quit") == 0) {			/* QUIT */
	printf ("Quitting....\n\n");
        if (sampShutdown (samp) != 0)
            fprintf (stderr, "Shutdown fails\n");
        sampClose (samp);

	return (0);

    } else if (strcasecmp (method, "start") == 0) {		/* START */
	printf ("Starting SAMP interface .....\n");
        sampStartup (samp);

    } else if (strcasecmp (method, "stop") == 0) {		/* STOP */
	printf ("Stopping SAMP interface .....\n");
        sampShutdown (samp);

    } else if (strcasecmp (method, "help") == 0) {		/* HELP */
	help_summary ();

    } else if (strcasecmp (method, "listClient") == 0) {	/* LISTCLIENT */
	samp_listClients (samp);

    } else if (strcasecmp (method, "trace") == 0) {		/* TRACE */
	trace++;
	if (trace % 2)
            setenv ("XMLRPC_TRACE_XML", "1", 1);
	else
	    unsetenv ("XMLRPC_TRACE_XML");

    } else if (strcasecmp (method, "target") == 0) {		/* TARGET */
	appName = arg1;



    } else if (strcasecmp (method, "envSet") == 0) {		/* ENVSET */
	if (nscan < 4) {
	    cmdUsage ("envSet appName keyw value");
	} else {
	    stat = samp_envSet (samp, arg1, arg2, arg3);
	    print_result (samp, stat);
	}

    } else if (strcasecmp (method, "envGet") == 0) {		/* ENVGET */
	if (nscan < 3) {
	    cmdUsage ("envGet appName keyw");
	} else {
	    char *v = samp_envGet (samp, arg1, arg2);
	    if (v) 
	 	printf ("OK  '%s' = '%s'\n", arg2, v);
	    else
	 	printf ("ERR  '%s' not found\n", arg2);
	}
        ;

    } else if (strcasecmp (method, "paramSet") == 0) {		/* PARAMSET */
	if (nscan < 4) {
	    cmdUsage ("envSet appName keyw value");
	} else {
	    stat = samp_paramSet (samp, arg1, arg2, arg3);
	    print_result (samp, stat);
	}

    } else if (strcasecmp (method, "paramGet") == 0) {		/* PARAMGET */
        ;



    } else if (strcasecmp (method, "notify") == 0) {		/* NOTIFY */
	if (nscan < 3) {
	    cmdUsage ("notify appName mtype");
	} else {
	    int msg = make_msg (arg2);
	    samp_notify (samp, arg1, msg);
	    print_result (samp, SAMP_OK);
	}

    } else if (strcasecmp (method, "notifyAll") == 0) {		/* NOTIFYALL */
	if (nscan < 2) {
	    cmdUsage ("notifyAll mtype");
	} else {
	    int msg = make_msg (arg1);
	    stat = samp_notifyAll (samp, msg);
	    print_result (samp, stat);
	}

    } else if (strcasecmp (method, "call") == 0) {		/* CALL */
	if (nscan < 3) {
	    cmdUsage ("call appName mtype");
	} else {
	    int msg = make_msg (arg2);
	    char *id = samp_call (samp, arg1, "testTag", msg);
	    fprintf (stderr, "msgId = '%s'\n", id);
	    print_result (samp, stat);
	}

    } else if (strcasecmp (method, "callAll") == 0) {		/* CALLALL */
	if (nscan < 2) {
	    cmdUsage ("callAll mtype");
	} else {
	    int msg = make_msg (arg1);
	    stat = samp_callAll (samp, "allTag", msg);
	    print_result (samp, stat);
	}

    } else if (strcasecmp (method, "callAndWait") == 0) {	/* CALLANDWAIT*/
	if (nscan < 3) {
	    cmdUsage ("callAndWait appName mtype");
	} else {
	    int msg = make_msg (arg2);
	    stat = samp_callAndWait (samp, arg1, "waitTag", msg);
	    print_result (samp, stat);
	}



    } else if (strcasecmp (method, "Register") == 0) {		/* REGISTER */
	if (nscan < 3) {
	    cmdUsage ("Register appName appDesc");
	} else {
    	    if (samp_Register (samp) != OK)
		fprintf (stderr, "Registration fails\n");
    	    else {
		registered = 1;
		fprintf (stderr, "Registration OK\n");
	    }
	}

    } else if (strcasecmp (method, "Unregister") == 0) {  	/* UNREGISTER */
	if (samp == 0) {
	    fprintf (stderr, "Samp Hub not connected\n");
	} else {
    	    if (samp_UnRegister (samp) != OK)
		fprintf (stderr, "UnRegistration fails\n");
    	    else {
		registered = 0;
		fprintf (stderr, "UnRegistration OK\n");
	    }
	}

    } else if (strcasecmp (method, "DeclareMetadata") == 0) {	/* METADATA */
	if (samp == 0) {
	    fprintf (stderr, "Samp Hub not connected\n");
	} else {
    	    if (samp_DeclareMetadata (samp) != OK)
		fprintf (stderr, "Metadata declaration fails\n");
    	    else
		fprintf (stderr, "Metadata declaration OK\n");
	}

    } else if (strcasecmp (method, "Ping") == 0) {		/* PING */
	if (nscan < 2) {
	    cmdUsage ("Ping appName");

	} else {
	    if ((samp_Ping (samp, arg1)) < 0)
		fprintf (stderr, "%s responds w/ ERR\n", arg1);
	    else
		fprintf (stderr, "%s responds OK [%d]\n", arg1, cmdNum++);
	}

    } else if (strcasecmp (method, "GetMetadata") == 0) {      /* GETMETADATA */
	if (nscan < 2) {
	    cmdUsage ("GetMetadata appName");
	} else {
    	    if ((resp = samp_GetMetadata (samp, arg1)) > 0) {
	        xr_getStringFromStruct (resp, "samp.name", &sres);
	            printf ("  Name  = '%s'\n", sres);
	        xr_getStringFromStruct (resp, "author.name", &sres);
	            printf (" Author = '%s'\n", sres);
	        xr_getStringFromStruct (resp, "samp.description.text", &sres);
	            printf ("  Descr = '%s'\n", sres);
	        xr_getStringFromStruct (resp, "samp.icon.url", &sres);
	            printf ("IconURL = '%s'\n", sres);
	        xr_getStringFromStruct (resp, "samp.documentation.url", &sres);
	            printf (" DocURL = '%s'\n", sres);
	    }
	}
	    
							     /* SUBSCRIPTIONS */
    } else if (strcasecmp (method, "DeclareSubscriptions") == 0) {
	if ((samp_DeclareSubscriptions (samp)) < 0)
	    fprintf (stderr, "DeclareSubscriptions responds w/ ERR\n");
	else
	    fprintf (stderr, "DeclareSubscriptions OK\n");

    } else if (strcasecmp (method, "GetSubscriptions") == 0) {
	Map subs = (Map) 0;

	if ((subs = samp_GetSubscriptions (samp, arg1)) > 0) {
	    /*  Need xmlrpc_struct_read_member() to scan map names ....
	     */
	}

							     /* CLIENTS       */
    } else if (strcasecmp (method, "GetRegisteredClients") == 0) {
	/*  Print the list of registered clients.
	*/
	List clients = samp_GetRegisteredClients (samp);

	for (i=0; i < samp_listLen (clients); i++)
	    printf ("%s\n", samp_getStringFromList (clients, i));
	samp_freeList (clients);

    } else if (strcasecmp (method, "GetSubscribedClients") == 0) {
	if (nscan < 2) {
	    cmdUsage ("GetSubscribedClients mtype");

	} else {
	    /*  Print the list of clients subscribed to a specific mtype.
	     */
	    List clients = samp_GetSubscribedClients (samp, arg1);

	    for (i=0; i < samp_listLen (clients); i++)
	        printf ("%s\n", samp_getStringFromList (clients, i));
	    samp_freeList (clients);
	}


			      /* table.load.votable (url, [table-id], [name]) */
    } else if (strcasecmp (method, "tableLoadVOTable") == 0) {
	if (nscan < 2) {
	    cmdUsage ("tableLoadVOTable appName url [table-id] [name]");
	} else {
	    if (samp_tableLoadVOTable (samp, arg1, arg2, arg3, arg4) == SAMP_OK)
	        printf ("OK\n");
	    else
	        printf ("Error: '%s'\n", samp_getErr (samp));
	}

			         /* table.load.fits (url, [table-id], [name]) */
    } else if (strcasecmp (method, "tableLoadFITS") == 0) {
	if (nscan < 2) {
	    cmdUsage ("tableLoadFITS appName url [table-id] [name]");
	} else {
	    if (samp_tableLoadFITS (samp, arg1, arg2, arg3, arg4) == SAMP_OK)
	        printf ("OK\n");
	    else
	        printf ("Error: '%s'\n", samp_getErr (samp));
	}

			    /* table.highlight.row (table-id, url, (int) row) */
    } else if (strcasecmp (method, "tableHighlightRow") == 0) {
	;

			/* table.highlight.rowList (table-id, url, (List)row) */
    } else if (strcasecmp (method, "tableSelectRowList") == 0) {
	;

				 /* image.load.fits (url, [table-id], [name]) */
    } else if (strcasecmp (method, "imageLoadFits") == 0) {
	if (nscan < 2) {
	    cmdUsage ("imageLoadFITS appName url [image-id] [name]");
	} else {
	    if (samp_imageLoadFITS (samp, arg1, arg2, arg3, arg4) == SAMP_OK)
	        printf ("OK\n");
	    else
	        printf ("Error: '%s'\n", samp_getErr (samp));
	}

				 /* coord.pointAt.sky ((float)ra, (float)dec) */
    } else if (strcasecmp (method, "coordPointAtSky") == 0) {
	;

	/* spectrum.load.ssa-generic (url, (Map)meta, [spectrum-id], [name])  */
    } else if (strcasecmp (method, "specLoadSSAGeneric") == 0) {
	;

						     /* send (appName, mtype) */
    } else if (strcasecmp (method, "send") == 0) {
	;

    } else if (method[0])
	fprintf (stderr, "Unknown command '%s'\n", method);


    return (0);
}
Esempio n. 4
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 );
}