SNAPKITTYWEST commited on
Commit
2232499
·
verified ·
1 Parent(s): d415087

Mirror from SNAPKITTYWEST: gateway/mumps_gateway.py

Browse files
Files changed (1) hide show
  1. gateway/mumps_gateway.py +43 -0
gateway/mumps_gateway.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # gateway/mumps_gateway.py
2
+ # Binary scaffold for MUMPS integration.
3
+ #
4
+ # Binary record layout (300 bytes):
5
+ # [0:8] command_id — ASCII, null-padded
6
+ # [8:16] tx_id — signed int64 big-endian
7
+ # [16:24] account_id — signed int64 big-endian
8
+ # [24:40] amount — signed int128 big-endian
9
+ # [40:43] currency — ASCII
10
+ # [43:44] direction — 'D' debit / 'C' credit
11
+ # [44:172] idem_key — UTF-8, null-padded (128 bytes)
12
+ # [172:300] corr_id — UTF-8, null-padded (128 bytes)
13
+
14
+ def pack_command(cmd_id: str, tx_id: int, acct_id: int, amount: int,
15
+ currency: str, direction: str, idem: str, corr: str) -> bytes:
16
+ return b''.join([
17
+ cmd_id.encode('ascii')[:8].ljust(8, b'\x00'),
18
+ tx_id.to_bytes(8, 'big', signed=True),
19
+ acct_id.to_bytes(8, 'big', signed=True),
20
+ amount.to_bytes(16, 'big', signed=True),
21
+ currency.encode('ascii')[:3].ljust(3, b'\x00'),
22
+ direction.encode('ascii')[:1],
23
+ idem.encode('utf-8')[:128].ljust(128, b'\x00'),
24
+ corr.encode('utf-8')[:128].ljust(128, b'\x00'),
25
+ ])
26
+
27
+ def unpack_command(buf: bytes) -> dict:
28
+ if len(buf) < 300:
29
+ raise ValueError(f"buffer too small: {len(buf)} < 300")
30
+ return {
31
+ 'cmd_id': buf[0:8].rstrip(b'\x00').decode('ascii'),
32
+ 'transaction_id': int.from_bytes(buf[8:16], 'big', signed=True),
33
+ 'account_id': int.from_bytes(buf[16:24], 'big', signed=True),
34
+ 'amount': int.from_bytes(buf[24:40], 'big', signed=True),
35
+ 'currency': buf[40:43].rstrip(b'\x00').decode('ascii'),
36
+ 'direction': buf[43:44].decode('ascii'),
37
+ 'idempotency_key': buf[44:172].rstrip(b'\x00').decode('utf-8'),
38
+ 'correlation_id': buf[172:300].rstrip(b'\x00').decode('utf-8'),
39
+ }
40
+
41
+ def dispatch_to_runtime(buf: bytes, runtime_dispatch):
42
+ """runtime_dispatch: callable(dict) -> dict"""
43
+ return runtime_dispatch(unpack_command(buf))