Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample.controllers;
- import javafx.application.Application;
- import javafx.fxml.FXML;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Node;
- import javafx.scene.Parent;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.input.MouseEvent;
- import javafx.stage.Stage;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- public class Client extends Application {
- // for I/O
- private ObjectInputStream sInput; // to read from the socket
- private ObjectOutputStream sOutput; // to write on the socket
- private Socket socket;
- // if I use a GUI or not
- private ClientGUI cg;
- // the server, the port and the username
- private String server, username;
- private int port;
- boolean keepGoing;
- private Object RegisterController;
- /*
- * Constructor called by console mode
- * server: the server address
- * port: the port number
- * username: the username
- */
- Client(String server, int port, String username) {
- // which calls the common constructor with the GUI set to null
- this(server, port, username, null);
- }
- /*
- * Constructor call when used from a GUI
- * in console mode the ClienGUI parameter is null
- */
- Client(String server, int port, String username, ClientGUI cg) {
- keepGoing = true;
- this.server = server;
- this.port = port;
- this.username = username;
- // save if we are in GUI mode or not
- this.cg = cg;
- }
- /*
- * To start the dialog
- */
- public boolean start() {
- // try to connect to the server
- try {
- socket = new Socket(server, port);
- }
- // if it failed not much I can so
- catch(Exception ec) {
- display("Error connectiong to server:" + ec);
- return false;
- }
- String msg = "Connection accepted " + socket.getInetAddress() + ":" + socket.getPort();
- display(msg);
- /* Creating both Data Stream */
- try
- {
- sInput = new ObjectInputStream(socket.getInputStream());
- sOutput = new ObjectOutputStream(socket.getOutputStream());
- }
- catch (IOException eIO) {
- display("Exception creating new Input/output Streams: " + eIO);
- return false;
- }
- // creates the Thread to listen from the server
- new sample.controllers.Client.ListenFromServer().start();
- // Send our username to the server this is the only message that we
- // will send as a String. All other messages will be ChatMessage objects
- try
- {
- sOutput.writeObject(username);
- }
- catch (IOException eIO) {
- display("Exception doing login : " + eIO);
- disconnect();
- return false;
- }
- // success we inform the caller that it worked
- return true;
- }
- /*
- * To send a message to the console or the GUI
- */
- private void display(String msg) {
- if(cg == null)
- System.out.println(msg); // println in console mode
- else
- cg.append(msg + "\n"); // append to the ClientGUI JTextArea (or whatever)
- }
- /*
- * To send a message to the server
- */
- void sendMessage(ChatMessage msg) {
- try {
- sOutput.writeObject(msg);
- }
- catch(IOException e) {
- display("Exception writing to server: " + e);
- }
- }
- /*
- * When something goes wrong
- * Close the Input/Output streams and disconnect not much to do in the catch clause
- */
- private void disconnect() {
- try {
- if(sInput != null) sInput.close();
- }
- catch(Exception e) {} // not much else I can do
- try {
- if(sOutput != null) sOutput.close();
- }
- catch(Exception e) {} // not much else I can do
- try{
- if(socket != null) socket.close();
- }
- catch(Exception e) {} // not much else I can do
- // inform the GUI
- if(cg != null)
- cg.connectionFailed();
- keepGoing = false;
- }
- /*
- * To start the Client in console mode use one of the following command
- * > java Client
- * > java Client username
- * > java Client username portNumber
- * > java Client username portNumber serverAddress
- * at the console prompt
- * If the portNumber is not specified 1500 is used
- * If the serverAddress is not specified "localHost" is used
- * If the username is not specified "Anonymous" is used
- * > java Client
- * is equivalent to
- * > java Client Anonymous 1500 localhost
- * are eqquivalent
- *
- * In console mode, if an error occurs the program simply stops
- * when a GUI id used, the GUI is informed of the disconnection
- */
- void mainWindow()
- {
- FXMLLoader loader = new FXMLLoader(getClass().getResource("register.fxml"));
- loader.setController(RegisterController);
- }
- /*
- * a class that waits for the message from the server and append them to the JTextArea
- * if we have a GUI or simply System.out.println() it in console mode
- */
- class ListenFromServer extends Thread {
- public void run() {
- while(true) {
- try {
- String msg = (String) sInput.readObject();
- // if console mode print the message and add back the prompt
- if(cg == null) {
- System.out.println(msg);
- System.out.print("> ");
- }
- else {
- cg.append(msg);
- }
- }
- catch(IOException e) {
- display("Server has close the connection: " + e);
- if(cg != null)
- cg.connectionFailed();
- break;
- }
- // can't happen with a String object but need the catch anyhow
- catch(ClassNotFoundException e2) {
- }
- }
- }
- }
- //Main FXML Controller
- @FXML
- private Button Lab_Button;
- @FXML
- private Button Tutorials_Button;
- @FXML
- private Button logout_button;
- @FXML
- void Lab(MouseEvent event) {
- }
- @FXML
- void Logout(MouseEvent event) {
- }
- @FXML
- void Tutorials(MouseEvent event) {
- }
- //Login FXML Controller
- @FXML
- private TextField tf_username;
- @FXML
- private PasswordField pf_password;
- @FXML
- void login(MouseEvent event) {
- }
- @FXML
- void Login_Reg(MouseEvent event) {
- }
- //Register FXML Controller//
- @FXML
- private TextField tf_reg_username;
- @FXML
- private TextField tf_reg_email;
- @FXML
- private PasswordField pf_reg_password;
- @FXML
- void Reg_login(MouseEvent event) {
- }
- @FXML
- void register(MouseEvent event) {
- }
- @Override
- public void start(Stage stage) throws Exception{
- Parent root = FXMLLoader.load(getClass().getResource("/sample/controllers/register.fxml"));
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.setTitle("PL SQL Yourself!");
- stage.show();
- }
- public static void main(String[] args) {
- launch(args);
- /*// default values
- int portNumber = 1700;
- String serverAddress = "localhost";
- String userName = "Anonymous";
- // depending of the number of arguments provided we fall through
- // create the Client object
- sample.controllers.Client client = new sample.controllers.Client(serverAddress, portNumber, userName);
- // test if we can start the connection to the Server
- // if it failed nothing we can do
- if(!client.start())
- return;
- // done disconnect
- client.disconnect();*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement