Ignore:
Timestamp:
Dec 27, 2011, 11:33:55 PM (12 years ago)
Author:
Jessica B. Hamrick <jhamrick@…>
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)
Message:

Implement send-document operation and add threading so that the gutenbach server can play jobs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • server/lib/gutenbach/server/requests.py

    r7c143c9 re58af05  
    1 from gutenbach.server.printer import GutenbachPrinter
    21import gutenbach.ipp as ipp
    32import logging
     
    4039class GutenbachRequestHandler(object):
    4140
    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
    4744
    4845    def generic_handle(self, request):
     
    223220        if uri_attr != ipp.PrinterUri(uri_attr.values[0].value):
    224221            raise ipp.errors.ClientErrorBadRequest(str(uri_attr))
    225         if printer_name not in self.printers:
     222        if printer_name != self.printer.name:
    226223            raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr)
    227         jobs = self.printers[printer_name].get_jobs()
    228224
    229225        # get the job attributes and add them to the response
    230         for job in self.printers[printer_name].get_jobs():
     226        for job in self.printer.get_jobs():
    231227            attrs = job.get_job_attributes(operation)
    232228            response.attribute_groups.append(ipp.AttributeGroup(
     
    295291        if uri_attr != ipp.PrinterUri(uri_attr.values[0].value):
    296292            raise ipp.errors.ClientErrorBadRequest(str(uri_attr))
    297         if printer_name not in self.printers:
     293        if printer_name != self.printer.name:
    298294            raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr)
    299295
    300296        # get attributes from the printer and add to response
    301         job = self.printers[printer_name].create_job(request)
     297        job = self.printer.create_job(request)
    302298        response.attribute_groups.append(ipp.AttributeGroup(
    303299            ipp.AttributeTags.JOB, job.get_job_attributes(operation)))
     
    357353        if uri_attr != ipp.PrinterUri(uri_attr.values[0].value):
    358354            raise ipp.errors.ClientErrorBadRequest(str(uri_attr))
    359         if printer_name not in self.printers:
     355        if printer_name != self.printer.name:
    360356            raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr)
    361         printer = self.printers[printer_name]
     357        printer = self.printer
    362358
    363359        # get attributes from the printer and add to response
     
    377373    @handler_for(ipp.OperationCodes.SEND_DOCUMENT)
    378374    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))
    380410
    381411    @handler_for(ipp.OperationCodes.SEND_URI)
     
    385415    @handler_for(ipp.OperationCodes.GET_JOB_ATTRIBUTES)
    386416    def get_job_attributes(self, request, response):
    387        
    388417        operation = request.attribute_groups[0]
    389418
     
    395424        if uri_attr != ipp.PrinterUri(uri_attr.values[0].value):
    396425            raise ipp.errors.ClientErrorBadRequest(str(uri_attr))
    397         if printer_name not in self.printers:
     426        if printer_name != self.printer.name:
    398427            raise ipp.errors.ClientErrorAttributes(str(uri_attr), uri_attr)
    399         printer = self.printers[printer_name]
     428        printer = self.printer
    400429
    401430        if 'job-id' not in operation:
     
    459488
    460489        operation = request.attribute_groups[0]
    461         printer = self.printers[self.default]
     490        printer = self.printer
    462491
    463492        # get attributes from the printer and add to response
     
    479508
    480509        # 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)))
    484512
    485513    @handler_for(ipp.OperationCodes.CUPS_GET_CLASSES)
Note: See TracChangeset for help on using the changeset viewer.