Permalink
Browse files

Added support for return values in methods

  • Loading branch information...
1 parent 6a5ec90 commit e192cb42ce897b199acf9255ba320971d5986675 @hintjens hintjens committed Nov 28, 2012
View
5 include/fmq.h
@@ -36,6 +36,11 @@
#define FMQ_VERSION \
FMQ_MAKE_VERSION(FMQ_VERSION_MAJOR, FMQ_VERSION_MINOR, FMQ_VERSION_PATCH)
+#include <czmq.h>
+#if CZMQ_VERSION < 10302
+# error "FileMQ needs CZMQ/1.3.2 or later"
+#endif
+
// Maximum length of a path + filename
#ifndef PATH_MAX
#define PATH_MAX 1024
View
2 include/fmq_patch.h
@@ -70,7 +70,7 @@ void
fmq_patch_virtual_set (fmq_patch_t *self, char *virtual);
// Calculate hash digest for file (create only)
-fmq_patch_t *
+void
fmq_patch_digest_set (fmq_patch_t *self);
// Return hash digest for patch file
View
10 include/fmq_server.h
@@ -41,14 +41,6 @@ fmq_server_t *
void
fmq_server_destroy (fmq_server_t **self_p);
-// Bind to endpoint
-void
- fmq_server_bind (fmq_server_t *self, const char *endpoint);
-
-// Connect to endpoint
-void
- fmq_server_connect (fmq_server_t *self, const char *endpoint);
-
// Load server configuration data
void
fmq_server_configure (fmq_server_t *self, const char *config_file);
@@ -58,7 +50,7 @@ void
fmq_server_setoption (fmq_server_t *self, const char *path, const char *value);
//
-void
+int
fmq_server_bind (fmq_server_t *self, const char *endpoint);
//
View
26 model/client_c.gsl
@@ -47,7 +47,14 @@ void
.for class.method
// $(method.text?'':)
-void
+. if return ?= "number"
+. method.ctype = "int"
+. elsif return ?= "string"
+. method.ctype = "char *"
+. else
+. method.ctype = "void"
+. endif
+$(ctype)
$(class.name)_$(name:c) ($(class.name)_t *self\
. for argument
. if type = "string"
@@ -171,7 +178,7 @@ $(class.name)_connect ($(class.name)_t *self, const char *endpoint)
.for class.method
// --------------------------------------------------------------------------
-void
+$(ctype)
$(class.name)_$(name:c) ($(class.name)_t *self\
. method.send = "send"
. for argument
@@ -205,6 +212,16 @@ $(class.name)_$(name:c) ($(class.name)_t *self\
zstr_$(send) (self->pipe, "$(format)", $(name));
. endif
. endfor
+. if defined (method.return)
+ char *reply = zstr_recv (self->pipe);
+. if return = "string"
+ return reply;
+. else
+ int rc = atoi (reply);
+ free (reply);
+ return rc;
+. endif
+. endif
}
@@ -466,7 +483,7 @@ client_restart (client_t *self, char *endpoint)
if (self->dealer)
zsocket_destroy (self->ctx, self->dealer);
self->dealer = zsocket_new (self->ctx, ZMQ_DEALER);
- zmq_connect (self->dealer, endpoint);
+ zsocket_connect (self->dealer, endpoint);
}
// Clear out any previous request data
$(codec)_destroy (&self->request);
@@ -501,6 +518,9 @@ control_message (client_t *self)
. endif
. endfor
$(string.trim (method.?''):block )
+. for method.return
+ $(string.trim (return.?''):block )
+. endfor
. for argument where type = "string"
free ($(name));
. endfor
View
7 model/fmq_server.xml
@@ -88,10 +88,10 @@ FileMQ protocol server
#define CHUNK_SIZE 1000000
</declare>
-
<self>
<context>
zlist_t *mounts; // Mount points
+int port; // Server port
</context>
<construct>
@@ -293,9 +293,10 @@ if (fmq_patch_op (client->patch) == patch_create) {
<!-- API methods -->
-<method name = "bind">
+<method name = "bind" return = "number">
<argument name = "endpoint" type = "string" />
-zmq_bind (self->router, endpoint);
+self->port = zsocket_bind (self->router, endpoint);
+<return>zstr_sendf (self->pipe, "%d", self->port);</return>
</method>
<method name = "publish">
View
9 model/fmq_server_selftest.xml
@@ -1,6 +1,7 @@
<selftest>
<init>
-fmq_server_bind (self, "tcp://*:5670");
+int port = fmq_server_bind (self, "tcp://*:5670");
+assert (port == 5670);
</init>
<step send = "OHAI" recv = "SRSLY" />
<step send = "ICANHAZ" recv = "RTFM" />
@@ -10,7 +11,8 @@ fmq_server_bind (self, "tcp://*:5670");
<selftest config = "anonymous.cfg">
<init>
-fmq_server_bind (self, "tcp://*:5670");
+port = fmq_server_bind (self, "tcp://*:5670");
+assert (port == 5670);
</init>
<step send = "OHAI" recv = "OHAI-OK" />
<step send = "NOM" />
@@ -20,7 +22,8 @@ fmq_server_bind (self, "tcp://*:5670");
<selftest config = "server_test.cfg">
<init>
-fmq_server_bind (self, "tcp://*:5670");
+port = fmq_server_bind (self, "tcp://*:5670");
+assert (port == 5670);
</init>
<step send = "OHAI" recv = "ORLY" />
<step send = "YARLY" recv = "OHAI-OK">
View
2 model/library.gsl
@@ -14,7 +14,7 @@
endif
endfor
.endfunction
-
+.#
.# can be moved to a pretty print format
.function java_class_name (name)
neatName = "$(Name)"
View
33 model/server_c.gsl
@@ -33,14 +33,6 @@ $(class.name)_t *
void
$(class.name)_destroy ($(class.name)_t **self_p);
-// Bind to endpoint, return port number
-int
- $(class.name)_bind ($(class.name)_t *self, const char *endpoint);
-
-// Connect to endpoint
-void
- $(class.name)_connect ($(class.name)_t *self, const char *endpoint);
-
// Load server configuration data
void
$(class.name)_configure ($(class.name)_t *self, const char *config_file);
@@ -51,7 +43,14 @@ void
.for class.method
// $(method.text?'':)
-void
+. if return ?= "number"
+. method.ctype = "int"
+. elsif return ?= "string"
+. method.ctype = "char *"
+. else
+. method.ctype = "void"
+. endif
+$(ctype)
$(class.name)_$(name:c) ($(class.name)_t *self\
. for argument
. if type = "string"
@@ -163,7 +162,7 @@ $(class.name)_setoption ($(class.name)_t *self, const char *path, const char *va
.for class.method
// --------------------------------------------------------------------------
-void
+$(ctype)
$(class.name)_$(name:c) ($(class.name)_t *self\
. method.send = "send"
. for argument
@@ -197,6 +196,16 @@ $(class.name)_$(name:c) ($(class.name)_t *self\
zstr_$(send) (self->pipe, "$(format)", $(name));
. endif
. endfor
+. if defined (method.return)
+ char *reply = zstr_recv (self->pipe);
+. if return = "string"
+ return reply;
+. else
+ int rc = atoi (reply);
+ free (reply);
+ return rc;
+. endif
+. endif
}
@@ -434,6 +443,7 @@ server_destroy (server_t **self_p)
assert (self_p);
if (*self_p) {
server_t *self = *self_p;
+ zsocket_destroy (self->ctx, self->router);
fmq_config_destroy (&self->config);
zhash_destroy (&self->clients);
.for class.self
@@ -506,6 +516,9 @@ server_control_message (server_t *self)
. endif
. endfor
$(string.trim (method.?''):block )
+. for method.return
+ $(string.trim (return.?''):block )
+. endfor
. for argument where type = "string"
free ($(name));
. endfor
View
4 src/anonymous.cfg
@@ -1,9 +1,5 @@
# Configure server to allow anonymous access
#
-bind
- echo = I: accepting FILEMQ connections on port 5670
- endpoint = tcp://*:5670
-
security
echo = I: server accepts anonymous access
anonymous = 1
View
2 src/fmq_client.c
@@ -639,7 +639,7 @@ client_restart (client_t *self, char *endpoint)
if (self->dealer)
zsocket_destroy (self->ctx, self->dealer);
self->dealer = zsocket_new (self->ctx, ZMQ_DEALER);
- zmq_connect (self->dealer, endpoint);
+ zsocket_connect (self->dealer, endpoint);
}
// Clear out any previous request data
fmq_msg_destroy (&self->request);
View
2 src/fmq_patch.c
@@ -146,7 +146,7 @@ fmq_patch_virtual (fmq_patch_t *self)
// --------------------------------------------------------------------------
// Calculate hash digest for file (create only)
-fmq_patch_t *
+void
fmq_patch_digest_set (fmq_patch_t *self)
{
if (self->op == patch_create
View
22 src/fmq_server.c
@@ -100,13 +100,17 @@ fmq_server_setoption (fmq_server_t *self, const char *path, const char *value)
// --------------------------------------------------------------------------
-void
+int
fmq_server_bind (fmq_server_t *self, const char *endpoint)
{
assert (self);
assert (endpoint);
zstr_sendm (self->pipe, "BIND");
zstr_sendf (self->pipe, "%s", endpoint);
+ char *reply = zstr_recv (self->pipe);
+ int rc = atoi (reply);
+ free (reply);
+ return rc;
}
@@ -175,6 +179,7 @@ typedef enum {
typedef struct {
// Properties accessible to client actions
zlist_t *mounts; // Mount points
+ int port; // Server port
// Properties you should NOT touch
zctx_t *ctx; // Own CZMQ context
@@ -589,6 +594,7 @@ server_destroy (server_t **self_p)
assert (self_p);
if (*self_p) {
server_t *self = *self_p;
+ zsocket_destroy (self->ctx, self->router);
fmq_config_destroy (&self->config);
zhash_destroy (&self->clients);
// Destroy mount points
@@ -628,7 +634,7 @@ server_apply_config (server_t *self)
}
if (streq (fmq_config_name (section), "bind")) {
char *endpoint = fmq_config_resolve (section, "endpoint", "?");
- zmq_bind (self->router, endpoint);
+ self->port = zsocket_bind (self->router, endpoint);
}
else
if (streq (fmq_config_name (section), "publish")) {
@@ -654,7 +660,8 @@ server_control_message (server_t *self)
char *method = zmsg_popstr (msg);
if (streq (method, "BIND")) {
char *endpoint = zmsg_popstr (msg);
- zmq_bind (self->router, endpoint);
+ self->port = zsocket_bind (self->router, endpoint);
+ zstr_sendf (self->pipe, "%d", self->port);
free (endpoint);
}
else
@@ -1188,7 +1195,8 @@ fmq_server_test (bool verbose)
// Run selftest using '' configuration
self = fmq_server_new ();
assert (self);
- fmq_server_bind (self, "tcp://*:5670");
+ int port = fmq_server_bind (self, "tcp://*:5670");
+ assert (port == 5670);
request = fmq_msg_new (FMQ_MSG_OHAI);
fmq_msg_send (&request, dealer);
reply = fmq_msg_recv (dealer);
@@ -1222,7 +1230,8 @@ fmq_server_test (bool verbose)
self = fmq_server_new ();
assert (self);
fmq_server_configure (self, "anonymous.cfg");
- fmq_server_bind (self, "tcp://*:5670");
+ port = fmq_server_bind (self, "tcp://*:5670");
+ assert (port == 5670);
request = fmq_msg_new (FMQ_MSG_OHAI);
fmq_msg_send (&request, dealer);
reply = fmq_msg_recv (dealer);
@@ -1252,7 +1261,8 @@ fmq_server_test (bool verbose)
self = fmq_server_new ();
assert (self);
fmq_server_configure (self, "server_test.cfg");
- fmq_server_bind (self, "tcp://*:5670");
+ port = fmq_server_bind (self, "tcp://*:5670");
+ assert (port == 5670);
request = fmq_msg_new (FMQ_MSG_OHAI);
fmq_msg_send (&request, dealer);
reply = fmq_msg_recv (dealer);
View
1 src/selftest
@@ -25,6 +25,7 @@ gcc -g -o fmq_selftest fmq_selftest.c \
if [ $? -eq 0 ]; then
if [ "$1" == "-q" ]; then
./fmq_selftest
+ exit
else
$VG ./fmq_selftest
fi

0 comments on commit e192cb4

Please sign in to comment.