Data flow sessionbase and Socketbase in ZeroMQ (Java)

Source: Internet
Author: User

The previous article has made it clear how Zeromq (Java) handles IO at the bottom,

The Selectablechannel object and the IO event callback are maintained through the Streamengine object, and then the selector object is maintained through the Poller object. Then use the Ioobject object to specifically manage Selectablechannel objects in the poller above the registration, as well as the event callback, the relationship between them can be described in the following diagram to a simple description:



For the received data, first handled by Streamengine, in fact, it will call the internal decoder to convert the byte data into a Msg object, and then to the upper object,

In fact, the upper object in this case is the session object, each Streamengine object has its own session object, and then the session object receives the following data, and then through the pipe, the data sent to its upper socket object,

Then the next data processing is referred to the user's code to deal with ...

For the level between the objects just mentioned, use the following graph to describe it:


This picture should be more direct, the bottom data Communication section is responsible for receiving data from the channel and sending binary data, and then decoder and encoder to take charge of the conversion between the byte data and msg ....

We will see many kinds of sockets in ZMQ, such as Req,dealer,router, they all inherit from the Socketbase type, each type has its own session, all inherit from the Sessionbase type ...

Well, then let's take a look at the sessionbase type, each Streamengine object has a session object associated with it, they are a one-to-ones relationship, first look at some of its important attribute definitions:

[Java]View Plaincopy
  1. private Boolean connect; If a connection is required, if it is fasle, then it is the connection created by listener
  2. private pipe pipe; The pipe that communicates with the socket
  3. Private final set<pipe> terminating_pipes;
  4. If true, there is also a message that is not executed in the pipe.
  5. Private Boolean incomplete_in;
  6. If true, means stop sending data to the network
  7. Private Boolean pending;
  8. Private Iengine engine; Bound to the bottom of the session above the communication
  9. Private socketbase socket; The socket that the current session belongs to
  10. Private Iothread Io_thread; The IO thread associated with the current session, and the underlying IO will be added to this IO thread
  11. private static int linger_timer_id = 0x20;
  12. True is linger timer is running.
  13. Private Boolean Has_linger_timer; Have a timeout event registered?
  14. Private Boolean identity_sent; If true, indicates that the flag has been sent
  15. Private Boolean identity_received; Indicates that the flag has been received
  16. Private final Address addr; The address of the connection
  17. Private Ioobject Io_object; Associated IO object, Io object in Iengine

Specifically these attributes are what, the above comments are basically already given, here is the more important properties are:

Pipe, which is used to communicate with the above socket, when the underlying data is parsed out, will be through the pipe to send msg to the upper socket, concrete pipe process, the previous article has said ...

Iengine, the reference to the Streamengine object of the underlying data communication, this importance is not said, ...

Iothread object, this is the IO thread that the current session object will depend on, that is, the command sent to the session is received by the mailbox of the IO thread, which executes the command in this thread, um ... It's important ...

There are also some sign-bits or something ...

Well, let's not go into detail here, and take a look at some important ways:

[Java]View Plaincopy
    1. In relation to pipe, this is basically to set the current pipe's event callback to the current object
    2. public void Attach_pipe (pipe pipe_) {
    3. ASSERT (!is_terminating ());
    4. ASSERT (pipe = = null);
    5. ASSERT (Pipe_! = null);
    6. pipe = Pipe_; Save the current pipe
    7. Pipe.set_event_sink (this); Sets the current pipe's event callback to the current
    8. }

For associating pipe objects, you can see that the event of the pipe is returned to the current session object. So let's see how these events are handled by the callback method:

