Skip to content

Commit 780b659

Browse files
committed
Zeromq hello world plugin.
1 parent 57b5ed8 commit 780b659

11 files changed

Lines changed: 827 additions & 1 deletion

File tree

package/linux/dfhack

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fi
3535

3636
# Now run
3737

38-
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"./stonesense/deplibs"
38+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"./stonesense/deplibs":"./hack/deplibs"
3939

4040
case "$1" in
4141
-g | --gdb)

plugins/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ if(BUILD_DEV_PLUGINS)
2525
add_subdirectory (devel)
2626
endif()
2727

28+
OPTION(BUILD_SERVER "Build server." OFF)
29+
if(BUILD_SERVER)
30+
add_subdirectory (server)
31+
endif()
32+
2833
OPTION(BUILD_DF2MC "Build DF2MC (needs a checkout first)." ON)
2934
if(BUILD_DF2MC)
3035
add_subdirectory (df2mc)

plugins/server/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
PROJECT (server)
2+
SET(PROJECT_SRCS
3+
main.cpp
4+
)
5+
6+
include_directories( ${CMAKE_SOURCE_DIR} )
7+
IF(UNIX)
8+
OPTION(SERVER_INTERNAL_SO "Link with prebuilt internal zeromq lib and headers." ON)
9+
IF(SERVER_INTERNAL_SO)
10+
SET(PROJECT_LIBS
11+
${server_SOURCE_DIR}/zeromq/libzmq.so.1
12+
${PROJECT_LIBS}
13+
)
14+
include_directories (
15+
${include_directories}
16+
${server_SOURCE_DIR}/zeromq
17+
)
18+
install(PROGRAMS ${server_SOURCE_DIR}/zeromq/libzmq.so.1 DESTINATION "hack/deplibs")
19+
ELSE()
20+
SET(PROJECT_LIBS
21+
zmq
22+
${PROJECT_LIBS}
23+
)
24+
ENDIF()
25+
ELSE()
26+
ENDIF()
27+
28+
DFHACK_PLUGIN(server ${PROJECT_SRCS} LINK_LIBRARIES ${PROJECT_LIBS})
29+
30+
add_executable ( helloclient hello.cpp )
31+
target_link_libraries ( helloclient ${server_SOURCE_DIR}/zeromq/libzmq.so.1)
32+
install(TARGETS helloclient RUNTIME DESTINATION . )

plugins/server/hello.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Hello World client
3+
// Connects REQ socket to tcp://localhost:5555
4+
// Sends "Hello" to server, expects "World" back
5+
//
6+
#include <zmq.h>
7+
#include <string.h>
8+
#include <stdio.h>
9+
#include <unistd.h>
10+
11+
int main (void)
12+
{
13+
void *context = zmq_init (1);
14+
15+
// Socket to talk to server
16+
printf ("Connecting to hello world server…\n");
17+
void *requester = zmq_socket (context, ZMQ_REQ);
18+
zmq_connect (requester, "tcp://localhost:5555");
19+
20+
int request_nbr;
21+
for (request_nbr = 0; request_nbr != 10; request_nbr++)
22+
{
23+
zmq_msg_t request;
24+
zmq_msg_init_size (&request, 5);
25+
memcpy (zmq_msg_data (&request), "Hello", 5);
26+
printf ("Sending Hello %d…\n", request_nbr);
27+
zmq_send (requester, &request, 0);
28+
zmq_msg_close (&request);
29+
30+
zmq_msg_t reply;
31+
zmq_msg_init (&reply);
32+
zmq_recv (requester, &reply, 0);
33+
printf ("Received World %d\n", request_nbr);
34+
zmq_msg_close (&reply);
35+
}
36+
zmq_close (requester);
37+
zmq_term (context);
38+
return 0;
39+
}

