clientserver.py 3.41 KB
Newer Older
1
2
3
4
#!/usr/bin/python3

from time import sleep, perf_counter as pc
from os import path, remove
5
from abc import ABC, abstractmethod
6

7
8
9
##############
# SUPERCLASS #
##############
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
class ClientServer(ABC): 
   #
   def __init__(self,client_lockfile,maxsec=21600,sleepsec=10):
      #
      self.client_lockfile = client_lockfile
      self.maxsec = maxsec
      self.sleepsec = sleepsec
      super().__init__()
   #
   @abstractmethod
   def before_sleep(self): # subclass needs to implement this method
      pass
   #
   @abstractmethod
   def after_sleep(self): # subclass needs to implement this method
      pass
   #
   @abstractmethod
   def awake_condition(self): # subclass needs to implement this method
      pass
   #
   def start(self):
      #
      # ====================================
      self.before_sleep()
      # ====================================
      #
      awake = 1 # I am awake if this is zero
      t0 = pc()
      while (pc()-t0 <= self.maxsec) :
         #
         # =================================
         self.awake_condition()
         # =================================
         #
         exists = path.exists(self.client_lockfile)
         if (not exists) : 
            awake = 0 
            break
         else : 
            sleep(self.sleepsec)
      #
      # ====================================
      self.after_sleep()
      # ====================================
      return awake
57

58
59
60
###############
# SERVERCLASS #
###############
61

62
63
64
65
66
67
68
69
class QboxServer(ClientServer) : 
   #
   def before_sleep(self):
       #
       # Determine the name of the server file 
       #
       client_image = self.client_lockfile.split(".")[1] 
       self.server_inputfile = f"qb.{client_image}.in" # we assume that server_number = client_image
Marco Govoni's avatar
Marco Govoni committed
70
       print(self.server_inputfile)
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
       #
       # List of perturbation files 
       #
       perturbation_list = []
       with open(self.client_lockfile,"r") as f:
          for cnt, line in enumerate(f):
             perturbation_list.append(line)
       #
       # Create the input file for the server 
       # 
       with open(self.server_inputfile,"w") as f: 
          f.write("load gs.xml\n")
          for pert in perturbation_list : 
              f.write(f"response -vext filename {pert}")
       #
       # Awake server, by removing its lockfile 
       #
       if(path.exists(self.server_inputfile+".lock")) :
          remove(self.server_inputfile+".lock")
90

91
92
93
94
95
96
97
98
99
100
   #
   def awake_condition(self): 
       #
       # If server gets to sleeps, awake the client 
       #
       if( path.exists(self.server_inputfile+".lock")) :  
          remove(self.client_lockfile)
   #
   def after_sleep(self):
       pass 
101

102
103
104
#############
# INTERFACE #
#############
105

Marco Govoni's avatar
Marco Govoni committed
106
def sleep_and_wait(*args, **kwargs):
107
    #
108
109
110
    client_lockfile = args[0] # name of client lockfile 
    maxsec = 12 * 60 * 60 # 12 hours, Max sleep time (in s) 
    sleepsec = 1 # 1 second, Sleep interval (in s)
111
    #
112
    # change defaults 
113
114
115
116
117
118
    #
    if "maxsec" in kwargs.keys() : 
       maxsec = kwargs["maxsec"]
    if "sleepsec" in kwargs.keys() : 
       sleepsec = kwargs["sleepsec"]
    #
119
120
    server = QboxServer(client_lockfile,maxsec,sleepsec)
    return_int = server.start()
121
    #
122
    return return_int
123
    
124
125
126
########
# TEST #
########
127
128

def test() :
Marco Govoni's avatar
Marco Govoni committed
129
130
    with open("I.1.lock","w") as f :
       f.write(" ")
Marco Govoni's avatar
Marco Govoni committed
131
    sleep_and_wait("I.1.lock",maxsec=60,sleepsec=2)
132
133
134
135
136

if __name__ == "__main__":
    # execute only if run as a script
    test()