zerorpc in multithreaded environment? - django

I'd like to use zerorpc as a internal service communication tool.
It seems zeromq can be perfectly used in multithread environments.
http://augustl.com/blog/2013/zeromq_instead_of_http/
However zerorpc documentation or tutorial is really sparse.
I'm trying to use zerorpc in a multithreaded environment (Django) and am stuck on to figure out how to reuse already connected socket (or pool of sockets) with zerorpc.
Is there an obvious solution?

There seems to be a communication breakdown here. ZeroMQ is great for multi-threaded environments, provided you follow the rules.
Rule # 1: ZMQ sockets are not thread-safe, you can't share them between threads.
That's the only rule. The usefulness of ZMQ in a multi-threaded environment is to have each thread spin up its own sockets which can connect to sockets in other threads and thus allow a more streamlined communication between threads.
So, you don't want to reuse already connected sockets. You want to create new sockets owned by your new threads.

Related

communication between OS services with c++

I am developing 2 Windows services, one of them will send pictures and word files to other and other service will give a string answer. That services are in same computer.
I will develop same program's Linux version also.
Which way is the best for communication between services in Linux and Windows.
By the way I am developing that services with C++.
There're different options for your task:
Network. Establish TCP connection between your services, with service that asks as a client and service that answers as a server. It's possible to implement cross-platform solution using Boost.Asio or any other portable network library.
Shared memory. You can implement inter-process communication using shared memory. Cross-platform library: Boost.Interprocess.
Pipes. I don't know cross-platform library for this.
I would recommend to use TCP communication as more flexible solution.
I would suggest reading up on C++ sockets. You're probably going to want to use TCP sockets, since you want to ensure that the data being transferred does so correctly.
Try checking these links out:
Linux Sockets
Windows Sockets
You should search for IPC.
There are a lot of possibilities for inter process communication. Because you are not very specific about your problem and your requirements but I would suggest to take a look at boost::interprocess.
As long as you are sure that both services run on the same machine this will do it.
If you want to switch to a distributed approach you need something different.
Like XML-RPC, thrift or corba. Just to mention some possibilities.

TCP server with state information using network library

I'm writing a tcp server for an online turn-based game. I've already written a prototype using php sockets, but would like to move to C++. I've been looking at the popular network libraries (ASIO, ACE, POCO, LibEvent), but currently unclear which one would best suit my needs:
1) Connections are persistent (on the order of minutes), and the server must be able to handle 100+ simultaneous connections.
2) Connections must be able to maintain state information (user login info). [my php prototype currently requires each client request to contain the login info]
3) Optionally and preferably multi-threaded, but a single process. Prefer not to have 1 thread per connection, but a fixed number of threads working on all open connections.
I'm leaning towards POCO's TCPServer or Reactor frameworks, but not exactly sure if they meet my requirements. I think the Reactor is single threaded, and the TCPServer enforces 1:1 threading/connection. Am I correct?
In either case case, I'm not exactly sure how to do the most important task of associating login info to a specific connection with connections coming and going at random.
Boost.Asio should meet your requirements. The reactor queue can be serviced by multiple threads. Using asynchronous methods will enable your design of a fixed number of threads servicing all connections.
The tutorials and examples are probably the best place to start if you are unfamiliar with the library.
You might also take a look at MUSCLE, a multi-user networking library and server I wrote with this sort of application in mind. It's BSD-licensed, handles hundreds of users, and includes a server-side database mechanism for storing and sharing any information you want the clients to know about each other. The server is single-threaded by default, but I haven't found that to be a problem in practice (and it's possible to extend the server to be multithreaded if that turns out to be necessary).

Interprocess communication: one server and multiple clients

I have one "server" process running, which will fetch data over the network for other processes running on the same machine as the server process.
How should I transfer data from the local server process and the local clients?
For retrieval of network data by the server process, Boost.Asio as suggested by #radman is a good choice.
Between server and local clients, Boost.Interprocess would be more efficient as this is interprocess data transfer, not requiring network usage.
Each of these Boost libraries provides a ready-to-run wrapper around complex underlying Win32 APIs, so you will likely get a working solution faster by using the libraries than by building your own special-purpose code with equivalent function.
You should check out Boost.Asio it fits your problem and is solid.
Standard TCP sockets work fine for interprocess communications between multiple processes on the same machine or different machines. It's standard, supported on almost all platforms and in almost all programming languages. You should be able to find sample C++ code easily.
To connect to a socket on the same machine, use "localhost" as its name or 127.0.0.1 as its IP.
I believe Windows has named pipes, which would work similarly to the suggestions in the other answers (especially #Irish's TCP sockets suggestion). See CreateNamedPipe() for details.

Client and server

I would like to create a connection between two applications. Should I be using Client-Server or is there another way of efficiently communicating between one another? Is there any premade C++ networking client server libraries which are easy to use/reuse and implement?
Application #1 <---> (Client) <---> (Server) <---> Application #2
Thanks!
Client / server is a generic architecture pattern (much like factory, delegation, inheritance, bridge are design patterns). What you probably want is a library to eliminate the tedium of packing and unpacking your data in a format that can be sent over the wire. I strongly recommend you take a look at the protocol buffers library, which is used extensively at Google and released as open source. It will automatically encode / decode data, and it makes it possible for programs written in different languages to send and receive messages of the same type with all the dirty work done for you automatically. Protobuf only deals with encoding, not actually sending and receiving. For that, you can use primitive sockets (strongly recommend against that) or the Boost.Asio asynchronous I/O library.
I should add that you seem to be confused about the meaning of client and server, since in your diagram you have the application talking to a client which talks to a server which talks to another application. This is wrong. Your application is the client (or the server). Client / server is simply a role that your application takes on during the communication. An application is considered to be a client when it initiates a connection or a request, while an application is considered to be a server when it waits for and processes incoming requests. Client / server are simply terms to describe application behavior.
If you know the applications will be running on the same machine, you can use sockets, message queues, pipes, or shared memory. Which option you choose depends on a lot of factors.
There is a ton of example code for any of these strategies as well as libraries that will abstract away a lot of the details.
If they are running on different machines, you will want to communicate through sockets.
There's a tutorial here, with decent code samples.

Communicating applications without Event hooks

I want to have my applications communicate to each other. I think something like a server-client model would suit me well, but I was also wondering if there was a different way. I want this way to not involve those windows event hooks.
Note:
I am assuming you want to communicate with different processes on the same machine, although many of these concepts can apply across computers as well.
What you are looking for is IPC (Inter Process Communication).
You can do IPC via:
File
Signal
Socket
Message queue
Pipe
Named pipe
Semaphore
Shared memory
Message passing
memory-mapped file
My personal recommendation is a loopback connection via socket commands. It is difficult to give you much help beyond that without knowing more details about what you want to do.
You could loopback over Ethernet or use named pipes.
Look up Inter-process Communication (IPC) for a list of all related topics.
Edit: Given your comments about both being in different processes, then you are best off sending information across a network (ie sockets programming). This would give you the added advantage of being able to run the main process and the debug process on different machines. It's a bit hard to give you much information on it. You'll need to figure out what sort of requests you will send across the network and what sort of data you will send back in response. Effectively you need to design your own simple protocol.