-
Notifications
You must be signed in to change notification settings - Fork 1
/
feudc.nim
134 lines (104 loc) · 2.52 KB
/
feudc.nim
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
import cligen, os, rdstdin, strformat, strutils
import "."/src/[globals, plugin, utils]
const
err = "Failed to send through channel locally"
var
gCh: Channel[string]
gCh.open()
proc handleCommand(ctx: Ctx, cmd: CmdData) =
if cmd.params.len != 0:
if cmd.params[0] == "runHook":
return
ctx.handlePluginCommand(cmd)
if cmd.failed:
cmd.failed = false
cmd.params = @["sendRemote"] & cmd.params
ctx.handlePluginCommand(cmd)
else:
cmd.failed = true
proc handleAck(ctx: Ctx, command: string) =
var
(_, val) = command.splitCmd()
valI = parseInt(val)
cmd = newCmdData("getAck")
for i in 0 .. valI-1:
cmd.returned = @[]
for j in 0 .. 100:
handleCommand(ctx, cmd)
if cmd.returned.len != 0:
break
ctx.syncPlugins()
sleep(100)
if cmd.returned.len == 0:
echo "Not all commands ack'd"
quit(1)
proc messageLoop(ctx: Ctx) =
var
run = executing
while run == executing:
let (ready, command) = gCh.tryRecv()
if ready:
if command == "fexit":
run = stopped
elif command.startsWith("ack "):
ctx.handleAck(command)
else:
var
cmd = newCmdData(command)
handleCommand(ctx, cmd)
ctx.syncPlugins()
sleep(100)
proc initCmd() =
var
run = executing
sleep(1000)
while run == executing:
let
command = readLineFromStdin("feud> ")
if command.len != 0:
doAssert gCh.trySend(command), err
if command == "fexit":
run = stopped
sleep(100)
proc main(
ip: string = "",
command: seq[string],
) =
var
ctx = new(Ctx)
server =
if ip.len == 0:
"ipc://tmp/feud"
else:
&"tcp://{ip}:3917"
# client =
# if ip.len == 0:
# "ipc://tmp/feudc"
# else:
# &"tcp://*:3918"
thread: Thread[void]
ctx.handleCommand = handleCommand
ctx.initPlugins(client)
while not ctx.ready:
ctx.syncPlugins()
var
cmd = newCmdData(&"initRemote dial {server}")
handleCommand(ctx, cmd)
if command.len == 0:
createThread(thread, initCmd)
else:
for cmd in command:
doAssert gCh.trySend(cmd), err
if "quit" notin command and "exit" notin command:
doAssert gCh.trySend("ack " & $command.len), err
doAssert gCh.trySend("fexit"), err
ctx.messageLoop()
thread.joinThread()
ctx.stopPlugins()
when isMainModule:
dispatch(main, help = {
"ip": "IP address of remote server",
"command" : "command to send server",
}, short = {
"ip": 'i',
})