|
1 |
| -using Common.Logging; |
2 |
| -using Newtonsoft.Json; |
3 |
| -using Newtonsoft.Json.Linq; |
4 |
| -using System; |
5 |
| -using System.Collections.Generic; |
6 |
| -using System.IO; |
7 |
| -using System.Linq; |
8 |
| -using System.Text; |
9 |
| -using System.Threading; |
10 |
| -using System.Threading.Tasks; |
11 |
| - |
12 |
| -namespace Ipfs.Api |
13 |
| -{ |
14 |
| - |
15 |
| - /// <summary> |
16 |
| - /// Allows you to publish messages to a given topic, and also to |
17 |
| - /// subscribe to new messages on a given topic. |
18 |
| - /// </summary> |
19 |
| - /// <remarks> |
20 |
| - /// This API is accessed via the <see cref="IpfsClient.PubSub"/> property. |
21 |
| - /// <para> |
22 |
| - /// This is an experimental feature. It is not intended in its current state |
23 |
| - /// to be used in a production environment. |
24 |
| - /// </para> |
25 |
| - /// <para> |
26 |
| - /// To use, the daemon must be run with '--enable-pubsub-experiment'. |
27 |
| - /// </para> |
28 |
| - /// </remarks> |
29 |
| - /// <seealso href="https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/PUBSUB.md">PUBSUB API</seealso> |
30 |
| - public class PubSubApi |
31 |
| - { |
32 |
| - static ILog log = LogManager.GetLogger<PubSubApi>(); |
33 |
| - |
34 |
| - IpfsClient ipfs; |
35 |
| - |
36 |
| - internal PubSubApi(IpfsClient ipfs) |
37 |
| - { |
38 |
| - this.ipfs = ipfs; |
39 |
| - } |
40 |
| - |
41 |
| - /// <summary> |
42 |
| - /// Get the subscribed topics. |
43 |
| - /// </summary> |
44 |
| - /// <param name="cancel"> |
45 |
| - /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
46 |
| - /// </param> |
47 |
| - /// <returns> |
48 |
| - /// A sequence of <see cref="string"/> for each topic. |
49 |
| - /// </returns> |
50 |
| - public async Task<IEnumerable<string>> SubscribedTopicsAsync(CancellationToken cancel = default(CancellationToken)) |
51 |
| - { |
52 |
| - var json = await ipfs.DoCommandAsync("pubsub/ls", cancel); |
53 |
| - var result = JObject.Parse(json); |
54 |
| - var strings = result["Strings"] as JArray; |
55 |
| - if (strings == null) return new string[0]; |
56 |
| - return strings.Select(s => (string)s); |
57 |
| - } |
58 |
| - |
59 |
| - /// <summary> |
60 |
| - /// Get the peers that are pubsubing with us. |
61 |
| - /// </summary> |
62 |
| - /// <param name="topic"> |
63 |
| - /// When specified, only peers pubsubing on the topic are returned. |
64 |
| - /// </param> |
65 |
| - /// <param name="cancel"> |
66 |
| - /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
67 |
| - /// </param> |
68 |
| - /// <returns> |
69 |
| - /// A sequence of <see cref="string"/> for each peer ID. |
70 |
| - /// </returns> |
71 |
| - public async Task<IEnumerable<string>> PeersAsync(string topic = null, CancellationToken cancel = default(CancellationToken)) |
72 |
| - { |
73 |
| - var json = await ipfs.DoCommandAsync("pubsub/peers", cancel, topic); |
74 |
| - var result = JObject.Parse(json); |
75 |
| - var strings = result["Strings"] as JArray; |
76 |
| - if (strings == null) return new string[0]; |
77 |
| - return strings.Select(s => (string)s); |
78 |
| - } |
79 |
| - |
80 |
| - /// <summary> |
81 |
| - /// Publish a message to a given topic. |
82 |
| - /// </summary> |
83 |
| - /// <param name="topic"> |
84 |
| - /// The topic name. |
85 |
| - /// </param> |
86 |
| - /// <param name="message"> |
87 |
| - /// The message to publish. |
88 |
| - /// </param> |
89 |
| - /// <param name="cancel"> |
90 |
| - /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
91 |
| - /// </param> |
92 |
| - public async Task Publish(string topic, string message, CancellationToken cancel = default(CancellationToken)) |
93 |
| - { |
94 |
| - var _ = await ipfs.PostCommandAsync("pubsub/pub", cancel, topic, "arg=" + message); |
95 |
| - return; |
96 |
| - } |
97 |
| - |
98 |
| - /// <summary> |
99 |
| - /// Subscribe to messages on a given topic. |
100 |
| - /// </summary> |
101 |
| - /// <param name="topic"> |
102 |
| - /// The topic name. |
103 |
| - /// </param> |
104 |
| - /// <param name="handler"> |
105 |
| - /// The action to perform when a <see cref="PublishedMessage"/> is received. |
106 |
| - /// </param> |
107 |
| - /// <param name="cancellationToken"> |
108 |
| - /// Is used to stop the topic listener. When cancelled, the <see cref="OperationCanceledException"/> |
109 |
| - /// is <b>NOT</b> raised. |
110 |
| - /// </param> |
111 |
| - /// <returns> |
112 |
| - /// After the topic listener is register with the IPFS server. |
113 |
| - /// </returns> |
114 |
| - /// <remarks> |
115 |
| - /// The <paramref name="handler"/> is invoked on the topic listener thread. |
116 |
| - /// </remarks> |
117 |
| - public async Task Subscribe(string topic, Action<PublishedMessage> handler, CancellationToken cancellationToken = default(CancellationToken)) |
118 |
| - { |
119 |
| - var messageStream = await ipfs.PostDownloadAsync("pubsub/sub", cancellationToken, topic); |
120 |
| -#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
121 |
| - Task.Run(() => ProcessMessages(topic, handler, messageStream, cancellationToken)); |
122 |
| -#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
123 |
| - return; |
124 |
| - } |
125 |
| - |
126 |
| - void ProcessMessages(string topic, Action<PublishedMessage> handler, Stream stream, CancellationToken ct) |
127 |
| - { |
128 |
| - log.DebugFormat("Start listening for '{0}' messages", topic); |
129 |
| - using (var sr = new StreamReader(stream)) |
130 |
| - { |
131 |
| - while (!sr.EndOfStream && !ct.IsCancellationRequested) |
132 |
| - { |
133 |
| - var json = sr.ReadLine(); |
134 |
| - if (log.IsDebugEnabled) |
135 |
| - log.DebugFormat("PubSub message {0}", json); |
136 |
| - if (json != "{}" && !ct.IsCancellationRequested) |
137 |
| - { |
138 |
| - handler(new PublishedMessage(json)); |
139 |
| - } |
140 |
| - } |
141 |
| - } |
142 |
| - log.DebugFormat("Stop listening for '{0}' messages", topic); |
143 |
| - } |
144 |
| - |
145 |
| - } |
146 |
| - |
147 |
| -} |
| 1 | +using Common.Logging; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace Ipfs.Api |
| 13 | +{ |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Allows you to publish messages to a given topic, and also to |
| 17 | + /// subscribe to new messages on a given topic. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// This API is accessed via the <see cref="IpfsClient.PubSub"/> property. |
| 21 | + /// <para> |
| 22 | + /// This is an experimental feature. It is not intended in its current state |
| 23 | + /// to be used in a production environment. |
| 24 | + /// </para> |
| 25 | + /// <para> |
| 26 | + /// To use, the daemon must be run with '--enable-pubsub-experiment'. |
| 27 | + /// </para> |
| 28 | + /// </remarks> |
| 29 | + /// <seealso href="https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/PUBSUB.md">PUBSUB API</seealso> |
| 30 | + public class PubSubApi |
| 31 | + { |
| 32 | + static ILog log = LogManager.GetLogger<PubSubApi>(); |
| 33 | + |
| 34 | + IpfsClient ipfs; |
| 35 | + |
| 36 | + internal PubSubApi(IpfsClient ipfs) |
| 37 | + { |
| 38 | + this.ipfs = ipfs; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Get the subscribed topics. |
| 43 | + /// </summary> |
| 44 | + /// <param name="cancel"> |
| 45 | + /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
| 46 | + /// </param> |
| 47 | + /// <returns> |
| 48 | + /// A sequence of <see cref="string"/> for each topic. |
| 49 | + /// </returns> |
| 50 | + public async Task<IEnumerable<string>> SubscribedTopicsAsync(CancellationToken cancel = default(CancellationToken)) |
| 51 | + { |
| 52 | + var json = await ipfs.DoCommandAsync("pubsub/ls", cancel); |
| 53 | + var result = JObject.Parse(json); |
| 54 | + var strings = result["Strings"] as JArray; |
| 55 | + if (strings == null) return new string[0]; |
| 56 | + return strings.Select(s => (string)s); |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Get the peers that are pubsubing with us. |
| 61 | + /// </summary> |
| 62 | + /// <param name="topic"> |
| 63 | + /// When specified, only peers pubsubing on the topic are returned. |
| 64 | + /// </param> |
| 65 | + /// <param name="cancel"> |
| 66 | + /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
| 67 | + /// </param> |
| 68 | + /// <returns> |
| 69 | + /// A sequence of <see cref="string"/> for each peer ID. |
| 70 | + /// </returns> |
| 71 | + public async Task<IEnumerable<string>> PeersAsync(string topic = null, CancellationToken cancel = default(CancellationToken)) |
| 72 | + { |
| 73 | + var json = await ipfs.DoCommandAsync("pubsub/peers", cancel, topic); |
| 74 | + var result = JObject.Parse(json); |
| 75 | + var strings = result["Strings"] as JArray; |
| 76 | + if (strings == null) return new string[0]; |
| 77 | + return strings.Select(s => (string)s); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Publish a message to a given topic. |
| 82 | + /// </summary> |
| 83 | + /// <param name="topic"> |
| 84 | + /// The topic name. |
| 85 | + /// </param> |
| 86 | + /// <param name="message"> |
| 87 | + /// The message to publish. |
| 88 | + /// </param> |
| 89 | + /// <param name="cancel"> |
| 90 | + /// Is used to stop the task. When cancelled, the <see cref="TaskCanceledException"/> is raised. |
| 91 | + /// </param> |
| 92 | + public async Task Publish(string topic, string message, CancellationToken cancel = default(CancellationToken)) |
| 93 | + { |
| 94 | + var _ = await ipfs.PostCommandAsync("pubsub/pub", cancel, topic, "arg=" + message); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Subscribe to messages on a given topic. |
| 100 | + /// </summary> |
| 101 | + /// <param name="topic"> |
| 102 | + /// The topic name. |
| 103 | + /// </param> |
| 104 | + /// <param name="handler"> |
| 105 | + /// The action to perform when a <see cref="PublishedMessage"/> is received. |
| 106 | + /// </param> |
| 107 | + /// <param name="cancellationToken"> |
| 108 | + /// Is used to stop the topic listener. When cancelled, the <see cref="OperationCanceledException"/> |
| 109 | + /// is <b>NOT</b> raised. |
| 110 | + /// </param> |
| 111 | + /// <returns> |
| 112 | + /// After the topic listener is register with the IPFS server. |
| 113 | + /// </returns> |
| 114 | + /// <remarks> |
| 115 | + /// The <paramref name="handler"/> is invoked on the topic listener thread. |
| 116 | + /// </remarks> |
| 117 | + public async Task Subscribe(string topic, Action<PublishedMessage> handler, CancellationToken cancellationToken) |
| 118 | + { |
| 119 | + var messageStream = await ipfs.PostDownloadAsync("pubsub/sub", cancellationToken, topic); |
| 120 | +#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
| 121 | + Task.Run(() => ProcessMessages(topic, handler, messageStream, cancellationToken)); |
| 122 | +#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + void ProcessMessages(string topic, Action<PublishedMessage> handler, Stream stream, CancellationToken ct) |
| 127 | + { |
| 128 | + log.DebugFormat("Start listening for '{0}' messages", topic); |
| 129 | + |
| 130 | + using (var sr = new StreamReader(stream)) |
| 131 | + { |
| 132 | + // .Net needs a ReadLine(CancellationToken) |
| 133 | + // As a work-around, we register a function to close the stream |
| 134 | + ct.Register(() => sr.Dispose()); |
| 135 | + try |
| 136 | + { |
| 137 | + while (!sr.EndOfStream && !ct.IsCancellationRequested) |
| 138 | + { |
| 139 | + var json = sr.ReadLine(); |
| 140 | + if (json == null) |
| 141 | + break; |
| 142 | + if (log.IsDebugEnabled) |
| 143 | + log.DebugFormat("PubSub message {0}", json); |
| 144 | + if (json != "{}" && !ct.IsCancellationRequested) |
| 145 | + { |
| 146 | + handler(new PublishedMessage(json)); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + catch (Exception e) |
| 151 | + { |
| 152 | + // Do not report errors when cancelled. |
| 153 | + if (!ct.IsCancellationRequested) |
| 154 | + log.Error(e); |
| 155 | + } |
| 156 | + } |
| 157 | + log.DebugFormat("Stop listening for '{0}' messages", topic); |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments