/* * 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.resaijavaclient.communicaton; import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.PacketListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.filter.PacketTypeFilter; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; public class XMPPCommunicator extends Communicator { private static XMPPConnection con; private static ConnectionConfiguration config; public String SERVERIP; public int SERVERPORT; public String SERVERACCOUNT; public String SERVERPASSWORD; public int MAXMISSINGBEACONS = 4; private String serverAccount = ""; public XMPPCommunicator() { } /* * (non-Javadoc) * * @see de.unibi.airobots.resai.communication.Communicator#isConnected() */ @Override public boolean isConnected() { return con.isConnected(); } /* * (non-Javadoc) * * @see de.unibi.airobots.resaijavaclient.communication.Communicator#isAuthenticated() */ @Override public boolean isAuthenticated() { return con.isAuthenticated(); } /* * (non-Javadoc) * * @see * de.unibi.airobots.resaijavaclient.communication.Communicator#login(java.lang.String * , java.lang.String) */ @Override public void login(String _user, String _pass) { try { con.login(_user, _pass); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void initConnection(String _server, int _port) { config = new ConnectionConfiguration(_server, _port); con = new XMPPConnection(config); } /* * (non-Javadoc) * * @see de.unibi.airobots.resaijavaclient.communication.Communicator#connect() */ @Override public void connect(){ try { con.connect(); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } receivedProperties = new HashMap(); PacketFilter filter = new PacketTypeFilter(Message.class); con.addPacketListener(new PacketListener() { public void processPacket(Packet packet) { if (packet instanceof Message) { Message mIn = (Message) packet; sender = mIn.getFrom(); body = mIn.getBody(); if (mIn.getPropertyNames().contains("TAG")) { recipientTAG = mIn.getProperty("TAG").toString(); } receivedProperties.clear(); for (String prop : mIn.getPropertyNames()) { receivedProperties.put(prop, mIn.getProperty(prop) .toString()); } setChanged(); notifyObservers(receivedProperties); } } }, filter); } /* * (non-Javadoc) * * @see de.unibi.airobots.resaijavaclient.communication.Communicator#disconnect() */ @Override public void disconnect() { con.disconnect(); } /* * (non-Javadoc) * * @see * de.unibi.airobots.resaijavaclient.communication.Communicator#sendMessage(java.lang * .String, java.util.HashMap) */ @Override public void sendMessage(String recipient, HashMap properties) { Message mOut = new Message(recipient); // TODO constants? mOut.setProperty("M_TYPE", "MESSAGE"); for (String prop : properties.keySet()) { mOut.setProperty(prop, properties.get(prop)); } con.sendPacket(mOut); } @Override public void sendMessage(String recipient, HashMap properties, String body) { Message mOut = new Message(recipient); mOut.setBody(body); // TODO Konstanten mOut.setProperty("M_TYPE", "MESSAGE_BODY"); for (String prop : properties.keySet()) { mOut.setProperty(prop, properties.get(prop)); } con.sendPacket(mOut); } @Override public void sendError(String recipient, Exception e) { Message mOut = new Message(recipient); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); e.printStackTrace(pw); pw.flush(); sw.flush(); mOut.setBody(sw.toString()); // TODO Konstanten mOut.setProperty("M_TYPE", "ERROR"); con.sendPacket(mOut); } public void addListener(PacketListener pl) { PacketFilter filter = new PacketTypeFilter(Message.class); con.addPacketListener(pl, filter); } public void removeListener(PacketListener pl) { con.removePacketListener(pl); } public void setServerAccount(String serverAccount) { this.serverAccount = serverAccount; } public String getServerAccount() { return serverAccount; } }