Skip to content

Commit c5f0ef7

Browse files
committed
funcall2
1 parent 06a86dd commit c5f0ef7

7 files changed

Lines changed: 1590 additions & 0 deletions

File tree

0mq/funbody2.dir/concore2.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import time
2+
import os
3+
from ast import literal_eval
4+
import sys
5+
import re
6+
7+
#if windows, create script to kill this process
8+
# because batch files don't provide easy way to know pid of last command
9+
# ignored for posix!=windows, because "concorepid" is handled by script
10+
# ignored for docker (linux!=windows), because handled by docker stop
11+
if hasattr(sys, 'getwindowsversion'):
12+
with open("concorekill.bat","w") as fpid:
13+
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
14+
15+
try:
16+
iport = literal_eval(open("concore.iport").read())
17+
except:
18+
iport = dict()
19+
try:
20+
oport = literal_eval(open("concore.oport").read())
21+
except:
22+
oport = dict()
23+
24+
25+
s = ''
26+
olds = ''
27+
delay = 1
28+
retrycount = 0
29+
inpath = "./in" #must be rel path for local
30+
outpath = "./out"
31+
32+
#9/21/22
33+
try:
34+
sparams = open(inpath+"1/concore.params").read()
35+
if sparams[0] == '"': #windows keeps "" need to remove
36+
sparams = sparams[1:]
37+
sparams = sparams[0:sparams.find('"')]
38+
if sparams != '{':
39+
print("converting sparams: "+sparams)
40+
sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}"
41+
print("converted sparams: " + sparams)
42+
try:
43+
params = literal_eval(sparams)
44+
except:
45+
print("bad params: "+sparams)
46+
except:
47+
params = dict()
48+
#9/30/22
49+
def tryparam(n,i):
50+
try:
51+
return params[n]
52+
except:
53+
return i
54+
55+
56+
#9/12/21
57+
def default_maxtime(default):
58+
global maxtime
59+
try:
60+
maxtime = literal_eval(open(inpath+"1/concore.maxtime").read())
61+
except:
62+
maxtime = default
63+
default_maxtime(100)
64+
65+
def unchanged():
66+
global olds,s
67+
if olds==s:
68+
s = ''
69+
return True
70+
else:
71+
olds = s
72+
return False
73+
74+
def read(port, name, initstr):
75+
global s,simtime,retrycount
76+
time.sleep(delay)
77+
try:
78+
infile = open(inpath+str(port)+"/"+name);
79+
ins = infile.read()
80+
except:
81+
ins = initstr
82+
while len(ins)==0:
83+
time.sleep(delay)
84+
ins = infile.read()
85+
retrycount += 1
86+
s += ins
87+
inval = literal_eval(ins)
88+
simtime = max(simtime,inval[0])
89+
return inval[1:]
90+
91+
def write(port, name, val, delta=0):
92+
global outpath,simtime
93+
if isinstance(val,str):
94+
time.sleep(2*delay)
95+
elif isinstance(val,list)==False:
96+
print("mywrite must have list or str")
97+
quit()
98+
try:
99+
with open(outpath+str(port)+"/"+name,"w") as outfile:
100+
if isinstance(val,list):
101+
outfile.write(str([simtime+delta]+val))
102+
simtime += delta
103+
else:
104+
outfile.write(val)
105+
except:
106+
print("skipping"+outpath+str(port)+"/"+name);
107+
108+
def initval(simtime_val):
109+
global simtime
110+
val = literal_eval(simtime_val)
111+
simtime = val[0]
112+
return val[1:]
113+

