[DRBD-cvs] svn commit by phil - r2335 - trunk/testing - Proove of
conect of the tag-based binary interface.
drbd-cvs at lists.linbit.com
drbd-cvs at lists.linbit.com
Sun Aug 6 15:11:44 CEST 2006
Author: phil
Date: 2006-08-06 15:11:43 +0200 (Sun, 06 Aug 2006)
New Revision: 2335
Added:
trunk/testing/tag-magic.h
trunk/testing/tag-test.h
trunk/testing/tag-test1.c
Log:
Proove of conect of the tag-based binary interface.
Added: trunk/testing/tag-magic.h
===================================================================
--- trunk/testing/tag-magic.h 2006-08-04 10:13:54 UTC (rev 2334)
+++ trunk/testing/tag-magic.h 2006-08-06 13:11:43 UTC (rev 2335)
@@ -0,0 +1,96 @@
+#ifndef TAG_MAGIC_H
+#define TAG_MAGIC_H
+
+#define TT_END 0
+
+// create packet_type enums
+enum packet_types {
+#define PACKET(name, fields) P_ ## name,
+#define INTEGER(pn,member)
+#define STRING(pn,member,len)
+#include "tag-test.h"
+};
+
+// declate structs
+#define PACKET(name, fields) struct name { fields };
+#define INTEGER(pn,member) int member;
+#define STRING(pn,member,len) unsigned char member[len];
+#include "tag-test.h"
+
+// declate tag-list-sizes
+#define PACKET(name, fields) const name ## _tag_size = 1 fields ;
+#define INTEGER(pn,member) +6
+#define STRING(pn, member,len) +2+len
+#include "tag-test.h"
+
+// convert to tag list fuctions
+#define PACKET(name, fields) \
+name ## _to_tags ( struct name * arg, unsigned char* tl) \
+{ \
+ int i=0; \
+ \
+ fields \
+ tl[i] = TT_END; \
+}
+#define INTEGER(pn,member) \
+ tl[i++] = pn; \
+ tl[i++] = sizeof(int); \
+ *(int*)(tl+i) = arg->member; \
+ i+=sizeof(int);
+#define STRING(pn,member,len) \
+ tl[i++] = pn; \
+ tl[i++] = len; \
+ strcpy(tl+i,arg->member); i+=len;
+#include "tag-test.h"
+
+// convert from tag list functions
+#define PACKET(name, fields) \
+name ## _from_tags ( unsigned char* tl, struct name * arg) \
+{ \
+ int i=0; \
+ \
+ while( tl[i] != TT_END ) { \
+ switch( tl[i++] ) { \
+ fields \
+ default: i += tl[i++]; /* ignoring unknown */ \
+ } \
+ } \
+}
+#define INTEGER(pn,member) \
+ case pn: i++; \
+ arg->member = *(int*)(tl+i); \
+ i+=sizeof(int); \
+ break;
+#define STRING(pn,member,len) \
+ case pn: i++; \
+ strcpy(tl+i,arg->member); \
+ tl+=len; \
+ break;
+#include "tag-test.h"
+
+
+// dump packet functions
+#define PACKET(name, fields) \
+dump_ ## name ( const char* n, struct name * arg) \
+{ \
+ fields \
+}
+#define INTEGER(pn,member) printf( "%s.%s = %d\n",n,#member,arg->member);
+#define STRING(pn,member,len) printf( "%s.%s = %s\n",n,#member,arg->member);
+#include "tag-test.h"
+
+
+void dump_tag_list( const char* n, unsigned char* tl)
+{
+ int len;
+
+ printf("Tag list %s:\n",n);
+
+ while(*tl != TT_END) {
+ printf("tag: %u\n",(int)*tl++);
+ len = *tl++;
+ printf("len: %u\n",len);
+ tl+=len;
+ }
+}
+#endif
Added: trunk/testing/tag-test.h
===================================================================
--- trunk/testing/tag-test.h 2006-08-04 10:13:54 UTC (rev 2334)
+++ trunk/testing/tag-test.h 2006-08-06 13:11:43 UTC (rev 2335)
@@ -0,0 +1,23 @@
+/*
+ PAKET( name,
+ TYPE ( pn, member )
+ ...
+ )
+
+ You may never reissue one of the pn arguments
+*/
+
+PACKET(syncer_conf,
+ INTEGER(1,rate)
+ INTEGER(2,after)
+ INTEGER(3,al_extents)
+)
+
+PACKET(net_conf,
+ STRING(4,my_addr,128)
+ INTEGER(5,timeout)
+)
+
+#undef PACKET
+#undef STRING
+#undef INTEGER
Added: trunk/testing/tag-test1.c
===================================================================
--- trunk/testing/tag-test1.c 2006-08-04 10:13:54 UTC (rev 2334)
+++ trunk/testing/tag-test1.c 2006-08-06 13:11:43 UTC (rev 2335)
@@ -0,0 +1,31 @@
+#include "tag-magic.h"
+
+int main(int argc, char** argv)
+{
+ struct syncer_conf sc = (struct syncer_conf) { 250, -1, 247 };
+ struct syncer_conf sc2;
+
+ struct net_conf nc = (struct net_conf) { "hallo welt",60 };
+ struct net_conf nc2;
+
+ char sct[syncer_conf_tag_size];
+ char nct[net_conf_tag_size];
+
+ dump_syncer_conf("sc",&sc);
+ dump_net_conf("nc",&nc);
+
+ printf("Converting to tag list\n");
+ syncer_conf_to_tags(&sc,sct);
+ net_conf_to_tags(&nc,nct);
+
+ dump_tag_list("sct",sct);
+ dump_tag_list("nct",nct);
+
+ printf("Converting from tag list\n");
+ syncer_conf_from_tags(sct,&sc2);
+ net_conf_from_tags(nct,&nc2);
+
+ dump_syncer_conf("sc2",&sc2);
+ dump_net_conf("nc2",&nc2);
+
+}
More information about the drbd-cvs
mailing list