@@ -91,7 +91,6 @@ def start_process(self):
9191            LOG .error ('Python Thread Instance cannot be started more than once. Please create a new BareSIP instance' )
9292            return 
9393        self .started_once  =  True 
94-         self .baresip  =  pexpect .spawn ('baresip -f '  +  self .config_path )
9594        super ().__init__ ()
9695        self .start ()
9796        if  self .block :
@@ -118,6 +117,10 @@ def do_command(self, action):
118117    def  login (self ):
119118        LOG .info ("Adding account: "  +  self .user )
120119        self .baresip .sendline ("/uanew "  +  self ._login )
120+     
121+     def  logout (self ):
122+         LOG .info ("Removing account: "  +  self .user )
123+         self .baresip .sendline ("/uadelall" )
121124
122125    def  call (self , number ):
123126        LOG .info ("Dialling: "  +  number )
@@ -179,24 +182,30 @@ def check_call_status(self):
179182        self .do_command ("/callstat" )
180183        sleep (0.1 )
181184        return  self .call_status 
185+     
186+     def  killBareSIPSubProcess (self ):
187+         if  self .baresip  !=  None  and  self .baresip .isalive ():
188+             LOG .info ("Killing BareSip process" )
189+             self .baresip .sendline ("/quit" )
190+             self .baresip .close ()
191+             self .baresip .kill (signal .SIGKILL )
192+         self .baresip  =  None  # this would prompt the run() loop to call startBareSIPProcess 
193+     
194+     def  startBareSIPSubProcess (self ):
195+         LOG .info ("Starting BareSip process" )
196+         self .baresip  =  pexpect .spawn ('baresip -f '  +  self .config_path )
182197
183198    def  quit (self ):
184199        if  self .updated_config :
185200            LOG .info ("restoring original config" )
186201            with  open (join (self .config_path , "config" ), "w" ) as  f :
187202                f .write (self ._original_config )
188- 
189-         if  self .running  or  (self .baresip  !=  None  and  self .baresip .isalive ()):
190-             LOG .info ("Closing BareSIP instance" )
203+         LOG .info ("Closing BareSIP instance" )
204+         if  self .running :
191205            if  self .current_call :
192206                self .hang ()
193-             
194-             if  self .baresip .isalive ():
195-                 LOG .info ("Killing BareSip process" )
196-                 self .baresip .sendline ("/quit" )
197-                 self .baresip .close ()
198-                 self .baresip .kill (signal .SIGKILL )
199207
208+             self .killBareSIPSubProcess ()
200209            self .current_call  =  None 
201210            self ._call_status  =  None 
202211            self .abort  =  True 
@@ -353,6 +362,14 @@ def run(self):
353362        self .running  =  True 
354363        while  self .running :
355364            try :
365+                 if  self .baresip  ==  None :
366+                     self .startBareSIPSubProcess ()
367+                     continue 
368+                 if  not  self .baresip .isalive ():
369+                     self .killBareSIPSubProcess ()
370+                     sleep (0.5 )
371+                     continue 
372+ 
356373                out  =  self .baresip .readline ().decode ("utf-8" )
357374
358375                if  out  !=  self ._prev_output :
0 commit comments