Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / recvfd.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Client-side of an example for sending file descriptors between processes over
6 UNIX sockets.  This client connects to a server listening on a UNIX socket and
7 waits for one file descriptor to arrive over the connection.  It displays the
8 name of the file and the first 80 bytes it contains, then exits.
9
10 To runb this example, run this program with one argument: a path giving the UNIX
11 socket the server side of this example is already listening on.  For example:
12
13     $ python recvfd.py /tmp/sendfd.sock
14
15 See sendfd.py for the server side of this example.
16 """
17
18 if __name__ == '__main__':
19     import recvfd
20     raise SystemExit(recvfd.main())
21
22 import os, sys
23
24 from zope.interface import implements
25
26 from twisted.python.log import startLogging
27 from twisted.python.filepath import FilePath
28 from twisted.internet.defer import Deferred
29 from twisted.internet.interfaces import IFileDescriptorReceiver
30 from twisted.internet.protocol import Factory
31 from twisted.protocols.basic import LineOnlyReceiver
32 from twisted.internet.endpoints import UNIXClientEndpoint
33 from twisted.internet import reactor
34
35 class ReceiveFDProtocol(LineOnlyReceiver):
36     implements(IFileDescriptorReceiver)
37
38     descriptor = None
39
40     def __init__(self):
41         self.whenDisconnected = Deferred()
42
43
44     def fileDescriptorReceived(self, descriptor):
45         # Record the descriptor sent to us
46         self.descriptor = descriptor
47
48
49     def lineReceived(self, line):
50         if self.descriptor is None:
51             print "Received %r without receiving descriptor!" % (line,)
52         else:
53             # Use the previously received descriptor, along with the newly
54             # provided information about which file it is, to present some
55             # information to the user.
56             data = os.read(self.descriptor, 80)
57             print "Received %r from the server." % (line,)
58             print "First 80 bytes are:\n%r\n" % (data,)
59         os.close(self.descriptor)
60         self.transport.loseConnection()
61
62
63     def connectionLost(self, reason):
64         self.whenDisconnected.callback(None)
65
66
67
68 def main():
69     address = FilePath(sys.argv[1])
70
71     startLogging(sys.stdout)
72
73     factory = Factory()
74     factory.protocol = ReceiveFDProtocol
75     factory.quiet = True
76
77     endpoint = UNIXClientEndpoint(reactor, address.path)
78     connected = endpoint.connect(factory)
79
80     def succeeded(client):
81         return client.whenDisconnected
82     def failed(reason):
83         print "Could not connect:", reason.getErrorMessage()
84     def disconnected(ignored):
85         reactor.stop()
86
87     connected.addCallbacks(succeeded, failed)
88     connected.addCallback(disconnected)
89
90     reactor.run()