4
4
* GNU Lesser General Public License (LGPL) version 2.1.
5
5
* See the COPYING file in the parent directory for full text.
7
package gearmanij.example;
9
import gearmanij.ClientRequest;
10
import gearmanij.Connection;
11
import gearmanij.Constants;
12
import gearmanij.SocketConnection;
13
import gearmanij.util.ByteUtils;
14
import gearmanij.util.Exceptions;
7
package org.gearman.example;
16
9
import java.io.PrintStream;
17
10
import java.util.concurrent.Callable;
12
import org.gearman.PacketConnection;
13
import org.gearman.Constants;
14
import org.gearman.client.ClientRequest;
15
import org.gearman.client.JobResponse;
16
import org.gearman.common.SocketConnection;
17
import org.gearman.util.ByteUtils;
18
import org.gearman.util.Exceptions;
19
20
public class DigestClient {
21
private Connection conn;
23
public DigestClient(Connection conn) {
27
public DigestClient(String host, int port) {
28
this(new SocketConnection(host, port));
31
public byte[] digest(byte[] input) {
32
String function = "digest";
33
String uniqueId = null;
34
Callable<byte[]> client = newClientJob(input, function, uniqueId);
35
return Exceptions.call(client);
38
protected Callable<byte[]> newClientJob(byte[] input, String function, String uniqueId) {
39
return new ClientRequest(conn, function, uniqueId, input);
42
public static void main(String[] args) {
43
if (args.length == 0 || args.length > 3) {
47
String host = Constants.GEARMAN_DEFAULT_TCP_HOST;
48
int port = Constants.GEARMAN_DEFAULT_TCP_PORT;
49
byte[] payload = ByteUtils.toUTF8Bytes(args[args.length - 1]);
50
for (String arg : args) {
51
if (arg.startsWith("-h")) {
52
host = arg.substring(2);
53
} else if (arg.startsWith("-p")) {
54
port = Integer.parseInt(arg.substring(2));
57
byte[] md5 = new DigestClient(host, port).digest(payload);
58
System.out.println(ByteUtils.toHex(md5));
61
public static void usage(PrintStream out) {
63
"usage: gearmanij.example.DigestClient [-h<host>] [-p<port>] <string>",
64
"\t-h<host> - job server host",
65
"\t-p<port> - job server port",
66
"\n\tExample: java gearmanij.example.DigestClient Foo",
67
"\tExample: java gearmanij.example.DigestClient -h127.0.0.1 -p4730 Bar", //
70
for (String line : usage) {
22
private PacketConnection conn;
24
public DigestClient(PacketConnection conn) {
28
public DigestClient(String host, int port) {
29
this(new SocketConnection(host, port));
32
public byte[] digest(byte[] input) {
33
String function = "digest";
34
String uniqueId = null;
35
Callable<JobResponse> client = newClientJob(input, function, uniqueId);
36
return Exceptions.call(client).responseData();
39
protected Callable<JobResponse> newClientJob(byte[] input, String function,
41
return new ClientRequest(conn, function, uniqueId, input);
44
public static void main(String[] args) {
45
if (args.length == 0 || args.length > 3) {
49
String host = Constants.GEARMAN_DEFAULT_TCP_HOST;
50
int port = Constants.GEARMAN_DEFAULT_TCP_PORT;
51
byte[] payload = ByteUtils.toUTF8Bytes(args[args.length - 1]);
52
for (String arg : args) {
53
if (arg.startsWith("-h")) {
54
host = arg.substring(2);
55
} else if (arg.startsWith("-p")) {
56
port = Integer.parseInt(arg.substring(2));
59
byte[] md5 = new DigestClient(host, port).digest(payload);
60
System.out.println(ByteUtils.toHex(md5));
63
public static void usage(PrintStream out) {
65
"usage: org.gearman.example.DigestClient [-h<host>] [-p<port>] <string>",
66
"\t-h<host> - job server host",
67
"\t-p<port> - job server port",
68
"\n\tExample: java org.gearman.example.DigestClient Foo",
69
"\tExample: java org.gearman.example.DigestClient -h127.0.0.1 -p4730 Bar", //
72
for (String line : usage) {