001/** 002 * 003 * Copyright 2020-2021 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.c2s.internal; 018 019import java.io.IOException; 020import java.nio.channels.ClosedChannelException; 021import java.nio.channels.SelectableChannel; 022import java.nio.channels.SelectionKey; 023import java.util.ListIterator; 024import java.util.Queue; 025 026import org.jivesoftware.smack.SmackException; 027import org.jivesoftware.smack.SmackException.NoResponseException; 028import org.jivesoftware.smack.SmackException.NotConnectedException; 029import org.jivesoftware.smack.SmackReactor; 030import org.jivesoftware.smack.SmackReactor.ChannelSelectedCallback; 031import org.jivesoftware.smack.XMPPException; 032import org.jivesoftware.smack.XMPPException.FailedNonzaException; 033import org.jivesoftware.smack.XmppInputOutputFilter; 034import org.jivesoftware.smack.c2s.ModularXmppClientToServerConnection; 035import org.jivesoftware.smack.c2s.XmppClientToServerTransport; 036import org.jivesoftware.smack.debugger.SmackDebugger; 037import org.jivesoftware.smack.fsm.ConnectionStateEvent; 038import org.jivesoftware.smack.internal.SmackTlsContext; 039import org.jivesoftware.smack.packet.Nonza; 040import org.jivesoftware.smack.packet.TopLevelStreamElement; 041import org.jivesoftware.smack.packet.XmlEnvironment; 042import org.jivesoftware.smack.util.Consumer; 043import org.jivesoftware.smack.util.PacketParserUtils; 044import org.jivesoftware.smack.util.Supplier; 045import org.jivesoftware.smack.xml.XmlPullParser; 046import org.jivesoftware.smack.xml.XmlPullParserException; 047 048public abstract class ModularXmppClientToServerConnectionInternal { 049 050 private final SmackReactor reactor; 051 052 public final ModularXmppClientToServerConnection connection; 053 054 public final SmackDebugger smackDebugger; 055 056 public final Queue<TopLevelStreamElement> outgoingElementsQueue; 057 058 public ModularXmppClientToServerConnectionInternal(ModularXmppClientToServerConnection connection, SmackReactor reactor, 059 SmackDebugger smackDebugger, Queue<TopLevelStreamElement> outgoingElementsQueue) { 060 this.connection = connection; 061 this.reactor = reactor; 062 this.smackDebugger = smackDebugger; 063 this.outgoingElementsQueue = outgoingElementsQueue; 064 } 065 066 public SelectionKey registerWithSelector(SelectableChannel channel, int ops, ChannelSelectedCallback callback) 067 throws ClosedChannelException { 068 return reactor.registerWithSelector(channel, ops, callback); 069 } 070 071 public void setInterestOps(SelectionKey selectionKey, int interestOps) { 072 reactor.setInterestOps(selectionKey, interestOps); 073 } 074 075 public final void withSmackDebugger(Consumer<SmackDebugger> smackDebuggerConsumer) { 076 if (smackDebugger == null) { 077 return; 078 } 079 080 smackDebuggerConsumer.accept(smackDebugger); 081 } 082 083 public abstract XmlEnvironment getOutgoingStreamXmlEnvironment(); 084 085 // TODO: The incomingElement parameter was previously of type TopLevelStreamElement, but I believe it has to be 086 // of type string. But would this also work for BOSH or WebSocket? 087 public abstract void parseAndProcessElement(String wrappedCompleteIncomingElement); 088 089 public abstract void notifyConnectionError(Exception e); 090 091 public final String onStreamOpen(String streamOpen) { 092 XmlPullParser streamOpenParser; 093 try { 094 streamOpenParser = PacketParserUtils.getParserFor(streamOpen); 095 } catch (XmlPullParserException | IOException e) { 096 // Should never happen. 097 throw new AssertionError(e); 098 } 099 String streamClose = onStreamOpen(streamOpenParser); 100 return streamClose; 101 } 102 103 public abstract String onStreamOpen(XmlPullParser parser); 104 105 public abstract void onStreamClosed(); 106 107 public abstract void fireFirstLevelElementSendListeners(TopLevelStreamElement element); 108 109 public abstract void invokeConnectionStateMachineListener(ConnectionStateEvent connectionStateEvent); 110 111 public abstract void addXmppInputOutputFilter(XmppInputOutputFilter xmppInputOutputFilter); 112 113 public abstract ListIterator<XmppInputOutputFilter> getXmppInputOutputFilterBeginIterator(); 114 115 public abstract ListIterator<XmppInputOutputFilter> getXmppInputOutputFilterEndIterator(); 116 117 public abstract void waitForFeaturesReceived(String waitFor) throws InterruptedException, SmackException, XMPPException; 118 119 public abstract void newStreamOpenWaitForFeaturesSequence(String waitFor) throws InterruptedException, 120 NoResponseException, NotConnectedException, SmackException, XMPPException; 121 122 public abstract SmackTlsContext getSmackTlsContext(); 123 124 public abstract <SN extends Nonza, FN extends Nonza> SN sendAndWaitForResponse(Nonza nonza, 125 Class<SN> successNonzaClass, Class<FN> failedNonzaClass) 126 throws NoResponseException, NotConnectedException, FailedNonzaException, InterruptedException; 127 128 public abstract void asyncGo(Runnable runnable); 129 130 public abstract void waitForConditionOrThrowConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, SmackException, XMPPException; 131 132 public abstract void notifyWaitingThreads(); 133 134 public abstract void setCompressionEnabled(boolean compressionEnabled); 135 136 /** 137 * Set the active transport (TCP, BOSH, WebSocket, …) to be used for the XMPP connection. Also marks the connection 138 * as connected. 139 * 140 * @param xmppTransport the active transport. 141 */ 142 public abstract void setTransport(XmppClientToServerTransport xmppTransport); 143}