|
##############################################################################
|
|
# -*- 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
|
|
# along with ControlIES. If not, see <http://www.gnu.org/licenses/>.
|
|
#
|
|
##############################################################################
|
|
|
|
from Utils.avahiClient import avahiClient
|
|
from Utils import Configs
|
|
from twisted.web import static, server
|
|
from twisted.web.server import Session
|
|
from twisted.python.components import registerAdapter
|
|
import logging,logging.handlers
|
|
import MainLoop
|
|
from zope.interface import Interface, Attribute, implements
|
|
from Plugins.LdapConnection import LdapConnection, ILdapConnection
|
|
import os.path
|
|
|
|
# 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
|
|
|
|
|
|
registerAdapter(LdapConnection, Session, ILdapConnection)
|
|
|
|
# Start up the web service.
|
|
Root = MainLoop.ControlIESProtocol() #Resource object
|
|
Root.PageDir='/home/manu/proyectos/controlies/trunk/www/'
|
|
#Root.PageDir='/home/chisco/Proyectos/controlies/trunk/www'
|
|
site = server.Site(Root)
|
|
|
|
fileNameServers = '/tmp/controlIES.ltpsSevers'
|
|
if os.path.isfile(fileNameServers):
|
|
os.remove(fileNameServers)
|
|
|
|
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()
|
|
|
|
|
|
def _remove_locationServers(self, name, address, port):
|
|
computerToDelete = name.split(" ")
|
|
f = open(fileNameServers, 'r')
|
|
computersList = f.read()
|
|
f.close()
|
|
|
|
computersList = computersList.replace(computerToDelete[0]+" ","")
|
|
|
|
f = open(fileNameServers, 'w')
|
|
f.write(computersList)
|
|
f.close()
|
|
|
|
|
|
def _add_locationTeachers(self, name, address, port):
|
|
teacherToAdd = name.split(" ")
|
|
f = open(fileNameTeachers, 'a')
|
|
f.write(teacherToAdd[0]+" ")
|
|
f.close()
|
|
|
|
|
|
def _remove_locationTeachers(self, name, address, port):
|
|
teacherToDelete = name.split(" ")
|
|
f = open(fileNameTeachers, 'r')
|
|
teachersList = f.read()
|
|
f.close()
|
|
|
|
teachersList = teachersList.replace(teacherToDelete[0]+" ","")
|
|
|
|
f = open(fileNameTeachers, 'w')
|
|
f.write(teachersList)
|
|
f.close()
|
|
|
|
|
|
try:
|
|
_monitor = avahiClient('_workstation._tcp')
|
|
_monitor.add_callback('new-service', _add_locationServers)
|
|
_monitor.add_callback('remove-service', _remove_locationServers)
|
|
_monitor.start()
|
|
except Exception, ex:
|
|
logging.getLogger().debug("Couldn't initialize Avahi monitor: workstations")
|
|
|
|
|
|
try:
|
|
_monitor = avahiClient('_controlaula._tcp')
|
|
_monitor.add_callback('new-service', _add_locationTeachers)
|
|
_monitor.add_callback('remove-service', _remove_locationTeachers)
|
|
_monitor.start()
|
|
except Exception, ex:
|
|
logging.getLogger().debug("Couldn't initialize Avahi monitor: controlaula")
|
|
|
|
|
|
from twisted.internet import reactor
|
|
|
|
reactor.listenTCP(7778,site)
|
|
reactor.run()
|