forked from w5xd/Digi-Rite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQsoInProgress.cs
248 lines (224 loc) · 8.86 KB
/
QsoInProgress.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DigiRite
{
// QsoInProgress
// Class that represents an attempt to work another station. It has:
// a reference to the sequencer that decides what message should be sent based on our state and messages received
// every message received as part of this QSO.
public class QsoInProgress
{
private class QsoInProgressKey
{
public QsoInProgressKey(string baseCall, short band)
{
this.band = band;
this.baseCall = baseCall;
}
public override int GetHashCode()
{ //
const int bandHashWidth = 32 - 1; // hash with 32 bands
return (band & bandHashWidth) | (baseCall.GetHashCode() & ~bandHashWidth);
}
public override bool Equals(object obj)
{
return Equals(obj as QsoInProgressKey);
}
public bool Equals(QsoInProgressKey obj)
{
return obj != null && obj.band == band && obj.baseCall == baseCall;
}
private short band;
private string baseCall;
}
private RecentMessage originatingMessage;
private int ackMessage;
private IQsoSequencer qsoSequencer=null;
private bool messagedThisCycle = true; // when created, we're not starved
private bool holdingForAnotherQso = false;
private bool active=true;
private short band = 0;
private DateTime timeOfLastReceived;
private bool markedAsLogged = false;
private uint transmitFrequency = 0;
private List<XDpack77.Pack77Message.ReceivedMessage> messages = new List<XDpack77.Pack77Message.ReceivedMessage>();
private const int MAX_CYCLES_WITHOUT_ANSWER = 5;
public delegate void OnChanged();
public OnChanged OnChangedCb { get; set; }
public QsoInProgress(RecentMessage rm, short band)
{
this.band = band;
originatingMessage = rm;
messages.Add(rm.Message);
this.AckMessage = Properties.Settings.Default.DefaultAcknowlegement;
timeOfLastReceived = DateTime.UtcNow;
}
public XDpack77.Pack77Message.ReceivedMessage Message { get { return messages.First(); } }
public String HisCall {
get {
return ((XDpack77.Pack77Message.ToFromCall)(Message.Pack77Message)).FromCall;
}
}
public int AckMessage { // FT8 supports 3 acknowledgement messages
set { ackMessage = value; // RRR 73 RR73
if (ackMessage < 0)
ackMessage = 0;
if (ackMessage >= MainForm.DefaultAcknowledgements.Length)
ackMessage = 0;
}
get { return ackMessage; }
}
public override String ToString()
{
XDpack77.Pack77Message.ToFromCall ft = (XDpack77.Pack77Message.ToFromCall)Message.Pack77Message;
String seq = "";
if (null != qsoSequencer)
{
if (qsoSequencer.IsFinished)
seq = "L";
else
{
if (holdingForAnotherQso)
seq = "H";
else
seq = qsoSequencer.DisplayState;
}
}
if (markedAsLogged)
seq = "L";
else if (AmTimedOut)
seq = "T";
return String.Format("{0,1} {1,6} {2,4} {3:####;;}", seq, ft.FromCall, Message.Hz, TransmitFrequency);
}
public IQsoSequencer Sequencer { get { return qsoSequencer; } set { qsoSequencer = value; } }
public uint SentSerialNumber { get; set; } = 0;
public String SentGrid { get; set; }
public bool IsLogged {
get {
if ((null != qsoSequencer) && qsoSequencer.IsFinished)
return true;
return markedAsLogged;
}
}
public int CyclesSinceMessaged { get; private set; } = 0;
public bool CanAcceptAckNotToMe { get; private set; } = true;
private bool MessagedThisCycle { get {
return messagedThisCycle || holdingForAnotherQso;
} }
public DateTime TimeOfLastReceived { get { return timeOfLastReceived; } }
public bool Active { get { return active; } set {
if (value && !active)
{
CyclesSinceMessaged = 0;
holdingForAnotherQso = false;
messagedThisCycle = true;
}
active = value;
if (null != OnChangedCb) OnChangedCb();
} }
public bool Dupe { get => originatingMessage.Dupe; }
public bool Mult { get => originatingMessage.Mult; }
public bool MarkedAsLogged { get => markedAsLogged; set {
markedAsLogged = value;
if (null != OnChangedCb) OnChangedCb();
}
}
public uint TransmitFrequency { get => transmitFrequency;
set { transmitFrequency = value;
holdingForAnotherQso = false;
if (null != OnChangedCb) OnChangedCb();
}
}
public void OnSentAlternativeMessage()
{
CyclesSinceMessaged = 0;
messagedThisCycle = true;
}
private bool AmTimedOut { get { return !messagedThisCycle &&
CyclesSinceMessaged >= MAX_CYCLES_WITHOUT_ANSWER; } }
public bool OnCycleBegin(bool wasReceiveCycle)
{
bool ret = MessagedThisCycle;
if (ret)
CyclesSinceMessaged = 0;
else if (!holdingForAnotherQso && wasReceiveCycle)
CyclesSinceMessaged += 1;
if (AmTimedOut)
{
CanAcceptAckNotToMe = false;
if (active)
{
active = false;
if (null != OnChangedCb) OnChangedCb();
}
}
messagedThisCycle = false;
return ret;
}
static private String toBaseCall(string call)
{
string toBaseCall = null;
if (XDft.Generator.checkCall(call, ref toBaseCall))
return toBaseCall;
else
return call;
}
public object GetKey()
{ return GetKey(Message, band); }
public static object GetKey(XDpack77.Pack77Message.ReceivedMessage rm, short band)
{ // separate QsoInProgress for each BaseCall on each band
XDpack77.Pack77Message.ToFromCall newcall = rm.Pack77Message as XDpack77.Pack77Message.ToFromCall;
if ((null != newcall) && !String.IsNullOrEmpty(newcall.FromCall))
return new QsoInProgressKey(toBaseCall(newcall.FromCall), band);
return null;
}
// if the message is to our same CALL...
public bool AddMessageOnMatch(XDpack77.Pack77Message.ReceivedMessage rm,
bool directlyToMe, string callsQsled)
{
if (null == qsoSequencer)
return false;
try
{
if (directlyToMe)
CanAcceptAckNotToMe = true;
if (!directlyToMe)
{
if (String.IsNullOrEmpty(callsQsled))
{
// if he sends multiple messages in the same cycle...
// ...then we need to "hold" only if this is the only one
if (!messagedThisCycle && !markedAsLogged)
{
// he sent to someone else
int freqDif = (int)transmitFrequency;
freqDif -= (int)Message.Hz;
if (freqDif < 0)
freqDif = -freqDif;
if (freqDif <= 60)
holdingForAnotherQso = true;
}
}
else
holdingForAnotherQso = false;
return false;
}
holdingForAnotherQso = false;
if (AmTimedOut || IsLogged)
active = true;
messagedThisCycle = true;
messages.Add(rm);
timeOfLastReceived = DateTime.UtcNow;
return true;
}
finally
{
if (null != OnChangedCb) OnChangedCb();
}
}
public List<XDpack77.Pack77Message.ReceivedMessage> MessageList { get { return messages; } }
}
}