<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12043] CalendarServer/trunk/twext/application/masterchild.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/12043">12043</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2013-12-09 14:01:25 -0800 (Mon, 09 Dec 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Change args to allow for multiple protocols in the future.
Define a MasterService.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwextapplicationmasterchildpy">CalendarServer/trunk/twext/application/masterchild.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwextapplicationmasterchildpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twext/application/masterchild.py (12042 => 12043)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twext/application/masterchild.py        2013-12-07 05:24:08 UTC (rev 12042)
+++ CalendarServer/trunk/twext/application/masterchild.py        2013-12-09 22:01:25 UTC (rev 12043)
</span><span class="lines">@@ -47,6 +47,7 @@
</span><span class="cx"> from twisted.application.service import IServiceMaker
</span><span class="cx"> from twisted.application.internet import TCPServer
</span><span class="cx"> from twisted.protocols.policies import WrappingFactory, ProtocolWrapper
</span><ins>+from twisted.internet.protocol import Protocol
</ins><span class="cx"> from twisted.internet.protocol import ServerFactory
</span><span class="cx"> from twisted.internet.protocol import ProcessProtocol
</span><span class="cx"> 
</span><span class="lines">@@ -67,23 +68,32 @@
</span><span class="cx"> 
</span><span class="cx">     def opt_protocol(self, value):
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        Protocol
</del><ins>+        Protocol and port (specify as proto:port).
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         try:
</span><del>-            namedClass(value)
</del><ins>+            protocol, port = value.split(&quot;:&quot;)
+        except ValueError:
+            if &quot;:&quot; in value:
+                raise UsageError(&quot;Invalid protocol argument.&quot;)
+            else:
+                raise UsageError(&quot;Port is required in protocol argument.&quot;)
+
+        # Validate protocol name
+        try:
+            protocolClass = namedClass(protocol)
</ins><span class="cx">         except (ValueError, AttributeError):
</span><del>-            raise UsageError(&quot;Unknown protocol: {0}&quot;.format(value))
</del><ins>+            raise UsageError(&quot;Unknown protocol: {0}&quot;.format(protocol))
</ins><span class="cx"> 
</span><del>-        self[&quot;protocol&quot;] = value
</del><ins>+        try:
+            if not issubclass(protocolClass, Protocol):
+                raise TypeError()
+        except TypeError:
+            raise UsageError(&quot;Not a protocol: {0}&quot;.format(protocol))
</ins><span class="cx"> 
</span><del>-
-    def opt_port(self, value):
-        &quot;&quot;&quot;
-        Inherited file descriptor
-        &quot;&quot;&quot;
</del><ins>+        # Validate port number
</ins><span class="cx">         try:
</span><span class="cx">             try:
</span><del>-                port = int(value)
</del><ins>+                port = int(port)
</ins><span class="cx">             except ValueError:
</span><span class="cx">                 raise ValueError(&quot;not an integer&quot;)
</span><span class="cx"> 
</span><span class="lines">@@ -92,14 +102,31 @@
</span><span class="cx"> 
</span><span class="cx">         except ValueError as e:
</span><span class="cx">             raise UsageError(
</span><del>-                &quot;Invalid port number {0!r}: {1}&quot;.format(value, e)
</del><ins>+                &quot;Invalid port number {0}: {1}&quot;.format(port, e)
</ins><span class="cx">             )
</span><span class="cx"> 
</span><del>-        self[&quot;port&quot;] = port
</del><ins>+        protocols = self.setdefault(&quot;protocol&quot;, [])
</ins><span class="cx"> 
</span><ins>+        for (otherProtocol, otherPort) in protocols:
+            # FIXME: Raise here because we don't properly handle multiple
+            # protocols yet.
+            raise UsageError(&quot;Only one protocol may be specified.&quot;)
</ins><span class="cx"> 
</span><ins>+            if otherPort == port:
+                if otherProtocol == protocol:
+                    return
+
+                raise UsageError(
+                    &quot;Port {0} cannot be registered more than once &quot;
+                    &quot;for different protocols: ({1}, {2})&quot;,
+                    otherProtocol, protocol
+                )
+
+        protocols.append((protocol, port))
+
+
</ins><span class="cx">     def postOptions(self):
</span><del>-        for parameter in (&quot;protocol&quot;, &quot;port&quot;):
</del><ins>+        for parameter in (&quot;protocol&quot;,):
</ins><span class="cx">             if parameter not in self:
</span><span class="cx">                 raise UsageError(&quot;{0} parameter is required&quot;.format(parameter))
</span><span class="cx"> 
</span><span class="lines">@@ -123,31 +150,119 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-@implementer(IServiceMaker)
-class MasterServiceMaker(object):
</del><ins>+@implementer(IStatusWatcher)
+class MasterService(MultiService, object):
</ins><span class="cx">     &quot;&quot;&quot;
</span><del>-    Master process service maker.
</del><ins>+    Service for master processes.
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    def makeService(self, options):
-        service = MultiService()
</del><ins>+    log = Logger()
</ins><span class="cx"> 
</span><ins>+
+    def __init__(self):
+        MultiService.__init__(self)
+
</ins><span class="cx">         # Dispatcher
</span><del>-        statusWatcher = StatusWatcher()
-        dispatcher = InheritedSocketDispatcher(statusWatcher)
</del><ins>+        self.dispatcher = InheritedSocketDispatcher(self)
</ins><span class="cx"> 
</span><span class="cx">         # Child Processes
</span><del>-        spawningService = ChildSpawningService(dispatcher)
-        spawningService.setServiceParent(service)
</del><ins>+        log.info(&quot;Setting up master/child spawning service...&quot;)
+        self.spawningService = ChildSpawningService(self.dispatcher)
+        self.spawningService.setServiceParent(self)
</ins><span class="cx"> 
</span><ins>+
+    def addProtocol(self, protocol, port):
+        log.info(
+            &quot;Setting service for protocol {protocol!r} on port {port}...&quot;,
+            protocol=protocol, port=port,
+        )
+
</ins><span class="cx">         # TCP Service
</span><span class="cx">         tcpFactory = SpawningInheritingProtocolFactory(
</span><del>-            dispatcher, spawningService, options[&quot;protocol&quot;]
</del><ins>+            self.dispatcher, self.spawningService, protocol
</ins><span class="cx">         )
</span><del>-        tcpService = TCPServer(options[&quot;port&quot;], tcpFactory)
</del><ins>+        tcpService = TCPServer(port, tcpFactory)
</ins><span class="cx"> 
</span><del>-        tcpService.setServiceParent(service)
</del><ins>+        tcpService.setServiceParent(self)
</ins><span class="cx"> 
</span><ins>+
+    def startService(self):
+        &quot;&quot;&quot;
+        Start up multiservice, then start up the dispatcher.
+        &quot;&quot;&quot;
+        super(MasterService, self).startService()
+        self.dispatcher.startDispatching()
+
+
+    # IStatusWatcher
+
+    @staticmethod
+    def initialStatus():
+        log.info(&quot;Status: init&quot;)
+        return ChildStatus(sentCount=0, ackedCount=0)
+
+
+    @staticmethod
+    def newConnectionStatus(previousStatus):
+        log.info(&quot;Status: {0} new&quot;.format(previousStatus))
+        return ChildStatus(
+            sentCount=previousStatus.sentCount + 1,
+            ackedCount=previousStatus.ackedCount,
+        )
+
+
+    @staticmethod
+    def statusFromMessage(previousStatus, message):
+        log.info(&quot;Status: {0}{1!r}&quot;.format(previousStatus, message))
+        if message == b&quot;-&quot;:
+            return ChildStatus(
+                sentCount=previousStatus.sentCount - 1,
+                ackedCount=previousStatus.ackedCount,
+            )
+        elif message == b&quot;+&quot;:
+            return ChildStatus(
+                sentCount=previousStatus.sentCount,
+                ackedCount=previousStatus.ackedCount + 1,
+            )
+        else:
+            raise AssertionError(&quot;Unknown message: {}&quot;.format(message))
+
+
+    @staticmethod
+    def closeCountFromStatus(previousStatus):
+        log.info(&quot;Status: {0} close&quot;.format(previousStatus))
+        return (
+            previousStatus.ackedCount,
+            ChildStatus(
+                sentCount=previousStatus.sentCount,
+                ackedCount=0,
+            )
+        )
+
+
+    @staticmethod
+    def statusesChanged(statuses):
+        log.info(&quot;Status changed: {0}&quot;.format(tuple(statuses)))
+        # FIXME: This isn't in IStatusWatcher, but is called by
+        # InheritedSocketDispatcher.
+        pass
+
+
+
+@implementer(IServiceMaker)
+class MasterServiceMaker(object):
+    &quot;&quot;&quot;
+    Master process service maker.
+    &quot;&quot;&quot;
+    log = Logger()
+
+
+    def makeService(self, options):
+        service = MasterService()
+
+        for protocol, port in options[&quot;protocol&quot;]:
+            service.addProtocol(protocol, port)
+
</ins><span class="cx">         return service
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -444,69 +559,10 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class Status(object):
</del><ins>+class ChildStatus(object):
</ins><span class="cx">     def __init__(self, sentCount, ackedCount):
</span><span class="cx">         self.sentCount = sentCount
</span><span class="cx">         self.ackedCount = ackedCount
</span><span class="cx"> 
</span><span class="cx">     def __repr__(self):
</span><span class="cx">         return &quot;({self.sentCount},{self.ackedCount})&quot;.format(self=self)
</span><del>-
-
-
-@implementer(IStatusWatcher)
-class StatusWatcher(object):
-    &quot;&quot;&quot;
-    This enables the dispatcher to keep track of how many connections are in
-    flight for each child.
-    &quot;&quot;&quot;
-    @staticmethod
-    def initialStatus():
-        log.info(&quot;Status: init&quot;)
-        return Status(sentCount=0, ackedCount=0)
-
-
-    @staticmethod
-    def newConnectionStatus(previousStatus):
-        log.info(&quot;Status: {0} new&quot;.format(previousStatus))
-        return Status(
-            sentCount=previousStatus.sentCount + 1,
-            ackedCount=previousStatus.ackedCount,
-        )
-
-
-    @staticmethod
-    def statusFromMessage(previousStatus, message):
-        log.info(&quot;Status: {0}{1!r}&quot;.format(previousStatus, message))
-        if message == b&quot;-&quot;:
-            return Status(
-                sentCount=previousStatus.sentCount - 1,
-                ackedCount=previousStatus.ackedCount,
-            )
-        elif message == b&quot;+&quot;:
-            return Status(
-                sentCount=previousStatus.sentCount,
-                ackedCount=previousStatus.ackedCount + 1,
-            )
-        else:
-            raise AssertionError(&quot;Unknown message: {}&quot;.format(message))
-
-
-    @staticmethod
-    def closeCountFromStatus(previousStatus):
-        log.info(&quot;Status: {0} close&quot;.format(previousStatus))
-        return (
-            previousStatus.ackedCount,
-            Status(
-                sentCount=previousStatus.sentCount,
-                ackedCount=0,
-            )
-        )
-
-
-    @staticmethod
-    def statusesChanged(statuses):
-        log.info(&quot;Status changed: {0}&quot;.format(tuple(statuses)))
-        # FIXME: This isn't in IStatusWatcher, but is called by
-        # InheritedSocketDispatcher.
-        pass
</del></span></pre>
</div>
</div>

</body>
</html>