-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClientConnect.java
More file actions
175 lines (152 loc) · 6.59 KB
/
Copy pathClientConnect.java
File metadata and controls
175 lines (152 loc) · 6.59 KB
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
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
class ClientConnect extends Thread
{
protected Socket clientSocket;
protected BufferedReader in = null;
protected PrintWriter out = null;
protected ObjectOutputStream objectOut;
protected StockMarket mySMRef;
protected boolean isRegistered = false;
protected String[] tokens;
Calendar cal;
SimpleDateFormat sdf;
public ClientConnect(Socket aSocket, StockMarket aSM)
{
clientSocket = aSocket;
mySMRef = aSM;
start();
cal = Calendar.getInstance();
sdf = new SimpleDateFormat("HH:mm:ss");
}
public void run()
{
System.out.println("New client has connected, new thread started.");
System.out.println("Client IP is: " + clientSocket.getRemoteSocketAddress() + "\n\n");
try
{
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
objectOut = new ObjectOutputStream(clientSocket.getOutputStream());
String inputText;
while((inputText = in.readLine()) != null)
{
System.out.println("Client: " + clientSocket.getLocalSocketAddress() + " : " + inputText);
if(inputText.equals("HELO"))
{
System.out.println("ACK:" + clientSocket.getLocalSocketAddress() + ":" + clientSocket.getRemoteSocketAddress());
out.println("ACK:" + clientSocket.getLocalSocketAddress() + ":" + clientSocket.getRemoteSocketAddress());
out.println("");
}
else if(inputText.equals("EXIT"))
{
System.out.println("ACK:EXIT:Goodbye!");
out.println("ACK:EXIT:Connection Closed.");
out.println("");
break;
}
else if(inputText.equals("REGI"))
{
System.out.println("ACK:REGI:"+clientSocket.getLocalSocketAddress());
String ID = "" + clientSocket.getRemoteSocketAddress();
tokens = ID.split(":");
ID = tokens[1];
out.println("REGI:SUCCESS:"+ID);
out.println("");
isRegistered = mySMRef.registerUser(Integer.parseInt(ID));
}
else if(inputText.equals("DISP"))
{ // Display Stock Market
tokens = inputText.split(":");
out.println("DEBUG: You entered: DISP");
System.out.println("DEBUG: DISP");
System.out.println("DEBUG: Tokens is: " + tokens.length + " in size -- Value of [1] is: " + tokens[1]);
if(mySMRef.checkID(Integer.parseInt(tokens[1])))
{
String [][] aStock = mySMRef.getStockMarketState();
//objectOut.writeObject(mySMRef.getStockMarketState());
for(int i = 0; i < aStock.length; i++)
{
out.println("STK:"+aStock[i][0]+":"+aStock[i][1]+":"+aStock[i][3]);
}
System.out.println("TIME:" + sdf.format(cal.getTime()) );
out.println("TIME:" + sdf.format(cal.getTime()) );
out.println("END:EOF");
out.println("");
}
else
{
out.println("ERR:Not Registered");
System.out.println("User not registered.");
out.println("");
}
}
else if(inputText.startsWith("BUY"))
{
tokens = inputText.split(":");
if(mySMRef.checkID(Integer.parseInt(tokens[3])))
{
out.println(mySMRef.buyShares(tokens));
}
else
{
out.println("ERR:Not Registered");
}
out.println("");
}
else if(inputText.startsWith("SELL"))
{
tokens = inputText.split(":");
if(mySMRef.checkID(Integer.parseInt(tokens[3])))
{
out.println(mySMRef.sellShares(tokens));
}
else
{
out.println("ERR:Not Registered");
}
out.println("");
}
else if(inputText.startsWith("CASH"))
{
tokens = inputText.split(":");
if(mySMRef.checkID(Integer.parseInt(tokens[1])) == true)
{
String tempStr = mySMRef.checkCash(tokens[1]);
out.println(tempStr);
}
else
{
out.println("ERR:Not Registered");
}
out.println("");
}
else if(inputText.equals("HELP"))
{
out.println("Commands:");
out.println("REGI: - Allows authentication with the system.");
out.println("BUY: - Allows the purchasing of shares.");
out.println("SELL: - Allows the selling of shares.");
out.println("EXIT: - Exit the system (lose all shares and funds).");
out.println("DISP: - Display current Stock market values.");
out.println("CASH: - Display your remaining cash balance (not including shares owned).");
out.println("");
}
else
{
System.out.println("DEBUG:"+inputText+":");
}
}
out.close();
in.close();
clientSocket.close();
isRegistered = false;
}
catch(IOException e)
{
System.out.println("Problem with socket: " + e);
}
}
}