-
Notifications
You must be signed in to change notification settings - Fork 1
/
Client.java
134 lines (107 loc) · 3.84 KB
/
Client.java
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
/** Created by andri **/
/** June 2018 **/
import java.io.*;
import java.net.*;
import java.util.*;
class ClientRun implements Runnable {
final private int cId1, amount, cId2;
final private char action;
ClientRun(int cId1, char action, int amount, int cId2) {
this.cId1 = cId1;
this.action = action;
this.amount = amount;
this.cId2 = cId2;
}
public void run() {
//final int requests = 100;
final Random rand = new Random();
Socket cSocket;
PrintWriter sockWriter;
Scanner sockReader;
String request; /* server to server request = "Action,Client1,Amount,Client2, forwarding_value"; */
int sId;
String response;
//int balance = 500;
try {
//sId = rand.nextInt(3) + 1; //we have 3 server daemons
sId = 1; //keep total order
try{
cSocket = new Socket("localhost", 4000 + sId);
}
catch (Exception e){
//if server1 is down, try out server2. If server2 is down too, there is no majority
sId++;
cSocket = new Socket("localhost", 4000 + sId);
}
//System.out.println("\nWill be served by server:" + sId + "\n");
switch (action) {
case '+':
request = "+," + cId1 + "," + amount + ",-1";
break;
case '-':
request = "-," + cId1 + "," + amount + ",-1";
break;
case '>':
request = ">," + cId1 + "," + amount + "," + cId2;
break;
case '?':
request = "?," + cId1 + ",0" + ",-1";
break;
default:
request = "?," + cId1 + ",0" + ",-1";
break;
}
//System.out.println("Client " + cId1 + ", Request " + request + " to Server " + sId);
//send request
sockWriter = new PrintWriter(cSocket.getOutputStream());
sockWriter.println(request);
sockWriter.flush();
//wait for the server to response before exiting
sockReader = new Scanner(cSocket.getInputStream());
response = sockReader.nextLine();
System.out.println(response);
cSocket.close();
} catch (Exception e) {
//try again
//System.out.println("Server crashed. Let's try again..");
new Thread(new ClientRun(cId1, action, amount, cId2 )).start();
}
}
}
public class Client {
public static void main(String[] args) {
int cId1, cId2, amount;
char action;
/*
MultiServer1.print_map();
System.out.println();
MultiServer2.print_map();
System.out.println();
MultiServer3.print_map();
System.out.println();
*/
Scanner in = new Scanner(System.in);
System.out.printf("Enter Client Id: ");
cId1 = in.nextInt();
while (true){
try{
System.out.printf("Enter Type of transaction ('+', '-', '>', '?'): ");
action = in.next().charAt(0);
if (action == '>') {
System.out.printf("Enter second Client Id : ");
cId2 = in.nextInt();
}
else { cId2 = 0; }
if (action != '?') {
System.out.printf("Enter amount: ");
amount = in.nextInt();
}
else { amount = 0; }
new Thread(new ClientRun(cId1, action, amount, cId2 )).start();
}
catch(Exception e){
System.out.println("Wrong type of transaction. Please try again.\n");
}
}
}
}