Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / _win32stdio.py
1 # -*- test-case-name: twisted.test.test_stdio -*-
2
3 """
4 Windows-specific implementation of the L{twisted.internet.stdio} interface.
5 """
6
7 import win32api
8 import os, msvcrt
9
10 from zope.interface import implements
11
12 from twisted.internet.interfaces import IHalfCloseableProtocol, ITransport, IAddress
13 from twisted.internet.interfaces import IConsumer, IPushProducer
14
15 from twisted.internet import _pollingfile, main
16 from twisted.python.failure import Failure
17
18
19 class Win32PipeAddress(object):
20     implements(IAddress)
21
22
23
24 class StandardIO(_pollingfile._PollingTimer):
25
26     implements(ITransport,
27                IConsumer,
28                IPushProducer)
29
30     disconnecting = False
31     disconnected = False
32
33     def __init__(self, proto):
34         """
35         Start talking to standard IO with the given protocol.
36
37         Also, put it stdin/stdout/stderr into binary mode.
38         """
39         from twisted.internet import reactor
40
41         for stdfd in range(0, 1, 2):
42             msvcrt.setmode(stdfd, os.O_BINARY)
43
44         _pollingfile._PollingTimer.__init__(self, reactor)
45         self.proto = proto
46
47         hstdin = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)
48         hstdout = win32api.GetStdHandle(win32api.STD_OUTPUT_HANDLE)
49
50         self.stdin = _pollingfile._PollableReadPipe(
51             hstdin, self.dataReceived, self.readConnectionLost)
52
53         self.stdout = _pollingfile._PollableWritePipe(
54             hstdout, self.writeConnectionLost)
55
56         self._addPollableResource(self.stdin)
57         self._addPollableResource(self.stdout)
58
59         self.proto.makeConnection(self)
60
61     def dataReceived(self, data):
62         self.proto.dataReceived(data)
63
64     def readConnectionLost(self):
65         if IHalfCloseableProtocol.providedBy(self.proto):
66             self.proto.readConnectionLost()
67         self.checkConnLost()
68
69     def writeConnectionLost(self):
70         if IHalfCloseableProtocol.providedBy(self.proto):
71             self.proto.writeConnectionLost()
72         self.checkConnLost()
73
74     connsLost = 0
75
76     def checkConnLost(self):
77         self.connsLost += 1
78         if self.connsLost >= 2:
79             self.disconnecting = True
80             self.disconnected = True
81             self.proto.connectionLost(Failure(main.CONNECTION_DONE))
82
83     # ITransport
84
85     def write(self, data):
86         self.stdout.write(data)
87
88     def writeSequence(self, seq):
89         self.stdout.write(''.join(seq))
90
91     def loseConnection(self):
92         self.disconnecting = True
93         self.stdin.close()
94         self.stdout.close()
95
96     def getPeer(self):
97         return Win32PipeAddress()
98
99     def getHost(self):
100         return Win32PipeAddress()
101
102     # IConsumer
103
104     def registerProducer(self, producer, streaming):
105         return self.stdout.registerProducer(producer, streaming)
106
107     def unregisterProducer(self):
108         return self.stdout.unregisterProducer()
109
110     # def write() above
111
112     # IProducer
113
114     def stopProducing(self):
115         self.stdin.stopProducing()
116
117     # IPushProducer
118
119     def pauseProducing(self):
120         self.stdin.pauseProducing()
121
122     def resumeProducing(self):
123         self.stdin.resumeProducing()
124