[Java]View Plaincopy
  1. When there is data to the current pipe, in fact, the pipe opposite the socket to send the data here, and then send a pipe readable command, then indicate that there is data to be sent through the underlying engine
  2. public void read_activated (Pipe pipe_) {
  3. Skip activating if we ' re detaching this pipe
  4. if (pipe! = pipe_) {
  5. ASSERT (Terminating_pipes.contains (pipe_));
  6. Return
  7. }
  8. if (engine! = NULL)
  9. Engine.activate_out (); Activates the write event for the channel of the underlying engine
  10. Else
  11. Pipe.check_read ();
  12. }
  13. Indicates that the data can be sent to the pipe, which means it needs to receive the data from the underlying engine.
  14. public void write_activated (Pipe pipe_) {
  15. Skip activating if we ' re detaching this pipe
  16. if (pipe! = pipe_) {
  17. ASSERT (Terminating_pipes.contains (pipe_));
  18. Return
  19. }
  20. if (engine! = NULL)
  21. Engine.activate_in (); Activates the Read event above the channel of the underlying engin, i.e. notifies the underlying engin that the data should be received from the network
  22. }

Here first the pipe can read the event callback method, this processing is very simple, direct activation of the underlying Streamengine Channle on the Poller register write events, then when the underlying channel can write data, Will read the data from the pipe in the current session and send it out.

The second method is when the pipe can be written, this is actually directly register the channel read event, then when the channel will be to receive data, and finally they will be sent through the pipe to the upper socket object ... The entire operation of the graph above should be clear ...

Then the most critical part of the session is almost there ... There are some details, after the need to introduce it ...

All right, let's take a look at socketbase. This type, the front has already said that all the socket types in ZMQ inherit from this type, visible his importance ... Let's take a look at some of its important attribute definitions:

[Java]View Plaincopy
  1. Private final map<string, own> endpoints; Here to save all open endpoint, where key is the connection address, value is actually the session object
  2. Private final map<string, pipe> Inprocs; IPC Communication Methods
  3. Used to check whether the object is a socket.
  4. private int tag; Flag bit that determines whether the current object is a socket type
  5. Private Boolean ctx_terminated; If true, indicates that the associated context has stopped
  6. Private Boolean destroyed; If true, indicates that the current socket object has been destroyed by the command.
  7. Private final Mailbox Mailbox; A mailbox for receiving commands sent from somewhere else.
  8. Private final list<pipe> pipes; All currently associated pipes, which are used for data communication with all sessions of the current socket.
  9. Private Poller Poller; Poller Object
  10. Private Selectablechannel handle; This is mailbox's handler.
  11. Private long LAST_TSC; Last time the command was executed
  12. private int ticks; The message received after the last command was executed
  13. Private Boolean Rcvmore; And then there's a message to receive
  14. Private Socketbase Monitor_socket; Socket for monitoring
  15. private int monitor_events;
  16. protected valuereference<integer> errno;

Here are some of the more important:

Enpoints, which is used to save all sessions with their connected addresses,

Pipes, save all the pipe associated with the underlying session, where the relationship between the socket and sesssion is one-to-many ...

Mailbox,socket also has its own mailbox, not attached to the Iothread object, but there is a pit, socket type object has its own mailbox, not dependent on the IO thread, does not mean that it has its own thread, because it directly depends on the user thread , depends on the user code ... In other words, the execution of the commands inside the mailbox is done in the user thread ... Oh, just started this place also tangled for a long time ...

Let's take a look at the event callback for its pipe:

[Java]View Plaincopy
    1.     //the callback method that needs to be executed when the pipe is readable. Indicates that the underlying streamengin has data sent here   
    2.     public  void read_activated  (Pipe pipe_)   {  
    3. &NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;
    4. &NBSP;&NBSP;
    5. //the callback to execute when the pipe can be written   
    6.     public void write_activated  (Pipe  Pipe_)  {  
    7.          xwrite_activated  (pipe_);   
    8.     }   

In fact, there is not much content, because it is done in the specific subclass, but to here the whole zeromq (Java) How the data flow, even if it is clear ... Of course, there are some details that are not listed.


Well, so far, ZeroMQ (Java) is still the specific socket type of operation and encoding method of two large places without analysis ...

Data flow sessionbase and Socketbase in ZeroMQ (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.