|
| 1 | +package com.berezin; |
| 2 | + |
| 3 | +import com.sun.jna.Pointer; |
| 4 | + |
| 5 | +import java.util.Scanner; |
| 6 | + |
| 7 | +public class Main { |
| 8 | + |
| 9 | + static Pointer http; |
| 10 | + public static void main(String[] args) { |
| 11 | + initCupsStuff(); |
| 12 | + while (true) { |
| 13 | + int item = promptInput(); |
| 14 | + if (item == 1) listSubscriptions(); |
| 15 | + else if (item == 2) startSubscription(); |
| 16 | + else if (item == 3) endSubscription(); |
| 17 | + else if (item == 4) break; |
| 18 | + else System.out.println("Invalid input"); |
| 19 | + } |
| 20 | + } |
| 21 | + static int promptInput(){ |
| 22 | + System.out.println("1: List active subscriptions"); |
| 23 | + System.out.println("2: Start subscription");; |
| 24 | + System.out.println("3: End subscription"); |
| 25 | + System.out.println("4: Exit"); |
| 26 | + Scanner s = new Scanner(System.in); |
| 27 | + return s.nextInt(); |
| 28 | + } |
| 29 | + static void initCupsStuff() { |
| 30 | + http = Cups.INSTANCE.httpConnectEncrypt( |
| 31 | + Cups.INSTANCE.cupsServer(), |
| 32 | + Cups.INSTANCE.ippPort(), |
| 33 | + Cups.INSTANCE.cupsEncryption()); |
| 34 | + } |
| 35 | + static void listSubscriptions() { |
| 36 | + Pointer request = Cups.INSTANCE.ippNewRequest(Cups.INSTANCE.ippOpValue("Get-Subscriptions")); |
| 37 | + |
| 38 | + Cups.INSTANCE.ippAddString(request, |
| 39 | + Cups.INSTANCE.ippTagValue("Operation"), |
| 40 | + Cups.INSTANCE.ippTagValue("uri"), |
| 41 | + "printer-uri", |
| 42 | + "", |
| 43 | + "ipp://localhost:631/printers/"); |
| 44 | + Pointer response = Cups.INSTANCE.cupsDoRequest(http, request, "/"); |
| 45 | + parseResponse(response); |
| 46 | + } |
| 47 | + static void startSubscription() { |
| 48 | + System.out.println("Enter subscription uri e.g. rss://localhost:8000"); |
| 49 | + Scanner s = new Scanner(System.in); |
| 50 | + String uri = s.next(); |
| 51 | + Pointer request = Cups.INSTANCE.ippNewRequest(Cups.INSTANCE.ippOpValue("Create-Printer-Subscription")); |
| 52 | + |
| 53 | + Cups.INSTANCE.ippAddString(request, |
| 54 | + Cups.INSTANCE.ippTagValue("Operation"), |
| 55 | + Cups.INSTANCE.ippTagValue("uri"), |
| 56 | + "printer-uri", |
| 57 | + "", |
| 58 | + "ipp://localhost:631/"); |
| 59 | + Cups.INSTANCE.ippAddString(request, |
| 60 | + Cups.INSTANCE.ippTagValue("Subscription"), |
| 61 | + Cups.INSTANCE.ippTagValue("uri"), |
| 62 | + "notify-recipient-uri", |
| 63 | + "", |
| 64 | + uri); |
| 65 | + Cups.INSTANCE.ippAddString(request, |
| 66 | + Cups.INSTANCE.ippTagValue("Subscription"), |
| 67 | + Cups.INSTANCE.ippTagValue("Keyword"), |
| 68 | + "notify-events", |
| 69 | + "", |
| 70 | + "printer-state-changed"); |
| 71 | + Pointer response = Cups.INSTANCE.cupsDoRequest(http, request, "/"); |
| 72 | + parseResponse(response); |
| 73 | + } |
| 74 | + static void endSubscription() { |
| 75 | + System.out.println("Enter subscription id, or -1 to cancel"); |
| 76 | + Scanner s = new Scanner(System.in); |
| 77 | + int id = s.nextInt(); |
| 78 | + Pointer request = Cups.INSTANCE.ippNewRequest(Cups.INSTANCE.ippOpValue("Cancel-Subscription")); |
| 79 | + |
| 80 | + Cups.INSTANCE.ippAddString(request, |
| 81 | + Cups.INSTANCE.ippTagValue("Operation"), |
| 82 | + Cups.INSTANCE.ippTagValue("uri"), |
| 83 | + "printer-uri", |
| 84 | + "", |
| 85 | + "ipp://localhost:631/"); |
| 86 | + Cups.INSTANCE.ippAddInteger(request, |
| 87 | + Cups.INSTANCE.ippTagValue("Operation"), |
| 88 | + Cups.INSTANCE.ippTagValue("Integer"), |
| 89 | + "notify-subscription-id", |
| 90 | + id); |
| 91 | + Pointer response = Cups.INSTANCE.cupsDoRequest(http, request, "/"); |
| 92 | + parseResponse(response); |
| 93 | + } |
| 94 | + static void parseResponse(Pointer response) { |
| 95 | + Pointer attr = Cups.INSTANCE.ippFirstAttribute(response); |
| 96 | + while (true) { |
| 97 | + if (attr == Pointer.NULL) { |
| 98 | + break; |
| 99 | + } |
| 100 | + int valueTag = Cups.INSTANCE.ippGetValueTag(attr); |
| 101 | + String data = Cups.INSTANCE.ippTagString(valueTag); |
| 102 | + String attrName = Cups.INSTANCE.ippGetName(attr); |
| 103 | + if (valueTag == Cups.INSTANCE.ippTagValue("Integer")) { |
| 104 | + data = "" + Cups.INSTANCE.ippGetInteger(attr, 0); |
| 105 | + } else { |
| 106 | + data = Cups.INSTANCE.ippGetString(attr, 0, ""); |
| 107 | + } |
| 108 | + if (attrName == null){ |
| 109 | + System.out.println("------------------------"); |
| 110 | + } else { |
| 111 | + System.out.printf("%s: %s\n", attrName, data); |
| 112 | + } |
| 113 | + attr = Cups.INSTANCE.ippNextAttribute(response); |
| 114 | + } |
| 115 | + System.out.println("------------------------"); |
| 116 | + } |
| 117 | +} |
0 commit comments