0mq/funbody2.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import concore
2+
import concore2
3+
import time
4+
from osparc_control import CommandManifest
5+
from osparc_control import CommandParameter
6+
from osparc_control import CommandType
7+
from osparc_control import PairedTransmitter
8+
# declare some commands to which a reply can be provided
9+
CONCORE_MANIFEST = CommandManifest(
10+
action="fun",
11+
description="function call",
12+
params=[
13+
CommandParameter(name="u", description="control move"),
14+
],
15+
command_type=CommandType.WITH_IMMEDIATE_REPLY,
16+
)
17+
18+
19+
print("funbody 0mq")
20+
# initialization of 0mq/osparc-control "paired_transmitter"
21+
paired_transmitter = PairedTransmitter(
22+
remote_host="localhost",
23+
exposed_commands=[CONCORE_MANIFEST],
24+
remote_port=2346,
25+
listen_port=2345,)
26+
27+
paired_transmitter.start_background_sync()
28+
29+
concore.delay = 0.07
30+
concore2.delay = 0.07
31+
concore2.inpath = concore.inpath
32+
concore2.outpath = concore.outpath
33+
concore2.simtime = 0
34+
concore.default_maxtime(100)
35+
init_simtime_u = "[0.0, 0.0, 0.0]"
36+
init_simtime_ym = "[0.0, 0.0, 0.0]"
37+
38+
u = concore.initval(init_simtime_u)
39+
ym = concore2.initval(init_simtime_ym)
40+
while(concore2.simtime<concore.maxtime):
41+
#while concore.unchanged():
42+
# u = concore.read(concore.iport['U1'],"u",init_simtime_u)
43+
command_list = paired_transmitter.get_incoming_requests()
44+
while len(command_list)==0:
45+
time.sleep(.01)
46+
command_list = paired_transmitter.get_incoming_requests()
47+
if len(command_list)>1:
48+
print("too many commands at once!")
49+
command = command_list[0]
50+
if command.action == CONCORE_MANIFEST.action:
51+
u = command.params["u"]
52+
concore.simtime = u[0]
53+
u = u[1:]
54+
concore.write(concore.oport['U2'],"u",u)
55+
print(u)
56+
old2 = concore2.simtime
57+
while concore2.unchanged() or concore2.simtime <= old2:
58+
ym = concore2.read(concore.iport['Y2'],"ym",init_simtime_ym)
59+
ym = [concore2.simtime]+ym
60+
print(f"Replying to {command.action} with {ym}")
61+
paired_transmitter.reply_to_command(
62+
request_id=command.request_id, payload=ym)
63+
else:
64+
print("undefined action"+str(command.action))
65+
quit()
66+
#concore2.write(concore.oport['Y1'],"ym",ym)
67+
print("funbody u="+str(u)+" ym="+str(ym)+" time="+str(concore2.simtime))
68+
paired_transmitter.stop_background_sync()
69+
print("retry="+str(concore.retrycount))

