Dreamine.Communication.Wpf 1.0.2
Dreamine.Communication.Wpf 통신 기능과 관련 API를 제공합니다.
로딩중...
검색중...
일치하는것 없음
CommunicationMonitorViewModel.cs
이 파일의 문서화 페이지로 가기
1using System.Collections.ObjectModel;
2using System.ComponentModel;
3using System.Runtime.CompilerServices;
4using System.Text;
5using System.Windows.Input;
6using Dreamine.Communication.Abstractions.Enums;
7using Dreamine.Communication.Abstractions.Models;
10
12
21public sealed class CommunicationMonitorViewModel : INotifyPropertyChanged
22{
32
45
54 public event PropertyChangedEventHandler? PropertyChanged;
55
64 public ObservableCollection<CommunicationChannelViewItem> Channels { get; } = new();
65
74 public ObservableCollection<CommunicationMessageLogItem> Logs { get; } = new();
75
85 {
86 get => _selectedChannel;
87 set => SetProperty(ref _selectedChannel, value);
88 }
89
98 public ICommand ClearLogsCommand { get; }
99
140 public void AddChannel(string name, TransportKind kind, string description = "")
141 {
142 ArgumentException.ThrowIfNullOrWhiteSpace(name);
143
144 if (Channels.Any(x => x.Name == name))
145 {
146 return;
147 }
148
150 {
151 Name = name,
152 Kind = kind,
153 State = ConnectionState.Disconnected,
154 Description = description
155 });
156 }
157
182 public void UpdateChannelState(string name, ConnectionState state)
183 {
184 ArgumentException.ThrowIfNullOrWhiteSpace(name);
185
186 var channel = Channels.FirstOrDefault(x => x.Name == name);
187
188 if (channel is null)
189 {
190 return;
191 }
192
193 channel.State = state;
194 }
195
220 public void UpdateChannelDescription(string name, string description)
221 {
222 ArgumentException.ThrowIfNullOrWhiteSpace(name);
223
224 var channel = Channels.FirstOrDefault(x => x.Name == name);
225
226 if (channel is null)
227 {
228 return;
229 }
230
231 channel.Description = description ?? string.Empty;
232 }
233
266 public void AddSendLog(string channelName, TransportKind kind, MessageEnvelope message)
267 {
268 AddMessageLog(channelName, kind, "SEND", message);
269 }
270
303 public void AddReceiveLog(string channelName, TransportKind kind, MessageEnvelope message)
304 {
305 AddMessageLog(channelName, kind, "RECV", message);
306 }
307
316 public void ClearLogs()
317 {
318 Logs.Clear();
319 }
320
361 private void AddMessageLog(
362 string channelName,
363 TransportKind kind,
364 string direction,
365 MessageEnvelope message)
366 {
367 ArgumentException.ThrowIfNullOrWhiteSpace(channelName);
368 ArgumentNullException.ThrowIfNull(message);
369
371 {
372 ChannelName = channelName,
373 Kind = kind,
374 Direction = direction,
375 MessageName = message.Name,
376 Route = message.Route,
377 PayloadLength = message.Payload.Length,
378 PayloadPreview = CreatePayloadPreview(message.Payload)
379 });
380 }
381
406 private static string CreatePayloadPreview(byte[] payload)
407 {
408 if (payload.Length == 0)
409 {
410 return string.Empty;
411 }
412
413 var text = Encoding.UTF8.GetString(payload);
414
415 if (text.Length <= 120)
416 {
417 return text;
418 }
419
420 return text[..120] + "...";
421 }
422
463 private void SetProperty<T>(
464 ref T storage,
465 T value,
466 [CallerMemberName] string? propertyName = null)
467 {
468 if (EqualityComparer<T>.Default.Equals(storage, value))
469 {
470 return;
471 }
472
473 storage = value;
474 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
475 }
476}
void AddChannel(string name, TransportKind kind, string description="")
void AddSendLog(string channelName, TransportKind kind, MessageEnvelope message)
void AddReceiveLog(string channelName, TransportKind kind, MessageEnvelope message)
void SetProperty< T >(ref T storage, T value, [CallerMemberName] string? propertyName=null)
void AddMessageLog(string channelName, TransportKind kind, string direction, MessageEnvelope message)