@SuppressWarnings("unchecked") private XMPPBean sendAndWaitForResult(B bean) { if (bean != null) { this.connection.sendPacket(new BeanIQAdapter(bean)); } while (true) { BeanIQAdapter adapter = (BeanIQAdapter) (beanCollector.nextResult(timeout)); XMPPBean resultBean = null; if (adapter != null) { if (adapter.getBean().getNamespace().equals(resultBeanPrototype.getNamespace())) { resultBean = (ResultBeanType) (adapter.getBean()); } else { resultBean = (B) (adapter.getBean()); } } if (adapter == null || resultBean == null) { if (retryCount >= maxRetries) { return null; } else { retryCount++; sendAndWaitForResult(bean); } } else if (resultBean.getId().equals(beanOut.getId())) { return resultBean; } } }
/** * Convert XMPPIQ to XMPPBean to simplify the handling of the IQ using the beanPrototypes. * * @param iq the XMPPIQ * @return the related XMPPBean or null if something goes wrong */ public XMPPBean convertXMPPIQToBean(XMPPIQ iq) { try { String childElement = iq.element; String namespace = iq.namespace; XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlPullParser parser = factory.newPullParser(); parser.setInput(new StringReader(iq.payload)); XMPPBean bean = null; Log.v( TAG, "prototypes contains ns: " + namespace + "? " + this.beanPrototypes.containsKey(namespace)); if (this.beanPrototypes.containsKey(namespace)) Log.v( TAG, "prototypes contains ce: " + childElement + "? " + this.beanPrototypes.get(namespace).containsKey(childElement)); synchronized (this.beanPrototypes) { if (namespace != null && this.beanPrototypes.containsKey(namespace) && this.beanPrototypes.get(namespace).containsKey(childElement)) { bean = (this.beanPrototypes.get(namespace).get(childElement)).clone(); bean.fromXML(parser); bean.setId(iq.packetID); bean.setFrom(iq.from); bean.setTo(iq.to); switch (iq.type) { case XMPPIQ.TYPE_GET: bean.setType(XMPPBean.TYPE_GET); break; case XMPPIQ.TYPE_SET: bean.setType(XMPPBean.TYPE_SET); break; case XMPPIQ.TYPE_RESULT: bean.setType(XMPPBean.TYPE_RESULT); break; case XMPPIQ.TYPE_ERROR: bean.setType(XMPPBean.TYPE_ERROR); break; } return bean; } } } catch (Exception e) { Log.e(TAG, "ERROR while parsing XMPPIQ to XMPPBean: " + e.getMessage()); } return null; }
/** * Unregister a single XMPPBean in MXA. * * @param callback the callback to be unregistered * @param beanNamespace the namespace of the IQ * @param beanElement the root element of the IQs payload * @throws RemoteException the remote exception if something goes wrong */ public void unregisterXMPPExtension( IXMPPIQCallback callback, String beanNamespace, String beanElement) throws RemoteException { if (mMXAProxy.isConnected()) { XMPPBean bean = getRegisteredBean(beanNamespace, beanElement); if (bean != null) mMXAProxy .getXMPPService() .unregisterIQCallback(callback, bean.getChildElement(), bean.getNamespace()); } }
/** * Unregister an XMPPBean from prototypes. * * @param prototype the prototype to remove */ public void unregisterXMPPBean(XMPPBean prototype) { String namespace = prototype.getNamespace(); String childElement = prototype.getChildElement(); synchronized (this.beanPrototypes) { if (this.beanPrototypes.containsKey(namespace)) { this.beanPrototypes.get(namespace).remove(childElement); if (this.beanPrototypes.get(namespace).size() > 0) this.beanPrototypes.remove(namespace); } } }
/** * Register a prototype of an XMPPBean. * * @param prototype the prototype XMPPBean */ public void registerXMPPBean(XMPPBean prototype) { String namespace = prototype.getNamespace(); String childElement = prototype.getChildElement(); synchronized (this.beanPrototypes) { if (!this.beanPrototypes.keySet().contains(namespace)) this.beanPrototypes.put( namespace, Collections.synchronizedMap(new HashMap<String, XMPPBean>())); this.beanPrototypes.get(namespace).put(childElement, prototype); } }
/* * (non-Javadoc) * * @see * org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware * .smack.packet.Packet) */ @Override public void processPacket(Packet packet) { if (packet instanceof BeanIQAdapter) { XMPPBean inBean = ((BeanIQAdapter) packet).getBean(); if (inBean instanceof PrepareServiceUploadBean) { handlePrepareServiceUploadBean((PrepareServiceUploadBean) inBean); } else if (inBean instanceof ServiceUploadConclusionBean && inBean.getType() == XMPPBean.TYPE_RESULT) { // Do nothing, just ack } else { handleUnknownBean(inBean); } } }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public void processIQ(XMPPIQ iq) throws RemoteException { if (!(iq.from.equals(mGameServiceJid) || iq.from.equals(mServerCoordinatorJid))) { String msg = "Discarded IQ from unknown JID " + iq.from + " to prevent GameService zombies from interfering" + " - see IQProxy.AbstractCallback.processIQ()"; Log.w(TAG, msg); return; } Log.v( TAG, "AbstractCallback: ID: " + iq.packetID + " type: " + iq.type + " ns: " + iq.namespace + " payload: " + iq.payload); XMPPBean inBean = convertXMPPIQToBean(iq); Log.v(TAG, "Converted Abstract Bean: " + beanToString(inBean)); if (_waitingCallbacks.containsKey(inBean.getId())) { IXMPPCallback callback = _waitingCallbacks.get(inBean.getId()); if (null != callback) { try { callback.invoke(inBean); } catch (ClassCastException e) { e.printStackTrace(); } } } else { // refer XMPPIQ as XMPPBean to the current active Activities GameState // (hold in XHuntService) mXhuntService.getGameState().processPacket(inBean); } }
/** * Register an callback extension which is used by the MXAProxy/MXA for listening for this kind of * IQ. * * @param callback the callback which should be notified if IQ is incoming * @param beanNamespace the namespace of the IQ * @param beanElement the root element tag of the IQ * @return true, if callback was registered successful * @throws RemoteException the remote exception if something goes wrong */ public boolean registerXMPPExtension( IXMPPIQCallback callback, String beanNamespace, String beanElement) throws RemoteException { boolean isRegistered = false; if (mMXAProxy.isConnected()) { XMPPBean bean = getRegisteredBean(beanNamespace, beanElement); // if there is no related prototype of this XMPPBean it can not be // registered a callback if (bean != null) { mMXAProxy .getXMPPService() .registerIQCallback(callback, bean.getChildElement(), bean.getNamespace()); Log.v(TAG, "child: " + bean.getChildElement() + " ns: " + bean.getNamespace()); isRegistered = true; } } return isRegistered; }
/** * Send an XMPPBean of type ERROR. * * <p>TODO: Option for custom ERROR tag and text is missing. * * @param inBean the XMPPBean to reply with an ERROR. The playload will be copied. */ public void sendXMPPBeanError(XMPPBean inBean) { XMPPBean resultBean = inBean.clone(); resultBean.setTo(inBean.getFrom()); resultBean.setFrom(mMXAProxy.getXmppJid()); resultBean.setType(XMPPBean.TYPE_ERROR); _proxy.getBindingStub().sendXMPPBean(resultBean); }
/** * Convert an XMPPBean to an XMPPIQ to send it via the MXAProxy/MXA. * * @param bean the bean to convert * @param mergePayload true if the playload should be merged * @return the XMPPIQ */ public XMPPIQ beanToIQ(XMPPBean bean, boolean mergePayload) { // default XMPP IQ type int type = XMPPIQ.TYPE_GET; switch (bean.getType()) { case XMPPBean.TYPE_GET: type = XMPPIQ.TYPE_GET; break; case XMPPBean.TYPE_SET: type = XMPPIQ.TYPE_SET; break; case XMPPBean.TYPE_RESULT: type = XMPPIQ.TYPE_RESULT; break; case XMPPBean.TYPE_ERROR: type = XMPPIQ.TYPE_ERROR; break; } XMPPIQ iq; if (mergePayload) iq = new XMPPIQ(bean.getFrom(), bean.getTo(), type, null, null, bean.toXML()); else iq = new XMPPIQ( bean.getFrom(), bean.getTo(), type, bean.getChildElement(), bean.getNamespace(), bean.payloadToXML()); iq.packetID = bean.getId(); return iq; }
/** * Formats a Bean to a string. * * @param bean the bean * @return the formatted string of the Bean */ public String beanToString(XMPPBean bean) { String str = "XMPPBean: [NS=" + bean.getNamespace() + " id=" + bean.getId() + " from=" + bean.getFrom() + " to=" + bean.getTo() + " type=" + bean.getType() + " payload=" + bean.payloadToXML(); if (bean.errorCondition != null) str += " errorCondition=" + bean.errorCondition; if (bean.errorText != null) str += " errorText=" + bean.errorText; if (bean.errorType != null) str += " errorType=" + bean.errorType; str += "]"; return str; }
@Override public void sendXMPPBean(XMPPBean out, IXMPPCallback<? extends XMPPBean> callback) { _waitingCallbacks.put(out.getId(), callback); mMXAProxy.sendIQ(beanToIQ(out, true)); }