0mq/funcall2.dir/concore2.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import time
2+
import os
3+
from ast import literal_eval
4+
import sys
5+
import re
6+
7+
#if windows, create script to kill this process
8+
# because batch files don't provide easy way to know pid of last command
9+
# ignored for posix!=windows, because "concorepid" is handled by script
10+
# ignored for docker (linux!=windows), because handled by docker stop
11+
if hasattr(sys, 'getwindowsversion'):
12+
with open("concorekill.bat","w") as fpid:
13+
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
14+
15+
try:
16+
iport = literal_eval(open("concore.iport").read())
17+
except:
18+
iport = dict()
19+
try:
20+
oport = literal_eval(open("concore.oport").read())
21+
except:
22+
oport = dict()
23+
24+
25+
s = ''
26+
olds = ''
27+
delay = 1
28+
retrycount = 0
29+
inpath = "./in" #must be rel path for local
30+
outpath = "./out"
31+
32+
#9/21/22
33+
try:
34+
sparams = open(inpath+"1/concore.params").read()
35+
if sparams[0] == '"': #windows keeps "" need to remove
36+
sparams = sparams[1:]
37+
sparams = sparams[0:sparams.find('"')]
38+
if sparams != '{':
39+
print("converting sparams: "+sparams)
40+
sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}"
41+
print("converted sparams: " + sparams)
42+
try:
43+
params = literal_eval(sparams)
44+
except:
45+
print("bad params: "+sparams)
46+
except:
47+
params = dict()
48+
#9/30/22
49+
def tryparam(n,i):
50+
try:
51+
return params[n]
52+
except:
53+
return i
54+
55+
56+
#9/12/21
57+
def default_maxtime(default):
58+
global maxtime
59+
try:
60+
maxtime = literal_eval(open(inpath+"1/concore.maxtime").read())
61+
except:
62+
maxtime = default
63+
default_maxtime(100)
64+
65+
def unchanged():
66+
global olds,s
67+
if olds==s:
68+
s = ''
69+
return True
70+
else:
71+
olds = s
72+
return False
73+
74+
def read(port, name, initstr):
75+
global s,simtime,retrycount
76+
time.sleep(delay)
77+
try:
78+
infile = open(inpath+str(port)+"/"+name);
79+
ins = infile.read()
80+
except:
81+
ins = initstr
82+
while len(ins)==0:
83+
time.sleep(delay)
84+
ins = infile.read()
85+
retrycount += 1
86+
s += ins
87+
inval = literal_eval(ins)
88+
simtime = max(simtime,inval[0])
89+
return inval[1:]
90+
91+
def write(port, name, val, delta=0):
92+
global outpath,simtime
93+
if isinstance(val,str):
94+
time.sleep(2*delay)
95+
elif isinstance(val,list)==False:
96+
print("mywrite must have list or str")
97+
quit()
98+
try:
99+
with open(outpath+str(port)+"/"+name,"w") as outfile:
100+
if isinstance(val,list):
101+
outfile.write(str([simtime+delta]+val))
102+
simtime += delta
103+
else:
104+
outfile.write(val)
105+
except:
106+
print("skipping"+outpath+str(port)+"/"+name);
107+
108+
def initval(simtime_val):
109+
global simtime
110+
val = literal_eval(simtime_val)
111+
simtime = val[0]
112+
return val[1:]
113+

0mq/funcall2.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import concore
2+
import concore2
3+
from osparc_control import PairedTransmitter
4+
print("funcall 0mq")
5+
6+
concore.delay = 0.07
7+
concore2.delay = 0.07
8+
concore2.inpath = concore.inpath
9+
concore2.outpath = concore.outpath
10+
concore2.simtime = 0
11+
concore.default_maxtime(100)
12+
init_simtime_u = "[0.0, 0.0, 0.0]"
13+
init_simtime_ym = "[0.0, 0.0, 0.0]"
14+
15+
u = concore.initval(init_simtime_u)
16+
ym = concore2.initval(init_simtime_ym)
17+
while(concore2.simtime<concore.maxtime):
18+
while concore.unchanged():
19+
u = concore.read(concore.iport['U'],"u",init_simtime_u)
20+
print(u)
21+
#concore.write(concore.oport['U1'],"u",u)
22+
#old2 = concore2.simtime
23+
#while concore2.unchanged() or concore2.simtime <= old2:
24+
# ym = concore2.read(concore.iport['Y1'],"ym",init_simtime_ym)
25+
paired_transmitter = PairedTransmitter(
26+
remote_host="localhost", exposed_commands=[],
27+
remote_port=2345, listen_port=2346,)
28+
paired_transmitter.start_background_sync()
29+
ym = paired_transmitter.request_with_immediate_reply(
30+
"fun", timeout=10.0, params={"u": [concore.simtime]+u})
31+
concore2.simtime = ym[0]
32+
ym = ym[1:]
33+
paired_transmitter.stop_background_sync()
34+
#print(ym)
35+
concore2.write(concore.oport['Y'],"ym",ym)
36+
print("funcall 0mq u="+str(u)+" ym="+str(ym)+" time="+str(concore2.simtime))
37+
print("retry="+str(concore.retrycount))

0 commit comments

Comments
 (0)