def SetServices(self, services):

        # doesn't need the dirty_object_lock because the caller takes it

        # first test available ports

        my_ports = {s.GetPort() for s in self._services}

        for service in services:

            port = service.GetPort()

            if port not in my_ports and HydrusNetworking.LocalPortInUse(port):

                raise HydrusExceptions.ServerException(
                    'Something was already bound to port ' + str(port))

        #

        self._services = services

        self.CallToThread(self.services_upnp_manager.SetServices,
                          self._services)

        [self._admin_service] = [
            service for service in self._services
            if service.GetServiceType() == HC.SERVER_ADMIN
        ]

        self.SetRunningTwistedServices(self._services)
    def InitView(self):

        HydrusController.HydrusController.InitView(self)

        port = self._admin_service.GetPort()

        if HydrusNetworking.LocalPortInUse(port):

            HydrusData.Print(
                'Something is already bound to port ' + str(port) +
                ', so your administration service cannot be started. Please quit the server and retry once the port is clear.'
            )

        else:

            self.SetRunningTwistedServices(self._services)

        #

        job = self.CallRepeating(5.0, 600.0, self.SyncRepositories)

        self._daemon_jobs['sync_repositories'] = job

        job = self.CallRepeating(0.0, 30.0, self.SaveDirtyObjects)

        self._daemon_jobs['save_dirty_objects'] = job

        job = self.CallRepeating(0.0, 86400.0, self.DeleteOrphans)

        self._daemon_jobs['delete_orphans'] = job
            def StartServices(*args, **kwargs):

                HydrusData.Print('Starting services\u2026')

                for service in services:

                    service_key = service.GetServiceKey()
                    service_type = service.GetServiceType()

                    try:

                        port = service.GetPort()

                        if service_type == HC.SERVER_ADMIN:

                            http_factory = ServerServer.HydrusServiceAdmin(
                                service)

                        elif service_type == HC.FILE_REPOSITORY:

                            http_factory = ServerServer.HydrusServiceRepositoryFile(
                                service)

                        elif service_type == HC.TAG_REPOSITORY:

                            http_factory = ServerServer.HydrusServiceRepositoryTag(
                                service)

                        else:

                            return

                        (ssl_cert_path, ssl_key_path) = self.db.GetSSLPaths()

                        sslmethod = twisted.internet.ssl.SSL.TLSv1_2_METHOD

                        context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory(
                            ssl_key_path, ssl_cert_path, sslmethod)

                        ipv6_port = None

                        try:

                            ipv6_port = reactor.listenSSL(port,
                                                          http_factory,
                                                          context_factory,
                                                          interface='::')

                        except Exception as e:

                            HydrusData.Print('Could not bind to IPv6:')

                            HydrusData.Print(str(e))

                        ipv4_port = None

                        try:

                            ipv4_port = reactor.listenSSL(
                                port, http_factory, context_factory)

                        except:

                            if ipv6_port is None:

                                raise

                        self._service_keys_to_connected_ports[service_key] = (
                            ipv4_port, ipv6_port)

                        if not HydrusNetworking.LocalPortInUse(port):

                            raise Exception(
                                'Tried to bind port {} for "{}" but it failed.'
                                .format(port, service.GetName()))

                    except Exception as e:

                        HydrusData.Print(traceback.format_exc())

                HydrusData.Print('Services started')