Repository navigation
Expand file tree
/
Copy pathMessageSender.cs
More file actions
85 lines (72 loc) · 2.88 KB
/
Copy pathMessageSender.cs
File metadata and controls
85 lines (72 loc) · 2.88 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
using ICSharpCore.Protocols;
using NetMQ;
using NetMQ.Sockets;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace ICSharpCore.Kernels
{
public class MessageSender
{
private string _key;
private NetMQSocket _iopub;
public MessageSender(string key, NetMQSocket iopub)
{
_key = key;
_iopub = iopub;
}
public bool Send<T, C>(Message<T> request, C content, string msgType)
{
var ioPubMessage = new Message<C>
{
Identifiers = request.Identifiers,
Delimiter = request.Delimiter,
ParentHeader = request.Header,
Header = new Header()
{
UserName = request.Header.UserName,
Session = request.Header.Session,
Date = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"),
MessageId = Guid.NewGuid().ToString(),
MessageType = msgType,
Version = request.Header.Version
},
Metadata = request.Metadata,
Content = content
};
var encoder = new UTF8Encoding();
List<string> messages = new List<string>();
var signature = Sign(_key, ioPubMessage, messages, _iopub);
// send
foreach (var id in request.Identifiers)
_iopub.TrySendFrame(id, true);
_iopub.SendFrame(ioPubMessage.Delimiter, true);
_iopub.SendFrame(signature, true);
for (int i = 0; i < messages.Count; i++)
_iopub.SendFrame(messages[i], i < messages.Count - 1);
return true;
}
private string Sign<T>(string key, Message<T> ioPubMessage, List<string> messages, NetMQSocket iopub)
{
var encoder = new UTF8Encoding();
var hMAC = new HMACSHA256(encoder.GetBytes(key));
hMAC.Initialize();
// https://jupyter-client.readthedocs.io/en/stable/messaging.html#the-wire-protocol
messages.Add(JsonConvert.SerializeObject(ioPubMessage.Header));
messages.Add(JsonConvert.SerializeObject(ioPubMessage.ParentHeader));
messages.Add(JsonConvert.SerializeObject(ioPubMessage.Metadata));
messages.Add(JsonConvert.SerializeObject(ioPubMessage.Content));
// signature
foreach (string item in messages)
{
var sourceBytes = encoder.GetBytes(item);
hMAC.TransformBlock(sourceBytes, 0, sourceBytes.Length, null, 0);
}
hMAC.TransformFinalBlock(new byte[0], 0, 0);
return BitConverter.ToString(hMAC.Hash).Replace("-", "").ToLower();
}
}
}