Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 1d816d5

Browse files
authored
Merge pull request #20 from thomasjm/master
Fix handling of multiple ZMQ identifiers
2 parents b0f4283 + 9ad9402 commit 1d816d5

File tree

7 files changed

+26
-22
lines changed

7 files changed

+26
-22
lines changed

src/Actions/Action.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414

1515
interface Action
1616
{
17-
public function call(array $header, array $content, $zmqId = null);
17+
public function call(array $header, array $content, $zmqIds = []);
1818
}

src/Actions/ExecuteAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(
5757
$this->shellSoul = $shellSoul;
5858
}
5959

60-
public function call(array $header, array $content, $zmqId = null)
60+
public function call(array $header, array $content, $zmqIds = [])
6161
{
6262
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'busy'], $header);
6363

@@ -85,7 +85,7 @@ public function call(array $header, array $content, $zmqId = null)
8585
'user_expressions' => new \stdClass
8686
];
8787

88-
$this->broker->send($this->shellSocket, 'execute_reply', $replyContent, $this->header, [], $zmqId);
88+
$this->broker->send($this->shellSocket, 'execute_reply', $replyContent, $this->header, [], $zmqIds);
8989

9090
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $this->header);
9191
}

src/Actions/HistoryAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public function __construct(JupyterBroker $broker, SocketWrapper $shellSocket)
3131
$this->shellSocket = $shellSocket;
3232
}
3333

34-
public function call(array $header, array $content, $zmqId = null)
34+
public function call(array $header, array $content, $zmqIds = [])
3535
{
36-
$this->broker->send($this->shellSocket, 'history_reply', ['history' => []], $header, [], $zmqId);
36+
$this->broker->send($this->shellSocket, 'history_reply', ['history' => []], $header, [], $zmqIds);
3737
}
3838
}

src/Actions/KernelInfoAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(JupyterBroker $broker, SocketWrapper $shellSocket, S
3333
$this->iopubSocket = $iopubSocket;
3434
}
3535

36-
public function call(array $header, array $content, $zmqId = null)
36+
public function call(array $header, array $content, $zmqIds = [])
3737
{
3838
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'busy'], $header);
3939

@@ -56,7 +56,7 @@ public function call(array $header, array $content, $zmqId = null)
5656
],
5757
$header,
5858
[],
59-
$zmqId
59+
$zmqIds
6060
);
6161

6262
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $header);

src/Actions/ShutdownAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public function __construct(JupyterBroker $broker, SocketWrapper $iopubSocket, S
3232
$this->shellSocket = $shellSocket;
3333
}
3434

35-
public function call(array $header, array $content, $zmqId = null)
35+
public function call(array $header, array $content, $zmqIds = [])
3636
{
3737
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'busy'], $header);
3838

3939
$replyContent = ['restart' => $content['restart']];
40-
$this->broker->send($this->shellSocket, 'shutdown_reply', $replyContent, $header, [], $zmqId);
40+
$this->broker->send($this->shellSocket, 'shutdown_reply', $replyContent, $header, [], $zmqIds);
4141

4242
$this->broker->send($this->iopubSocket, 'status', ['execution_state' => 'idle'], $header);
4343
}

src/Handlers/ShellMessagesHandler.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,23 @@ public function __construct(
6767

6868
public function __invoke(array $msg)
6969
{
70-
list($zmqId, $delim, $hmac, $header, $parentHeader, $metadata, $content) = $msg;
70+
// Read ZMQ IDs until we reach the delimiter
71+
$zmqIds = array();
72+
while (!empty($msg)) {
73+
$item = array_shift($msg);
74+
if ($item === '<IDS|MSG>') break;
75+
else array_push($zmqIds, $item);
76+
}
77+
78+
// Read the remaining items
79+
list($hmac, $header, $parentHeader, $metadata, $content) = $msg;
7180

7281
$header = json_decode($header, true);
7382
$content = json_decode($content, true);
7483

7584
$this->logger->debug('Received message', [
7685
'processId' => getmypid(),
77-
'zmqId' => htmlentities($zmqId, ENT_COMPAT, "UTF-8"),
78-
'delim' => $delim,
86+
'zmqIds' => htmlentities(implode(", ", $zmqIds), ENT_COMPAT, "UTF-8"),
7987
'hmac' => $hmac,
8088
'header' => $header,
8189
'parentHeader' => $parentHeader,
@@ -84,13 +92,13 @@ public function __invoke(array $msg)
8492
]);
8593

8694
if ('kernel_info_request' === $header['msg_type']) {
87-
$this->kernelInfoAction->call($header, $content, $zmqId);
95+
$this->kernelInfoAction->call($header, $content, $zmqIds);
8896
} elseif ('execute_request' === $header['msg_type']) {
89-
$this->executeAction->call($header, $content, $zmqId);
97+
$this->executeAction->call($header, $content, $zmqIds);
9098
} elseif ('history_request' === $header['msg_type']) {
91-
$this->historyAction->call($header, $content, $zmqId);
99+
$this->historyAction->call($header, $content, $zmqIds);
92100
} elseif ('shutdown_request' === $header['msg_type']) {
93-
$this->shutdownAction->call($header, $content, $zmqId);
101+
$this->shutdownAction->call($header, $content, $zmqIds);
94102
} elseif ('comm_open' === $header['msg_type']) {
95103
// TODO: Research about what should be done.
96104
} else {

src/JupyterBroker.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function send(
5151
array $content = [],
5252
array $parentHeader = [],
5353
array $metadata = [],
54-
$zmqId = null
54+
$zmqIds = []
5555
) {
5656
$header = $this->createHeader($msgType);
5757

@@ -62,11 +62,7 @@ public function send(
6262
json_encode(empty($content) ? new \stdClass : $content),
6363
];
6464

65-
if (null !== $zmqId) {
66-
$finalMsg = [$zmqId];
67-
} else {
68-
$finalMsg = [];
69-
}
65+
$finalMsg = $zmqIds;
7066

7167
$finalMsg = array_merge(
7268
$finalMsg,

0 commit comments

Comments
 (0)