55 #include "rnr/rnrconfig.h" 62 #include "AstroMsgs.h" 75 #define APP_EC_USAGE 2
91 "Simple example packing and unpacking NetMsgs messages.",
94 "The %P application uses the python NetMsgs package generated C code " 95 "from the AstroMsgs.xml NetMsgs XML specification and the libnetmsgs " 96 "library to pack and unpack the messages." 108 .has_arg = no_argument,
111 .fn_cvt = OptsCvtArgBool,
112 .fn_fmt = OptsFmtBool,
114 .opt_desc =
"Do [not] enable tracing of packing and unpacking " 115 "operations. If enabled, tracing is printed to stderr." 144 Argv0 = basename(argv[0]);
159 typedef int (*
Packer_T)(
void *, byte_t [], size_t, bool_t);
160 typedef int (*
Unpacker_T)(byte_t [], size_t,
void *, bool_t);
162 #define MSG_NS "Astro" 169 #define CAT(a, b) a ## b 177 #define CAT3(a, b, c) a ## b ## c 188 #define PRCMP(mem, fmt) \ 192 k = fprintf(stderr, "%s: ", #mem); \ 195 fprintf(stderr, "\n"); \ 198 fprintf(stderr, "%*stx " fmt "\n", 40-k, "", txmsg.mem); \ 199 fprintf(stderr, "%*srx " fmt "\n\n", 40, "", rxmsg.mem); \ 209 #define IDENTITY(xmlname, txmsg, rxmsg) \ 210 Identity(#xmlname, (Packer_T)CAT3(Astro, Pack, xmlname), &txmsg, \ 211 (Unpacker_T)CAT3(Astro, Unpack, xmlname), &rxmsg) 233 if( (n = fnPacker(pTxMsg, buf,
sizeof(buf),
OptsTrace)) < 0 )
235 LOGERROR(
"%s(ecode=%d): %sPack%s()", nmStrError(n), n,
MSG_NS, sXmlName);
239 else if( (n = fnUnpacker(buf, (
size_t)n, pRxMsg,
OptsTrace)) < 0 )
241 LOGERROR(
"%s(ecode=%d): %sUnpack%s()", nmStrError(n), n,
MSG_NS, sXmlName);
259 AstroRspErr_T txmsg, rxmsg;
263 strcpy(txmsg.m_EMsg,
"this is an error message");
269 fprintf(stderr,
"\n--- AstroRspErr_T Results\n");
270 PRCMP(m_ECode,
"%u");
271 PRCMP(m_EMsg,
"'%s'");
289 AstroCmdZodiac_T txmsg, rxmsg;
291 strcpy(txmsg.m_Family,
"test const");
293 strcpy(txmsg.m_Constellation.m_Name,
"Orion");
295 txmsg.m_Constellation.m_Designation.u.m_buf[
ALPHA] =
ALPHA;
296 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
ALPHA].m_Name,
"Betelgeuse");
297 txmsg.m_Constellation.m_Star.u.m_buf[
ALPHA].m_Type =
'M';
298 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
ALPHA].m_Color,
"Red");
299 txmsg.m_Constellation.m_Star.u.m_buf[
ALPHA].m_TempK = 3500;
300 txmsg.m_Constellation.m_Star.u.m_buf[
ALPHA].m_MassSun = 19.0;
302 txmsg.m_Constellation.m_Designation.u.m_buf[
BETA] =
BETA;
303 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
BETA].m_Name,
"Rigel");
304 txmsg.m_Constellation.m_Star.u.m_buf[
BETA].m_Type =
'B';
305 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
BETA].m_Color,
"Blue");
306 txmsg.m_Constellation.m_Star.u.m_buf[
BETA].m_TempK = 11000;
307 txmsg.m_Constellation.m_Star.u.m_buf[
BETA].m_MassSun = 17.0;
309 txmsg.m_Constellation.m_Designation.u.m_buf[
GAMMA] =
GAMMA;
310 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
GAMMA].m_Name,
"Bellatrix");
311 txmsg.m_Constellation.m_Star.u.m_buf[
GAMMA].m_Type =
'B';
312 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
GAMMA].m_Color,
"Blue");
313 txmsg.m_Constellation.m_Star.u.m_buf[
GAMMA].m_TempK = 21500;
314 txmsg.m_Constellation.m_Star.u.m_buf[
GAMMA].m_MassSun = 8.5;
316 txmsg.m_Constellation.m_Designation.u.m_buf[
DELTA] =
DELTA;
317 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
DELTA].m_Name,
"Mintaka");
318 txmsg.m_Constellation.m_Star.u.m_buf[
DELTA].m_Type =
'B';
319 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
DELTA].m_Color,
"Blue");
320 txmsg.m_Constellation.m_Star.u.m_buf[
DELTA].m_TempK = 30000;
321 txmsg.m_Constellation.m_Star.u.m_buf[
DELTA].m_MassSun = 20.0;
324 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
EPSILON].m_Name,
"Alnilam");
325 txmsg.m_Constellation.m_Star.u.m_buf[
EPSILON].m_Type =
'B';
326 strcpy(txmsg.m_Constellation.m_Star.u.m_buf[
EPSILON].m_Color,
"Blue");
327 txmsg.m_Constellation.m_Star.u.m_buf[
EPSILON].m_TempK = 25000;
328 txmsg.m_Constellation.m_Star.u.m_buf[
EPSILON].m_MassSun = 40.0;
330 txmsg.m_Constellation.m_Designation.m_count =
EPSILON+1;
331 txmsg.m_Constellation.m_Star.m_count =
EPSILON+1;
333 n =
IDENTITY(CmdZodiac, txmsg, rxmsg);
337 fprintf(stderr,
"\n--- AstroCmdZodiac_T Results\n");
338 PRCMP(m_Family,
"%s");
339 PRCMP(m_Constellation.m_Name,
"%s");
340 PRCMP(m_Constellation.m_Designation.m_count,
"%zu");
341 PRCMP(m_Constellation.m_Star.m_count,
"%zu");
342 for(i=0; i<txmsg.m_Constellation.m_Star.m_count; ++i)
344 PRCMP(m_Constellation.m_Designation.u.m_buf[i],
"%hhu");
345 PRCMP(m_Constellation.m_Star.u.m_buf[i].m_Name,
"%s");
346 PRCMP(m_Constellation.m_Star.u.m_buf[i].m_Type,
"'%c'");
347 PRCMP(m_Constellation.m_Star.u.m_buf[i].m_Color,
"%s");
348 PRCMP(m_Constellation.m_Star.u.m_buf[i].m_TempK,
"%u");
349 PRCMP(m_Constellation.m_Star.u.m_buf[i].m_MassSun,
"%f");
365 static char *p1 =
"p-branes";
366 static char *p2 =
"bad captain kirk";
368 AstroCmdUniverse_T txmsg =
375 .m_StellarObj = -31000,
376 .m_StarSystem = 999999,
378 .m_GalaxyGroup = 0xffffffff,
380 .m_VisUnivLY = (float)-53.42,
381 .m_FullUnivLY = 53e12,
384 .m_Math.m_DotDotDot = 101,
386 .m_StarInfo.m_Type =
'M',
387 .m_StarInfo.m_TempK = 10000,
388 .m_StarInfo.m_MassSun = (
float)2.6,
390 AstroCmdUniverse_T rxmsg;
393 txmsg.m_HiddenDim = p1;
394 txmsg.m_ParallelUniv = p2;
395 strcpy(txmsg.m_TheUnamed,
"God");
396 txmsg.m_Physics.m_count = 3;
397 txmsg.m_Physics.u.m_buf[0] = 31415;
398 txmsg.m_Physics.u.m_buf[1] = 214;
399 txmsg.m_Physics.u.m_buf[2] = 317;
400 txmsg.m_Physics.u.m_buf[3] = 555555;
401 strcpy(txmsg.m_StarInfo.m_Name,
"Rigel");
402 strcpy(txmsg.m_StarInfo.m_Color,
"Blue");
404 n =
IDENTITY(CmdUniverse, txmsg, rxmsg);
408 fprintf(stderr,
"\n--- AstroCmdUniverse_T Results\n");
409 PRCMP(m_Truth,
"%hhu");
410 PRCMP(m_Aura,
"'%c'");
411 PRCMP(m_Gluon,
"%hhu");
412 PRCMP(m_Electron,
"%hhd");
413 PRCMP(m_Planet,
"%hu");
414 PRCMP(m_StellarObj,
"%hd");
415 PRCMP(m_StarSystem,
"%u");
416 PRCMP(m_Galaxy,
"%d");
417 PRCMP(m_GalaxyGroup,
"%llu");
418 PRCMP(m_Filament,
"%lld");
419 PRCMP(m_VisUnivLY,
"%f");
420 PRCMP(m_FullUnivLY,
"%e");
421 fprintf(stderr,
"%*s%p->\"%s\"\n", 43,
"", p1, p1);
422 PRCMP(m_HiddenDim,
"%p");
423 fprintf(stderr,
"%*s%p->\"%s\"\n", 43,
"", p2, p2);
424 PRCMP(m_ParallelUniv,
"%p");
425 PRCMP(m_TheUnamed,
"%s");
426 PRCMP(m_Math.m_One,
"%hu");
427 PRCMP(m_Math.m_Two,
"%u");
428 PRCMP(m_Math.m_DotDotDot,
"%llu");
429 PRCMP(m_Physics.m_count,
"%zu");
430 PRCMP(m_Physics.u.m_buf[0],
"%u");
431 PRCMP(m_Physics.u.m_buf[1],
"%u");
432 PRCMP(m_Physics.u.m_buf[2],
"%u");
433 PRCMP(m_StarType,
"'%c'");
434 PRCMP(m_StarInfo.m_Type,
"'%c'");
435 PRCMP(m_StarInfo.m_TempK,
"%u");
436 PRCMP(m_StarInfo.m_MassSun,
"%f");
437 PRCMP(m_StarInfo.m_Name,
"%s");
438 PRCMP(m_StarInfo.m_Color,
"%s");
464 int main(
int argc,
char *argv[])
473 for(i=0; (fnTst=
TstTbl[i])!=NULL; ++i)
475 if( (n = fnTst()) < 0 )
static OptsInfo_T OptsInfo[]
Command-line options information.
int(* Unpacker_T)(byte_t[], size_t, void *, bool_t)
unpacker type
static char * Argv0
this command basename
#define MSG_NS
message namespace
#define APP_EC_OK
success exit code
int Identity(const char *sXmlName, Packer_T fnPacker, void *pTxMsg, Unpacker_T fnUnpacker, void *pRxMsg)
Execute the pack, unpack identiy function.
#define APP_EC_EXEC
execution error exit code
#define NM_OK
not an error, success
static int MainInit(int argc, char *argv[])
Initialize simple_khr2 command.
int tstCmdZodiac()
Test CmdZodiac message packing and unpacking.
int(* Packer_T)(void *, byte_t[], size_t, bool_t)
packer type
int tstCmdUniverse()
Test CmdUniverse message packing and unpacking.
static bool_t OptsTrace
do [not] trace packing/unpacking
#define PRCMP(mem, fmt)
Print side-by-side comparision of prepacked tx and post-unpacked rx member.
static const PkgInfo_T PkgInfo
Package version information.
#define IDENTITY(xmlname, txmsg, rxmsg)
Execute the pack, unpack identiy function.
static OptsPgmInfo_T PgmInfo
Program information.
int main(int argc, char *argv[])
Simple NetMsg example main.
Network Messaging declarations.
Example NetMsgs message packing/unpacking application declarations.
int tstRspErr()
Test AstroRspMsg message packing and unpacking.