g4zmq.py
Go to the documentation of this file.
1 #/usr/bin/env python
2 # ==================================================================
3 # python client for zmq-geant4
4 #
5 # *** Python3 ***
6 # ==================================================================
7 import zmq
8 
9 # global vars
10 charset = 'utf-8'
11 context = zmq.Context(1)
12 socket = context.socket(zmq.REQ)
13 
14 # ===============================================================
15 def connect(endpoint="tcp://127.0.0.1:5555") :
16  socket.setsockopt(zmq.LINGER, 0)
17  socket.connect(endpoint)
18  ping()
19 
20 def ping() :
21  socket.send(b"@@ping")
22  poller = zmq.Poller()
23  poller.register(socket, zmq.POLLIN)
24  if poller.poll(1*1000) :
25  output = socket.recv()
26  print ("@@ G4ZMQ server connected.")
27  else :
28  raise ConnectionError("*** connection timeout")
29 
30 def debug(dflag) :
31  if dflag :
32  socket.send(b"@@debug")
33  else :
34  socket.send(b"@@nodebug")
35  output = socket.recv()
36  print(output.decode(charset))
37 
38 def apply(command) :
39  cmd_str= command.encode(charset)
40  socket.send(cmd_str)
41  output = socket.recv()
42  print(output.decode(charset))
43 
44 def execute(command) :
45  apply(command)
46 
47 def pwd() :
48  socket.send(b"pwd")
49  output = socket.recv()
50  print(output.decode(charset))
51 
52 def cwd() :
53  socket.send(b"cwd")
54  output = socket.recv()
55  print(output.decode(charset))
56 
57 def cd(dir="") :
58  cmd = "cd " + dir
59  socket.send(cmd.encode(charset))
60  output = socket.recv()
61  print(output.decode(charset))
62 
63 def lc(target="") :
64  cmd = "lc " + target
65  socket.send(cmd.encode(charset))
66  output = socket.recv()
67  print(output.decode(charset))
68 
69 def ls(target="") :
70  cmd = "ls " + target
71  socket.send(cmd.encode(charset))
72  output = socket.recv()
73  print(output.decode(charset))
74 
75 def help(target="") :
76  if ( target == "" ) :
77  raise SyntaxWarning("*** no command specified.")
78  else :
79  cmd = "help " + target
80  socket.send(cmd.encode(charset))
81  output = socket.recv()
82  print(output.decode(charset))
83 
84 def history() :
85  socket.send(b"history")
86  output = socket.recv()
87  print(output.decode(charset))
88 
89 def beamOn(nevent) :
90  cmd = "/run/beamOn " + str(nevent)
91  apply(cmd)
92 
93 def getvalue(target="") :
94  cmd = "?" + target
95  socket.send(cmd.encode(charset))
96  output = socket.recv()
97  print(output.decode(charset))
98 
99 def exit() :
100  socket.send(b"exit")
101  output = socket.recv()
102  print(output.decode(charset))
103 
104 # ===============================================================
105 if __name__ == '__main__' :
106  pass
def history()
Definition: g4zmq.py:84
def exit()
Definition: g4zmq.py:99
def apply(command)
Definition: g4zmq.py:38
def cd(dir="")
Definition: g4zmq.py:57
def pwd()
Definition: g4zmq.py:47
def connect(endpoint="tcp://127.0.0.1:5555")
Definition: g4zmq.py:15
def cwd()
Definition: g4zmq.py:52
bool print
def getvalue(target="")
Definition: g4zmq.py:93
def execute(command)
Definition: g4zmq.py:44
def help(target="")
Definition: g4zmq.py:75
def lc(target="")
Definition: g4zmq.py:63
def beamOn(nevent)
Definition: g4zmq.py:89
def ls(target="")
Definition: g4zmq.py:69
def ping()
Definition: g4zmq.py:20
def debug(dflag)
Definition: g4zmq.py:30