Add a simple nanomsg pubsub example

This commit is contained in:
R. Tyler Croy 2016-01-10 12:25:24 -08:00
parent 815a4a4390
commit e75ce27106
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
5 changed files with 124 additions and 4 deletions

View File

@ -2,23 +2,29 @@
GPRBUILD=`which gprbuild`
GPRCLEAN=`which gprclean`
EXECUTABLE=build/pipeline
all: $(EXECUTABLE)
all: build/pipeline build/pubsub
clean:
$(GPRCLEAN) -Ppipeline
$(GPRCLEAN) -Ppubsub
check: all build/c_pipeline
check: all build/c_pipeline build/c_pubsub
./t/bin/bats ./t/*.bats
generated: build/generated/nanomsg_nn_h.ads
build/pipeline: generated
build/pipeline: pipeline.adb
$(GPRBUILD) -Ppipeline
build/pubsub: pubsub.adb
$(GPRBUILD) -Ppubsub
build/c_pipeline: contrib/pipeline.c
$(CC) -lnanomsg -o build/c_pipeline contrib/pipeline.c
build/c_pubsub: contrib/pubsub_client.c
$(CC) -lnanomsg -o build/c_pubsub contrib/pubsub_client.c
build/generated/nanomsg_nn_h.ads:
(cd build/generated && gcc -fdump-ada-spec /usr/include/nanomsg/nn.h)

28
contrib/pubsub_client.c Normal file
View File

@ -0,0 +1,28 @@
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <nanomsg/nn.h>
#include <nanomsg/pubsub.h>
int client (const char *url, const char *name)
{
int sock = nn_socket (AF_SP, NN_SUB);
assert (sock >= 0);
// TODO learn more about publishing/subscribe keys
assert (nn_setsockopt (sock, NN_SUB, NN_SUB_SUBSCRIBE, "", 0) >= 0);
assert (nn_connect (sock, url) >= 0);
while (1)
{
char *buf = NULL;
int bytes = nn_recv (sock, &buf, NN_MSG, 0);
assert (bytes >= 0);
printf ("CLIENT (%s): RECEIVED %s\n", name, buf);
nn_freemsg (buf);
}
return nn_shutdown (sock, 0);
}
int main (const int argc, const char **argv)
{
return client ("tcp://127.0.0.1:5018", argv[1]);
}

59
pubsub.adb Normal file
View File

@ -0,0 +1,59 @@
--
-- Pubsub demo with nanomsg
--
with Ada.Calendar,
Ada.Calendar.Formatting;
with Ada.Text_IO;
with Interfaces.C,
Interfaces.C.Strings;
with nanomsg_nn_h;
procedure Pubsub is
pragma Linker_Options ("-lnanomsg");
use nanomsg_nn_h;
use Interfaces.C,
Interfaces.C.Strings;
use Ada.Text_IO;
NN_PROTO_PUBSUB : constant := 2;
NN_PUB : constant := (NN_PROTO_PUBSUB * 16 + 0);
NN_SUB : constant := (NN_PROTO_PUBSUB * 16 + 1);
NN_SUB_SUBSCRIBE : constant := 1;
NN_SUB_UNSUBSCRIBE : constant := 2;
subtype C_Socket is int;
Socket : constant C_Socket := nn_socket (AF_SP, NN_PUB);
Status : int;
procedure Publish_Tick (Bound_Socket : in C_Socket) is
Bytes_Sent : int := 0;
Buffer : constant String := Ada.Calendar.Formatting.Image (Ada.Calendar.Clock);
begin
Bytes_Sent := nn_send (Bound_Socket,
To_C (Buffer)'Address,
unsigned_long (Buffer'Last),
0);
Put_Line ("Sent" & int'Image (Bytes_Sent));
end Publish_Tick;
begin
Put_Line ("Running pubsub server");
Put_Line ("Socket " & int'Image (Socket));
Status := nn_bind (Socket,
New_String ("tcp://127.0.0.1:5018"));
Put_Line ("Status: " & int'Image (Socket));
loop
Publish_Tick (Socket);
delay 2.0;
end loop;
Status := nn_shutdown (Socket, 0);
end Pubsub;

12
pubsub.gpr Normal file
View File

@ -0,0 +1,12 @@
with "shared";
project Pubsub is
for Main use ("pubsub.adb");
for Source_Dirs use Shared'Source_Dirs;
for Object_Dir use Shared'Object_Dir;
for Exec_Dir use Shared'Exec_Dir;
package Compiler renames Shared.Compiler;
end Pubsub;

15
t/pubsub.bats Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env bats
@test "Simple pubsub" {
./build/pubsub &
server=$!
sleep 1
./build/c_pubsub client0 &
client0=$!
sleep 2
kill $server $client0
}