Repository navigation
Expand file tree
/
Copy pathKernel.cs
More file actions
104 lines (95 loc) · 4.09 KB
/
Copy pathKernel.cs
File metadata and controls
104 lines (95 loc) · 4.09 KB
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
using ICSharpCore.Kernels;
using ICSharpCore.Protocols;
using ICSharpCore.RequestHandlers;
using Microsoft.Extensions.Logging;
using NetMQ;
using NetMQ.Sockets;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ICSharpCore
{
/// <summary>
/// A 'kernel' is a program that runs and introspects the user's code.
/// https://jupyter-client.readthedocs.io/en/stable/kernels.html
/// </summary>
public class Kernel
{
private ConnInfo _conn;
private string _shellAddress;
private string _iopubAddress;
private bool _exit = false;
private KernelInfoHandler<KernelInfoRequest> _kernelInfoHandler;
private ExecuteHandler<ExecuteRequest> _executeHandler;
private ILoggerFactory _loggerFactory;
public Kernel(ConnInfo conn, ILoggerFactory loggerFactory)
{
_conn = conn;
// https://netmq.readthedocs.io/en/latest/router-dealer/
_shellAddress = $"@tcp://{conn.IP}:{conn.ShellPort}";
_iopubAddress = $"@tcp://{conn.IP}:{conn.IOPubPort}";
_loggerFactory = loggerFactory;
}
public void Start()
{
// catch CTRL+C as exit command
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
_exit = true;
};
using (var shell = new RouterSocket(_shellAddress))
using (var iopub = new PublisherSocket(_iopubAddress))
using (var poller = new NetMQPoller())
{
var iopubSender = new MessageSender(_conn.Key, iopub);
var shellSender = new MessageSender(_conn.Key, shell);
_kernelInfoHandler = new KernelInfoHandler<KernelInfoRequest>(iopubSender, shellSender);
_executeHandler = new ExecuteHandler<ExecuteRequest>(iopubSender, shellSender, _loggerFactory);
// Handler for messages coming in to the frontend
shell.ReceiveReady += (s, e) =>
{
var raw = e.Socket.ReceiveMultipartMessage();
var header = JsonConvert.DeserializeObject<Header>(raw[3].ConvertToString());
Console.WriteLine($"{header.MessageType}: [{raw.ToString()}]");
switch (header.MessageType)
{
case "kernel_info_request":
{
var message = new Message<KernelInfoRequest>(header, raw);
iopubSender.Send(message,
new Status { ExecutionState = StatusType.Idle },
MessageType.Status);
_kernelInfoHandler.Process(message);
}
break;
case "execute_request":
{
var message = new Message<ExecuteRequest>(header, raw);
iopubSender.Send(message,
new Status { ExecutionState = StatusType.Busy },
MessageType.Status);
_executeHandler.Process(message);
iopubSender.Send(message,
new Status { ExecutionState = StatusType.Idle },
MessageType.Status);
}
break;
}
};
poller.Add(shell);
poller.RunAsync();
// var heartbeat = new HeartBeat(conn);
Console.WriteLine($"Listening Shell {_shellAddress}");
Console.WriteLine($"Listening IOPub {_iopubAddress}");
// hit CRTL+C to stop the while loop
while (!_exit)
Thread.Sleep(100);
}
}
}
}