~elambert/gearmanij/gearman_java_library

« back to all changes in this revision

Viewing changes to src/org/gearman/example/DigestClient.java

  • Committer: Eric Lambert
  • Date: 2009-07-07 02:18:15 UTC
  • mfrom: (57.1.65 gearmanij-trunk)
  • Revision ID: eric.d.lambert@gmail.com-20090707021815-0xbupi72ubyoa62a
merge from trunk. ReverseWorkerTest has been ignored, it was failing claiming that certain ops were not supported, since there is duplication in the worker code, for the time being will just ignore this issue, will resolve once the worker code has been straightened out

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * GNU Lesser General Public License (LGPL) version 2.1.
5
5
 * See the COPYING file in the parent directory for full text.
6
6
 */
7
 
package gearmanij.example;
8
 
 
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;
15
8
 
16
9
import java.io.PrintStream;
17
10
import java.util.concurrent.Callable;
18
11
 
 
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
 
19
20
public class DigestClient {
20
21
 
21
 
  private Connection conn;
22
 
 
23
 
  public DigestClient(Connection conn) {
24
 
    this.conn = conn;
25
 
  }
26
 
 
27
 
  public DigestClient(String host, int port) {
28
 
    this(new SocketConnection(host, port));
29
 
  }
30
 
 
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);
36
 
  }
37
 
 
38
 
  protected Callable<byte[]> newClientJob(byte[] input, String function, String uniqueId) {
39
 
    return new ClientRequest(conn, function, uniqueId, input);
40
 
  }
41
 
 
42
 
  public static void main(String[] args) {
43
 
    if (args.length == 0 || args.length > 3) {
44
 
      usage(System.out);
45
 
      return;
46
 
    }
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));
55
 
      }
56
 
    }
57
 
    byte[] md5 = new DigestClient(host, port).digest(payload);
58
 
    System.out.println(ByteUtils.toHex(md5));
59
 
  }
60
 
 
61
 
  public static void usage(PrintStream out) {
62
 
    String[] usage = {
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", //
68
 
    };
69
 
 
70
 
    for (String line : usage) {
71
 
      out.println(line);
72
 
    }
73
 
  }
 
22
    private PacketConnection conn;
 
23
 
 
24
    public DigestClient(PacketConnection conn) {
 
25
        this.conn = conn;
 
26
    }
 
27
 
 
28
    public DigestClient(String host, int port) {
 
29
        this(new SocketConnection(host, port));
 
30
    }
 
31
 
 
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();
 
37
    }
 
38
 
 
39
    protected Callable<JobResponse> newClientJob(byte[] input, String function,
 
40
            String uniqueId) {
 
41
        return new ClientRequest(conn, function, uniqueId, input);
 
42
    }
 
43
 
 
44
    public static void main(String[] args) {
 
45
        if (args.length == 0 || args.length > 3) {
 
46
            usage(System.out);
 
47
            return;
 
48
        }
 
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));
 
57
            }
 
58
        }
 
59
        byte[] md5 = new DigestClient(host, port).digest(payload);
 
60
        System.out.println(ByteUtils.toHex(md5));
 
61
    }
 
62
 
 
63
    public static void usage(PrintStream out) {
 
64
        String[] usage = {
 
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", //
 
70
        };
 
71
 
 
72
        for (String line : usage) {
 
73
            out.println(line);
 
74
        }
 
75
    }
74
76
 
75
77
}