-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plaptop.java
126 lines (112 loc) · 2.9 KB
/
Plaptop.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
/**
* @author Thomas Moroney
*/
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.util.Scanner;
/**
*
* Claptop class
*
* An instance accepts user input
*
*/
public class Plaptop extends Node {
static final int PLAPTOP_PORT = 50002;
static final String PLAPTOP_ID = "011";
static final int GW2_PORT = 50004;
static final String GW2_NODE = "GW2";
InetSocketAddress dstAddress;
/**
* Constructor
*
* Attempts to create socket at given port and create an InetSocketAddress for the destinations
*/
Plaptop(String dstHost, int dstPort, int srcPort) {
try {
dstAddress= new InetSocketAddress(dstHost, dstPort);
socket= new DatagramSocket(srcPort);
listener.go();
}
catch(java.lang.Exception e) {e.printStackTrace();}
}
/**
* Assume that incoming packets contain a String and print the string.
*/
public synchronized void onReceipt(DatagramPacket packet) {
try {
PacketContent content = PacketContent.fromDatagramPacket(packet);
if (content.getType() == PacketContent.TEXTPACKET) {
TextPacket inPacket = ((TextPacket)content);
String s = inPacket.text;
String output = s.substring(3); // remove destination header
System.out.println(output + "\n");
notify();
}
}
catch(Exception e) {e.printStackTrace();}
}
/**
* Sender Method
*
*/
public synchronized void start() throws Exception {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Would you like to check your balance (1) or make a deposit (2)? ");
String input = scanner.nextLine();
if (input.equals("exit")) {
scanner.close();
return;
}
else if (input.equals("2")) {
System.out.print("Enter an amount to deposit: ");
input = scanner.nextLine();
}
else {
input = "balance";
}
int serverNum = 1;
boolean hasInt = false;
while(!hasInt) {
System.out.print("Which bank account would you like to access (1 or 2)?: ");
if (scanner.hasNextInt()) {
serverNum = scanner.nextInt();
hasInt = true;
}
else {
System.out.println("Please enter a number!");
}
}
String dest;
switch (serverNum) {
case 1:
dest = "000"; // DServer
break;
case 2:
dest = "001"; // DServer2
break;
default:
dest = "000";
}
input = dest + PLAPTOP_ID + input; // add identifier so DServer knows where to send the packet back to
DatagramPacket request;
request = new TextPacket(input).toDatagramPacket();
request.setSocketAddress(dstAddress);
socket.send(request);
this.wait();
}
}
/**
* Test method
*
* Sends a packet to a given address
*/
public static void main(String[] args) {
try {
(new Plaptop(GW2_NODE, GW2_PORT, PLAPTOP_PORT)).start();
System.out.println("Program completed");
} catch(java.lang.Exception e) {e.printStackTrace();}
}
}