-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientConnection.java
167 lines (134 loc) · 4.35 KB
/
ClientConnection.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
package com.bham.fsd.assignments.jabberserver;
//REMOVE ALL PRINT STATEMENTS AND RESET DATABASE FUNCTION
import java.io.BufferedReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
public class ClientConnection implements Runnable {
Socket socket;
JabberDatabase JD;
int thisClientID;
String thisClientName;
boolean quit;
public ClientConnection(Socket socket,JabberDatabase JD) {
this.socket = socket;
this.JD = JD;
this.quit = false;
thisClientID = -1;
thisClientName = null;
new Thread(this).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
JabberMessage request;
String message, answer;
while (quit==false) {
try {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
request = (JabberMessage) ois.readObject();
//System.out.println(request.getMessage());
message = request.getMessage();
if (message.startsWith("signin")) {
//System.out.println("sign in request recieved");
answer = message.substring(7,message.length());
answer = signingIn(answer);
JabberMessage toSend = new JabberMessage(answer);
respond(toSend);
}
else if(message.startsWith("register")) {
answer = registering(message.substring(9,message.length()));
JabberMessage toSend = new JabberMessage(answer);
respond(toSend);
}
else if (message.equals("signout")) {
//System.out.println("kill connection");
quit = true;//possibly remove this feature
socket.close();
}
else if (message.equals("reset")) {
//JD.resetDatabase();//remove feature after
}
else if (message.equals("timeline")) {
ArrayList<ArrayList<String>> data = getTimeline();
JabberMessage toSend = new JabberMessage("timeline",data);
respond(toSend);
}
else if (message.startsWith("like")) {
addingLike(message.substring(5,message.length()));
JabberMessage toSend = new JabberMessage("posted");
respond(toSend);
}
else if (message.equals("users")) {
ArrayList<ArrayList<String>> data = getNotFollowed();
JabberMessage toSend = new JabberMessage("users",data);
respond(toSend);
}
else if (message.startsWith("follow")) {
ArrayList<ArrayList<String>> data = addingFollow(message.substring(7,message.length()));
JabberMessage toSend = new JabberMessage("posted",data);
respond(toSend);
}
else if (message.startsWith("post")) {
ArrayList<ArrayList<String>> data = postingJab(message.substring(5,message.length()));
JabberMessage toSend = new JabberMessage("posted",data);
respond(toSend);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void respond(JabberMessage reply) {
try {
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(reply);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String signingIn(String user) {
int ID = JD.getUserID(user);
thisClientID = ID;
if (ID == -1) {
thisClientName = null;
return "unknown-user";
}
else {
thisClientName = user;
return "signedin";
}
}
public String registering(String username) {
String email = username + "@gmail.com";
JD.addUser(username, email);
return signingIn(username);
}
public ArrayList<ArrayList<String>> getTimeline() {
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
result = JD.getTimelineOfUserEx(thisClientID);
return result;
}
public void addingLike(String toLike) {
int jabID = Integer.parseInt(toLike);
JD.addLike(thisClientID, jabID);
}
public ArrayList<ArrayList<String>> getNotFollowed(){
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
result = JD.getUsersNotFollowed(thisClientID);
return result;
}
public ArrayList<ArrayList<String>> addingFollow(String toFollow) {
JD.addFollower(thisClientID, toFollow);
return getTimeline();
}
public ArrayList<ArrayList<String>> postingJab(String jab) {
JD.addJab(thisClientName, jab);
return getTimeline();
}
}