root/controlies/trunk/Server.py @ 170
61 | manumora | ##############################################################################
|
|
# -*- coding: utf-8 -*-
|
|||
# Project: ControlIES
|
|||
# Module: Server.py
|
|||
# Purpose: ControlIES web server
|
|||
# Language: Python 2.5
|
|||
# Date: 7-Feb-2011.
|
|||
# Ver: 7-Feb-2011.
|
|||
# Author: Manuel Mora Gordillo
|
|||
# Copyright: 2011 - Manuel Mora Gordillo <manuito @no-spam@ gmail.com>
|
|||
#
|
|||
# ControlIES is free software: you can redistribute it and/or modify
|
|||
# it under the terms of the GNU General Public License as published by
|
|||
# the Free Software Foundation, either version 3 of the License, or
|
|||
# (at your option) any later version.
|
|||
# ControlIES is distributed in the hope that it will be useful,
|
|||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|||
# GNU General Public License for more details.
|
|||
# You should have received a copy of the GNU General Public License
|
|||
91 | manumora | # along with ControlIES. If not, see <http://www.gnu.org/licenses/>.
|
|
61 | manumora | #
|
|
##############################################################################
|
|||
91 | manumora | from Utils.avahiClient import avahiClient
|
|
97 | manumora | from Utils import Configs
|
|
61 | manumora | from twisted.web import static, server
|
|
from twisted.web.server import Session
|
|||
from twisted.python.components import registerAdapter
|
|||
97 | manumora | import logging,logging.handlers
|
|
61 | manumora | import MainLoop
|
|
from zope.interface import Interface, Attribute, implements
|
|||
from Plugins.LdapConnection import LdapConnection, ILdapConnection
|
|||
91 | manumora | import os.path
|
|
61 | manumora | ||
97 | manumora | # Logging
|
|
log_handler = logging.handlers.RotatingFileHandler(Configs.LOG_FILENAME, maxBytes=100000, backupCount=5)
|
|||
log_formatter = logging.Formatter(fmt='%(asctime)s %(levelname)-8s %(message)s',datefmt='%a, %d %b %Y %H:%M:%S')
|
|||
log_handler.setFormatter(log_formatter)
|
|||
root_logger=logging.getLogger()
|
|||
root_logger.addHandler(log_handler)
|
|||
root_logger.level=logging.DEBUG
|
|||
91 | manumora | ||
97 | manumora | ||
61 | manumora | registerAdapter(LdapConnection, Session, ILdapConnection)
|
|
# Start up the web service.
|
|||
Root = MainLoop.ControlIESProtocol() #Resource object
|
|||
141 | manumora | Root.PageDir='/home/manu/proyectos/controlies/trunk/www/'
|
|
#Root.PageDir='/home/chisco/Proyectos/controlies/trunk/www'
|
|||
61 | manumora | site = server.Site(Root)
|
|
93 | manumora | fileNameServers = '/tmp/controlIES.ltpsSevers'
|
|
if os.path.isfile(fileNameServers):
|
|||
os.remove(fileNameServers)
|
|||
61 | manumora | ||
93 | manumora | f = open(fileNameServers, 'w')
|
|
fileNameTeachers = '/tmp/controlIES.ltpsTeachers'
|
|||
if os.path.isfile(fileNameTeachers):
|
|||
os.remove(fileNameTeachers)
|
|||
f = open(fileNameTeachers, 'w')
|
|||
def _add_locationServers(self, name, address, port):
|
|||
computerToAdd = name.split(" ")
|
|||
f = open(fileNameServers, 'a')
|
|||
f.write(computerToAdd[0]+" ")
|
|||
f.close()
|
|||
91 | manumora | ||
93 | manumora | def _remove_locationServers(self, name, address, port):
|
|
computerToDelete = name.split(" ")
|
|||
f = open(fileNameServers, 'r')
|
|||
computersList = f.read()
|
|||
f.close()
|
|||
91 | manumora | ||
96 | manumora | computersList = computersList.replace(computerToDelete[0]+" ","")
|
|
93 | manumora | ||
f = open(fileNameServers, 'w')
|
|||
f.write(computersList)
|
|||
f.close()
|
|||
91 | manumora | ||
93 | manumora | ||
def _add_locationTeachers(self, name, address, port):
|
|||
teacherToAdd = name.split(" ")
|
|||
f = open(fileNameTeachers, 'a')
|
|||
f.write(teacherToAdd[0]+" ")
|
|||
91 | manumora | f.close()
|
|
93 | manumora | def _remove_locationTeachers(self, name, address, port):
|
|
teacherToDelete = name.split(" ")
|
|||
f = open(fileNameTeachers, 'r')
|
|||
teachersList = f.read()
|
|||
f.close()
|
|||
91 | manumora | ||
96 | manumora | teachersList = teachersList.replace(teacherToDelete[0]+" ","")
|
|
93 | manumora | ||
f = open(fileNameTeachers, 'w')
|
|||
f.write(teachersList)
|
|||
f.close()
|
|||
91 | manumora | ||
93 | manumora | try:
|
|
_monitor = avahiClient('_workstation._tcp')
|
|||
_monitor.add_callback('new-service', _add_locationServers)
|
|||
_monitor.add_callback('remove-service', _remove_locationServers)
|
|||
_monitor.start()
|
|||
except Exception, ex:
|
|||
97 | manumora | logging.getLogger().debug("Couldn't initialize Avahi monitor: workstations")
|
|
91 | manumora | ||
93 | manumora | ||
91 | manumora | try:
|
|
93 | manumora | _monitor = avahiClient('_controlaula._tcp')
|
|
_monitor.add_callback('new-service', _add_locationTeachers)
|
|||
_monitor.add_callback('remove-service', _remove_locationTeachers)
|
|||
97 | manumora | _monitor.start()
|
|
91 | manumora | except Exception, ex:
|
|
97 | manumora | logging.getLogger().debug("Couldn't initialize Avahi monitor: controlaula")
|
|
91 | manumora | ||
93 | manumora | ||
91 | manumora | from twisted.internet import reactor
|
|
61 | manumora | reactor.listenTCP(7778,site)
|
|
reactor.run()
|