plugins/server/main.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include <dfhack/Core.h>
2+
#include <dfhack/Console.h>
3+
#include <dfhack/Export.h>
4+
#include <dfhack/PluginManager.h>
5+
#include <zmq.hpp>
6+
using namespace DFHack;
7+
8+
// Here go all the command declarations...
9+
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
10+
DFhackCExport command_result server (Core * c, std::vector <std::string> & parameters);
11+
12+
// A plugins must be able to return its name. This must correspond to the filename - skeleton.plug.so or skeleton.plug.dll
13+
DFhackCExport const char * plugin_name ( void )
14+
{
15+
return "server";
16+
}
17+
18+
// Mandatory init function. If you have some global state, create it here.
19+
DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
20+
{
21+
// Fill the command list with your commands.
22+
commands.clear();
23+
commands.push_back(PluginCommand("server",
24+
"Inane zeromq example turned into a plugin.",
25+
server));
26+
return CR_OK;
27+
}
28+
29+
// This is called right before the plugin library is removed from memory.
30+
DFhackCExport command_result plugin_shutdown ( Core * c )
31+
{
32+
// You *MUST* kill all threads you created before this returns.
33+
// If everythin fails, just return CR_FAILURE. Your plugin will be
34+
// in a zombie state, but things won't crash.
35+
return CR_OK;
36+
}
37+
38+
// This is WRONG and STUPID. Never use this as an example!
39+
DFhackCExport command_result server (Core * c, std::vector <std::string> & parameters)
40+
{
41+
// It's nice to provide a 'help' option for your command.
42+
// It's also nice to print the same help if you get invalid options from the user instead of just acting strange
43+
for(int i = 0; i < parameters.size();i++)
44+
{
45+
if(parameters[i] == "help" || parameters[i] == "?")
46+
{
47+
// Core has a handle to the console. The console is thread-safe.
48+
// Only one thing can read from it at a time though...
49+
c->con.print("This command is a simple Hello World example for zeromq!\n");
50+
return CR_OK;
51+
}
52+
}
53+
// Prepare our context and socket
54+
zmq::context_t context (1);
55+
zmq::socket_t socket (context, ZMQ_REP);
56+
socket.bind ("tcp://*:5555");
57+
58+
while (true)
59+
{
60+
zmq::message_t request;
61+
62+
// Wait for next request from client
63+
socket.recv (&request);
64+
c->con.print("Received Hello\n");
65+
66+
// Do some 'work'
67+
sleep (1);
68+
69+
// Send reply back to client
70+
zmq::message_t reply (5);
71+
memcpy ((void *) reply.data (), "World", 5);
72+
socket.send (reply);
73+
}
74+
return CR_OK;
75+
}

plugins/server/zeromq/libzmq.a

245 KB
Binary file not shown.

plugins/server/zeromq/libzmq.la

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# libzmq.la - a libtool library file
2+
# Generated by ltmain.sh (GNU libtool) 2.2.6b Debian-2.2.6b-2ubuntu1
3+
#
4+
# Please DO NOT delete this file!
5+
# It is necessary for linking the library.
6+
7+
# The name that we can dlopen(3).
8+
dlname='libzmq.so.1'
9+
10+
# Names of this library.
11+
library_names='libzmq.so.1.0.0 libzmq.so.1 libzmq.so'
12+
13+
# The name of the static archive.
14+
old_library='libzmq.a'
15+
16+
# Linker flags that can not go in dependency_libs.
17+
inherited_linker_flags=''
18+
19+
# Libraries that this one depends upon.
20+
dependency_libs=' -luuid -lrt -lpthread'
21+
22+
# Names of additional weak libraries provided by this library
23+
weak_library_names=''
24+
25+
# Version information for libzmq.
26+
current=1
27+
age=0
28+
revision=0
29+
30+
# Is this an already installed library?
31+
installed=yes
32+
33+
# Should we warn about portability when linking against -modules?
34+
shouldnotlink=no
35+
36+
# Files to dlopen/dlpreopen
37+
dlopen=''
38+
dlpreopen=''
39+
40+
# Directory that this library needs to be installed in:
41+
libdir='/home/peterix/zeromq/lib'

plugins/server/zeromq/libzmq.so.1

210 KB
Binary file not shown.

0 commit comments

Comments
 (0)