Skip to content

Commit 75ac9fa

Browse files
committed
initial commit
0 parents  commit 75ac9fa

11 files changed

+247
-0
lines changed

.idea/artifacts/cupstool_jar.xml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/description.html

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/net_java_dev_jna_jna_4_4_0.xml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/project-template.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cupstool.iml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="library" name="net.java.dev.jna:jna:4.4.0" level="project" />
11+
</component>
12+
</module>

cupstool.jar

1.05 MB
Binary file not shown.

src/META-INF/MANIFEST.MF

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: com.berezin.Main
3+

src/com/berezin/Cups.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.berezin;
2+
3+
import com.sun.jna.*;
4+
5+
/**
6+
* Created by kyle on 4/27/17.
7+
*/
8+
public interface Cups extends Library {
9+
Cups INSTANCE = (Cups ) Native.loadLibrary("cups", Cups.class);
10+
11+
Pointer httpConnectEncrypt(String host, int port, Pointer encryption);
12+
String cupsServer ();
13+
int ippPort ();
14+
Pointer cupsEncryption ();
15+
Pointer ippNewRequest (int op);
16+
int httpAssembleURI (int encoding, Memory uri, int urilen, String sceme, String username, String host, int port, String resourcef);
17+
int ippAddString (Pointer ipp, int group, int tag, String name, String charset, String value);
18+
int ippAddInteger (Pointer ipp, int group, int tag, String name, int value);
19+
Pointer cupsDoRequest (Pointer http, Pointer request, String resource);
20+
Pointer ippFindAttribute (Pointer ipp, String name, int tag);
21+
String ippGetName(Pointer attr);
22+
int ippGetCount(Pointer attr);
23+
String ippGetString(Pointer attr, int element, String language);
24+
int ippGetInteger(Pointer attr, int element);
25+
int ippTagValue(String name);
26+
int ippOpValue(String name);
27+
String ippOpString(int op);
28+
Pointer ippFirstAttribute (Pointer ipp);
29+
Pointer ippNextAttribute (Pointer ipp);
30+
String ippTagString(int tag);
31+
int ippGetValueTag (Pointer ipp);
32+
void ippDelete (Pointer ipp);
33+
void httpClose (Pointer http);
34+
}

src/com/berezin/Main.java

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)