Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.Vector;
  5.  
  6. import javax.swing.JFrame;
  7.  
  8. public class ServerThread extends Thread {
  9.  
  10. private static Socket socket; // The Socket connected to our client
  11. public String input;
  12. public String output;
  13. public static String username;
  14. public static int userNumber = -1;//if user number remains 0 then the user hasn't logged in
  15. public String action;
  16. private String password; //you don't want the password accessible from other classes so private
  17. private static boolean destroyThread;
  18. boolean loggedIn = false;
  19. static int userThreadNumber;
  20. static DataInputStream din;
  21. static DataOutputStream dout;
  22. Vector <ServerConvo> convoArray = new Vector <ServerConvo>();
  23. public ServerThread(Socket socket, int threadNumber) {
  24. userThreadNumber = threadNumber;
  25. Server.console("Connect: socket =\""+socket+"\""+" threadNumber =\""+userThreadNumber+"\"");
  26. this.socket = socket;//save the parameters
  27. try {
  28. this.socket.setKeepAlive(true);
  29.  
  30. } catch (SocketException e) {
  31. Server.console("Socket Broken: "+socket);
  32. destroyThread = true;
  33.  
  34.  
  35. }
  36.  
  37. start();//start the thread running
  38.  
  39. }
  40.  
  41. public void run() {
  42. try {
  43. //Create a DataInputStream for communication;
  44. //the client is using a DataOutputStream to write to us
  45. din = new DataInputStream( socket.getInputStream() );
  46. dout = new DataOutputStream( socket.getOutputStream() );
  47. destroyThread=false;
  48. while (!destroyThread) {
  49. action = din.readUTF();
  50.  
  51.  
  52. if (action.equals("logIn")){
  53. username = din.readUTF();
  54. password = din.readUTF();
  55.  
  56. for (int i = 0; i < Server.userList.size(); i++){
  57. if (Server.userList.get(i).username.equals(username)){
  58. if (Server.userList.get(i).password.equals(password) && !Server.userList.get(i).isloggedin){
  59. Server.console("Login: username =\""+username+"\" threadNumber=\""+userThreadNumber+"\"");
  60. loggedIn = true;
  61. userNumber = i;//sets the userNumber to the place in the userList array
  62. Server.userList.get(userNumber).userThreadNumber=userThreadNumber;
  63. Server.userList.get(userNumber).isloggedin=true;
  64. dout.writeUTF("confirmLogin");//tell the user that the login worked
  65. dout.flush();
  66.  
  67. //for the amount of friend requests in the users "inbox"
  68. for (int ii=0; ii< Server.userList.get(userNumber).friendRequestArray.size();ii++) {
  69. dout.writeUTF("friendRequest");//the protocol
  70. dout.flush();
  71. dout.writeUTF(Server.userList.get(userNumber).friendRequestArray.get(ii)+"");//send the user number of the person trying to add you
  72. dout.flush();
  73. dout.writeUTF(Server.userList.get( Server.userList.get(userNumber).friendRequestArray.get(ii) ).username+"");//send the username of the person trying to add you
  74. dout.flush();
  75. }
  76. //tells the client the list of friends in the userlist
  77. for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
  78. int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
  79. dout.writeUTF("friendList");
  80. dout.flush();
  81. dout.writeUTF(""+friendNumber);//write the user ID
  82. dout.flush();
  83. dout.writeUTF(Server.userList.get(friendNumber).username);//write the friend's name;
  84. dout.flush();
  85.  
  86. }
  87.  
  88. //tell user which friends are online
  89. for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
  90. int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
  91. if (Server.userList.get(Server.userList.get(userNumber).friendArray.get(ii)).isloggedin){// if the friend is online
  92. dout.writeUTF("friendOnline");
  93. dout.flush();
  94. dout.writeUTF(friendNumber+"");//write the user ID
  95. dout.flush();
  96. }
  97. }
  98. //tell user which friends are offline
  99. for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
  100. int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
  101. if (!Server.userList.get(friendNumber).isloggedin){// if the friend is online
  102. dout.writeUTF("friendOffline");
  103. dout.flush();
  104. dout.writeUTF(friendNumber+"");//write the user ID
  105. dout.flush();
  106. }
  107. }
  108. dout.writeUTF("updateFriendDisplay");
  109. dout.flush();
  110.  
  111. //update all my friend's lists if they're online
  112. for (int ii=0; ii<Server.userList.get(userNumber).friendArray.size();ii++){
  113. int friendNumber = Server.userList.get(userNumber).friendArray.get(ii); //the friend ID
  114. if (Server.userList.get(friendNumber).isloggedin){// if the friend is online
  115.  
  116. //go into other friends arrays and invoke the change online method with the current usernumber
  117. int friendThreadNumber = Server.userList.get(friendNumber).userThreadNumber;
  118. JFrame hi = new JFrame(friendThreadNumber+"=friend thread number");
  119. hi.setVisible(true);
  120. Server.serverThreadList.get( friendThreadNumber ).changeOnline(userNumber);
  121.  
  122.  
  123.  
  124. }
  125. }
  126.  
  127.  
  128.  
  129.  
  130. break;
  131. }
  132.  
  133. else {
  134. Server.console("Login Error: Wrong username/password");
  135. dout.writeUTF("wrong:user/pass");
  136. dout.flush();
  137. dout.close();
  138. din.close();
  139. socket.close();
  140. destroyThread = true;
  141. break;
  142. }
  143. }
  144.  
  145.  
  146.  
  147.  
  148. //once the login process has finished for the right user
  149.  
  150. }
  151.  
  152.  
  153.  
  154.  
  155. //if the username hasn't been found in the userList
  156. if (!loggedIn){
  157. Server.console("Login Error: Cannot Find user in the database");
  158. dout.writeUTF("wrong:user/pass");
  159. dout.flush();
  160. dout.close();
  161. din.close();
  162. socket.close();
  163. destroyThread = true;
  164. }
  165. }
  166. else if (action.equals("invite")){
  167. int friendID=0;
  168. friendID = Integer.parseInt(din.readUTF());
  169. int convoNumber = 0;
  170. convoNumber =Integer.parseInt(din.readUTF());
  171.  
  172. }
  173. else if (action.equals("message")){
  174. int convoNumber = Integer.parseInt(din.readUTF());
  175. String message = din.readUTF();
  176. convoArray.get(convoNumber).sendMessage(message);
  177. }
  178. else if (action.equals("register")){
  179. username = din.readUTF();
  180. password = din.readUTF();
  181. for (int i = 0; i < Server.userList.size(); i++){
  182. if (Server.userList.get(i).username.equals(username)){
  183. dout.writeUTF("taken");//if the username already exists display taken
  184. destroyThread = true;
  185. Server.console("Register: username taken =\""+username+"\"");
  186. break;//stops the loop
  187. }
  188. }
  189. //if the thread hasn't been stopped
  190. if (!destroyThread){
  191. dout.writeUTF("confirm");
  192. Server.userList.add(new UserInfo(Server.userList.size(), username, password));
  193. Server.console("Register: username added =\""+username+"\" Password =\""+password+"\" UserNumber =\""+ (Server.userList.size()-1)+"\"");
  194. destroyThread=true;
  195. }
  196.  
  197.  
  198. }
  199. else if (action.equals("addFriend")){
  200. String friend = "";
  201. friend = din.readUTF();
  202. System.out.println("Friend added to PendingArray");
  203. boolean friendFound = false;
  204. for (int i = 0; i < Server.userList.size(); i++){
  205.  
  206. if (Server.userList.get(i).username.equals(friend)){
  207. Server.userList.get(i).friendRequestArray.add(userNumber);
  208. Server.userList.get(userNumber).pendingFriendArray.add(i);
  209. System.out.print("successfully");
  210. friendFound = true;
  211. break;
  212.  
  213. }
  214. }
  215. if (!friendFound) {
  216. System.out.print(" unsuccessfully");
  217. }
  218. }
  219. else if (action.equals("removeFriend")){
  220. int friendID = 0;
  221. friendID = Integer.parseInt(din.readUTF());
  222. //goes through current user's friends
  223. for (int i = 0; i < Server.userList.get(userNumber).friendArray.size(); i++){
  224. if (Server.userList.get(userNumber).friendArray.get(i).equals(friendID)){
  225. //removes the friend from the current user's friend list.
  226. Server.userList.get(userNumber).friendArray.remove(i);
  227.  
  228. }
  229. }
  230. //removes the user from the friend's list
  231. for (int i = 0; i < Server.userList.get(friendID).friendArray.size(); i++){
  232. if (Server.userList.get(friendID).friendArray.get(i).equals(userNumber)){
  233.  
  234. Server.userList.get(friendID).friendArray.remove(i);
  235. }
  236. }
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243. else if (action.equals("")){
  244.  
  245.  
  246. }
  247. else if (action.equals("logOut")){
  248. destroyThread=true;
  249.  
  250.  
  251.  
  252. }
  253. else if (action.equals("requestReply")){
  254. int friendID = Integer.parseInt(din.readUTF());
  255. String status = din.readUTF();
  256.  
  257.  
  258.  
  259. if (status.equals("accept")){
  260. for (int i =0; i<Server.userList.get(userNumber).friendRequestArray.size() ;i++ ){//removes the numbers from the friendRequestArray
  261. if (Server.userList.get(userNumber).friendRequestArray.get(i).equals(friendID)){
  262. Server.userList.get(userNumber).friendRequestArray.remove(i);
  263. break;
  264. }
  265. }
  266. for (int i=0; i < Server.userList.get(friendID).pendingFriendArray.size(); i++){
  267. if (Server.userList.get(friendID).pendingFriendArray.get(i).equals(userNumber)){
  268. Server.userList.get(friendID).pendingFriendArray.remove(i);
  269. }
  270. }
  271. Server.userList.get(userNumber).friendArray.add(friendID);
  272. Server.userList.get(friendID).friendArray.add(userNumber);
  273. }
  274. else if (status.equals("deny")){
  275. for (int i =0; i<Server.userList.get(userNumber).friendRequestArray.size() ;i++ ){//removes the numbers from the friendRequestArray
  276. if (Server.userList.get(userNumber).friendRequestArray.get(i).equals(friendID)){
  277. Server.userList.get(userNumber).friendRequestArray.remove(i);
  278. break;
  279. }
  280. }
  281. for (int i=0; i < Server.userList.get(friendID).pendingFriendArray.size(); i++){
  282. if (Server.userList.get(friendID).pendingFriendArray.get(i).equals(userNumber)){
  283. Server.userList.get(friendID).pendingFriendArray.remove(i);
  284. }
  285. }
  286. }
  287. }
  288.  
  289.  
  290. }
  291. } catch( EOFException ie ) {
  292. Server.console("flag1");
  293. } catch( IOException ie ) {
  294. // This does; tell the world!
  295. Server.console("flag2");
  296. }
  297.  
  298.  
  299. logout();
  300. }
  301.  
  302. public void changeOnline(int ID){
  303. try {
  304. dout.writeUTF("friendOnline");
  305. dout.flush();
  306. dout.writeUTF(""+ID);
  307. dout.flush();
  308. dout.writeUTF("updateFriendDisplay");
  309. dout.flush();
  310.  
  311. } catch (IOException e) {
  312. // TODO Auto-generated catch block
  313. e.printStackTrace();
  314. }
  315.  
  316. }
  317. public static void changeOffline(int ID){
  318. try {
  319. dout.writeUTF("friendOffline");
  320. dout.flush();
  321. dout.writeUTF(""+ID);
  322. dout.flush();
  323. dout.writeUTF("updateFriendDisplay");
  324. dout.flush();
  325. } catch (IOException e) {
  326. // TODO Auto-generated catch block
  327. e.printStackTrace();
  328. }
  329.  
  330. }
  331. public static void sendMessageToClient(int convoID, String name, String message){
  332. try {
  333. dout.writeUTF("message");
  334. dout.flush();
  335. dout.writeUTF(""+convoID);
  336. dout.flush();
  337. dout.writeUTF(name);
  338. dout.flush();
  339. dout.writeUTF(message);
  340. dout.flush();
  341. //need to finish sending the message to the user
  342. } catch (IOException e) {
  343. // TODO Auto-generated catch block
  344. e.printStackTrace();
  345. }
  346.  
  347. }
  348. public static void logout(){
  349. try {
  350. dout.close();
  351. din.close();
  352. socket.close();
  353. } catch (IOException e) {
  354. // TODO Auto-generated catch block
  355. e.printStackTrace();
  356. }
  357.  
  358.  
  359. Server.console("Disconnect: username =\""+username+ "\" socket =\""+socket+"\"");
  360. if (userNumber>-1){
  361. Server.userList.get(userNumber).isloggedin=false;
  362.  
  363. }
  364.  
  365.  
  366. //change everybody's thread numbers as removing this thread
  367.  
  368.  
  369.  
  370. for (int i = userThreadNumber; i < Server.serverThreadList.size();i++){
  371.  
  372. int currentUserNumber = Server.serverThreadList.get(i).userNumber;
  373. Server.console(""+Server.serverThreadList.get(i).userNumber);
  374. Server.userList.get(currentUserNumber).userThreadNumber-=1;//reduces every user's thread id by 1
  375. Server.console(""+Server.userList.get(currentUserNumber).userThreadNumber);
  376.  
  377.  
  378. Server.serverThreadList.remove(userThreadNumber);
  379.  
  380.  
  381. }
  382.  
  383. }
  384. protected void finalize(){
  385.  
  386. }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement