/* * 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. * */ /** * Concrete implementation of a communicator using XMPP as * communication layer. * * @author akipp */ package de.unibi.airobots.resaidroid.communication; /* * MAYBE CRASHED THIS FILE WITH LAST MERGE! */ import java.util.HashMap; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.SASLAuthentication; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.packet.Message; import android.app.Activity; import android.util.Log; import de.unibi.airobots.resaidroid.constants.Constants.Commands; import de.unibi.airobots.resaidroid.constants.Constants.Properties; import de.unibi.airobots.resaidroid.constants.ServerConfig; import de.unibi.airobots.resaidroid.helper.Beacon; import de.unibi.airobots.resaidroid.tabactivities.template.TabTemplate; public class XmppConnection extends Communicator { // TODO BASECLASS AND INTERFACE private static ConnectionConfiguration connConfig; private static XMPPConnection connection; private static String TAG = "XmppConnection"; private static long startTime = System.currentTimeMillis(); private static long timeDiff = 0; private static long clientTime = 0; private static long serverTime = 0; private static long beaconDuration = 0; private static long lastBeacon = 0; private static Beacon beacon; private static Activity usingActivity = null; private static boolean connectedToReSAI = false; @Override public void login(String _user, String _pass) { if (connection != null && connection.isConnected()) { Log.i(TAG, "Log in " + _user); try { connection.login(_user, _pass); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } // , "ReSAIDroid"); Log.i(TAG, "Loggin done."); } } @Override public void disconnect() { if (connection != null && connection.isConnected()) { if (beacon != null && !beacon.isStopped()) { beacon.setStopped(true); } if (connectedToReSAI) { Message mOut; mOut = new Message(ServerConfig.RECIPIENT_FULL); mOut.setProperty(Properties.CMD.name(), Commands.disconnect.name()); connection.sendPacket(mOut); } connection.disconnect(); } else { Log.i(TAG, "XMPPException: not connected"); } connectedToReSAI = false; } @Override public void sendMessage(String recipient, HashMap properties, String body) { if (connection != null && connection.isConnected()) { Message mOut = new Message(recipient); mOut.setBody(body); for (String property : properties.keySet()) { mOut.setProperty(property, properties.get(property)); } connection.sendPacket(mOut); } } @Override public void sendError(String recipient, Exception e) { // TODO Auto-generated method stub } @Override public void connect(String _server, int _port) { connConfig = new ConnectionConfiguration(_server, _port); SASLAuthentication.supportSASLMechanism("PLAIN", 0); connection = new XMPPConnection(connConfig); try { connection.connect(); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i(TAG, "\nCONNECTED\n"); } @Override public void sendMessage(String recipient, HashMap properties) { if (connection != null && connection.isConnected()) { Message mOut = new Message(recipient); for (String property : properties.keySet()) { mOut.setProperty(property, properties.get(property)); } connection.sendPacket(mOut); } } /** * Start beacon to show ReSAI that connection is alive. */ public void startBeacon() { if (beacon == null) { beacon = new Beacon(5000, usingActivity, this); beacon.start(); } else { beacon.setStopped(false); synchronized (beacon) { beacon.notify(); } } } /** * Stop beacon. ReSAI will now kick the connection after serveral seconds. */ public static void stopBeacon() { beacon.setStopped(true); // beacon = null; } /** * Add a new listener to connection using given filter. * * @param packetListener * the packetListener to add * @param packetFilter * the filter to be used for the packetListener */ public static void addListener(PacketListener packetListener, PacketFilter packetFilter) { if (connection != null && connection.isConnected()) { connection.addPacketListener(packetListener, packetFilter); } } /** * Remove given listener from connection. * * @param packetListener * the packetListener to remove */ public void removeListener(PacketListener packetListener) { Log.i(TAG, "Disconnect listener..."); if (connection != null && connection.isConnected()) { connection.removePacketListener(packetListener); Log.i(TAG, "Disconnect listener...done"); } } public boolean isLoggedIn() { if (connection != null && connection.isConnected()) { return connection.isAuthenticated(); } return false; } // ########## SETTER AND GETTER ############################ public static void setStartTime(long startTime) { XmppConnection.startTime = startTime; } public static long getStartTime() { return startTime; } public static void setTimeDiff(long timeDiff) { XmppConnection.timeDiff = timeDiff; } public static long getTimeDiff() { return timeDiff; } public static void setServerTime(long serverTime) { XmppConnection.serverTime = serverTime; } public static long getServerTime() { return serverTime; } public static void setClientTime(long clientTime) { XmppConnection.clientTime = clientTime; } public static long getClientTime() { return clientTime; } public static void setBeaconDuration(long beaconDuration) { XmppConnection.beaconDuration = beaconDuration; } public static long getBeaconDuration() { return beaconDuration; } public boolean isConnectedToReSAI() { return connectedToReSAI; } public void setConnectedToReSAI(boolean connectedToReSAI) { XmppConnection.connectedToReSAI = connectedToReSAI; } public static Activity getUsingActivity() { return usingActivity; } public void setUsingActivity(TabTemplate usingActivity) { XmppConnection.usingActivity = usingActivity; if (beacon != null) { beacon.setActivity(usingActivity); } } public static void setLastBeacon(long lastBeacon) { XmppConnection.lastBeacon = lastBeacon; } public static long getLastBeacon() { return lastBeacon; } // INTERFACE ELEMENTS @Override public boolean isConnected() { if (connection != null) { return connection.isConnected(); } return false; } @Override public boolean isAuthenticated() { // TODO Auto-generated method stub return false; } }