/* * This file is part of the Remote Sensor Actuator Interface (ReSAI). * * Copyright(c) Andreas Kipp, Frederic Siepmann * http://opensource.cit-ec.de/projects/resai * * This file may be licensed under the terms of of the * GNU Lesser General Public License Version 3 (the ``LGPL''), * or (at your option) any later version. * * Software distributed under the License is distributed * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either * express or implied. See the LGPL for the specific language * governing rights and limitations. * * You should have received a copy of the LGPL along with this * program. If not, go to http://www.gnu.org/licenses/lgpl.html * or write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * The development of this software was supported by the * Excellence Cluster EXC 277 Cognitive Interaction Technology. * The Excellence Cluster EXC 277 is a grant of the Deutsche * Forschungsgemeinschaft (DFG) in the context of the German * Excellence Initiative. * */ /** * Controller for login. * * @author akipp */ package de.unibi.airobots.resaijavaclient.control; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Observable; import java.util.Observer; import java.util.Timer; import java.util.TimerTask; import javax.swing.JOptionPane; import de.unibi.airobots.resaijavaclient.communicaton.Beacon; import de.unibi.airobots.resaijavaclient.communicaton.Communicator; import de.unibi.airobots.resaijavaclient.constants.Constants; import de.unibi.airobots.resaijavaclient.gui.Login; import de.unibi.airobots.resaijavaclient.gui.Overview; public class LoginController implements ActionListener, Observer { private Communicator com; private Login loginWindow; private static boolean connectedToReSAI = false; private Timer timer; private int connectionTries = 0; public LoginController(Login login, Communicator communicator) { com = communicator; loginWindow = login; loginWindow.getBtnLogin().addActionListener(this); loginWindow.getBtnCancel().addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource().equals(loginWindow.getBtnLogin())) { activateGui(false); com.initConnection(loginWindow.getTxtServer().getText(), Integer.parseInt(loginWindow.getTxtPort().getText())); com.connect(); com.login(loginWindow.getTxtUsername().getText(), String .valueOf(loginWindow.getTxtPassword().getPassword())); com.setServerAccount(loginWindow.getTxtServerAccount().getText()); if (com.isConnected()) { System.out.println("CONNECTED TO XMPP SERVER..."); connectToReSAI(); loginWindow.getProgBar().setEnabled(true); timer = new Timer(); timer.schedule(new CheckConnect(), 0, 2000); } else { JOptionPane.showConfirmDialog(null, "Login not successful.", "Error", JOptionPane.CLOSED_OPTION); } } else if (e.getSource().equals(loginWindow.getBtnCancel())) { System.exit(0); } } private void connectToReSAI() { System.out.println("SENDING CONNECT REQUEST TO RESAI SERVER..."); HashMap propertiesToSend = new HashMap(); propertiesToSend.put(Constants.CMD, Constants.CONNECT); propertiesToSend.put(Constants.TAG, Constants.MAINDISPATCHER); //System.out.println("RECIPIENT: " + loginWindow.getTxtServerAccount().getText()); com.sendMessage(loginWindow.getTxtServerAccount().getText(), propertiesToSend, "Calling ReSAI Server..."); System.out.println("SENDING CONNECT REQUEST TO RESAI SERVER...done"); } @Override public void update(Observable o, Object arg1) { if (o instanceof Communicator) { com = (Communicator) o; //System.out.println("NEW MESSAGE FROM COMMUNICATOR...."); if (arg1 instanceof HashMap) { @SuppressWarnings("unchecked") HashMap valuePairs = (HashMap) arg1; if (valuePairs.containsKey(Constants.CONNECTED)) { if (valuePairs.get(Constants.CONNECTED).equals(Constants.TRUE)) { connectedToReSAI = true; System.out.println("CONNECTED TO RESAI SERVER..."); // System.out.println(connectedToReSAI); } } } } } private class CheckConnect extends TimerTask { @Override public void run() { if (connectedToReSAI) { new Beacon(com); Overview ov = new Overview(com); OverviewController ovc = new OverviewController(ov,com); ov.getTabHolder().addChangeListener(ovc); loginWindow.close(); ov.addObserver(ovc); com.addObserver(ovc); com.deleteObserver(LoginController.this); timer.cancel(); connectionTries = 0; } else { connectionTries++; // System.out.println("ReSAI not responding...try again..."); loginWindow.getProgBar().setValue(connectionTries); } if (connectionTries > 5) { JOptionPane.showConfirmDialog(null, "ReSAI Server not responding.", "Error", JOptionPane.CLOSED_OPTION); timer.cancel(); activateGui(true); loginWindow.getProgBar().setEnabled(false); loginWindow.getProgBar().setValue(0); } } } private void activateGui(boolean active) { loginWindow.getBtnCancel().setEnabled(active); loginWindow.getBtnLogin().setEnabled(active); loginWindow.getTxtPassword().setEnabled(active); loginWindow.getTxtPort().setEnabled(active); loginWindow.getTxtServer().setEnabled(active); loginWindow.getTxtUsername().setEnabled(active); loginWindow.getTxtServerAccount().setEnabled(active); } }