Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import com.mysql.fabric.xmlrpc.Client;
- import com.mysql.jdbc.DatabaseMetaData;
- import com.mysql.jdbc.MySQLConnection;
- import com.mysql.jdbc.PreparedStatement;
- public class MainCTS extends Thread {// Main Client To Server
- private ObjectOutputStream out;
- private ObjectInputStream in;
- private Socket s;
- private Boolean running = true;
- private String msg;
- private ArrayList<String> AddedFriends = new ArrayList<String>();
- private String Username;
- private int ChatPort; // Chat With Friend
- private int VoicePort;
- private int port;
- Connection conn;
- Statement myStmt;
- ResultSet res;
- java.sql.DatabaseMetaData dbmd;
- Boolean checked = false;
- String table;
- MainCTS(Socket sockMain) {
- s = sockMain;
- }
- public void run() {
- try {
- out = new ObjectOutputStream(s.getOutputStream());
- in = new ObjectInputStream(s.getInputStream());
- chat.OutputArrayMain.add(out);
- } catch (IOException e) {
- e.printStackTrace();
- try {
- chat.serverMain.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- while(running) {
- try {
- msg = in.readObject().toString();
- if(msg != null) {
- String[] part = msg.split("/");
- if(msg.equals("QUIT")) {
- chat.serverMain.close();
- }
- if(part[0].equals("Login")) {
- Username = part[1];
- String Password = part[2];
- try {
- Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "holton7021");
- Statement myStmt = conn.createStatement();
- ResultSet res = myStmt.executeQuery("SELECT Username,Password FROM users WHERE Username= '"+Username+"' AND Password = '"+Password+"'");
- if(res.next() && res.getRow() == 1) {
- System.out.println(s+" Logged in as "+Username);
- chat.UsersConnected.add(Username);
- chat.OutputLoggedIn.add(out);
- out.writeObject("Login-Accepted");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/FriendList", "root", "holton7021");
- myStmt = conn.createStatement();
- String stm = "SELECT friend,friendname FROM "+Username+" WHERE friend = 1";
- res = myStmt.executeQuery(stm);
- if(res.next()) {
- do {
- String message = "Friend-"+res.getString("friendname");
- out.writeObject(message);
- AddedFriends.add(res.getString("friendname"));
- }while(res.next());
- }
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "holton7021");
- myStmt = conn.createStatement();
- for(String friend : AddedFriends) {
- res = myStmt.executeQuery("SELECT Username,EMail,Age,Location,Description,ProfilePic FROM users WHERE Username = '"+friend+"'");
- if(res.next()) {
- String EMail = res.getString("EMail");
- String Age = res.getString("Age"); // SET UP LOCATION, DESCRIPTION AND PROFILEPIC
- out.writeObject("FriendInfo-"+EMail+"-"+Age);
- }
- }
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Messages", "root", "holton7021"); // DONT CHECH DB FOR EVERY MESSAGE IF ITS TO THE SAME PERSON
- myStmt = conn.createStatement();
- dbmd = conn.getMetaData();
- for(String friend : AddedFriends) {
- if(dbmd.getTables(null, null, Username+friend, null).next()) {
- res = myStmt.executeQuery("SELECT * FROM "+Username+friend);
- if(res.next()) {
- do{
- String ts = res.getString("Time_Stamp");
- String msg = res.getString("Msg");
- out.writeObject("MSG-"+friend+"-"+msg);
- }while(res.next());
- }else{}// ` ???
- }else if(dbmd.getTables(null, null, friend+Username, null).next()) {
- res = myStmt.executeQuery("SELECT * FROM "+friend+Username);
- if(res.next()) {
- do{
- String ts = res.getString("Time_Stamp");
- String msg = res.getString("Msg");
- out.writeObject("MSG-"+friend+"-"+msg);
- }while(res.next());
- }
- }else{}
- }
- }else {
- out.writeObject("Login-Failed");
- }
- } catch (SQLException e) {
- System.out.println("ERROR SQL");
- e.printStackTrace();
- }
- }else if(part[0].equals("Call") || part[0].equals("End")) {
- if(!chat.UsersConnected.contains(part[1])) {
- out.writeObject("IsOffline-"+part[1]);
- }else {
- int pos = chat.UsersConnected.indexOf(part[1]);
- ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
- if(part[0].equals("Call")) {
- friendSock.writeObject("Call-"+Username);
- }else {
- friendSock.writeObject("End-"+Username);
- }
- }
- }else if(part[0].equals("Accepted") || part[0].equals("Declined")) {
- if(part[0].equals("Accepted")) {
- int pos = chat.UsersConnected.indexOf(part[1]);
- ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
- friendSock.writeObject("Accepted-"+Username);
- }else {
- int pos = chat.UsersConnected.indexOf(part[1]);
- ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
- friendSock.writeObject("Declined-"+part[1]);
- }
- }else if(part[0].equals("StartServer")) {
- if(chat.ports.size() != 4) {
- for(int p = 65510;p < 65514;p++) {
- if(!chat.ports.contains(p)) {
- port = p;
- VoicePort = port+1;
- chat.ports.add(port);
- chat.ports.add(VoicePort);
- System.out.println("Chat Port: "+port+" Voice Chat Port: "+VoicePort);
- break;
- }
- }
- }
- new accept(port, VoicePort).start();
- out.writeObject("Join-74.133.13.176-"+port+"-"+VoicePort);
- int pos = chat.UsersConnected.indexOf(part[1]);
- ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
- friendSock.writeObject("Join-74.133.13.176-"+port+"-"+VoicePort);
- }else if(part[0].equals("JoinCall")) {
- int pos = chat.UsersConnected.indexOf(part[1]);
- ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
- friendSock.writeObject("Join-74.133.13.176-"+part[2]+"-"+part[3]);
- }else if(part[0].equals("MSG")) {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Messages", "root", "holton7021"); // DONT CHECH DB FOR EVERY MESSAGE IF ITS TO THE SAME PERSON
- dbmd = conn.getMetaData();
- res = dbmd.getTables(null, null, part[1]+part[2], null);
- if(res.next()) {
- table = part[1]+part[2];
- }else if((res = dbmd.getTables(null, null, part[2]+part[1], null)).next()) {
- table = part[2]+part[1];
- }else {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Messages", "root", "holton7021");
- myStmt = conn.createStatement();
- myStmt.executeUpdate("CREATE TABLE "+part[1]+part[2]+"(Time_Stamp TEXT,Msg TEXT,ID INT NOT NULL AUTO_INCREMENT,PRIMARY KEY(ID))");
- table = part[1]+part[2];
- }
- myStmt = conn.createStatement();
- PreparedStatement preparedStmt = (PreparedStatement) conn.prepareStatement("INSERT INTO "+table+" (Time_Stamp, Msg) VALUES (?, ?)");
- preparedStmt.setString(1, "NULL");
- preparedStmt.setString(2, part[3]);
- preparedStmt.execute();
- if(chat.UsersConnected.contains(part[2])) {
- int pos = chat.UsersConnected.indexOf(part[2]);
- ObjectOutputStream friendSock = chat.OutputLoggedIn.get(pos);
- friendSock.writeObject("MSG-"+Username+"-"+part[3]);
- }
- out.writeObject("MSG-"+Username+part[2]+"-"+part[3]);
- }
- }
- } catch (ClassNotFoundException | IOException e) {
- chat.OutputLoggedIn.remove(out);
- chat.UsersConnected.remove(Username);
- chat.OutputArrayMain.remove(out);
- running = false;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- System.out.println(s + " Disconnected from main server");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement