Advertisement
Guest User

Client.java Class

a guest
Nov 13th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.02 KB | None | 0 0
  1. package sample.controllers;
  2.  
  3. import javafx.application.Application;
  4. import javafx.fxml.FXML;
  5. import javafx.fxml.FXMLLoader;
  6. import javafx.scene.Node;
  7. import javafx.scene.Parent;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.PasswordField;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.input.MouseEvent;
  13. import javafx.stage.Stage;
  14.  
  15. import java.io.IOException;
  16. import java.io.ObjectInputStream;
  17. import java.io.ObjectOutputStream;
  18. import java.net.Socket;
  19.  
  20. public class Client extends Application {
  21.  
  22.         // for I/O
  23.         private ObjectInputStream sInput;       // to read from the socket
  24.         private ObjectOutputStream sOutput;     // to write on the socket
  25.         private Socket socket;
  26.  
  27.         // if I use a GUI or not
  28.         private ClientGUI cg;
  29.  
  30.         // the server, the port and the username
  31.         private String server, username;
  32.         private int port;
  33.  
  34.  
  35.         boolean keepGoing;
  36.         private Object RegisterController;
  37.  
  38.         /*
  39.          *  Constructor called by console mode
  40.          *  server: the server address
  41.          *  port: the port number
  42.          *  username: the username
  43.          */
  44.         Client(String server, int port, String username) {
  45.             // which calls the common constructor with the GUI set to null
  46.             this(server, port, username, null);
  47.         }
  48.  
  49.         /*
  50.          * Constructor call when used from a GUI
  51.          * in console mode the ClienGUI parameter is null
  52.          */
  53.         Client(String server, int port, String username, ClientGUI cg) {
  54.             keepGoing = true;
  55.             this.server = server;
  56.             this.port = port;
  57.             this.username = username;
  58.             // save if we are in GUI mode or not
  59.             this.cg = cg;
  60.         }
  61.  
  62.         /*
  63.          * To start the dialog
  64.          */
  65.         public boolean start() {
  66.             // try to connect to the server
  67.             try {
  68.                 socket = new Socket(server, port);
  69.             }
  70.             // if it failed not much I can so
  71.             catch(Exception ec) {
  72.                 display("Error connectiong to server:" + ec);
  73.                 return false;
  74.             }
  75.  
  76.             String msg = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort();
  77.             display(msg);
  78.  
  79.             /* Creating both Data Stream */
  80.             try
  81.             {
  82.                 sInput  = new ObjectInputStream(socket.getInputStream());
  83.                 sOutput = new ObjectOutputStream(socket.getOutputStream());
  84.             }
  85.             catch (IOException eIO) {
  86.                 display("Exception creating new Input/output Streams: " + eIO);
  87.                 return false;
  88.             }
  89.  
  90.             // creates the Thread to listen from the server
  91.             new sample.controllers.Client.ListenFromServer().start();
  92.             // Send our username to the server this is the only message that we
  93.             // will send as a String. All other messages will be ChatMessage objects
  94.             try
  95.             {
  96.                 sOutput.writeObject(username);
  97.             }
  98.             catch (IOException eIO) {
  99.                 display("Exception doing login : " + eIO);
  100.                 disconnect();
  101.                 return false;
  102.             }
  103.             // success we inform the caller that it worked
  104.             return true;
  105.         }
  106.  
  107.         /*
  108.          * To send a message to the console or the GUI
  109.          */
  110.         private void display(String msg) {
  111.             if(cg == null)
  112.                 System.out.println(msg);      // println in console mode
  113.             else
  114.                 cg.append(msg + "\n");      // append to the ClientGUI JTextArea (or whatever)
  115.         }
  116.  
  117.         /*
  118.          * To send a message to the server
  119.          */
  120.         void sendMessage(ChatMessage msg) {
  121.             try {
  122.                 sOutput.writeObject(msg);
  123.             }
  124.             catch(IOException e) {
  125.                 display("Exception writing to server: " + e);
  126.             }
  127.         }
  128.  
  129.         /*
  130.          * When something goes wrong
  131.          * Close the Input/Output streams and disconnect not much to do in the catch clause
  132.          */
  133.         private void disconnect() {
  134.             try {
  135.                 if(sInput != null) sInput.close();
  136.             }
  137.             catch(Exception e) {} // not much else I can do
  138.             try {
  139.                 if(sOutput != null) sOutput.close();
  140.             }
  141.             catch(Exception e) {} // not much else I can do
  142.             try{
  143.                 if(socket != null) socket.close();
  144.             }
  145.             catch(Exception e) {} // not much else I can do
  146.  
  147.             // inform the GUI
  148.             if(cg != null)
  149.                 cg.connectionFailed();
  150.             keepGoing = false;
  151.         }
  152.         /*
  153.          * To start the Client in console mode use one of the following command
  154.          * > java Client
  155.          * > java Client username
  156.          * > java Client username portNumber
  157.          * > java Client username portNumber serverAddress
  158.          * at the console prompt
  159.          * If the portNumber is not specified 1500 is used
  160.          * If the serverAddress is not specified "localHost" is used
  161.          * If the username is not specified "Anonymous" is used
  162.          * > java Client
  163.          * is equivalent to
  164.          * > java Client Anonymous 1500 localhost
  165.          * are eqquivalent
  166.          *
  167.          * In console mode, if an error occurs the program simply stops
  168.          * when a GUI id used, the GUI is informed of the disconnection
  169.          */
  170.  
  171.  
  172.  
  173.  
  174.         void mainWindow()
  175.         {
  176.  
  177.             FXMLLoader loader = new FXMLLoader(getClass().getResource("register.fxml"));
  178.             loader.setController(RegisterController);
  179.         }
  180.  
  181.  
  182.  
  183.         /*
  184.          * a class that waits for the message from the server and append them to the JTextArea
  185.          * if we have a GUI or simply System.out.println() it in console mode
  186.          */
  187.         class ListenFromServer extends Thread {
  188.  
  189.             public void run() {
  190.                 while(true) {
  191.                     try {
  192.                         String msg = (String) sInput.readObject();
  193.  
  194.                         // if console mode print the message and add back the prompt
  195.                         if(cg == null) {
  196.  
  197.                             System.out.println(msg);
  198.                             System.out.print("> ");
  199.                         }
  200.                         else {
  201.                             cg.append(msg);
  202.                         }
  203.                     }
  204.                     catch(IOException e) {
  205.                         display("Server has close the connection: " + e);
  206.                         if(cg != null)
  207.                             cg.connectionFailed();
  208.                         break;
  209.                     }
  210.                     // can't happen with a String object but need the catch anyhow
  211.                     catch(ClassNotFoundException e2) {
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.         //Main FXML Controller
  217.         @FXML
  218.         private Button Lab_Button;
  219.  
  220.         @FXML
  221.         private Button Tutorials_Button;
  222.  
  223.         @FXML
  224.         private Button logout_button;
  225.  
  226.         @FXML
  227.         void Lab(MouseEvent event) {
  228.  
  229.         }
  230.  
  231.         @FXML
  232.         void Logout(MouseEvent event) {
  233.  
  234.         }
  235.  
  236.         @FXML
  237.         void Tutorials(MouseEvent event) {
  238.  
  239.         }
  240.  
  241.         //Login FXML Controller
  242.         @FXML
  243.         private TextField tf_username;
  244.  
  245.         @FXML
  246.         private PasswordField pf_password;
  247.  
  248.         @FXML
  249.         void login(MouseEvent event) {
  250.  
  251.         }
  252.  
  253.         @FXML
  254.         void Login_Reg(MouseEvent event) {
  255.  
  256.         }
  257.  
  258.         //Register FXML Controller//
  259.     @FXML
  260.     private TextField tf_reg_username;
  261.  
  262.     @FXML
  263.     private TextField tf_reg_email;
  264.  
  265.     @FXML
  266.     private PasswordField pf_reg_password;
  267.  
  268.     @FXML
  269.     void Reg_login(MouseEvent event) {
  270.  
  271.     }
  272.  
  273.     @FXML
  274.     void register(MouseEvent event) {
  275.  
  276.     }
  277.  
  278.  
  279.     @Override
  280.     public void start(Stage stage) throws Exception{
  281.         Parent root = FXMLLoader.load(getClass().getResource("/sample/controllers/register.fxml"));
  282.         Scene scene = new Scene(root);
  283.  
  284.         stage.setScene(scene);
  285.  
  286.         stage.setTitle("PL SQL Yourself!");
  287.  
  288.         stage.show();
  289.     }
  290.  
  291.  
  292.     public static void main(String[] args) {
  293.         launch(args);
  294.  
  295.         /*// default values
  296.         int portNumber = 1700;
  297.         String serverAddress = "localhost";
  298.         String userName = "Anonymous";
  299.  
  300.         // depending of the number of arguments provided we fall through
  301.  
  302.         // create the Client object
  303.         sample.controllers.Client client = new sample.controllers.Client(serverAddress, portNumber, userName);
  304.         // test if we can start the connection to the Server
  305.         // if it failed nothing we can do
  306.         if(!client.start())
  307.             return;
  308.         // done disconnect
  309.         client.disconnect();*/
  310.     }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement