Skip to content

Commit 31f59a1

Browse files
author
Ian Barber
committed
Test with intermediate device
1 parent 16048d1 commit 31f59a1

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ CC = g++
22
CFLAGS = -g
33
LDFLAGS = -lzmq -lssl -lcrypto
44

5-
all: tlsserver tlsclient
5+
all: tlsserver tlsclient device
66

77
tlsserver: tlsserver.cpp tlszmq.cpp
88
$(CC) $^ $(CFLAGS) $(LDFLAGS) -o $@
99

1010
tlsclient: tlsclient.cpp tlszmq.cpp
1111
$(CC) $^ $(CFLAGS) $(LDFLAGS) -o $@
1212

13+
device: device.cpp
14+
$(CC) $^ $(CFLAGS) $(LDFLAGS) -o $@
15+
1316
clean:
1417
rm tlsclient
1518
rm tlsserver

device.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <pthread.h>
2+
#include <unistd.h>
3+
#include <cassert>
4+
#include <string>
5+
#include <iostream>
6+
#include <zmq.hpp>
7+
8+
int main ()
9+
{
10+
// Prepare our context and sockets
11+
zmq::context_t context (1);
12+
zmq::socket_t servers (context, ZMQ_DEALER);
13+
servers.bind ("tcp://*:5555");
14+
zmq::socket_t clients (context, ZMQ_ROUTER);
15+
clients.bind ("tcp://*:5556");
16+
17+
zmq_device (ZMQ_FORWARDER, (void *)clients, (void *)servers);
18+
19+
return 0;
20+
}

tlsclient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int main(int argc, char* argv[]) {
88
try {
99
zmq::context_t ctx(1);
1010
zmq::socket_t s1(ctx,ZMQ_REQ);
11-
s1.connect ("tcp://localhost:5555");
11+
s1.connect ("tcp://localhost:5556");
1212
TLSZmq *tls = new TLSZmq();
1313
bool loop = true;
1414
zmq::message_t request (12);

tlsserver.cpp

+15-10
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@ int main(int argc, char* argv[]) {
88
try {
99
zmq::context_t ctx(1);
1010
zmq::socket_t s1(ctx,ZMQ_ROUTER);
11-
s1.bind ("tcp://*:5555");
11+
//s1.connect ("tcp://localhost:5555");
12+
s1.bind ("tcp://*:5556");
1213
std::string s_crt("server.crt");
1314
std::string s_key("server.key");
1415
std::map<std::string, TLSZmq*> conns;
1516

1617
while (true) {
1718
// Wait for a message
1819
zmq::message_t identifier(0);
20+
zmq::message_t request(0);
21+
std::string ident;
22+
size_t size;
23+
24+
// Ignore seperator & intermediate ID stack
25+
do {
26+
s1.recv (&request);
27+
size = request.size();
28+
if (size > 0) {
29+
ident.assign(static_cast<char*>(request.data()), request.size());
30+
}
31+
s1.send(request, ZMQ_SNDMORE);
32+
} while(size > 0);
1933

2034
// Retrieve or create the TLSZmq handler for this client
21-
s1.recv (&identifier);
22-
std::string ident (static_cast<char*>(identifier.data()), identifier.size());
2335
TLSZmq *tls;
2436
if(conns.find(ident) == conns.end()) {
2537
tls = new TLSZmq(s_crt.c_str(), s_key.c_str());
@@ -28,10 +40,6 @@ int main(int argc, char* argv[]) {
2840
tls = conns[ident];
2941
}
3042

31-
// Ignore seperator
32-
zmq::message_t request(0);
33-
s1.recv (&request);
34-
3543
// Push message on to TLS and update
3644
s1.recv (&request);
3745
tls->put_data(&request);
@@ -51,9 +59,6 @@ int main(int argc, char* argv[]) {
5159
// If we need to send to the network, do so
5260
if(tls->needs_write()) {
5361
zmq::message_t *data = tls->get_data();
54-
s1.send(identifier, ZMQ_SNDMORE);
55-
request.rebuild(0); // reuse request as delimiter
56-
s1.send(request, ZMQ_SNDMORE);
5762
s1.send(*data);
5863
}
5964
}

0 commit comments

Comments
 (0)