forked from dawehner/jupyter-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipython_php.php
129 lines (106 loc) · 3.82 KB
/
ipython_php.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
use dawehner\IPythonPhp\Kernel;
use dawehner\IPythonPhp\MessageExecuteRequest;
use dawehner\IPythonPhp\MessageHistoryRequest;
use dawehner\IPythonPhp\MessageKernelInfoRequest;
use dawehner\IPythonPhp\MessageShutdownRequest;
use Rhumsaa\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventDispatcher;
require 'vendor/autoload.php';
// Setup the connection strings we want to use, coming from the kernel argument.
$kernel = new Kernel();
if (isset($argv[1])) {
$config = json_decode(file_get_contents($argv[1]));
$ip = $config->ip;
$transport = $config->transport;
$stdin_port = $config->stdin_port;
$control_port = $config->control_port;
$hb_port = $config->hb_port;
$shell_port = $config->shell_port;
$iopub_port = $config->iopub_port;
$kernel->setSecureKey($config->key);
$kernel->setSignatureScheme($config->signature_scheme);
}
else {
trigger_error("no config file specified.");
exit;
}
$connection = "$transport://$ip:";
$stdin_connection = $connection . $stdin_port;
$control_connection = $connection . $control_port;
$hb_connection = $connection . $hb_port;
$shell_connection = $connection . $shell_port;
$iopub_connection = $connection . $iopub_port;
$session_id = Uuid::uuid4();
$kernel->setEngineId(Uuid::uuid4());
if (!isset(Kernel::getSignatureSchemes()[$kernel->getSignatureScheme()])) {
trigger_error("invalid signature scheme:{$kernel->getSignatureScheme()}\n");
exit;
}
// Open all needed sockets.
$context = new ZMQContext();
$loop = React\EventLoop\Factory::create();
/** @var $context \ZMQContext */
$context = new React\ZMQ\Context($loop);
/** @var $hb_socket \ZMQSocket */
$hb_socket = $context->getSocket(ZMQ::SOCKET_REP);
$hb_socket->bind($hb_connection);
/** @var $iopub_socket \ZMQSocket */
$iopub_socket = $context->getSocket(ZMQ::SOCKET_PUB);
$iopub_socket->bind($iopub_connection);
/** @var $control_socket \ZMQSocket */
$control_socket = $context->getSocket(ZMQ::SOCKET_ROUTER);
$control_socket->bind($control_connection);
/** @var $stdin_socket \ZMQSocket */
$stdin_socket = $context->getSocket(ZMQ::SOCKET_ROUTER);
$stdin_socket->bind($stdin_connection);
/** @var $shell_socket \ZMQSocket */
$shell_socket = $context->getSocket(ZMQ::SOCKET_ROUTER);
$shell_socket->bind($shell_connection);
// Register handlers.
$hb_socket->on(
'error',
function ($e) {
}
);
// The heartbeat socket just sends its recieved data to tell ipython, that it
// still lives.
$hb_socket->on(
'messages',
function ($msg) {
}
);
$event_dispatcher = new EventDispatcher();
$message_execute_request = new MessageExecuteRequest($kernel, $iopub_socket, $shell_socket);
$message_kernel_info_request = new MessageKernelInfoRequest($kernel, $shell_socket);
$message_shutdown_request = new MessageShutdownRequest($kernel, $shell_socket);
$message_history_request = new MessageHistoryRequest($kernel, $shell_socket);
$shell_socket->on(
'messages',
function ($messages) use ($shell_socket, $iopub_socket, $kernel, $message_execute_request, $message_kernel_info_request, $message_shutdown_request, $message_history_request) {
list($zmq_id, $delim, $hmac, $header, $parent_header, $metadata, $content) = $messages;
$header = json_decode($header);
$content = json_decode($content);
if ($header->msg_type == 'kernel_info_request') {
$message_kernel_info_request->execute($header, $content);
}
elseif ($header->msg_type == 'execute_request') {
$message_execute_request->execute($header, $content);
}
elseif ($header->msg_type == 'history_request') {
$message_history_request->execute($header, $content);
}
elseif ($header->msg_type == 'shutdown_request') {
$message_shutdown_request->execute($header, $content);
}
else {
trigger_error('unknown msg_type: ' . $header->msg_type);
}
}
);
$iopub_socket->on(
'messages',
function ($messages) {
}
);
$loop->run();