[zeromq-dev] Code-style question

Andrew Hume andrew at research.att.com
Wed May 23 13:32:19 CEST 2012


as you point out, "commands" are different from "data".

in my world, we are all using identical intel hardware/software, so data is sent as C structs.

commands are different. the time spent parsing/generating commands is almost unmeasurably small,
so i hew to Plan 9 style and simply send blank-delimited words.
i use a very efficient field splitter, and then use strcmp, as in

	nf = split(msg, f, " ");
	if(nf < 1) return;
	if(strcmp(f[0], "func1") == 0){
		func1(strtol(f[1], 10, 0), f[2]);
	} else if(strcmp(f[0], "func2") == 0){
		so on ...
	} else {
		// rats; complain
}

its not terribly elegant, but it is straightforward and easy to maintain.
and having regular text makes it easier to manually scan and debug.
i have used something like JSON before, but it seemed like a lot of unnecessary work.

On May 22, 2012, at 11:11 AM, Marco Trapanese wrote:

> Hello,
> 
> currently I'm using zmq on several projects to control remote 
> applications. In fact, I send 'commands' rather 'data'.
> 
> The architecture of the whole software is pretty simple. There are the 
> basic functions that do the hard job (my own API). Let's call 'em 
> func1(), func2() ...
> 
> Then I wrote a raw protocol to call these functions through the zmq 
> connection. After a message is received, I check the first part to 
> decode the command and the followings to get the parameters (if any). 
> Something like this:
> 
> void parseMessage() {
> 	// receive the message, get cmd, argc and argv[]
> 
> 	switch (cmd) {
> 	case CMD1:
> 		func1();
> 		break;
> 
> 	case CMD2:
> 		if (argc == 1) func2(argv[0]);
> 		break;
> 	}
> }
> 
> 
> I wonder how do you implement such an interface. Do you use a method 
> like this or there is something more flexible/elegant?
> 
> Thanks
> Marco
> 
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev


------------------
Andrew Hume  (best -> Telework) +1 623-551-2845
andrew at research.att.com  (Work) +1 973-236-2014
AT&T Labs - Research; member of USENIX and LOPSA




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120523/beb342ec/attachment.htm>


More information about the zeromq-dev mailing list