/
printermanager.go
473 lines (396 loc) · 12.9 KB
/
printermanager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
Copyright 2015 Google Inc. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
package manager
import (
"fmt"
"hash/adler32"
"os"
"reflect"
"strings"
"sync"
"time"
"github.com/POTION4/cloud-print-connector/cdd"
"github.com/POTION4/cloud-print-connector/gcp"
"github.com/POTION4/cloud-print-connector/lib"
"github.com/POTION4/cloud-print-connector/log"
"github.com/POTION4/cloud-print-connector/notification"
"github.com/POTION4/cloud-print-connector/privet"
"github.com/POTION4/cloud-print-connector/xmpp"
)
type NativePrintSystem interface {
GetPrinters() ([]lib.Printer, error)
GetJobState(printerName string, jobID uint32) (*cdd.PrintJobStateDiff, error)
Print(printer *lib.Printer, fileName, title, user, gcpJobID string, ticket *cdd.CloudJobTicket) (uint32, error)
ReleaseJob(printerName string, jobID uint32) error
RemoveCachedPPD(printerName string)
}
// Manages state and interactions between the native print system and Google Cloud Print.
type PrinterManager struct {
native NativePrintSystem
gcp *gcp.GoogleCloudPrint
xmpp *xmpp.XMPP
privet *privet.Privet
printers *lib.ConcurrentPrinterMap
// Job stats are numbers reported to monitoring.
jobStatsMutex sync.Mutex
jobsDone uint
jobsError uint
// Jobs in flight are jobs that have been received, and are not
// finished printing yet. Key is Job ID.
jobsInFlightMutex sync.Mutex
jobsInFlight map[string]struct{}
nativeJobQueueSize uint
jobFullUsername bool
shareScope string
quit chan struct{}
useFcm bool
}
func NewPrinterManager(native NativePrintSystem, gcp *gcp.GoogleCloudPrint, privet *privet.Privet, printerPollInterval time.Duration, nativeJobQueueSize uint, jobFullUsername bool, shareScope string, jobs <-chan *lib.Job, notifications <-chan notification.PrinterNotification, useFcm bool) (*PrinterManager, error) {
var printers *lib.ConcurrentPrinterMap
var queuedJobsCount map[string]uint
var err error
if gcp != nil {
// Get all GCP printers.
var gcpPrinters []lib.Printer
gcpPrinters, queuedJobsCount, err = gcp.ListPrinters()
if err != nil {
return nil, err
}
// Organize the GCP printers into a map.
for i := range gcpPrinters {
gcpPrinters[i].NativeJobSemaphore = lib.NewSemaphore(nativeJobQueueSize)
}
printers = lib.NewConcurrentPrinterMap(gcpPrinters)
} else {
printers = lib.NewConcurrentPrinterMap(nil)
}
// Construct.
pm := PrinterManager{
native: native,
gcp: gcp,
privet: privet,
printers: printers,
jobStatsMutex: sync.Mutex{},
jobsDone: 0,
jobsError: 0,
jobsInFlightMutex: sync.Mutex{},
jobsInFlight: make(map[string]struct{}),
nativeJobQueueSize: nativeJobQueueSize,
jobFullUsername: jobFullUsername,
shareScope: shareScope,
quit: make(chan struct{}),
useFcm: useFcm,
}
// Sync once before returning, to make sure things are working.
// Ignore privet updates this first time because Privet always starts
// with zero printers.
if err = pm.SyncPrinters(true); err != nil {
return nil, err
}
// Initialize Privet printers.
if privet != nil {
for _, printer := range pm.printers.GetAll() {
err := privet.AddPrinter(printer, pm.printers.GetByNativeName)
if err != nil {
log.WarningPrinterf(printer.Name, "Failed to register locally: %s", err)
} else {
log.InfoPrinterf(printer.Name, "Registered locally")
}
}
}
pm.syncPrintersPeriodically(printerPollInterval)
pm.listenNotifications(jobs, notifications)
if gcp != nil {
for gcpPrinterID := range queuedJobsCount {
p, _ := printers.GetByGCPID(gcpPrinterID)
go gcp.HandleJobs(&p, func() { pm.incrementJobsProcessed(false) })
}
}
return &pm, nil
}
func (pm *PrinterManager) Quit() {
close(pm.quit)
}
func (pm *PrinterManager) syncPrintersPeriodically(interval time.Duration) {
go func() {
t := time.NewTimer(interval)
defer t.Stop()
for {
select {
case <-t.C:
if err := pm.SyncPrinters(false); err != nil {
log.Error(err)
}
t.Reset(interval)
case <-pm.quit:
return
}
}
}()
}
func (pm *PrinterManager) SyncPrinters(ignorePrivet bool) error {
log.Debug("Synchronizing printers, stand by")
// Get current snapshot of native printers.
nativePrinters, err := pm.native.GetPrinters()
if err != nil {
return fmt.Errorf("Sync failed while calling GetPrinters(): %s", err)
}
// Set CapsHash on all printers.
for i := range nativePrinters {
h := adler32.New()
lib.DeepHash(nativePrinters[i].Tags, h)
nativePrinters[i].Tags["tagshash"] = fmt.Sprintf("%x", h.Sum(nil))
h = adler32.New()
lib.DeepHash(nativePrinters[i].Description, h)
nativePrinters[i].CapsHash = fmt.Sprintf("%x", h.Sum(nil))
if pm.useFcm {
nativePrinters[i].NotificationChannel = gcp.FCP_CHANNEL
} else {
nativePrinters[i].NotificationChannel = gcp.XMPP_CHANNEL
}
}
// Compare the snapshot to what we know currently.
diffs := lib.DiffPrinters(nativePrinters, pm.printers.GetAll())
if diffs == nil {
log.Debugf("Printers are already in sync; there are %d", len(nativePrinters))
return nil
}
// Update GCP.
ch := make(chan lib.Printer, len(diffs))
for i := range diffs {
go pm.applyDiff(&diffs[i], ch, ignorePrivet)
}
currentPrinters := make([]lib.Printer, 0, len(diffs))
for _ = range diffs {
p := <-ch
if p.Name != "" {
currentPrinters = append(currentPrinters, p)
}
}
// Update what we know.
pm.printers.Refresh(currentPrinters)
log.Debugf("Finished synchronizing %d printers", len(currentPrinters))
return nil
}
func (pm *PrinterManager) applyDiff(diff *lib.PrinterDiff, ch chan<- lib.Printer, ignorePrivet bool) {
switch diff.Operation {
case lib.RegisterPrinter:
if pm.gcp != nil {
if err := pm.gcp.Register(&diff.Printer); err != nil {
log.ErrorPrinterf(diff.Printer.Name, "Failed to register: %s", err)
break
}
log.InfoPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Registered in the cloud")
if pm.gcp.CanShare() {
if err := pm.gcp.Share(diff.Printer.GCPID, pm.shareScope, gcp.User, true, false); err != nil {
log.ErrorPrinterf(diff.Printer.Name, "Failed to share: %s", err)
} else {
log.InfoPrinterf(diff.Printer.Name, "Shared")
}
}
}
diff.Printer.NativeJobSemaphore = lib.NewSemaphore(pm.nativeJobQueueSize)
if pm.privet != nil && !ignorePrivet {
err := pm.privet.AddPrinter(diff.Printer, pm.printers.GetByNativeName)
if err != nil {
log.WarningPrinterf(diff.Printer.Name, "Failed to register locally: %s", err)
} else {
log.InfoPrinterf(diff.Printer.Name, "Registered locally")
}
}
ch <- diff.Printer
return
case lib.UpdatePrinter:
if pm.gcp != nil {
if err := pm.gcp.Update(diff); err != nil {
log.ErrorPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Failed to update: %s", err)
} else {
log.InfoPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Updated in the cloud")
}
}
if pm.privet != nil && !ignorePrivet && diff.DefaultDisplayNameChanged {
err := pm.privet.UpdatePrinter(diff)
if err != nil {
log.WarningPrinterf(diff.Printer.Name, "Failed to update locally: %s", err)
} else {
log.InfoPrinterf(diff.Printer.Name, "Updated locally")
}
}
ch <- diff.Printer
return
case lib.DeletePrinter:
pm.native.RemoveCachedPPD(diff.Printer.Name)
if pm.gcp != nil {
if err := pm.gcp.Delete(diff.Printer.GCPID); err != nil {
log.ErrorPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Failed to delete from the cloud: %s", err)
break
}
log.InfoPrinterf(diff.Printer.Name+" "+diff.Printer.GCPID, "Deleted from the cloud")
}
if pm.privet != nil && !ignorePrivet {
err := pm.privet.DeletePrinter(diff.Printer.Name)
if err != nil {
log.WarningPrinterf(diff.Printer.Name, "Failed to delete: %s", err)
} else {
log.InfoPrinterf(diff.Printer.Name, "Deleted locally")
}
}
case lib.NoChangeToPrinter:
ch <- diff.Printer
return
}
ch <- lib.Printer{}
}
// listenNotifications handles the messages found on the channels.
func (pm *PrinterManager) listenNotifications(jobs <-chan *lib.Job, messages <-chan notification.PrinterNotification) {
go func() {
for {
select {
case <-pm.quit:
return
case job := <-jobs:
log.DebugJobf(job.JobID, "Received job: %+v", job)
go pm.printJob(job.NativePrinterName, job.Filename, job.Title, job.User, job.JobID, job.Ticket, job.UpdateJob)
case message := <-messages:
log.Debugf("Received message: %+v", message)
if message.Type == notification.PrinterNewJobs {
if p, exists := pm.printers.GetByGCPID(message.GCPID); exists {
go pm.gcp.HandleJobs(&p, func() { pm.incrementJobsProcessed(false) })
}
}
}
}
}()
}
func (pm *PrinterManager) incrementJobsProcessed(success bool) {
pm.jobStatsMutex.Lock()
defer pm.jobStatsMutex.Unlock()
if success {
pm.jobsDone += 1
} else {
pm.jobsError += 1
}
}
// addInFlightJob adds a job ID to the in flight set.
//
// Returns true if the job ID was added, false if it already exists.
func (pm *PrinterManager) addInFlightJob(jobID string) bool {
pm.jobsInFlightMutex.Lock()
defer pm.jobsInFlightMutex.Unlock()
if _, exists := pm.jobsInFlight[jobID]; exists {
return false
}
pm.jobsInFlight[jobID] = struct{}{}
return true
}
// deleteInFlightJob deletes a job from the in flight set.
func (pm *PrinterManager) deleteInFlightJob(jobID string) {
pm.jobsInFlightMutex.Lock()
defer pm.jobsInFlightMutex.Unlock()
delete(pm.jobsInFlight, jobID)
}
// printJob prints a new job to a native printer, then polls the native job state
// and updates the GCP/Privet job state. then returns when the job state is DONE
// or ABORTED.
//
// All errors are reported and logged from inside this function.
func (pm *PrinterManager) printJob(nativePrinterName, filename, title, user, jobID string, ticket *cdd.CloudJobTicket, updateJob func(string, *cdd.PrintJobStateDiff) error) {
defer os.Remove(filename)
if !pm.addInFlightJob(jobID) {
// This print job was already received. We probably received it
// again because the first instance is still QUEUED (ie not
// IN_PROGRESS). That's OK, just throw away the second instance.
return
}
defer pm.deleteInFlightJob(jobID)
if !pm.jobFullUsername {
user = strings.Split(user, "@")[0]
}
printer, exists := pm.printers.GetByNativeName(nativePrinterName)
if !exists {
pm.incrementJobsProcessed(false)
state := cdd.PrintJobStateDiff{
State: &cdd.JobState{
Type: cdd.JobStateAborted,
ServiceActionCause: &cdd.ServiceActionCause{ErrorCode: cdd.ServiceActionCausePrinterDeleted},
},
}
if err := updateJob(jobID, &state); err != nil {
log.ErrorJob(jobID, err)
}
return
}
nativeJobID, err := pm.native.Print(&printer, filename, title, user, jobID, ticket)
if err != nil {
pm.incrementJobsProcessed(false)
log.ErrorJobf(jobID, "Failed to submit to native print system: %s", err)
state := cdd.PrintJobStateDiff{
State: &cdd.JobState{
Type: cdd.JobStateAborted,
DeviceActionCause: &cdd.DeviceActionCause{ErrorCode: cdd.DeviceActionCausePrintFailure},
},
}
if err := updateJob(jobID, &state); err != nil {
log.ErrorJob(jobID, err)
}
return
}
log.InfoJobf(jobID, "Submitted as native job %d", nativeJobID)
var state cdd.PrintJobStateDiff
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
defer pm.releaseJob(printer.Name, nativeJobID, jobID)
for _ = range ticker.C {
nativeState, err := pm.native.GetJobState(printer.Name, nativeJobID)
if err != nil {
log.WarningJobf(jobID, "Failed to get state of native job %d: %s", nativeJobID, err)
state = cdd.PrintJobStateDiff{
State: &cdd.JobState{
Type: cdd.JobStateAborted,
DeviceActionCause: &cdd.DeviceActionCause{ErrorCode: cdd.DeviceActionCauseOther},
},
PagesPrinted: state.PagesPrinted,
}
if err := updateJob(jobID, &state); err != nil {
log.ErrorJob(jobID, err)
}
pm.incrementJobsProcessed(false)
return
}
if !reflect.DeepEqual(*nativeState, state) {
state = *nativeState
if err = updateJob(jobID, &state); err != nil {
log.ErrorJob(jobID, err)
}
log.InfoJobf(jobID, "State: %s", state.State.Type)
}
if state.State.Type != cdd.JobStateInProgress && state.State.Type != cdd.JobStateStopped {
if state.State.Type == cdd.JobStateDone {
pm.incrementJobsProcessed(true)
} else {
pm.incrementJobsProcessed(false)
}
return
}
}
}
func (pm *PrinterManager) releaseJob(printerName string, nativeJobID uint32, jobID string) {
if err := pm.native.ReleaseJob(printerName, nativeJobID); err != nil {
log.ErrorJob(jobID, err)
}
}
// GetJobStats returns information that is useful for monitoring
// the connector.
func (pm *PrinterManager) GetJobStats() (uint, uint, uint, error) {
var processing uint
for _, printer := range pm.printers.GetAll() {
processing += printer.NativeJobSemaphore.Count()
}
pm.jobStatsMutex.Lock()
defer pm.jobStatsMutex.Unlock()
return pm.jobsDone, pm.jobsError, processing, nil
}