summaryrefslogtreecommitdiff
path: root/java/JACE/tests/netsvcs/Token/ProxyClientTest.java
blob: 1b2118b7f9e33b583a8c8e801f67ff47a4aa3466 (plain)
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
package JACE.tests.netsvcs.Token;

import java.net.*;
import java.io.*;
import JACE.ASX.*;
import JACE.OS.*;
import JACE.Misc.*;
import JACE.Concurrency.*;
import JACE.netsvcs.Token.*;
 
public class ProxyClientTest
{ 
  public static void report(String s) {
    System.out.println(s);
  }

  static InputStreamReader stdin = new InputStreamReader (System.in);
  static RemoteLock lock;
  static int requeuePosition = -1, timeout = -1;

  public static void showMenu ()
  {
    report ("\n[A]cquire");
    report ("Acquire [w]rite");
    report ("Acquire rea[d]");
    report ("[T]ry Acquire");
    report ("Try acquire wr[i]te");
    report ("Try acquire r[e]ad");
    report ("Re[n]ew");
    report ("[R]elease");

    report ("\n[Q]uit");
    System.out.print ("\nOption (AWDTIENRQ): ");
  }
  
  public static void processCommands () throws IOException
  {
    int ch = 0, result = -1;

    showMenu ();
    do {

      ch = stdin.read();
      
      try {

	switch (ch) 
	  {
	  case 'a':
	  case 'A':
	    if (timeout >= 0) {
	      TimeValue tv = TimeValue.relativeTimeOfDay (timeout, 0);
	      result = lock.acquire (tv);
	    } else
	      result = lock.acquire ();
	    break;
	  case 'w':
	  case 'W':
	    if (timeout >= 0) {
	      TimeValue tv = TimeValue.relativeTimeOfDay (timeout, 0);
	      result = lock.acquireWrite (tv);
	    } else
	      result = lock.acquire ();
	    break;
	  case 'd':
	  case 'D':
	    if (timeout >= 0) {
	      TimeValue tv = TimeValue.relativeTimeOfDay (timeout, 0);
	      result = lock.acquireRead (tv);
	    } else
	      result = lock.acquire ();
	    break;
	  case 't':
	  case 'T':
	    result = lock.tryAcquire ();
	    break;
	  case 'i':
	  case 'I':
	    result = lock.tryAcquireWrite ();
	    break;
	  case 'e':
	  case 'E':
	    result = lock.tryAcquireRead ();
	    break;
	  case 'r':
	  case 'R':
	    result = lock.release ();
	    break;
	  case 'n':
	  case 'N':
	    if (timeout >= 0) {
	      TimeValue tv = TimeValue.relativeTimeOfDay (timeout, 0);
	      result = lock.renew (requeuePosition, tv);
	    } else
	      result = lock.renew (requeuePosition);
	    break;
	  case 'q':
	  case 'Q':
	    continue;
	  case -1:
	  case 10:
	  case 13:
	    // Ignore line feeds and carriage returns
	    continue;
	  default:
	    report ("Unknown command: " + (char)ch + " (" + ch + ")\n");
	    showMenu ();
	    continue;
	  }

	report ("\nResult: " + (result == lock.SUCCESS ? "Success" : 
				"Failure"));

      } catch (TimeoutException e) {
	report ("\nRequest timed out");
	/*
      } catch (InterruptedException e) {
	report ("\nInterrupted while attemping operation");
	*/
      } catch (LockException e) {
	report ("\nLock failure: " + e.getMessage ());
      }

      showMenu ();

    } while (ch != 'Q' && ch != 'q');
  }

  public static void printUsage ()
  {
    report ("Valid options:\n");
    report ("-h <host name>      Specify token server host");
    report ("-p <port number>    Port to connect to");
    report ("-t <time in sec>    Relative timeout to use");
    report ("-l <lock type>      0 Mutex, 1 RWMutex");
    report ("-c <client ID>      Specify ID (default is this machine name)");
    report ("-n <token name>     Specify token name");
    report ("-q <requeue pos>    Use this requeue position in renew");
    report ("-d                  Enable debugging");
  }

  public static void main (String args[]) throws IOException 
  { 
    GetOpt opt = new GetOpt (args, "h:p:t:l:c:n:q:d", true);

    String host = "localhost", tokenName = "token1";
    int port = ACE.DEFAULT_SERVER_PORT;
    int type = LockTypes.MUTEX, c;
    String clientID = InetAddress.getLocalHost().getHostName ();

    try {
      while ((c = opt.next ()) != -1) {
	switch (c) 
	  {
	  case 'h':
	    host = opt.optarg ();
	    break;
	  case 'p':
	    port = Integer.parseInt (opt.optarg ());
	    break;
	  case 't':
	    timeout = Integer.parseInt (opt.optarg ());
	    break;
	  case 'l':
	    type = Integer.parseInt (opt.optarg ());
	    break;
	  case 'c':
	    clientID = opt.optarg ();
	    break;
	  case 'n':
	    tokenName = opt.optarg ();
	    break;
	  case 'q':
	    requeuePosition = Integer.parseInt (opt.optarg ());
	    break;
	  case 'd':
	    ACE.enableDebugging ();
	    ACE.DEBUG ("Debugging is enabled");
	    break;
	  default:
	    printUsage ();
	    return;
	  }
      }
    } catch (ArrayIndexOutOfBoundsException e) {
      printUsage ();
      return;
    }

    report ("Using    : " + host + " on port " + port);
    report ("Token    : " + tokenName);
    report ("Client ID: " + clientID);

    lock = new RemoteLock (type, 0, tokenName, clientID, host, port);

    processCommands ();

    lock.close ();
  }
}