-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATMServer.java
194 lines (172 loc) · 9.75 KB
/
ATMServer.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
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
package atmserver;
import java.net.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class ATMServer {
static public int destport = 4445;
static public int bufsize = 512;
static public final int timeout = 15000; // time in milliseconds
static public void main(String args[]) {
DatagramSocket s; // UDP uses DatagramSockets
try {
s = new DatagramSocket(destport);
} catch (SocketException se) {
System.err.println("cannot create socket with port " + destport);
return;
}
try {
s.setSoTimeout(timeout); // set timeout in milliseconds
} catch (SocketException se) {
System.err.println("socket exception: timeout not set!");
}
// create DatagramPacket object for receiving data:
DatagramPacket msg = new DatagramPacket(new byte[bufsize], bufsize);
Map<InetAddress, Integer> openSessions = new HashMap<>();
while (true) { // read loop
try {
msg.setLength(bufsize); // max received packet size
s.receive(msg); // the actual receive operation
System.err.println("message from <"
+ msg.getAddress().getHostAddress() + "," + msg.getPort() + ">");
byte[] data = msg.getData();
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
Client atmMessage = (Client) is.readObject();
System.out.println("Message received.");
int requestType = atmMessage.getRequest();
Integer value = openSessions.get(msg.getAddress());
switch (requestType) {
case 0: //Login
if (value != null) {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("This address already has an account signed in!");
break;
}
else {
int count = ClientDatabase.accountList.size();
Boolean foundAccount = false;
for (int i = 0; i <= count; i++) {
if (ClientDatabase.accountList.get(i).getAccountnum() == atmMessage.getAccountNumber()) {
if (ClientDatabase.accountList.get(i).getPin() == atmMessage.getPin()) {
openSessions.put(msg.getAddress(), atmMessage.getAccountNumber());
requestResponse(5, -1, msg.getAddress(), msg.getPort());
System.out.println(msg.getAddress() + " has successfully signed in with account #" + atmMessage.getAccountNumber() + ".");
System.out.println(msg.getPort());
foundAccount = true;
break;
} else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Wrong PIN for account #" + atmMessage.getAccountNumber() + ".");
break;
}
}
}
if (foundAccount == false) {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("The account number #" + atmMessage.getAccountNumber() + " does not exist!");
break;
}
break;
}
case 1: //Balance
if (value != null) {
for (Account account : ClientDatabase.accountList) {
if (account.getAccountnum() == value) {
requestResponse(5, account.getBalance(), msg.getAddress(), msg.getPort());
System.out.println("Account #" + value + "'s balance is " + account.getBalance());
break;
}
}
} else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Not Logged In!");
}
break;
case 2: //Withdrawl
if (value != null) {
for (Account account : ClientDatabase.accountList) {
if (account.getAccountnum() == value) {
if (account.getBalance() > atmMessage.getAmount()) {
account.setBalance((account.getBalance() - atmMessage.getAmount()));
requestResponse(5, account.getBalance(), msg.getAddress(), msg.getPort());
System.out.println("After the withdrawal, account #" + value + "'s balance is " + account.getBalance());
break;
}
else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Not enough funds for this withdrawal!");
break;
}
}
else {
System.out.println("Account not found!");
}
}
break;
} else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Not Logged In!");
break;
}
case 3: //Deposit
if (value != null) {
for (Account account : ClientDatabase.accountList) {
if (account.getAccountnum() == value) {
account.setBalance((account.getBalance() + atmMessage.getAmount()));
requestResponse(5, account.getBalance(), msg.getAddress(), msg.getPort());
System.out.println("After the deposit, account #" + value + "'s balance is " + account.getBalance());
break;
}
else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Account not found!");
break;
}
}
break;
}
else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Not Logged In!");
break;
}
case 4: //Logout
if (value != null) {
openSessions.remove(msg.getAddress());
requestResponse(5, -1, msg.getAddress(), msg.getPort());
System.out.println("Account #" + value + "(" + msg.getAddress() + ") has logged out of their session!");
break;
} else {
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Not Logged In!");
break;
}
default:
requestResponse(6, -1, msg.getAddress(), msg.getPort());
System.out.println("Error. Request not valid or packet is not recognized.");
break;
}
value = null;
} catch (SocketTimeoutException ste) { // receive() timed out
System.err.println("Response timed out!");
} catch (Exception ioe) { // should never happen!
System.err.println("Bad receive");
ioe.printStackTrace();
}
// newline must be part of str
}
// s.close();
} // end of main
static public void requestResponse(int responseType, int responseAmount, InetAddress clientAddress, int clientPort) throws IOException, ClassNotFoundException {
DatagramSocket Socket;
Socket = new DatagramSocket();
Client response = new Client(responseType, -1, -1, responseAmount);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // creates new byte output stream
ObjectOutputStream os = new ObjectOutputStream(outputStream); // creates new out stream
os.writeObject(response); // writes new client to send message
byte[] b = outputStream.toByteArray(); // writes bytes to array
DatagramPacket msg = new DatagramPacket(b, b.length, clientAddress, clientPort); // creates new datagram to send with coordinates
Socket.send(msg); // sends message
}
}