| 1 |
#--------------------------------------------------------------------------- |
| 2 |
# |
| 3 |
# Title : EMESARY inter-object communication |
| 4 |
# |
| 5 |
# File Type : Implementation File |
| 6 |
# |
| 7 |
# Description : Provides generic inter-object communication. For an object to receive a message it |
| 8 |
# : must first register with an instance of a Transmitter, and provide a Receive method |
| 9 |
# |
| 10 |
# : To send a message use a Transmitter with an object. That's all there is to it. |
| 11 |
# |
| 12 |
# References : http://chateau-logic.com/content/emesary-nasal-implementation-flightgear |
| 13 |
# : http://www.chateau-logic.com/content/class-based-inter-object-communication |
| 14 |
# : http://chateau-logic.com/content/emesary-efficient-inter-object-communication-using-interfaces-and-inheritance |
| 15 |
# : http://chateau-logic.com/content/c-wpf-application-plumbing-using-emesary |
| 16 |
# |
| 17 |
# Author : Richard Harrison ([email protected]) |
| 18 |
# |
| 19 |
# Creation Date : 29 January 2016 |
| 20 |
# |
| 21 |
# Version : 4.8 |
| 22 |
# |
| 23 |
# Copyright 2016 Richard Harrison Released under GPL V2 |
| 24 |
# |
| 25 |
#--------------------------------------------------------------------------- |
| 26 |
# Classes in this file: |
| 27 |
# Transmitter |
| 28 |
# Notification |
| 29 |
# Recipient |
| 30 |
#---------------------------------------------------------------------------*/ |
| 31 |
|
| 32 |
var __emesaryUniqueId = 14; # 0-15 are reserved, this way the global transmitter will be 15. |
| 33 |
|
| 34 |
# add registry so we can find a transmitter by name in genericEmesaryGlobalTransmitterTransmit |
| 35 |
var _transmitters = std.Hash.new({}, "transmitters"); |
| 36 |
|
| 37 |
var _registerTransmitter = func (key, t) { |
| 38 |
_transmitters.set(key, t); |
| 39 |
} |
| 40 |
var getTransmitter = func (key) { |
| 41 |
return _transmitters.get(key); |
| 42 |
} |
| 43 |
|
| 44 |
# Transmitters send notifications to all recipients that are registered. |
| 45 |
var Transmitter = |
| 46 |
{ |
| 47 |
ReceiptStatus_OK : 0, # Processing completed successfully |
| 48 |
ReceiptStatus_Fail : 1, # Processing resulted in at least one failure |
| 49 |
ReceiptStatus_Abort : 2, # Fatal error, stop processing any further recipients of this message. Implicitly failed. |
| 50 |
ReceiptStatus_Finished : 3, # Definitive completion - do not send message to any further recipients |
| 51 |
ReceiptStatus_NotProcessed : 4,# Return value when method doesn't process a message. |
| 52 |
ReceiptStatus_Pending : 5, # Message sent with indeterminate return status as processing underway |
| 53 |
ReceiptStatus_PendingFinished : 6,# Message definitively handled, status indeterminate. The message will not be sent any further |
| 54 |
|
| 55 |
# create a new transmitter. shouldn't need many of these |
| 56 |
# _ident: string; name of the transmitter, used in debug messages |
| 57 |
new: func(_ident) |
| 58 |
{ |
| 59 |
if (!isscalar(_ident)) { |
| 60 |
logprint(LOG_ALERT, "Transmitter.new: argument must be a scalar!") |
| 61 |
} |
| 62 |
__emesaryUniqueId += 1; |
| 63 |
var new_class = { |
| 64 |
parents : [Transmitter], |
| 65 |
Recipients : [], |
| 66 |
Ident : _ident, |
| 67 |
Timestamp : nil, |
| 68 |
MaxMilliseconds : 1, |
| 69 |
UniqueId: __emesaryUniqueId, |
| 70 |
}; |
| 71 |
_registerTransmitter(_ident, new_class); |
| 72 |
return new_class; |
| 73 |
}, |
| 74 |
|
| 75 |
OverrunDetection: func(max_ms=0){ |
| 76 |
if (isnum(max_ms) and max_ms) { |
| 77 |
if (me.Timestamp == nil) |
| 78 |
me.Timestamp = maketimestamp(); |
| 79 |
me.MaxMilliseconds = max_ms; |
| 80 |
logprint(LOG_INFO, "Set overrun detection ",me.Ident, " to ", me.MaxMilliseconds); |
| 81 |
return 1; |
| 82 |
} else { |
| 83 |
# me.Timestamp = nil; |
| 84 |
me.MaxMilliseconds = 0; |
| 85 |
logprint(LOG_INFO, "Disable overrun detection ",me.Ident); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
}, |
| 89 |
|
| 90 |
# Add a recipient to receive notifications from this transmitter |
| 91 |
Register: func (recipient) |
| 92 |
{ |
| 93 |
# not inheriting from Recipient is maybe strange but will not crash |
| 94 |
if (!isa(recipient, Recipient)) |
| 95 |
{ |
| 96 |
logprint(LOG_INFO, "Transmitter.Register: argument is not a Recipient object"); |
| 97 |
} |
| 98 |
# Warn if recipient doesn't have a Receive function - this is not an error because |
| 99 |
#a receive function could be added after the recipient has been registered - so it is |
| 100 |
# deprecated to do this. |
| 101 |
if (!isfunc(recipient["Receive"])) |
| 102 |
{ |
| 103 |
logprint(DEV_ALERT, "Transmitter.Register: Error, argument has no Receive method!"); |
| 104 |
} |
| 105 |
foreach (var r; me.Recipients) |
| 106 |
{ |
| 107 |
if (r == recipient) { |
| 108 |
logprint(DEV_ALERT, "Transmitter.Register: Recipient already registered: " ~ r.Ident ~ " (" ~ r.UniqueId ~")"); |
| 109 |
return 1; |
| 110 |
} |
| 111 |
} |
| 112 |
append(me.Recipients, recipient); |
| 113 |
return 1; |
| 114 |
}, |
| 115 |
|
| 116 |
DeleteAllRecipients: func |
| 117 |
{ |
| 118 |
me.Recipients = []; |
| 119 |
}, |
| 120 |
|
| 121 |
# Stops a recipient from receiving notifications from this transmitter. |
| 122 |
DeRegister: func(todelete_recipient) |
| 123 |
{ |
| 124 |
var out_idx = 0; |
| 125 |
var element_deleted = 0; |
| 126 |
|
| 127 |
for (var idx = 0; idx < size(me.Recipients); idx += 1) |
| 128 |
{ |
| 129 |
if (me.Recipients[idx] != todelete_recipient) |
| 130 |
{ |
| 131 |
me.Recipients[out_idx] = me.Recipients[idx]; |
| 132 |
out_idx = out_idx + 1; |
| 133 |
} |
| 134 |
else |
| 135 |
element_deleted = 1; |
| 136 |
} |
| 137 |
|
| 138 |
if (element_deleted) |
| 139 |
pop(me.Recipients); |
| 140 |
}, |
| 141 |
|
| 142 |
RecipientCount: func |
| 143 |
{ |
| 144 |
return size(me.Recipients); |
| 145 |
}, |
| 146 |
|
| 147 |
PrintRecipients: func |
| 148 |
{ |
| 149 |
print("Emesary: Recipient list for ",me.Ident,"(",me.UniqueId,")"); |
| 150 |
for (var idx = 0; idx < size(me.Recipients); idx += 1) |
| 151 |
print("Emesary: Recipient[",idx,"] ",me.Recipients[idx].Ident," (",me.Recipients[idx].UniqueId,")"); |
| 152 |
}, |
| 153 |
|
| 154 |
# Notify all registered recipients. Stop when receipt status of abort or finished are received. |
| 155 |
# The receipt status from this method will be |
| 156 |
# - OK > message handled |
| 157 |
# - Fail > message not handled. A status of Abort from a recipient will result in our status |
| 158 |
# being fail as Abort means that the message was not and cannot be handled, and |
| 159 |
# allows for usages such as access controls. |
| 160 |
# message: hash; Notification passed to the Receive() method of registered recipients |
| 161 |
NotifyAll: func(message) |
| 162 |
{ |
| 163 |
if (!isa(message, Notification)) |
| 164 |
{ |
| 165 |
logprint(DEV_ALERT, "Transmitter.NotifyAll: argument must be a Notification!"); |
| 166 |
return Transmitter.ReceiptStatus_NotProcessed; |
| 167 |
} |
| 168 |
me._return_status = Transmitter.ReceiptStatus_NotProcessed; |
| 169 |
me.TimeTaken = 0; |
| 170 |
foreach (var recipient; me.Recipients) |
| 171 |
{ |
| 172 |
if (recipient.RecipientActive) |
| 173 |
{ |
| 174 |
me._rstat = nil; |
| 175 |
if (me.MaxMilliseconds > 0 and me.Timestamp != nil) |
| 176 |
me.Timestamp.stamp(); |
| 177 |
|
| 178 |
message.Timestamp = me.Timestamp; |
| 179 |
call(func {me._rstat = recipient.Receive(message);},nil,nil,nil,var err = []); |
| 180 |
|
| 181 |
if (size(err)){ |
| 182 |
foreach(var line; err) { |
| 183 |
print(line); |
| 184 |
} |
| 185 |
logprint(LOG_ALERT, "Recipient ",recipient.Ident, |
| 186 |
" has been removed from transmitter (", me.Ident, |
| 187 |
") because of the above error"); |
| 188 |
me.DeRegister(recipient); |
| 189 |
#need to break the foreach due to having modified what its iterating over. |
| 190 |
return Transmitter.ReceiptStatus_Abort; |
| 191 |
} |
| 192 |
if (me.Timestamp != nil) { |
| 193 |
recipient.TimeTaken = me.Timestamp.elapsedUSec()/1000.0; |
| 194 |
me.TimeTaken += recipient.TimeTaken; |
| 195 |
} |
| 196 |
|
| 197 |
if(me._rstat == Transmitter.ReceiptStatus_Fail) |
| 198 |
{ |
| 199 |
me._return_status = Transmitter.ReceiptStatus_Fail; |
| 200 |
} |
| 201 |
elsif(me._rstat == Transmitter.ReceiptStatus_Pending) |
| 202 |
{ |
| 203 |
me._return_status = Transmitter.ReceiptStatus_Pending; |
| 204 |
} |
| 205 |
elsif(me._rstat == Transmitter.ReceiptStatus_PendingFinished) |
| 206 |
{ |
| 207 |
return me._rstat; |
| 208 |
} |
| 209 |
# elsif(rstat == Transmitter.ReceiptStatus_NotProcessed) |
| 210 |
# { |
| 211 |
# ; |
| 212 |
# } |
| 213 |
elsif(me._rstat == Transmitter.ReceiptStatus_OK) |
| 214 |
{ |
| 215 |
if (me._return_status == Transmitter.ReceiptStatus_NotProcessed) |
| 216 |
me._return_status = me._rstat; |
| 217 |
} |
| 218 |
elsif(me._rstat == Transmitter.ReceiptStatus_Abort) |
| 219 |
{ |
| 220 |
# this is a final results, e.g. no more recipients will be |
| 221 |
# notified but the result is returned as NotifyAll result. |
| 222 |
return Transmitter.ReceiptStatus_Abort; |
| 223 |
} |
| 224 |
elsif(me._rstat == Transmitter.ReceiptStatus_Finished) |
| 225 |
{ |
| 226 |
# this is a final results, e.g. no more recipients will be |
| 227 |
# notified but the result is returned as NotifyAll result. |
| 228 |
return Transmitter.ReceiptStatus_OK; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
if (me.MaxMilliseconds and me.TimeTaken > me.MaxMilliseconds) { |
| 233 |
logprint(LOG_WARN, sprintf("Overrun: %s ['%s'] %1.2fms max (%d)", |
| 234 |
me.Ident, message.NotificationType, me.TimeTaken, me.MaxMilliseconds)); |
| 235 |
foreach (var recipient; me.Recipients) { |
| 236 |
if (recipient.TimeTaken) { |
| 237 |
logprint(LOG_WARN, sprintf(" -- Recipient %25s %7.2f ms", |
| 238 |
recipient.Ident, recipient.TimeTaken)); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
return me._return_status; |
| 243 |
}, |
| 244 |
|
| 245 |
# Returns true if a return value from NotifyAll is to be considered a failure. |
| 246 |
IsFailed: func(receiptStatus) |
| 247 |
{ |
| 248 |
# Failed is either Fail or Abort. |
| 249 |
# NotProcessed isn't a failure because it hasn't been processed. |
| 250 |
if (receiptStatus == Transmitter.ReceiptStatus_Fail or receiptStatus == Transmitter.ReceiptStatus_Abort) |
| 251 |
return 1; |
| 252 |
return 0; |
| 253 |
} |
| 254 |
}; |
| 255 |
|
| 256 |
var QueuedTransmitter = |
| 257 |
{ |
| 258 |
new: func(_ident){ |
| 259 |
var new_class = { parents:[QueuedTransmitter], base:emesary.Transmitter}; |
| 260 |
new_class = emesary.Transmitter.new(_ident); |
| 261 |
new_class.baseNotifyAll = new_class.NotifyAll; |
| 262 |
new_class.Q = []; |
| 263 |
|
| 264 |
new_class.NotifyAll = func(message){ |
| 265 |
append(me.Q, message); |
| 266 |
return emesary.Transmitter.ReceiptStatus_Pending; |
| 267 |
}; |
| 268 |
|
| 269 |
new_class.Process = func { |
| 270 |
foreach (var m ; me.Q) |
| 271 |
me.baseNotifyAll(m); |
| 272 |
me.Q = []; |
| 273 |
return emesary.Transmitter.ReceiptStatus_PendingFinished; |
| 274 |
}; |
| 275 |
new_class.size = func { |
| 276 |
return size(me.Q); |
| 277 |
} |
| 278 |
return new_class; |
| 279 |
} |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
#--------------------------------------------------------------------------- |
| 284 |
# Notification - base class |
| 285 |
# By convention a Notification has a type and a value. Derived classes can add |
| 286 |
# extra properties or methods. |
| 287 |
# |
| 288 |
# NotificationType: Notification Type |
| 289 |
# Ident: Can be an ident, or for simple messages a value that needs transmitting. |
| 290 |
# IsDistinct: non zero if this message supercedes previous messages of this type. |
| 291 |
# Distinct messages are usually sent often and self contained |
| 292 |
# (i.e. no relative state changes such as toggle value) |
| 293 |
# Messages that indicate an event (such as after a pilot action) |
| 294 |
# will usually be non-distinct. So an example would be gear/up down |
| 295 |
# or ATC acknowledgements that all need to be transmitted |
| 296 |
# The IsDistinct is important for any messages that are bridged over MP as |
| 297 |
# only the most recently sent distinct message will be transmitted over MP. |
| 298 |
# Example: |
| 299 |
# position update, where only current position is relevant -> IsDistinct=1; |
| 300 |
# 0 = queue all messages for MP bridging |
| 301 |
# 1 = queue only latest message (replace any old message of same type+ident) |
| 302 |
# |
| 303 |
var TypeIdUnspecified = 1; |
| 304 |
var NotificationAutoTypeId = 1; |
| 305 |
var Notification = |
| 306 |
{ |
| 307 |
new: func(_type, _ident, _typeid=0) |
| 308 |
{ |
| 309 |
if (!isscalar(_type)) { |
| 310 |
logprint(DEV_ALERT, "Notification.new: _type must be a scalar!"); |
| 311 |
return nil; |
| 312 |
} |
| 313 |
if (!isscalar(_ident) and _ident != nil) { |
| 314 |
logprint(DEV_ALERT, "Notification.new: _ident is not scalar but ", typeof(_ident)); |
| 315 |
return nil; |
| 316 |
} |
| 317 |
|
| 318 |
# typeID of 0 means that the notification does not have an assigned type ID |
| 319 |
# <0 means an automatic ID is required |
| 320 |
# >= 16 is a reserved ID |
| 321 |
# normally the typeID should be unique across all of FlightGear. |
| 322 |
# use of automatic ID's is really only for notifications that will never be bridged, |
| 323 |
# or more accurate when bridged the type isn't going to be known fully. |
| 324 |
|
| 325 |
if (_typeid < 0) { |
| 326 |
if (_ident != nil){ |
| 327 |
logprint(DEV_ALERT, "_typeid can only be omitted when registering class"); |
| 328 |
return nil; |
| 329 |
} |
| 330 |
|
| 331 |
# IDs >= 16 are reserved; see http://wiki.flightgear.org/Emesary_Notifications |
| 332 |
if (NotificationAutoTypeId >= 16) { |
| 333 |
logprint(LOG_ALERT, "Notification: AutoTypeID limit exceeded: "~NotificationAutoTypeId); |
| 334 |
return nil; |
| 335 |
} |
| 336 |
NotificationAutoTypeId += 1; |
| 337 |
_typeid = NotificationAutoTypeId; |
| 338 |
} |
| 339 |
|
| 340 |
var new_class = { |
| 341 |
parents: [Notification], |
| 342 |
NotificationType: _type, |
| 343 |
Ident: _ident, |
| 344 |
IsDistinct: 1, #1: MP bridge only latest notification |
| 345 |
FromIncomingBridge: 0, |
| 346 |
Callsign: nil, |
| 347 |
TypeId: _typeid, # used in MP bridged |
| 348 |
}; |
| 349 |
return new_class; |
| 350 |
}, |
| 351 |
|
| 352 |
setType: func(_type) { |
| 353 |
if (!isscalar(_type)) { |
| 354 |
logprint(DEV_ALERT, "Notification.new: _type must be a scalar!"); |
| 355 |
return nil; |
| 356 |
} |
| 357 |
me.NotificationType = _type; |
| 358 |
return me; |
| 359 |
}, |
| 360 |
|
| 361 |
setIdent: func(_ident) { |
| 362 |
if (!isscalar(_ident)) { |
| 363 |
logprint(DEV_ALERT, "Notification.new: _ident is not scalar but ", typeof(_ident)); |
| 364 |
return nil; |
| 365 |
} |
| 366 |
me.Ident = _ident; |
| 367 |
return me; |
| 368 |
}, |
| 369 |
|
| 370 |
GetBridgeMessageNotificationTypeKey: func { |
| 371 |
return me.NotificationType~"."~me.Ident; |
| 372 |
}, |
| 373 |
}; |
| 374 |
|
| 375 |
#--------------------------------------------------------------------------- |
| 376 |
# Recipient - base class for receiving notifications. |
| 377 |
# |
| 378 |
# You have to implement the Receive method |
| 379 |
# The Receive method must return a sensible ReceiptStatus_* code |
| 380 |
var Recipient = |
| 381 |
{ |
| 382 |
new: func(_ident) |
| 383 |
{ |
| 384 |
if (_ident == nil or _ident == "") |
| 385 |
{ |
| 386 |
_ident = id(new_class); |
| 387 |
logprint(LOG_WARN, "Emesary Error: Ident required when creating a recipient, defaulting to ", _ident); |
| 388 |
} |
| 389 |
__emesaryUniqueId += 1; |
| 390 |
var new_class = { |
| 391 |
parents: [Recipient], |
| 392 |
Ident: _ident, |
| 393 |
RecipientActive: 1, |
| 394 |
UniqueId: __emesaryUniqueId, |
| 395 |
}; |
| 396 |
return new_class; |
| 397 |
}, |
| 398 |
|
| 399 |
Receive: func(notification) |
| 400 |
{ |
| 401 |
logprint(DEV_ALERT, "Emesary Error: Receive function not implemented in recipient ", me.Ident); |
| 402 |
return Transmitter.ReceiptStatus_NotProcessed; |
| 403 |
}, |
| 404 |
|
| 405 |
setReceive: func(f) |
| 406 |
{ |
| 407 |
if (isfunc(f)) { me.Receive = f; } |
| 408 |
else { logprint(DEV_ALERT, "Recipient.addReceive: argument must be a function!"); } |
| 409 |
return me; |
| 410 |
}, |
| 411 |
}; |
| 412 |
|
| 413 |
# |
| 414 |
# Instantiate a Global Transmitter, this is a convenience and a known starting point. |
| 415 |
# Generally most classes will use this transmitters, however other transmitters |
| 416 |
# can be created and merely use the global transmitter to discover each other. |
| 417 |
var GlobalTransmitter = Transmitter.new("GlobalTransmitter"); |
| 418 |
|
| 419 |
# |
| 420 |
# Base method of transferring all numeric based values. |
| 421 |
# Using the same techinque as base64 - except this is base248 because we can use a much wider range of characters. |
| 422 |
# |
| 423 |
var BinaryAsciiTransfer = |
| 424 |
{ |
| 425 |
#excluded chars 32 (<space>), 33 (!), 35 (#), 36($), 126 (~), 127 (<del>) |
| 426 |
alphabet : |
| 427 |
chr(1)~chr(2)~chr(3)~chr(4)~chr(5)~chr(6)~chr(7)~chr(8) |
| 428 |
~chr(9)~chr(10)~chr(11)~chr(12)~chr(13)~chr(14)~chr(15)~chr(16)~chr(17)~chr(18)~chr(19) |
| 429 |
~chr(20)~chr(21)~chr(22)~chr(23)~chr(24)~chr(25)~chr(26)~chr(27)~chr(28)~chr(29) |
| 430 |
~chr(30)~chr(31)~chr(34) |
| 431 |
~"%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}" |
| 432 |
~chr(128)~chr(129) |
| 433 |
~chr(130)~chr(131)~chr(132)~chr(133)~chr(134)~chr(135)~chr(136)~chr(137)~chr(138)~chr(139) |
| 434 |
~chr(140)~chr(141)~chr(142)~chr(143)~chr(144)~chr(145)~chr(146)~chr(147)~chr(148)~chr(149) |
| 435 |
~chr(150)~chr(151)~chr(152)~chr(153)~chr(154)~chr(155)~chr(156)~chr(157)~chr(158)~chr(159) |
| 436 |
~chr(160)~chr(161)~chr(162)~chr(163)~chr(164)~chr(165)~chr(166)~chr(167)~chr(168)~chr(169) |
| 437 |
~chr(170)~chr(171)~chr(172)~chr(173)~chr(174)~chr(175)~chr(176)~chr(177)~chr(178)~chr(179) |
| 438 |
~chr(180)~chr(181)~chr(182)~chr(183)~chr(184)~chr(185)~chr(186)~chr(187)~chr(188)~chr(189) |
| 439 |
~chr(190)~chr(191)~chr(192)~chr(193)~chr(194)~chr(195)~chr(196)~chr(197)~chr(198)~chr(199) |
| 440 |
~chr(200)~chr(201)~chr(202)~chr(203)~chr(204)~chr(205)~chr(206)~chr(207)~chr(208)~chr(209) |
| 441 |
~chr(210)~chr(211)~chr(212)~chr(213)~chr(214)~chr(215)~chr(216)~chr(217)~chr(218)~chr(219) |
| 442 |
~chr(220)~chr(221)~chr(222)~chr(223)~chr(224)~chr(225)~chr(226)~chr(227)~chr(228)~chr(229) |
| 443 |
~chr(230)~chr(231)~chr(232)~chr(233)~chr(234)~chr(235)~chr(236)~chr(237)~chr(238)~chr(239) |
| 444 |
~chr(240)~chr(241)~chr(242)~chr(243)~chr(244)~chr(245)~chr(246)~chr(247)~chr(248)~chr(249) |
| 445 |
~chr(250)~chr(251)~chr(252)~chr(253)~chr(254)~chr(255), |
| 446 |
# base248: powers of 2 (i.e. po2(x) = f(248 ^ x); |
| 447 |
# 0 based list so the first item is really[1]; i.e. 124 which is 248/2 as po2 is the magnitude excluding sign |
| 448 |
po2: [1, 124, 30752, 7626496, 1891371008, 469060009984, 116326882476032, 28849066854055936], |
| 449 |
|
| 450 |
_base: 248, |
| 451 |
spaces: " ", |
| 452 |
empty_encoding: chr(1)~chr(1)~chr(1)~chr(1)~chr(1)~chr(1)~chr(1)~chr(1)~chr(1)~chr(1)~chr(1), |
| 453 |
encodeNumeric : func(_num,length,factor) |
| 454 |
{ |
| 455 |
var num = int(_num / factor); |
| 456 |
|
| 457 |
var irange = int(BinaryAsciiTransfer.po2[length]); |
| 458 |
|
| 459 |
if (num < -irange) num = -irange; |
| 460 |
else if (num > irange) num = irange; |
| 461 |
|
| 462 |
num = int(num + irange); |
| 463 |
|
| 464 |
if (num == 0) |
| 465 |
return substr(BinaryAsciiTransfer.empty_encoding,0,length); |
| 466 |
var arr=""; |
| 467 |
|
| 468 |
while (num > 0 and length > 0) { |
| 469 |
var num0 = num; |
| 470 |
num = int(num / BinaryAsciiTransfer._base); |
| 471 |
rem = num0-(num*BinaryAsciiTransfer._base); |
| 472 |
arr =substr(BinaryAsciiTransfer.alphabet, rem,1) ~ arr; |
| 473 |
length -= 1; |
| 474 |
} |
| 475 |
if (length>0) |
| 476 |
arr = substr(BinaryAsciiTransfer.spaces,0,length)~arr; |
| 477 |
return arr; |
| 478 |
}, |
| 479 |
retval : {value:0, pos:0}, |
| 480 |
decodeNumeric : func(str, length, factor, pos) |
| 481 |
{ |
| 482 |
var irange = int(BinaryAsciiTransfer.po2[length]); |
| 483 |
var power = length-1; |
| 484 |
BinaryAsciiTransfer.retval.value = 0; |
| 485 |
BinaryAsciiTransfer.retval.pos = pos; |
| 486 |
|
| 487 |
while (length > 0 and power > 0) { |
| 488 |
var c = substr(str,BinaryAsciiTransfer.retval.pos,1); |
| 489 |
if (c != " ") break; |
| 490 |
power = power -1; |
| 491 |
length = length-1; |
| 492 |
BinaryAsciiTransfer.retval.pos = BinaryAsciiTransfer.retval.pos + 1; |
| 493 |
} |
| 494 |
while (length >= 0 and power >= 0) { |
| 495 |
var c = substr(str,BinaryAsciiTransfer.retval.pos,1); |
| 496 |
# spaces are used as padding so ignore them. |
| 497 |
if (c != " ") { |
| 498 |
var cc = find(c,BinaryAsciiTransfer.alphabet); |
| 499 |
if (cc < 0) |
| 500 |
{ |
| 501 |
print("Emesary: BinaryAsciiTransfer.decodeNumeric: Bad encoding "); |
| 502 |
return BinaryAsciiTransfer.retval; |
| 503 |
} |
| 504 |
BinaryAsciiTransfer.retval.value += int(cc * math.exp(math.ln(BinaryAsciiTransfer._base) * power)); |
| 505 |
power = power - 1; |
| 506 |
} |
| 507 |
length = length-1; |
| 508 |
BinaryAsciiTransfer.retval.pos = BinaryAsciiTransfer.retval.pos + 1; |
| 509 |
} |
| 510 |
BinaryAsciiTransfer.retval.value -= irange; |
| 511 |
BinaryAsciiTransfer.retval.value = BinaryAsciiTransfer.retval.value * factor; |
| 512 |
return BinaryAsciiTransfer.retval; |
| 513 |
}, |
| 514 |
encodeInt : func(num,length){ |
| 515 |
return me.encodeNumeric(num, length, 1.0); |
| 516 |
}, |
| 517 |
decodeInt : func(str, length, pos){ |
| 518 |
return me.decodeNumeric(str, length, 1.0, pos); |
| 519 |
} |
| 520 |
}; |
| 521 |
|
| 522 |
var TransferString = |
| 523 |
{ |
| 524 |
MaxLength:16, |
| 525 |
# |
| 526 |
# just to pack a valid range and keep the lower and very upper control codes for seperators |
| 527 |
# that way we don't need to do anything special to encode the string. |
| 528 |
getalphanumericchar : func(v) |
| 529 |
{ |
| 530 |
if (find(v,BinaryAsciiTransfer.alphabet) > 0)#"-./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_abcdefghijklmnopqrstuvwxyz") > 0) |
| 531 |
return v; |
| 532 |
return nil; |
| 533 |
}, |
| 534 |
encode : func(v) |
| 535 |
{ |
| 536 |
if (v==nil) |
| 537 |
return "0"; |
| 538 |
var l = size(v); |
| 539 |
if (l > TransferString.MaxLength) |
| 540 |
l = TransferString.MaxLength; |
| 541 |
var rv = ""; |
| 542 |
var actual_len = 0; |
| 543 |
for(var ii = 0; ii < l; ii = ii + 1) |
| 544 |
{ |
| 545 |
ev = TransferString.getalphanumericchar(substr(v,ii,1)); |
| 546 |
if (ev != nil) { |
| 547 |
rv = rv ~ ev; |
| 548 |
actual_len = actual_len + 1; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
rv = BinaryAsciiTransfer.encodeNumeric(l,1,1.0) ~ rv; |
| 553 |
return rv; |
| 554 |
}, |
| 555 |
decode : func(v,pos) |
| 556 |
{ |
| 557 |
var dv = BinaryAsciiTransfer.decodeNumeric(v,1,1.0,pos); |
| 558 |
var length = dv.value; |
| 559 |
if (length == 0){ |
| 560 |
dv.value = ""; |
| 561 |
return dv; |
| 562 |
} |
| 563 |
var rv = substr(v,dv.pos,length); |
| 564 |
dv.pos = dv.pos + length; |
| 565 |
dv.value = rv; |
| 566 |
return dv; |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
# |
| 571 |
# encode an int into a specified number of characters. |
| 572 |
var TransferInt = |
| 573 |
{ |
| 574 |
encode : func(v, length) |
| 575 |
{ |
| 576 |
return BinaryAsciiTransfer.encodeNumeric(v,length, 1.0); |
| 577 |
}, |
| 578 |
decode : func(v, length, pos) |
| 579 |
{ |
| 580 |
return BinaryAsciiTransfer.decodeNumeric(v,length, 1.0, pos); |
| 581 |
} |
| 582 |
}; |
| 583 |
|
| 584 |
var TransferFixedDouble = |
| 585 |
{ |
| 586 |
encode : func(v, length, factor) |
| 587 |
{ |
| 588 |
return BinaryAsciiTransfer.encodeNumeric(v, length, factor); |
| 589 |
}, |
| 590 |
decode : func(v, length, factor, pos) |
| 591 |
{ |
| 592 |
return BinaryAsciiTransfer.decodeNumeric(v, length, factor, pos); |
| 593 |
} |
| 594 |
}; |
| 595 |
|
| 596 |
var TransferNorm = |
| 597 |
{ |
| 598 |
powers: [1,10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 10000000.0, 100000000.0, 1000000000.0, 10000000000.0, 100000000000.0], |
| 599 |
|
| 600 |
encode : func(v, length) |
| 601 |
{ |
| 602 |
return BinaryAsciiTransfer.encodeNumeric(int(v * BinaryAsciiTransfer.po2[length]),length, 1.0); |
| 603 |
}, |
| 604 |
decode : func(v, length, pos) |
| 605 |
{ |
| 606 |
dv = BinaryAsciiTransfer.decodeNumeric(v, length, 1.0, pos); |
| 607 |
dv.value = (dv.value/BinaryAsciiTransfer.po2[length]); |
| 608 |
return dv; |
| 609 |
} |
| 610 |
}; |
| 611 |
|
| 612 |
var TransferByte = |
| 613 |
{ |
| 614 |
encode : func(v) |
| 615 |
{ |
| 616 |
return BinaryAsciiTransfer.encodeNumeric(v,1, 1.0); |
| 617 |
}, |
| 618 |
decode : func(v, pos) |
| 619 |
{ |
| 620 |
return BinaryAsciiTransfer.decodeNumeric(v, 1, 1.0, pos); |
| 621 |
} |
| 622 |
}; |
| 623 |
|
| 624 |
var TransferCoord = |
| 625 |
{ |
| 626 |
# LatLon scaling; |
| 627 |
# 1 degree = 110574 meters; |
| 628 |
# requires 4 bytes for 1 meter resolution. |
| 629 |
# permits 0.1 meter resolution. |
| 630 |
LatLonLength: 4, |
| 631 |
LatLonFactor: 0.000001, |
| 632 |
AltLength: 3, |
| 633 |
|
| 634 |
encode : func(v) |
| 635 |
{ |
| 636 |
return BinaryAsciiTransfer.encodeNumeric(v.lat(), TransferCoord.LatLonLength, TransferCoord.LatLonFactor) |
| 637 |
~ BinaryAsciiTransfer.encodeNumeric(v.lon(), TransferCoord.LatLonLength, TransferCoord.LatLonFactor) |
| 638 |
~ emesary.TransferInt.encode(v.alt(), TransferCoord.AltLength); |
| 639 |
}, |
| 640 |
decode : func(v,pos) |
| 641 |
{ |
| 642 |
var dv = BinaryAsciiTransfer.decodeNumeric(v, TransferCoord.LatLonLength, TransferCoord.LatLonFactor, pos); |
| 643 |
var lat = (dv.value); |
| 644 |
dv = BinaryAsciiTransfer.decodeNumeric(v, TransferCoord.LatLonLength, TransferCoord.LatLonFactor, dv.pos); |
| 645 |
var lon = (dv.value); |
| 646 |
dv = emesary.TransferInt.decode(v, TransferCoord.AltLength, dv.pos); |
| 647 |
var alt =dv.value; |
| 648 |
|
| 649 |
dv.value = geo.Coord.new().set_latlon(lat, lon).set_alt(alt); |
| 650 |
return dv; |
| 651 |
} |
| 652 |
}; |
| 653 |
|
| 654 |
# genericEmesaryGlobalTransmitterTransmit allowes to use the emesary.GlobalTransmitter via fgcommand |
| 655 |
# which in turn allows using it in XML bindings, e.g. |
| 656 |
# <binding> |
| 657 |
# <command>emesary-transmit</command> |
| 658 |
# <type>cockpit-switch</type> |
| 659 |
# <ident>eicas-page-select</ident> |
| 660 |
# <page>hydraulic</page> |
| 661 |
# </binding> |
| 662 |
# |
| 663 |
var genericEmesaryGlobalTransmitterTransmit = func(node) |
| 664 |
{ |
| 665 |
var transmitter = emesary.GlobalTransmitter; |
| 666 |
var t = node.getNode("transmitter",1).getValue(); |
| 667 |
if (t != nil) { |
| 668 |
transmitter = emesary.getTransmitter(t); |
| 669 |
if (transmitter == nil) { |
| 670 |
logprint(LOG_WARN, "Invalid transmitter "~t); |
| 671 |
return; |
| 672 |
} |
| 673 |
} |
| 674 |
var type = node.getNode("type").getValue(); |
| 675 |
if (type == nil) { |
| 676 |
logprint(LOG_WARN, "emesary-transmit requires a type"); |
| 677 |
return; |
| 678 |
} |
| 679 |
var ident = node.getNode("ident").getValue(); |
| 680 |
if (ident == nil) { |
| 681 |
logprint(LOG_WARN, "emesary-transmit requires an ident"); |
| 682 |
return; |
| 683 |
} |
| 684 |
var typeid = node.getNode("typeid",1).getValue() or 0; |
| 685 |
if (typeid == 0) { |
| 686 |
typeid = TypeIdUnspecified; |
| 687 |
logprint(LOG_WARN, "emesary-transmit using generic typeid ", typeid); |
| 688 |
} |
| 689 |
|
| 690 |
var message = emesary.Notification.new(type, ident, typeid); |
| 691 |
node.removeChild("type"); |
| 692 |
node.removeChild("id"); |
| 693 |
node.removeChild("typeid"); |
| 694 |
|
| 695 |
# add remaining nodes to the message hash |
| 696 |
var children = node.getValues(); |
| 697 |
if (children != nil) { |
| 698 |
foreach (var key; keys(children)) { |
| 699 |
message[key] = children[key]; |
| 700 |
} |
| 701 |
} |
| 702 |
transmitter.NotifyAll(message); |
| 703 |
}; |
| 704 |
|
| 705 |
# Temporary bugfix -- FIXME |
| 706 |
# removecommand("emesary-transmit"); #in case of reload |
| 707 |
addcommand("emesary-transmit", genericEmesaryGlobalTransmitterTransmit); |
| 708 |
|
| 709 |
#setprop("/sim/startup/terminal-ansi-colors",0); |
| 710 |
#for(i=-1;i<=1;i+=0.1) |
| 711 |
#print ("i ",i, " --> ", (TransferNorm.decode(TransferNorm.encode(i,2), 2,0)).value); |
| 712 |
#debug.dump(TransferNorm.decode(TransferNorm.encode(-1,2), 2,0)); |
| 713 |
#debug.dump(TransferNorm.decode(TransferNorm.encode(0,2), 2,0)); |
| 714 |
#debug.dump(TransferNorm.decode(TransferNorm.encode(1,2), 2,0)); |