| 1 | package Devel::IPerl::Kernel; | ||
| 2 | $Devel::IPerl::Kernel::VERSION = '0.012'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use namespace::autoclean; | ||
| 6 | |||
| 7 | use Moo; | ||
| 8 | use Env qw(@PATH); | ||
| 9 | use if $^O eq 'MSWin32', 'Alien::ZMQ::latest'; | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | if ( $^O eq 'MSWin32' ) { | ||
| 13 | unshift @PATH, Alien::ZMQ::latest->bin_dir; | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | |||
| 18 | use ZMQ::FFI 1.18 qw( ZMQ_PUB ZMQ_REP ZMQ_ROUTER ); | ||
| 19 | use JSON::MaybeXS; | ||
| 20 | use Path::Class; | ||
| 21 | use IO::Async::Loop; | ||
| 22 | use IO::Async::Handle; | ||
| 23 | use IO::Handle; | ||
| 24 | use IO::Async::Routine; | ||
| 25 | use Net::Async::ZMQ 0.002; | ||
| 26 | use Net::Async::ZMQ::Socket; | ||
| 27 | use Devel::IPerl::Kernel::Callback::REPL; | ||
| 28 | use Devel::IPerl::Message::ZMQ; | ||
| 29 | |||
| 30 | has callback => ( | ||
| 31 | is => 'rw', | ||
| 32 | default => sub { | ||
| 33 | Devel::IPerl::Kernel::Callback::REPL->new; | ||
| 34 | }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | has _heartbeat_child => ( is => 'rw' ); | ||
| 38 | |||
| 39 | # the ZeroMQ context (not fork/thread-safe) | ||
| 40 | has zmq => ( is => 'lazy', clearer => 1 ); | ||
| 41 | sub _build_zmq { ZMQ::FFI->new } | ||
| 42 | after clear_zmq => sub { | ||
| 43 | my ($self) = @_; | ||
| 44 | for my $clear_fn ( \&clear_heartbeat, \&clear_shell, \&clear_control, \&clear_stdin, \&clear_iopub ) { | ||
| 45 | $self->$clear_fn(); | ||
| 46 | } | ||
| 47 | }; | ||
| 48 | |||
| 49 | has message_format => ( | ||
| 50 | is => 'ro', | ||
| 51 | default => sub { 'Devel::IPerl::Message::ZMQ'; }, | ||
| 52 | ); | ||
| 53 | |||
| 54 | # Loop {{{ | ||
| 55 | has _loop => ( is => 'lazy' ); | ||
| 56 | sub _build__loop { IO::Async::Loop->new; } | ||
| 57 | #}}} | ||
| 58 | |||
| 59 | # Connection configuration {{{ | ||
| 60 | # Read in connection info from JSON file {{{ | ||
| 61 | # path to JSON file with connection data | ||
| 62 | has connection_file => ( is => 'ro', trigger => 1 ); | ||
| 63 | sub _trigger_connection_file { | ||
| 64 | my ($self) = @_; | ||
| 65 | $self->connection_data; | ||
| 66 | } | ||
| 67 | |||
| 68 | has connection_data => ( is => 'lazy' ); | ||
| 69 | sub _build_connection_data { | ||
| 70 | my ($self) = @_; | ||
| 71 | # read JSON file | ||
| 72 | my $data = decode_json file($self->connection_file)->slurp; | ||
| 73 | $self->_connection_data_config( $data ); | ||
| 74 | $data; | ||
| 75 | } | ||
| 76 | # set configuration attributes using the connection data | ||
| 77 | sub _connection_data_config { | ||
| 78 | my ($self, $data) = @_; | ||
| 79 | my $conf_dispatch = { | ||
| 80 | ip => \&ip, | ||
| 81 | signature_scheme => \&signature_scheme, # TODO check the signature_scheme eq "hmac-sha256" | ||
| 82 | transport => \&transport, | ||
| 83 | key => \&key, | ||
| 84 | }; | ||
| 85 | for my $conf_name ( keys %$conf_dispatch ) { | ||
| 86 | my $conf_fn = $conf_dispatch->{ $conf_name }; | ||
| 87 | $self->$conf_fn( $data->{$conf_name} ) if exists $data->{$conf_name}; | ||
| 88 | } | ||
| 89 | $self->_assign_ports_from_data( $data ); | ||
| 90 | }; | ||
| 91 | #}}} | ||
| 92 | # Misc configuration {{{ | ||
| 93 | has ip => ( is => 'rw' ); | ||
| 94 | has transport => ( is => 'rw' ); | ||
| 95 | has signature_scheme => ( is => 'rw' ); | ||
| 96 | has key => ( is => 'rw', predicate => 1 ); # has_key | ||
| 97 | #}}} | ||
| 98 | # Ports {{{ | ||
| 99 | # Heartbeat {{{ | ||
| 100 | # REP socket | ||
| 101 | has hb_port => ( is => 'rw' ); | ||
| 102 | has heartbeat => ( is => 'lazy', clearer => 1 ); | ||
| 103 | sub _build_heartbeat { | ||
| 104 | my ($self) = @_; | ||
| 105 | $self->_create_and_bind_socket( ZMQ_REP, $self->hb_port); | ||
| 106 | } | ||
| 107 | #}}} | ||
| 108 | # Shell {{{ | ||
| 109 | # ROUTER socket | ||
| 110 | has shell_port => ( is => 'rw' ); | ||
| 111 | has shell => ( is => 'lazy', clearer => 1 ); | ||
| 112 | sub _build_shell { | ||
| 113 | my ($self) = @_; | ||
| 114 | $self->_create_and_bind_socket( ZMQ_ROUTER, $self->shell_port); | ||
| 115 | } | ||
| 116 | #}}} | ||
| 117 | # Control {{{ | ||
| 118 | # ROUTER socket | ||
| 119 | has control_port => ( is => 'rw' ); | ||
| 120 | has control => ( is => 'lazy', clearer => 1 ); | ||
| 121 | sub _build_control { | ||
| 122 | my ($self) = @_; | ||
| 123 | $self->_create_and_bind_socket( ZMQ_ROUTER, $self->control_port); | ||
| 124 | } | ||
| 125 | #}}} | ||
| 126 | # Stdin {{{ | ||
| 127 | # ROUTER socket | ||
| 128 | has stdin_port => ( is => 'rw' ); | ||
| 129 | has stdin => ( is => 'lazy', clearer => 1 ); | ||
| 130 | sub _build_stdin { | ||
| 131 | my ($self) = @_; | ||
| 132 | $self->_create_and_bind_socket( ZMQ_ROUTER, $self->stdin_port); | ||
| 133 | } | ||
| 134 | #}}} | ||
| 135 | # IOPub {{{ | ||
| 136 | # PUB socket | ||
| 137 | has iopub_port => ( is => 'rw' ); | ||
| 138 | has iopub => ( is => 'lazy', clearer => 1 ); | ||
| 139 | sub _build_iopub { | ||
| 140 | my ($self) = @_; | ||
| 141 | $self->_create_and_bind_socket( ZMQ_PUB, $self->iopub_port); | ||
| 142 | } | ||
| 143 | #}}} | ||
| 144 | |||
| 145 | # Helper functions {{{ | ||
| 146 | sub _create_and_bind_socket { | ||
| 147 | my ($self, $type, $port) = @_; | ||
| 148 | die "type of socket not given" unless $type; | ||
| 149 | die "port not given" unless $port; | ||
| 150 | |||
| 151 | my $socket = $self->zmq->socket( $type ); | ||
| 152 | my $transport = $self->transport; | ||
| 153 | my $ip = $self->ip; | ||
| 154 | my $bind_address = "${transport}://${ip}:${port}"; | ||
| 155 | $socket->bind( $bind_address ); | ||
| 156 | $socket; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _assign_ports_from_data { | ||
| 160 | my ($self, $data) = @_; | ||
| 161 | my $port_dispatch = { | ||
| 162 | control_port => \&control_port, | ||
| 163 | shell_port => \&shell_port, | ||
| 164 | iopub_port => \&iopub_port, | ||
| 165 | stdin_port => \&stdin_port, | ||
| 166 | hb_port => \&hb_port, | ||
| 167 | }; | ||
| 168 | for my $port_name (keys %$port_dispatch) { | ||
| 169 | my $port_fn = $port_dispatch->{ $port_name }; | ||
| 170 | $self->$port_fn( $data->{$port_name} ) if exists $data->{$port_name}; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | #}}} | ||
| 174 | #}}} | ||
| 175 | #}}} | ||
| 176 | |||
| 177 | sub run {#{{{ | ||
| 178 | my ($self) = @_; | ||
| 179 | STDOUT->autoflush(1); | ||
| 180 | |||
| 181 | local $ENV{PERL_IPERL_RUNNING} = 1; | ||
| 182 | |||
| 183 | local $ENV{PERL_IPERL_IPYTHON} = 1; # in IPython environment | ||
| 184 | |||
| 185 | $self->_setup_heartbeat; | ||
| 186 | |||
| 187 | my $zmq = Net::Async::ZMQ->new; | ||
| 188 | |||
| 189 | my @socket_funcs = ( \&shell, \&control, \&stdin, \&iopub ); | ||
| 190 | for my $socket_fn (@socket_funcs) { | ||
| 191 | my $socket = $self->$socket_fn(); | ||
| 192 | |||
| 193 | my $async_socket = Net::Async::ZMQ::Socket->new( | ||
| 194 | socket => $socket, | ||
| 195 | on_read_ready => sub { | ||
| 196 | # Keep reading for more messages because the | ||
| 197 | # socket filehandle is edge-triggered. | ||
| 198 | # | ||
| 199 | # See: | ||
| 200 | # - ZMQ_FD <http://api.zeromq.org/3-2:zmq-getsockopt#toc23> | ||
| 201 | # - <https://funcptr.net/2012/09/10/zeromq---edge-triggered-notification/> | ||
| 202 | while (1) { | ||
| 203 | my @blobs; | ||
| 204 | while ( $socket->has_pollin ) { | ||
| 205 | push @blobs, $socket->recv_multipart; | ||
| 206 | } | ||
| 207 | last unless (@blobs); | ||
| 208 | |||
| 209 | $self->route_message(\@blobs, $socket); | ||
| 210 | } | ||
| 211 | }, | ||
| 212 | ); | ||
| 213 | |||
| 214 | $zmq->add_child( $async_socket ); | ||
| 215 | } | ||
| 216 | |||
| 217 | $self->_loop->add( $zmq ); | ||
| 218 | |||
| 219 | $self->_loop->loop_forever; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub stop { | ||
| 223 | my ($self) = @_; | ||
| 224 | $self->_heartbeat_child->kill('INT'); | ||
| 225 | $self->_loop->loop_stop; | ||
| 226 | } | ||
| 227 | |||
| 228 | sub route_message { | ||
| 229 | my ($self, $blobs, $socket) = @_; | ||
| 230 | my @msgs = $self->message_format->messages_from_zmq_blobs( | ||
| 231 | $blobs, | ||
| 232 | (shared_key => $self->key) x !!( $self->has_key ), | ||
| 233 | ); | ||
| 234 | for my $msg (@msgs) { | ||
| 235 | my $fn = "msg_" . $msg->msg_type; | ||
| 236 | if( $self->callback->can( $fn ) ) { | ||
| 237 | $self->callback->$fn( $self, $msg, $socket ); | ||
| 238 | } | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | sub send_message { | ||
| 243 | my ($self, $socket, $message) = @_; | ||
| 244 | my $blobs = $message->zmq_blobs_from_message; | ||
| 245 | |||
| 246 | $socket->send_multipart( $blobs ); | ||
| 247 | } | ||
| 248 | |||
| 249 | sub kernel_exit { | ||
| 250 | my ($self) = @_; | ||
| 251 | $self->_heartbeat_child->kill('INT'); | ||
| 252 | $self->clear_zmq; | ||
| 253 | } | ||
| 254 | |||
| 255 | sub _setup_heartbeat { | ||
| 256 | my ($self) = @_; | ||
| 257 | # heartbeat socket is just an echo server | ||
| 258 | my $child = IO::Async::Routine->new( | ||
| 259 | code => sub { | ||
| 260 | $self->clear_zmq; # need to create new context for this process | ||
| 261 | my $hb = $self->heartbeat; | ||
| 262 | while(1) { | ||
| 263 | sleep 1; | ||
| 264 | until( $hb->has_pollin ) { | ||
| 265 | $hb->send($hb->recv); | ||
| 266 | } | ||
| 267 | } | ||
| 268 | }, | ||
| 269 | on_return => sub { | ||
| 270 | $self->kernel_exit; | ||
| 271 | }, | ||
| 272 | ); | ||
| 273 | $self->_loop->add( $child ); | ||
| 274 | $SIG{INT} = sub { $self->kernel_exit }; | ||
| 275 | $self->_heartbeat_child( $child ); | ||
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | #}}} | ||
| 280 | |||
| 281 | 1; | ||
| 282 | # vim: fdm=marker | ||
| 283 | |||
| 284 | __END__ |