Advertisement
Guest User

derp

a guest
Oct 16th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.35 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.ObjectInputStream;
  3. import java.io.ObjectOutputStream;
  4. import java.net.Socket;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import com.mysql.fabric.xmlrpc.Client;
  12. import com.mysql.jdbc.DatabaseMetaData;
  13. import com.mysql.jdbc.MySQLConnection;
  14. import com.mysql.jdbc.PreparedStatement;
  15.  
  16. public class MainCTS extends Thread {//     Main Client To Server
  17.         private ObjectOutputStream out;
  18.         private ObjectInputStream in;
  19.         private Socket s;
  20.         private Boolean running = true;
  21.         private String msg;
  22.         private ArrayList<String> AddedFriends = new ArrayList<String>();
  23.         private String Username;
  24.        
  25.         private int ChatPort; // Chat With Friend
  26.         private int VoicePort;
  27.         private int port;
  28.         Connection conn;
  29.         Statement myStmt;
  30.         ResultSet res;
  31.         java.sql.DatabaseMetaData dbmd;
  32.         Boolean checked = false;
  33.         String table;
  34.        
  35.         MainCTS(Socket sockMain) {
  36.             s = sockMain;
  37.         }
  38.         public void run() {
  39.             try {
  40.                 out = new ObjectOutputStream(s.getOutputStream());
  41.                 in = new ObjectInputStream(s.getInputStream());
  42.                 chat.OutputArrayMain.add(out);
  43.             } catch (IOException e) {
  44.                 e.printStackTrace();
  45.                 try {
  46.                     chat.serverMain.close();
  47.                 } catch (IOException e1) {
  48.                     e1.printStackTrace();
  49.                 }
  50.             }
  51.             while(running) {
  52.                 try {
  53.                     msg = in.readObject().toString();
  54.                     if(msg != null) {
  55.                         String[] part = msg.split("/");
  56.                         if(msg.equals("QUIT")) {
  57.                             chat.serverMain.close();
  58.                         }
  59.                         if(part[0].equals("Login")) {
  60.                             Username = part[1];
  61.                             String Password = part[2];
  62.                             try {
  63.                                 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "holton7021");
  64.                                 Statement myStmt = conn.createStatement();
  65.                                 ResultSet res = myStmt.executeQuery("SELECT Username,Password FROM users WHERE Username= '"+Username+"' AND Password = '"+Password+"'");
  66.                                 if(res.next() && res.getRow() == 1) {
  67.                                     System.out.println(s+" Logged in as "+Username);
  68.                                     chat.UsersConnected.add(Username);
  69.                                     chat.OutputLoggedIn.add(out);
  70.                                     out.writeObject("Login-Accepted");
  71.                                     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/FriendList", "root", "holton7021");
  72.                                     myStmt = conn.createStatement();
  73.                                     String stm = "SELECT friend,friendname FROM "+Username+" WHERE friend = 1";
  74.                                     res = myStmt.executeQuery(stm);
  75.                                     if(res.next()) {
  76.                                         do {
  77.                                             String message = "Friend-"+res.getString("friendname");
  78.                                             out.writeObject(message);
  79.                                             AddedFriends.add(res.getString("friendname"));
  80.                                         }while(res.next());
  81.                                     }
  82.                                     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "holton7021");
  83.                                     myStmt = conn.createStatement();
  84.                                     for(String friend : AddedFriends) {
  85.                                         res = myStmt.executeQuery("SELECT Username,EMail,Age,Location,Description,ProfilePic FROM users WHERE Username = '"+friend+"'");
  86.                                         if(res.next()) {
  87.                                             String EMail = res.getString("EMail");
  88.                                             String Age = res.getString("Age");  // SET UP LOCATION, DESCRIPTION AND PROFILEPIC
  89.                                             out.writeObject("FriendInfo-"+EMail+"-"+Age);
  90.                                         }
  91.                                     }
  92.                                     conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Messages", "root", "holton7021"); // DONT CHECH DB FOR EVERY MESSAGE IF ITS TO THE SAME PERSON
  93.                                     myStmt = conn.createStatement();
  94.                                     dbmd = conn.getMetaData();
  95.                                     for(String friend : AddedFriends) {
  96.                                         if(dbmd.getTables(null, null, Username+friend, null).next()) {
  97.                                             res = myStmt.executeQuery("SELECT * FROM "+Username+friend);
  98.                                             if(res.next()) {
  99.                                                 do{
  100.                                                     String ts = res.getString("Time_Stamp");
  101.                                                     String msg = res.getString("Msg");
  102.                                                     out.writeObject("MSG-"+friend+"-"+msg);
  103.                                                 }while(res.next());
  104.                                             }else{}// `                                                                             ???
  105.                                         }else if(dbmd.getTables(null, null, friend+Username, null).next()) {
  106.                                             res = myStmt.executeQuery("SELECT * FROM "+friend+Username);
  107.                                             if(res.next()) {
  108.                                                 do{
  109.                                                     String ts = res.getString("Time_Stamp");
  110.                                                     String msg = res.getString("Msg");
  111.                                                     out.writeObject("MSG-"+friend+"-"+msg);
  112.                                                 }while(res.next());
  113.                                             }
  114.                                         }else{}
  115.                                     }
  116.                                 }else {
  117.                                     out.writeObject("Login-Failed");
  118.                                 }
  119.                             } catch (SQLException e) {
  120.                                 System.out.println("ERROR SQL");
  121.                                 e.printStackTrace();
  122.                             }
  123.                         }else if(part[0].equals("Call") || part[0].equals("End")) {
  124.                             if(!chat.UsersConnected.contains(part[1])) {
  125.                                 out.writeObject("IsOffline-"+part[1]);
  126.                             }else {
  127.                                 int pos = chat.UsersConnected.indexOf(part[1]);
  128.                                 ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
  129.                                 if(part[0].equals("Call")) {
  130.                                     friendSock.writeObject("Call-"+Username);
  131.                                 }else {
  132.                                     friendSock.writeObject("End-"+Username);
  133.                                 }
  134.                             }
  135.                         }else if(part[0].equals("Accepted") || part[0].equals("Declined")) {
  136.                             if(part[0].equals("Accepted")) {
  137.                                 int pos = chat.UsersConnected.indexOf(part[1]);
  138.                                 ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
  139.                                 friendSock.writeObject("Accepted-"+Username);
  140.                             }else {
  141.                                 int pos = chat.UsersConnected.indexOf(part[1]);
  142.                                 ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
  143.                                 friendSock.writeObject("Declined-"+part[1]);
  144.                             }
  145.                         }else if(part[0].equals("StartServer")) {
  146.                             if(chat.ports.size() != 4) {
  147.                                 for(int p = 65510;p < 65514;p++) {
  148.                                     if(!chat.ports.contains(p)) {
  149.                                         port = p;
  150.                                         VoicePort = port+1;
  151.                                         chat.ports.add(port);
  152.                                         chat.ports.add(VoicePort);
  153.                                         System.out.println("Chat Port: "+port+" Voice Chat Port: "+VoicePort);
  154.                                         break;
  155.                                     }
  156.                                 }
  157.                             }
  158.                             new accept(port, VoicePort).start();
  159.                             out.writeObject("Join-74.133.13.176-"+port+"-"+VoicePort);
  160.                             int pos = chat.UsersConnected.indexOf(part[1]);
  161.                             ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
  162.                             friendSock.writeObject("Join-74.133.13.176-"+port+"-"+VoicePort);
  163.                         }else if(part[0].equals("JoinCall")) {
  164.                             int pos = chat.UsersConnected.indexOf(part[1]);
  165.                             ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
  166.                             friendSock.writeObject("Join-74.133.13.176-"+part[2]+"-"+part[3]);
  167.                         }else if(part[0].equals("MSG")) {
  168.                             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Messages", "root", "holton7021"); // DONT CHECH DB FOR EVERY MESSAGE IF ITS TO THE SAME PERSON
  169.                             dbmd = conn.getMetaData();
  170.                             res = dbmd.getTables(null, null, part[1]+part[2], null);
  171.                             if(res.next()) {
  172.                                 table = part[1]+part[2];
  173.                             }else if((res = dbmd.getTables(null, null, part[2]+part[1], null)).next()) {
  174.                                 table = part[2]+part[1];
  175.                             }else {
  176.                                 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Messages", "root", "holton7021");
  177.                                 myStmt = conn.createStatement();
  178.                                 myStmt.executeUpdate("CREATE TABLE "+part[1]+part[2]+"(Time_Stamp TEXT,Msg TEXT,ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(ID))");
  179.                                 table = part[1]+part[2];
  180.                             }
  181.                             myStmt = conn.createStatement();
  182.                             PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement("INSERT INTO "+table+" (Time_Stamp, Msg) VALUES (?, ?)");
  183.                             preparedStmt.setString(1, "NULL");
  184.                             preparedStmt.setString(2, part[3]);
  185.                             preparedStmt.execute();
  186.                             if(chat.UsersConnected.contains(part[2])) {
  187.                                 int pos = chat.UsersConnected.indexOf(part[2]);
  188.                                 ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
  189.                                 friendSock.writeObject("MSG-"+Username+"-"+part[3]);
  190.                             }
  191.                             out.writeObject("MSG-"+Username+part[2]+"-"+part[3]);
  192.                         }
  193.                     }
  194.                 } catch (ClassNotFoundException | IOException e) {
  195.                     chat.OutputLoggedIn.remove(out);
  196.                     chat.UsersConnected.remove(Username);
  197.                     chat.OutputArrayMain.remove(out);
  198.                     running = false;
  199.                 } catch (SQLException e) {
  200.                     e.printStackTrace();
  201.                 }
  202.             }
  203.             System.out.println(s + " Disconnected from main server");
  204.         }
  205.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement