Java RMI服务器方法只起作用一次

时间:2019-05-25 08:31:48

标签: java database server client rmi

我写了一个基于RMI和JavaFX的简单图书图书馆数据库。它的工作原理是:先启动服务器,然后启动客户端。在客户端程序中有2个选项卡,一个用于注册新用户,第二个用于登录用户。 问题是我只能登录到第一个创建的用户帐户。当我注册(saveUser)更多用户时,loginPreson方法不会返回它们,而只会创建第一个。 我有很多代码,我什至都不知道在这里有用什么,因此,我将首先粘贴服务器代码,我的userMenuControllermainMenuController类。

ServerOperation.java

public class ServerOperation extends UnicastRemoteObject implements RMIInterface {

    private static final long serialVersionUID = 20120731125400L;
    private ArrayList<Person> users;
    private Library library;

    public ServerOperation() throws RemoteException {
        super();
        users = new ArrayList<>();
        library = new Library();
    }

    @Override
    public void saveUser(String name, String surname, String email, String password) throws RemoteException {

        users.add(new Person(name, surname, email, password));
        System.out.println("email=" + email + " : password=" + password + " REGISTERED");
    }

    @Override
    public Person accessUser(String email, String password) throws RemoteException {
        for (Person p : users) {
            if (p.getPersonEmail().equals(email) && p.getPersonPassword().equals(password)) {
                System.out.println("email=" + email + " : password=" + password + " LOGGED IN");
                return p;
            } else {
                System.err.println("accessUser: Wrong email or password");
                return null;
            }
        }
        System.err.println("accessUser: null?");
        return null;
    }

    @Override
    public ArrayList<Book> getAvailableBooks() throws RemoteException {
        return library.availableLibraryBooks();
    }

    public static void main(String[] args) {

        try {
            Naming.rebind("//localhost/MyServer", new ServerOperation());
            System.err.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }
    }
}

mainMenuController.java

public class mainMenuController implements Initializable {

    //...
    private static RMIInterface look_up;

    public mainMenuController() throws MalformedURLException, RemoteException, NotBoundException {
        look_up = (RMIInterface) Naming.lookup("//localhost/MyServer");
    }

    public void loginPerson(ActionEvent e) {

        try {

            Person p = look_up.accessUser(loginTabEmail.getText(), loginTabPassword.getText());

            if (p == null) {
                System.out.println("loginPerson: Bad login");
            } else {
                System.out.println("User logged in! " + loginTabEmail.getText() + " " + loginTabPassword.getText());
                loginTabEmail.clear();
                loginTabPassword.clear();

                FXMLLoader loader = new FXMLLoader(getClass().getResource("userMenu.fxml"));
                SplitPane newWindow = (SplitPane) loader.load();
                userMenuController controller = loader.getController();
                controller.setMainMenu(this);
                controller.setMyUser(p); // LOGGED USER
                Stage stage = new Stage();
                stage.initModality(Modality.WINDOW_MODAL);
                stage.initOwner(loginTabSignIn.getScene().getWindow());
                stage.setTitle("MyLibrary");
                Scene scene = new Scene(newWindow);
                stage.setScene(scene);
                stage.show();
            }
        } catch (Exception i) {
            System.err.println("Client exception: " + e.toString());
            i.printStackTrace();
        }

    }

    public void registerPerson(ActionEvent e) {

        try {
            look_up.saveUser(registerTabName.getText(), registerTabSurname.getText(),
                    registerTabEmail.getText(), registerTabPassword.getText());

            System.out.println("User registered! " + registerTabName.getText() + " " + registerTabSurname.getText() + " " +
                    registerTabEmail.getText() + " " + registerTabPassword.getText());

            registerTabName.clear();
            registerTabSurname.clear();
            registerTabEmail.clear();
            registerTabPassword.clear();

            FXMLLoader loader = new FXMLLoader(getClass().getResource("userRegistered.fxml"));
            AnchorPane newWindow = (AnchorPane) loader.load();
            userRegisteredController controller = loader.getController();
            controller.setMainMenu(this);
            Stage stage = new Stage();
            stage.initModality(Modality.WINDOW_MODAL);
            stage.initOwner(registerTabSignUp.getScene().getWindow());
            stage.setTitle("");
            Scene scene = new Scene(newWindow);
            stage.setScene(scene);
            stage.show();
        } catch (Exception i) {
            System.err.println("Client exception: " + e.toString());
            i.printStackTrace();
        }
    }
    //...
}

userMenuController.java-在loginPerson

中调用
public class userMenuController implements Initializable {

    private mainMenuController mainMenu;
    private Person myUser;
    private static RMIInterface look_up;

    //...

    public userMenuController() throws MalformedURLException, RemoteException, NotBoundException {
        look_up = (RMIInterface) Naming.lookup("//localhost/MyServer");
    }

    public void showMyBooks() {
        myBooksSection.setItems(userMenuBooks);

        userMenuBooks.add("asdsdasdas");
    }

    public void showLibraryBooks() throws MalformedURLException, RemoteException, NotBoundException {

        libraryBooksSection.setItems(libraryObservableList);
        for (Book b : look_up.getAvailableBooks()) {
            libraryObservableList.add(b.getBookTitle());
        }
    }

    //...
}

0 个答案:

没有答案