Changeset e58af05
- Timestamp:
- Dec 27, 2011, 11:33:55 PM (12 years ago)
- Branches:
- no-cups
- Children:
- ce2abc5
- Parents:
- 7c143c9
- git-author:
- Jessica B. Hamrick <jhamrick@…> (12/27/11 23:33:55)
- git-committer:
- Jessica B. Hamrick <jhamrick@…> (12/27/11 23:33:55)
- Location:
- server/lib
- Files:
-
- 1 added
- 6 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
server/lib/gutenbach/ipp/attributes/operation.py
ree8e6d0 re58af05 2 2 'PrinterUri', 3 3 'RequestingUserName', 4 'IppAttributeFidelity', 5 'LastDocument' 4 6 ] 5 7 … … 7 9 from .. import Value 8 10 from .. import errors 9 from .. import CharacterStringTags 11 from .. import CharacterStringTags, IntegerTags 10 12 11 13 def PrinterUri(val): … … 21 23 def IppAttributeFidelity(val): 22 24 raise errors.ClientErrorAttributes, 'ipp-attribute-fidelity' 25 26 def LastDocument(val): 27 return Attribute( 28 'last-document', 29 [Value(IntegerTags.BOOLEAN, val)]) -
server/lib/gutenbach/server/__init__.py
ree8e6d0 re58af05 1 from server import GutenbachIPPServer 1 import errors 2 from errors import * 3 __all__ = ['errors'] 4 __all__.extend(errors.__all__) 5 6 from job import Job 7 __all__.append('Job') 8 9 from printer import GutenbachPrinter 10 __all__.append('GutenbachPrinter') 11 12 from requests import make_empty_response, GutenbachRequestHandler 13 __all__.append('make_empty_response') 14 __all__.append('GutenbachRequestHandler') 15 16 from server import GutenbachServer, IPPServer 17 __all__.append('GutenbachServer') 18 __all__.append('IPPServer') 19 2 20 import BaseHTTPServer 3 21 import logging … … 5 23 import traceback 6 24 7 # configure logging25 # configure and initialize logging 8 26 logging.basicConfig(level=logging.DEBUG) 9 10 # initialize logger11 27 logger = logging.getLogger(__name__) 12 28 13 def error(self, request , client_address):29 def error(self, request=None, client_address=None): 14 30 logger.fatal(traceback.format_exc()) 31 self.gutenbach_server.request_stop = True 15 32 sys.exit(1) 16 33 17 34 def start(): 35 logger.info("Starting Gutenbach server...") 36 gutenbach = GutenbachServer() 37 gutenbach.start() 38 39 logger.info("Starting IPP server...") 18 40 server_address = ('', 8000) 19 httpd = BaseHTTPServer.HTTPServer(server_address, GutenbachIPPServer)41 httpd = BaseHTTPServer.HTTPServer(server_address, IPPServer) 20 42 httpd.handle_error = error.__get__(httpd) 21 httpd.serve_forever() 43 httpd.gutenbach_server = gutenbach 44 while gutenbach.isAlive(): 45 try: 46 httpd.handle_request() 47 except: 48 error(httpd) 22 49 23 if __name__ == "__main__": 24 start() 50 __all__.append('start') -
server/lib/gutenbach/server/errors.py
rc16ebf6 re58af05 1 __all__ = [ 2 'InvalidJobException', 3 'InvalidPrinterStateException' 4 ] 5 1 6 class InvalidJobException(Exception): 2 7 def __init__(self, jobid): -
server/lib/gutenbach/server/job.py
ree8e6d0 re58af05 1 from exceptionsimport InvalidJobException, InvalidPrinterStateException1 from . import InvalidJobException, InvalidPrinterStateException 2 2 import os 3 3 import gutenbach.ipp as ipp … … 24 24 This sets the status to 'initializing' and optionally sets the 25 25 document to print to the value of document. 26 26 27 """ 27 28 … … 33 34 self.size = size 34 35 35 self.status = ipp.JobStates.PENDING 36 self.document = None 37 self.document_name = None 38 39 self.status = ipp.JobStates.HELD 36 40 37 41 def __getattr__(self, attr): … … 92 96 93 97 def play(self): 94 if self.status != 'active': 95 raise InvalidJobException( 96 "Cannot play an inactive job!") 97 98 self.status = const.JobStates.PROCESSING 98 logger.info("playing job %s" % str(self)) 99 99 # TODO: add external call to music player 100 print "Playing job %s" % str(self) 100 self.status = ipp.JobStates.PROCESSING 101 101 self.printer.complete_job(self.jid) 102 102 103 103 def finish(self): 104 self.status = const.JobStates.COMPLETE 104 logger.info("finished job %s" % str(self)) 105 self.status = ipp.JobStates.COMPLETE 105 106 106 107 def __repr__(self): -
server/lib/gutenbach/server/printer.py
r7c143c9 re58af05 1 1 #import alsaaudio as aa 2 from . exceptionsimport InvalidJobException, InvalidPrinterStateException3 from . jobimport Job2 from . import InvalidJobException, InvalidPrinterStateException 3 from . import Job 4 4 import gutenbach.ipp as ipp 5 5 import logging … … 221 221 return job 222 222 223 def send_document(self, jobid, document): 224 job = self.jobs[jobid] 225 if job.status != ipp.JobStates.HELD: 226 raise InvalidPrinterStateException( 227 "Invalid job state: %d" % job.status) 228 job.document = document 229 job.status = ipp.JobStates.PENDING 230 223 231 def print_job(self, job): 224 232 pass … … 226 234 def complete_job(self, jobid): 227 235 job = self.jobs[self.active_jobs.pop(0)] 228 if job.jobid != jobid:229 raise InvalidJobException(230 "Completed job %d has unexpected job id %d!" % \231 (job.jobid, jobid))232 233 236 self.finished_jobs.append(job) 234 237 job.finish() 235 return job.j obid238 return job.jid 236 239 237 240 def start_job(self, jobid): 238 241 job = self.jobs[self.active_jobs[0]] 239 if job.jobid != jobid: 240 raise InvalidJobException( 241 "Completed job %d has unexpected job id %d!" % \ 242 (job.jobid, jobid)) 243 244 if job.status == 'playing': 242 if job.status != ipp.JobStates.PENDING: 245 243 raise InvalidPrinterStateException( 246 "Next job in queue (id %d) is " + \ 247 "already playing!" % jobid) 248 244 "Invalid job state: %s" % job.status) 249 245 job.play() 246 247 @property 248 def next_job(self): 249 if len(self.active_jobs) == 0: 250 job = None 251 else: 252 job = self.active_jobs[0] 253 return job 250 254 251 255 def get_job(self, jobid): -
server/lib/gutenbach/server/requests.py
r7c143c9 re58af05 1 from gutenbach.server.printer import GutenbachPrinter2 1 import gutenbach.ipp as ipp 3 2 import logging … … 40 39 class GutenbachRequestHandler(object): 41 40 42 def __init__(self): 43 self.printers = { 44 "test": GutenbachPrinter(name="test") 45 } 46 self.default = "test" 41 def __init__(self, gutenbach_server): 42 self.gutenbach_server = gutenbach_server 43 self.printer = gutenbach_server.printer 47 44 48 45 def generic_handle(self, request): … … 223 220 if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): 224 221 raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) 225 if printer_name not in self.printers:222 if printer_name != self.printer.name: 226 223 raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) 227 jobs = self.printers[printer_name].get_jobs()228 224 229 225 # get the job attributes and add them to the response 230 for job in self.printer s[printer_name].get_jobs():226 for job in self.printer.get_jobs(): 231 227 attrs = job.get_job_attributes(operation) 232 228 response.attribute_groups.append(ipp.AttributeGroup( … … 295 291 if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): 296 292 raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) 297 if printer_name not in self.printers:293 if printer_name != self.printer.name: 298 294 raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) 299 295 300 296 # get attributes from the printer and add to response 301 job = self.printer s[printer_name].create_job(request)297 job = self.printer.create_job(request) 302 298 response.attribute_groups.append(ipp.AttributeGroup( 303 299 ipp.AttributeTags.JOB, job.get_job_attributes(operation))) … … 357 353 if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): 358 354 raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) 359 if printer_name not in self.printers:355 if printer_name != self.printer.name: 360 356 raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) 361 printer = self.printer s[printer_name]357 printer = self.printer 362 358 363 359 # get attributes from the printer and add to response … … 377 373 @handler_for(ipp.OperationCodes.SEND_DOCUMENT) 378 374 def send_document(self, request, response): 379 raise ipp.errors.ServerErrorOperationNotSupported 375 operation = request.attribute_groups[0] 376 377 # requested printer uri 378 if 'printer-uri' in operation: 379 uri_attr = operation['printer-uri'] 380 printer_name = uri_attr.values[0].value.split("/")[-1] 381 if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): 382 raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) 383 if printer_name != self.printer.name: 384 raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) 385 printer = self.printer 386 387 if 'job-id' not in operation: 388 raise ipp.errors.ClientErrorBadRequest("Missing 'job-id' attribute") 389 job_id_attr = operation['job-id'] 390 job_id = job_id_attr.values[0].value 391 if job_id_attr != ipp.JobId(job_id_attr.values[0].value): 392 raise ipp.errors.ClientErrorBadRequest(str(job_id_attr)) 393 if job_id not in printer.jobs: 394 raise ipp.errors.ClientErrorAttributes(str(job_id_attr)) 395 job = printer.jobs[job_id] 396 397 if 'last-document' not in operation: 398 raise ipp.errors.ClientErrorBadRequest("Missing 'last-document' attribute") 399 last_attr = operation['last-document'] 400 last = last_attr.values[0].value 401 if last_attr != ipp.LastDocument(last): 402 raise ipp.errors.ClientErrorBadRequest(str(last_attr)) 403 if not last: 404 raise ipp.errors.ServerErrorMultipleJobsNotSupported 405 406 printer.send_document(job_id, request.data) 407 attrs = job.get_job_attributes() 408 response.attribute_groups.append(ipp.AttributeGroup( 409 ipp.AttributeTags.JOB, attrs)) 380 410 381 411 @handler_for(ipp.OperationCodes.SEND_URI) … … 385 415 @handler_for(ipp.OperationCodes.GET_JOB_ATTRIBUTES) 386 416 def get_job_attributes(self, request, response): 387 388 417 operation = request.attribute_groups[0] 389 418 … … 395 424 if uri_attr != ipp.PrinterUri(uri_attr.values[0].value): 396 425 raise ipp.errors.ClientErrorBadRequest(str(uri_attr)) 397 if printer_name not in self.printers:426 if printer_name != self.printer.name: 398 427 raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr) 399 printer = self.printer s[printer_name]428 printer = self.printer 400 429 401 430 if 'job-id' not in operation: … … 459 488 460 489 operation = request.attribute_groups[0] 461 printer = self.printer s[self.default]490 printer = self.printer 462 491 463 492 # get attributes from the printer and add to response … … 479 508 480 509 # get attributes from the printer and add to response 481 for printer in self.printers.values(): 482 response.attribute_groups.append(ipp.AttributeGroup( 483 ipp.AttributeTags.PRINTER, printer.get_printer_attributes(operation))) 510 response.attribute_groups.append(ipp.AttributeGroup( 511 ipp.AttributeTags.PRINTER, self.printer.get_printer_attributes(operation))) 484 512 485 513 @handler_for(ipp.OperationCodes.CUPS_GET_CLASSES) -
server/lib/gutenbach/server/server.py
r7c143c9 re58af05 1 from gutenbach.server.requests import GutenbachRequestHandler 1 import gutenbach.ipp as ipp 2 3 from . import InvalidPrinterStateException, InvalidJobException 4 from . import GutenbachPrinter 5 from . import GutenbachRequestHandler 6 2 7 import BaseHTTPServer 3 import gutenbach.ipp as ipp4 8 import logging 5 9 import sys 6 10 import traceback 7 11 import tempfile 12 import threading 13 import time 8 14 9 15 # initialize logger 10 16 logger = logging.getLogger(__name__) 11 17 12 # initialize handler 13 handler = GutenbachRequestHandler() 18 class GutenbachServer(threading.Thread): 14 19 15 class GutenbachIPPServer(BaseHTTPServer.BaseHTTPRequestHandler): 20 def run(self): 21 self.printer = GutenbachPrinter(name="test") 22 self.request_stop = False 23 24 while not self.request_stop: 25 job = self.printer.next_job 26 if job is not None: 27 try: 28 self.printer.start_job(job) 29 except InvalidPrinterStateException: 30 pass 31 except: 32 logger.fatal(traceback.format_exc()) 33 sys.exit(1) 34 time.sleep(0.1) 35 36 class IPPServer(BaseHTTPServer.BaseHTTPRequestHandler): 37 16 38 def send_continue(self): 17 39 self.send_response(100, "continue") … … 83 105 # throw a fatal error. 84 106 logger.debug("request: %s" % repr(request)) 85 response = handler.handle(request)107 response = GutenbachRequestHandler(self.server.gutenbach_server).handle(request) 86 108 self.send_ok(response)
Note: See TracChangeset
for help on using the changeset viewer.