-
Notifications
You must be signed in to change notification settings - Fork 1
/
ttemacs.el
448 lines (389 loc) · 14.8 KB
/
ttemacs.el
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
;;; ttemacs.el ---Send and receive Edifact messages from emacs.
;; Author: Sylvain Roy <[email protected]>
(require 'bindat)
(require 'network-stream)
;;
;; High level function (these are the ones you want to use in a scenario).
;;
(defun tt-configuration (config)
"Set the network configuration to use."
(setq ttemacs-sequencer-queue
(cons `(lambda ()
(scenario-set-configuration ',config))
ttemacs-sequencer-queue)))
(defun tt-send (query)
"Add a message to send in the sequencer."
(setq ttemacs-sequencer-queue
(cons `(lambda () (scenario-send ',query))
ttemacs-sequencer-queue)))
(defun tt-match (template)
"Matche the reply with template."
(setq ttemacs-sequencer-queue
(cons `(lambda () (scenario-match ',template))
ttemacs-sequencer-queue)))
(defun tt-process (fun)
"Execute the function fun"
(setq ttemacs-sequencer-queue
(cons `(lambda ()
(scenario-process ,fun))
ttemacs-sequencer-queue)))
(defun tt-done ()
"Start the injection. To call at the very end of the scenario."
(tt-log ">> Starting")
(ttemacs-clean-log)
(ttemacs-clean-variables)
(sequencer-next-action))
(defun tt-get-var (name)
"Get the value of a scenario variable.
To use within a tt-process directive only!"
(lax-plist-get tt-variables name))
(defun tt-set-var (name value)
"Set the value of a scenario variable.
To use within a tt-process directive only!"
(lax-plist-put tt-variables name value))
(defun tt-log (message)
"Log message in ttemacs-output.
To use within a tt-process directive only!"
(with-current-buffer (get-buffer-create "ttemacs-output")
(goto-char (point-max))
(insert (concat message "\n"))
(display-buffer "ttemacs-output")
))
;;
;; Scenario layer
;;
(defvar tt-config
'(protocol erplv2
ip "127.0.0.1"
port 40000)
"The configuration used by tt-emacs to send messages.
Use 'tt-configuration function to update it.")
(defvar tt-variables ()
"The variables defined in queries/replies via {%varname%=*} pattern.")
(defvar ttemacs-sequencer-queue ()
"The list of actions pending to be processed.")
(defvar tt-last-reply ()
"The last reply received by the injector.")
(defun sequencer-next-action ()
"Process the next action of the sequencer"
(let ((toeval (car (last ttemacs-sequencer-queue))))
(if toeval
(progn
;; Dequeue and exec next action
(setq ttemacs-sequencer-queue (butlast ttemacs-sequencer-queue))
(funcall toeval))
(progn
(tt-log ">> Cxn Closed")
(delete-process ttemacs-process)))))
(defun scenario-set-configuration (config)
"Set the network configuration to use."
(tt-log (format ">> Configuration:\n - ip:%s\n - port:%s\n - protocol:%s\n"
(plist-get tt-config 'ip)
(plist-get tt-config 'port)
(plist-get tt-config 'protocol)))
(setq tt-config config)
(sequencer-next-action))
(defun scenario-process (fun)
"Process fun in the context of the scenario."
(tt-log ">> Processed:\n")
(setq variables (funcall fun))
(sequencer-next-action))
(defun ttemacs-clean-variables ()
"Reset the registered variables."
(setq tt-variables ()))
(defun scenario-send (query)
"Send a message and save the response in the global var 'reply."
(session-send (implement-query-template query tt-variables)))
(defun scenario-reply-handler (msg)
"Called upon reception of the reply. Save the reply in 'tt-last-reply."
(setq tt-last-reply msg)
(sequencer-next-action))
(defun scenario-match (template)
"Matches the last reply against template and save variables."
(setq template (chomp template))
(setq tt-variables (append (parse-reply template tt-last-reply)
tt-variables))
(let ((l tt-variables)
(out ""))
(while l
(setq out (concat out "\n " (car l) " = " (car (cdr l))))
(setq l (cddr l)))
(tt-log (concat ">> Matched: " out "\n")))
(sequencer-next-action))
(defun implement-query-template (query-template variables)
"Implement the template with the variables given in parameter."
(defun set-variable-in-query-template (matched-region)
(let* ((var-name matched-region)
(var-value (lax-plist-get variables var-name)))
(if var-value
var-value
(format "[[%s Not Found]]" var-name))))
(replace-regexp-in-string "\\(%[^%]+%\\)"
'set-variable-in-query-template
query-template
t nil 0 0))
(defun parse-reply (template-reply reply)
"Return a plist of assoc (variable name => value), nil if no match."
(let* ((r (build-reply-regexp template-reply))
(regexp-of-template (car r))
(variables-of-template (car (cdr r)))
(variables ()))
(if (string-match regexp-of-template reply)
(progn
(while variables-of-template
(let ((name (car variables-of-template))
(position (cadr variables-of-template)))
(setq variables (lax-plist-put
variables
name
(substring reply
(match-beginning (+ 1 position))
(match-end (+ 1 position)))))
(setq variables-of-template (cddr variables-of-template))))
variables)
nil)))
(defun build-reply-regexp (template-reply)
"Return:
- a regular expression that matches instances of the template
- a plist that gives the assoc (variable name => index of regexp group)
where a variable is def in the template by {%varname%=*}"
(let* ((variable-position 0)
(variables-strings ())
(variables-of-template ()))
;; Find all variable regions, list variables, replace regions by regexp
(defun list-and-replace (matched-region)
"Collects and replaces variable regions."
(setq variables-strings (cons matched-region variables-strings))
"\\\\([^+:'*]*\\\\)")
(setq template-reply (replace-regexp-in-string
"{[ \t]*\\(%[^%]*%[ \t]*=[ \t]*\\)?\\*[ \t]*}"
'list-and-replace
template-reply
t nil 0 0))
;; Escape '+' which is a special character in regexp
;; todo: what about the other special characters?!
(setq template-reply (replace-regexp-in-string
"\\+"
"\\\\+"
template-reply
t nil 0 0))
;; Build plist of (variable => variable position in regexp)
(setq variables-strings (reverse variables-strings))
(while variables-strings
(setq variable (car variables-strings))
(setq split-name (split-string variable "%"))
;; If it can be split by %, then there is a variable name in it
;; (e.g. {%varname%=*}) and then we want to index it.
(if (= 3 (length split-name))
(setq variables-of-template
(lax-plist-put variables-of-template
(concat "%" (nth 1 split-name) "%")
variable-position)))
(setq variable-position (+ variable-position 1))
(setq variables-strings (cdr variables-strings)))
(list template-reply variables-of-template)))
;;
;; Session layer
;;
(defvar ttemacs-session-context '(local-conv-id nil
local-seq-number nil
remote-conv-id ""
remote-seq-number "")
"Context information at session level (e.g. the conversations ID).")
(defun session-send (msg)
"Send 'msg' to ip:port using. session-reply-handler will be called with reply."
(setq msg (chomp msg))
(setq msg (unpretty-print msg))
(update-session-with-query msg)
(setq msg (update-query-based-on-context msg))
(tt-log (format ">> Sent:\n%s\n" (pretty-print msg)))
(setq msg (update-with-syntax-separators msg))
(transport-send msg))
(defun session-reply-handler (msg)
"Callback to handle message received at session level."
(setq msg (update-with-display-separators msg))
(update-session-with-reply msg)
(setq msg (pretty-print msg))
(tt-log (format ">> Received:\n%s\n" msg))
(scenario-reply-handler msg))
(defun unpretty-print (string)
"Remove '\n&' at end of lines."
(setq string (replace-regexp-in-string "^[ \t\x0a]*" "" string t nil 0 0))
(setq string (replace-regexp-in-string "'\\(&?[ \t\x0a]*\\)" "" string t nil 1 0)))
(defun pretty-print (string)
"Add newline between segment of the message."
(setq string (replace-regexp-in-string "\\('\\)." "'&\x0a" string t nil 1 0)))
(defun update-with-syntax-separators (string)
"Detect syntax in UNB segment and changes separators accordingly."
(unless (numberp (string-match "^UNB\\+IATA" string))
(setq string (replace-regexp-in-string "'" "\x1c" string t nil 0 0))
(setq string (replace-regexp-in-string "\\+" "\x1d" string t nil 0 0))
(setq string (replace-regexp-in-string ":" "\x1f" string t nil 0 0))
(setq string (replace-regexp-in-string "\\*" "\x19" string t nil 0 0)))
string)
(defun update-with-display-separators (string)
"Change EDI separators by printable ones (the ones of IATA)."
(setq string (replace-regexp-in-string "\x1c" "'" string t nil 0 0))
(setq string (replace-regexp-in-string "\x1d" "+" string t nil 0 0))
(setq string (replace-regexp-in-string "\x1f" ":" string t nil 0 0))
(setq string (replace-regexp-in-string "\x19" "*" string t nil 0 0)))
(defun update-query-based-on-context (query)
"Update message according to session context."
(setq query (update-local-part-of-message
query
(plist-get ttemacs-session-context 'local-conv-id)
(plist-get ttemacs-session-context 'local-seq-number)))
(setq query (update-remote-part-of-message
query
(plist-get ttemacs-session-context 'remote-conv-id)
(plist-get ttemacs-session-context 'remote-seq-number)))
query)
(defun update-session-with-query (query)
"Update the context with the new message to send."
(let ((parsed-msg (parse-message query)))
(if (not (stringp (plist-get ttemacs-session-context 'local-conv-id)))
;; Init local ID if first message in conv
(progn
(setq ttemacs-session-context
(plist-put ttemacs-session-context 'local-conv-id
(plist-get parsed-msg 'local-conv-id)))
(setq ttemacs-session-context
(plist-put ttemacs-session-context 'local-seq-number
"0000"))))
;; Increment sequence number
(setq ttemacs-session-context
(plist-put ttemacs-session-context 'local-seq-number
(format "%04d" (+ 1 (string-to-number
(plist-get ttemacs-session-context
'local-seq-number))))))))
(defun update-session-with-reply (reply)
"Update the session context with a newly received reply."
(let ((parsed-msg (parse-message reply)))
(setq ttemacs-session-context
(plist-put ttemacs-session-context 'remote-conv-id
(plist-get parsed-msg 'local-conv-id)))
(setq ttemacs-session-context
(plist-put ttemacs-session-context 'remote-seq-number
(plist-get parsed-msg 'local-seq-number)))))
(defun update-local-part-of-message (msg conv-id seq-number)
"Return updated message with local conv ID / seq number."
(setq msg (replace-regexp-in-string
"^UNB\\+[^+]*\\+[^+]*\\+[^+]*\\+[^+]*\\+\\([^+]*\\)"
(concat conv-id seq-number)
msg t nil 1 0))
(replace-regexp-in-string
"UNZ\\+[^+]*\\+\\([^+']*\\)"
(concat conv-id seq-number)
msg t nil 1 0))
(defun update-remote-part-of-message (msg conv-id seq-number)
"Return updated message with remote conv ID / seq number."
(replace-regexp-in-string
"^UNB\\+[^+]*\\+[^+]*\\+[^+]*\\+[^+]*\\+[^+]*\\+\\([^+]*\\)"
(concat conv-id seq-number)
msg t nil 1 0))
(defun parse-message (msg)
"Parse UNB part of a message and return plist of the conv-ids and
sequence numbers."
(string-match
"^UNB\\+[^+]*\\+[^+]*\\+[^+]*\\+[^+]*\\+\\([^+]*\\)\\+\\([^+]*\\)"
msg)
`(local-conv-id ,(substring msg
(match-beginning 1)
(- (match-end 1) 4))
local-seq-number ,(substring msg
(- (match-end 1) 4)
(match-end 1))
remote-conv-id ,(if (not (= (match-beginning 2) (match-end 2)))
(substring msg
(match-beginning 1)
(- (match-end 1) 4))
"")
remote-seq-number ,(if (not (= (match-beginning 2) (match-end 2)))
(substring msg
(- (match-end 2) 4)
(match-end 2))
"")))
;;
;; Transport layer
;;
(defvar ttemacs-process nil
"The process that handle the cxn")
(defun transport-send (data)
"Send 'data' to ip:port using ad-hoc transport encoder/decoder.
Then, call transport-reply-handler with the decoded reply."
(setq ttemacs-recv-buffer "")
(defun handle-output-flow (process output)
"Aggregate output data, decode them and call handler."
(setq ttemacs-recv-buffer (concat ttemacs-recv-buffer output))
(condition-case nil
(transport-reply-handler
(transport-decoder
(string-make-unibyte ttemacs-recv-buffer)))
(error nil)))
(let ((ip (plist-get tt-config 'ip))
(port (plist-get tt-config 'port)))
(setq ttemacs-process
(open-network-stream "ttemacs-process" "*Messages*" ip port))
(set-process-filter ttemacs-process 'handle-output-flow)
(process-send-string ttemacs-process (transport-encoder data))))
(defun transport-reply-handler (msg)
"Callback to handle message received at transport level."
(session-reply-handler msg))
(defun transport-encoder (data)
"Return encoded data"
(let ((protocol (plist-get tt-config 'protocol)))
(cond ((string= protocol 'erplv2) (erplv2-encoder data))
(t (error "protocol %s not supported" protocol)))))
(defun transport-decoder (data)
"Return decoded data"
(let ((protocol (plist-get tt-config 'protocol)))
(cond ((string= protocol 'erplv2) (erplv2-decoder data))
(t (error "protocol %s not supported" protocol)))))
;;
;; Transport: ERPLv2 Protocol
;;
(defvar erplv2-header-spec
'((len1 u16) ; len of the message (= #x0000)
(len2 u16) ; len of the message (= len(data) + 12 + len(rscv))
(checksum1 u16) ; one's complement of the len (= #xFFFF)
(checksum2 u16) ; one's complement of the len
(headerlen u16) ; len of the erplv2 header (= len rscv + 4)
(headerversion u8) ; 0x32
(unused u8) ; 0x00
(rscv str (eval (- (bindat-get-field struct 'headerlen) 4)))
(message str (eval (- (bindat-get-field struct 'len2)
(bindat-get-field struct 'headerlen)
8))))
"Definition of an ERPLv2 header.")
(defun erplv2-encoder (msg)
"Encode a message in ERPLv2."
(let* ((len (+ (length msg) 12))
(fields `((len1 . #x0000)
(len2 . ,len)
(checksum1 . #xFFFF)
(checksum2 . ,(logxor len #xFFFF))
(headerlen . 4)
(headerversion . #x32)
(unused . 0)
(rscv . "")
(message . ,msg)))
(data (bindat-pack erplv2-header-spec fields)))
data))
(defun erplv2-decoder (data)
"Decode a message in ERPLv2."
(let* ((fields (bindat-unpack erplv2-header-spec data)))
(bindat-get-field fields 'message)))
;;
;; Misc utilities
;;
(defun chomp (string)
"Remove heading/trailing whitespace, new lines, etc."
(setq string (when (string-match "^[ \t\n]+" string)
(replace-match "" nil nil string)))
(setq string (when (string-match "[ \t\n]+$" string)
(replace-match "" nil nil string)))
string)
(defun ttemacs-clean-log ()
"Clean ttemacs-output message log buffer."
(with-current-buffer (get-buffer-create "ttemacs-output")
(delete-region (point-min) (point-max))))