[Twisted-Python] How to make a service dependent of another?

2009-05-15 Thread coder_gus
Hi,
I have a pb client and a tcp server. How can I make the server start 
only after/if the client started?

Thanks.


___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] How to make a service dependent of another?

2009-05-15 Thread Terry Jones
> "Gus" == coder gus  writes:
Gus> I have a pb client and a tcp server. How can I make the server start 
Gus> only after/if the client started?

Hi Gus

You might take a look at the code here

  
http://www.twistedmatrix.com/pipermail/twisted-python/2009-February/019249.html

and if you go back a couple of mails in that thread you'll see some more
discussion.  BTW, this approach hasn't received much comment, so you
shouldn't assume it's bulletproof or even recommended. But I use it many
times every day to launch about half a dozen twistd multiservices, and have
never had a problem.

You use it like a regular multiservice, but you can pass it a service which
is treated slightly specially. The other services you add will not be
started unless the service you pass to __init__ starts properly (you can
know this by having its startService return a deferred). Make sense?

Terry

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] How to make a service dependent of another?

2009-05-15 Thread Christopher Armstrong
On Fri, May 15, 2009 at 4:11 AM, coder_gus  wrote:
> Hi,
> I have a pb client and a tcp server. How can I make the server start
> only after/if the client started?

Basically, you want to call listenTCP in one of the callbacks that get
invoked when a client is successfully connected. There are a few of
these:

* the connectionMade method of your client protocol
* the clientConnectionMade method of your client factory
* the deferred that's returned from ClientCreator, if you're using it.

Depending on your situation and the factoring that you want, any of
these could be the most appropriate.

-- 
Christopher Armstrong
http://radix.twistedmatrix.com/
http://planet-if.com/
http://canonical.com/

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] How to make a service dependent of another?

2009-05-15 Thread Jean-Paul Calderone
On Fri, 15 May 2009 11:11:58 +0300, coder_gus  wrote:
>Hi,
>I have a pb client and a tcp server. How can I make the server start
>only after/if the client started?

In general, the answer to "how do I do X when Y?" is "Put the code to do X
in the callback that tells you Y has happened."

So, for example, if you're using PBClientFactory.login, you might add a
callback to the Deferred it returns which starts your TCP server.  ie,


f = PBClientFactory(...)
d = f.login(...)
def cbLoggedIn(avatar):
reactor.listenTCP(...)
...
d.addCallback(cbLoggedIn)
...
reactor.connectTCP(..., f)
reactor.run()

Jean-Paul

___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python


Re: [Twisted-Python] help using deferred

2009-05-15 Thread Luigi Conte
2009/5/11 Jean-Paul Calderone 

> On Mon, 11 May 2009 19:49:19 +0200, Luigi Conte <
> luigiandcosoluti...@gmail.com> wrote:
> >
> > [snip]
> >in my script I have to use connect method and then I have to do some
> >operations first of calling the start method
> >1) start connection
> >
> >def startConnection(self):
> >d = my_api.connect(self.ctrl_ip, self.ctrl_port, self.user,
> >self.pwd)
> >d.addCallback(self.postConnection)
>
> Since you omitted the definition of postConnection, I have no way to know
> what this does.
>
> >d.addErrback(twisted.python.log.err)
>
> Regardless, this is the wrong place to insert this errback.  You should
> read
> the Deferred documentation to learn what consequence it will have on your
> program's flow.
>
> http://twistedmatrix.com/projects/core/documentation/howto/defer.html is
> probably a good place to start.
>
> >print "Connection added"
> >return d
> >2) operation before starting a virtual machine:
> >def newVMCfg(self, new_vms_cfg):
> >   #...
> >   #some operations
> >   #if condition valid I try to start the virtual machine
> ># is this the correct way to pass args to the
> start
> >method?
> >d = self.d.addCallback(self.startVM,(new_vm,
> >self.lnms[i])
> >print "started vm %s"%new_vm
> >return d
> >
> >in the main process I call them as:
> >d = startConnection()
> >d.addCallback(newVMCfg, arg)
> >
> >Is it correct? Because the process stops at the first method called: I see
> >only "connection added".
>
> That should indicate to you that something is going wrong after that print
> statement is reached.  So examine what your program tries to do after that
> point.  You may want to use a debugger.  You may want to try writing some
> unit tests.
>
> Jean-Paul
>
> ___
> Twisted-Python mailing list
> Twisted-Python@twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>

I looked at the documentation and I understood something about defferd and
callbacks. I edited my code but it does nothing...
I'm going to explain the scenario and what I want to do:
- there is an api that provides some methods (connect, start, disconnect).
- all those methods return a deferred object
- I have an object that first tries to connect, then reads a file and calls
the start method many times in a loop cicle of his method.

I want to use the connect and wait until I'm really connected. How can I do
this using the deferred object that the api returns to me?
I want to do the same thing each time I call the start method from the api:
I want to wait the completing of start operation before doing another start
invocation.

so I do:
in the main process:
d = gest.connettiCLI_to_CTRL()

in gest class I have:
def connettiCLI_to_CTRL(self):
d = api.connect(self.ctrl_ip, self.ctrl_port, self.user, self.pwd)
d.addCallback(self.cbConnected)
return d

def cbConnected(self, connectResult):
return self.postConnection()

def postConnection(self):
# I do something but I don't call api's methods here
# then I go to the next step returning the next method
return self.interpretaNewVMCfg(self.vms_cfg)

def interpretaNewVMCfg(self, new_vms_cfg):
for i in candidati:
#start operation in loop
#here I want to control each start operation
d = api.startVM(new_vm, self.lnms[i])
return d

thank you so much
___
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python