Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Тестовые примеры / ch12 / call_back / aclk_cls
.C#include <netconfig.h>
#include "aclock.h"
#include "RPC.h"
RPC_svc *svcp = 0;
#define CLNTPROGNUM 0x20000105
int callback( SVCXPRT* xtrp )
{
u_long timv;
if (svcp->getargs( xtrp, (xdrproc_t)xdr_u_long, (caddr_t)&timv)!=RPC_SUCCESS)
{
cerr << "client: get alarm time fails\n";
return -1;
}
cerr << "client: alarm time left is: " << timv << endl;
if (svcp->reply(xtrp, (xdrproc_t)xdr_void, 0)!=RPC_SUCCESS)
{
cerr << "client: send reply failed\n";
return -2;
}
/* do work */
exit(0);
}
/* register a call back with a RPC server */
int register_callback( char* local_host, char* svc_host, u_long alarm_time)
{
struct arg_rec argRec;
/* tell remote server the process's hotsname, prog no, vers. no, proc. no,
and the alarm time
*/
argRec.hostname = local_host;
argRec.prognum = svcp->progno();
argRec.versnum = CLNTVERNUM;
argRec.procnum = CLNTPROCNUM;
argRec.atime = alarm_time;
RPC_cls clnt( svc_host, ACLKPROG, ACLKVER, "netpath");
if (!clnt.good()) return 1;
if (clnt.call( ACLKPROC, (xdrproc_t)xdr_arg_rec, (caddr_t)&argRec,
(xdrproc_t)xdr_void, (caddr_t)0 ) !=RPC_SUCCESS) {
return 2;
}
cerr << "client: " << getpid() <<": RPC called done\n";
return 0;
}
/* client main function */
int main (int argc, char* argv[])
{
if (argc!=4) {
cerr << "usage: " << argv[0] << " <local-host> <svc-host> <transport>\n";
return 1;
}
/* create a server object to receieve call back from a remote server */
if (!(svcp= new RPC_svc( CLNTPROGNUM, CLNTVERNUM, argv[3] )))
return 2;
svcp->add_proc( CLNTPROCNUM, callback ); // define the callback function
/* register the callback with a remote server */
if (register_callback( argv[1], argv[2], 10)) return 3;
/* do work here .... */
svcp->run(); /* wait for alarm to expire */
return 0;
}
Соседние файлы в папке call_back