Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.rmi.NotBoundException;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.util.Scanner;
- public class Client {
- private String mancom;
- private String taskDir;
- private static int PortRMI;
- private static String HostRMI;
- private static boolean alreadyLoggedIn = false;
- public Client (String mancom,String taskDir){
- this.mancom = mancom;
- this.setTaskDir(taskDir);
- }
- public static void main(String args[]) throws FileNotFoundException{
- RemoteLoginInterface loginObject = null;
- RemoteGeneralUserInterface user = null;
- Registry registry;
- if((args.length != 2) ) {
- System.out.println("The arguments that you have entered is missing or invalid");
- System.out.println("Usage: <managementComponent> <taskDir>");
- System.exit(0);
- }
- Client client = new Client(args[0],args[1]);
- client.readRegistryProperties();
- try {
- registry = LocateRegistry.getRegistry(HostRMI,PortRMI);
- loginObject = (RemoteLoginInterface) registry.lookup(client.getManagementComponent());
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (NotBoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Scanner sc = new Scanner(System.in);
- try {
- while(sc.hasNext()){
- String[] befehl = sc.nextLine().split(" ");
- if(befehl[0].equals("!login")) {
- if(alreadyLoggedIn){
- System.out.println("You are already LoggedIn!");
- }else{
- String companyName = befehl[1]; // UVN
- String companyPassword = befehl[2]; // 12345
- user = loginObject.login(companyName,companyPassword);
- alreadyLoggedIn = true;
- System.out.print("Succesfully logged in. ");
- if(user instanceof RemoteAdminInterface ){
- System.out.println("Using admin mode.");
- }else
- System.out.println("Using company mode.");
- }
- }else if(befehl[0].equals("!logout")){
- if(!alreadyLoggedIn) {
- System.out.println("You have to log in first.");
- } else {
- user.logout();
- alreadyLoggedIn = false;
- user = null;
- System.out.println("Succesfully logged out.");
- }
- }else if(befehl[0].equals("!exit")){
- System.out.println("Exiting..");
- System.exit(0);
- }else if(befehl[0].equals("!list")){
- if(!alreadyLoggedIn){
- System.out.println("You have to log in first.");
- }else if(user instanceof RemoteAdminInterface){
- System.out.println("Command not allowed. You are not a company.");
- }else{
- File file = new File(client.getTaskDir());
- File[] listOfFiles = file.listFiles();
- for (int i = 0; i < listOfFiles.length; i++)
- {
- if (listOfFiles[i].isFile())
- {
- System.out.println(listOfFiles[i].getName());
- }
- }
- }
- }
- }
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- private String getManagementComponent() {
- return mancom;
- }
- private void setPortRMI(int PortRMI) {
- this.PortRMI = PortRMI;
- }
- private void setHostRMI(String HostRMI) {
- this.HostRMI = HostRMI;
- }
- public void readRegistryProperties() throws FileNotFoundException {
- InputStream is = new DataInputStream(new FileInputStream(new File("registry.properties")));
- if (is != null) {
- java.util.Properties props = new java.util.Properties();
- try {
- props.load(is);
- setPortRMI(Integer.valueOf(props.getProperty("registry.port")));
- } catch (IOException e) {
- try {
- is.close();
- System.exit(0);
- } catch (IOException e1) {
- System.err.println("Error mit inputstream !!");
- }
- }
- }
- }
- public String getTaskDir() {
- return taskDir;
- }
- public void setTaskDir(String taskDir) {
- this.taskDir = taskDir;
- }
Add Comment
Please, Sign In to add comment