mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 06:12:05 +01:00
Merge branch '4.3'
This commit is contained in:
commit
fb2a9b2167
5 changed files with 186 additions and 1 deletions
|
@ -1095,6 +1095,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
|
||||||
if (packet instanceof IQ) {
|
if (packet instanceof IQ) {
|
||||||
final IQ iq = (IQ) packet;
|
final IQ iq = (IQ) packet;
|
||||||
if (iq.isRequestIQ()) {
|
if (iq.isRequestIQ()) {
|
||||||
|
final IQ iqRequest = iq;
|
||||||
final String key = XmppStringUtils.generateKey(iq.getChildElementName(), iq.getChildElementNamespace());
|
final String key = XmppStringUtils.generateKey(iq.getChildElementName(), iq.getChildElementNamespace());
|
||||||
IQRequestHandler iqRequestHandler;
|
IQRequestHandler iqRequestHandler;
|
||||||
final IQ.Type type = iq.getType();
|
final IQ.Type type = iq.getType();
|
||||||
|
@ -1160,6 +1161,11 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
|
||||||
// e.g. to avoid presence leaks.
|
// e.g. to avoid presence leaks.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert (response.getType() == IQ.Type.result || response.getType() == IQ.Type.error);
|
||||||
|
|
||||||
|
response.setTo(iqRequest.getFrom());
|
||||||
|
response.setStanzaId(iqRequest.getStanzaId());
|
||||||
try {
|
try {
|
||||||
sendStanza(response);
|
sendStanza(response);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
@ -45,6 +46,7 @@ import org.jxmpp.jid.impl.JidCreate;
|
||||||
import org.jxmpp.jid.parts.Resourcepart;
|
import org.jxmpp.jid.parts.Resourcepart;
|
||||||
import org.jxmpp.stringprep.XmppStringprepException;
|
import org.jxmpp.stringprep.XmppStringprepException;
|
||||||
import org.minidns.dnsname.DnsName;
|
import org.minidns.dnsname.DnsName;
|
||||||
|
import org.minidns.util.InetAddressUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration to use while establishing the connection to the server.
|
* Configuration to use while establishing the connection to the server.
|
||||||
|
@ -175,6 +177,14 @@ public abstract class ConnectionConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DnsName getHost() {
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
InetAddress getHostAddress() {
|
||||||
|
return hostAddress;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the server name of the target server.
|
* Returns the server name of the target server.
|
||||||
*
|
*
|
||||||
|
@ -646,6 +656,33 @@ public abstract class ConnectionConfiguration {
|
||||||
return getThis();
|
return getThis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the host to connect to by either its fully qualified domain name (FQDN) or its IP.
|
||||||
|
*
|
||||||
|
* @param fqdnOrIp a CharSequence either representing the FQDN or the IP of the host.
|
||||||
|
* @return a reference to this builder.
|
||||||
|
* @see #setHost(DnsName)
|
||||||
|
* @see #setHostAddress(InetAddress)
|
||||||
|
* @since 4.3.2
|
||||||
|
*/
|
||||||
|
public B setHostAddressByNameOrIp(CharSequence fqdnOrIp) {
|
||||||
|
String fqdnOrIpString = fqdnOrIp.toString();
|
||||||
|
if (InetAddressUtil.isIpAddress(fqdnOrIp)) {
|
||||||
|
InetAddress hostInetAddress;
|
||||||
|
try {
|
||||||
|
hostInetAddress = InetAddress.getByName(fqdnOrIpString);
|
||||||
|
}
|
||||||
|
catch (UnknownHostException e) {
|
||||||
|
// Should never happen.
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
setHostAddress(hostInetAddress);
|
||||||
|
} else {
|
||||||
|
setHost(fqdnOrIpString);
|
||||||
|
}
|
||||||
|
return getThis();
|
||||||
|
}
|
||||||
|
|
||||||
public B setPort(int port) {
|
public B setPort(int port) {
|
||||||
if (port < 0 || port > 65535) {
|
if (port < 0 || port > 65535) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2018 Florian Schmaus.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.jxmpp.jid.JidTestUtil;
|
||||||
|
|
||||||
|
public class ConnectionConfigurationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setIp() {
|
||||||
|
DummyConnectionConfiguration.Builder builder = newUnitTestBuilder();
|
||||||
|
|
||||||
|
final String ip = "192.168.0.1";
|
||||||
|
builder.setHostAddressByNameOrIp(ip);
|
||||||
|
|
||||||
|
DummyConnectionConfiguration connectionConfiguration = builder.build();
|
||||||
|
assertEquals('/' + ip, connectionConfiguration.getHostAddress().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setFqdn() {
|
||||||
|
DummyConnectionConfiguration.Builder builder = newUnitTestBuilder();
|
||||||
|
|
||||||
|
final String fqdn = "foo.example.org";
|
||||||
|
builder.setHostAddressByNameOrIp(fqdn);
|
||||||
|
|
||||||
|
DummyConnectionConfiguration connectionConfiguration = builder.build();
|
||||||
|
assertEquals(fqdn, connectionConfiguration.getHost().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DummyConnectionConfiguration.Builder newUnitTestBuilder() {
|
||||||
|
DummyConnectionConfiguration.Builder builder = DummyConnectionConfiguration.builder();
|
||||||
|
builder.setXmppDomain(JidTestUtil.DOMAIN_BARE_JID_1);
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class DummyConnectionConfiguration extends ConnectionConfiguration {
|
||||||
|
|
||||||
|
protected DummyConnectionConfiguration(Builder builder) {
|
||||||
|
super(builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Builder builder() {
|
||||||
|
return new Builder();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class Builder
|
||||||
|
extends ConnectionConfiguration.Builder<Builder, DummyConnectionConfiguration> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DummyConnectionConfiguration build() {
|
||||||
|
return new DummyConnectionConfiguration(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Builder getThis() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,10 +16,13 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smack.sm;
|
package org.jivesoftware.smack.sm;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.packet.Element;
|
||||||
import org.jivesoftware.smack.packet.Stanza;
|
import org.jivesoftware.smack.packet.Stanza;
|
||||||
|
|
||||||
public abstract class StreamManagementException extends SmackException {
|
public abstract class StreamManagementException extends SmackException {
|
||||||
|
@ -110,5 +113,56 @@ public abstract class StreamManagementException extends SmackException {
|
||||||
return ackedStanzas;
|
return ackedStanzas;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final class UnacknowledgedQueueFullException extends StreamManagementException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private final int overflowElementNum;
|
||||||
|
private final int droppedElements;
|
||||||
|
private final List<Element> elements;
|
||||||
|
private final List<Stanza> unacknowledgesStanzas;
|
||||||
|
|
||||||
|
private UnacknowledgedQueueFullException(String message, int overflowElementNum, int droppedElements, List<Element> elements,
|
||||||
|
List<Stanza> unacknowledgesStanzas) {
|
||||||
|
super(message);
|
||||||
|
this.overflowElementNum = overflowElementNum;
|
||||||
|
this.droppedElements = droppedElements;
|
||||||
|
this.elements = elements;
|
||||||
|
this.unacknowledgesStanzas = unacknowledgesStanzas;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOverflowElementNum() {
|
||||||
|
return overflowElementNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDroppedElements() {
|
||||||
|
return droppedElements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Element> getElements() {
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Stanza> getUnacknowledgesStanzas() {
|
||||||
|
return unacknowledgesStanzas;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UnacknowledgedQueueFullException newWith(int overflowElementNum, List<Element> elements,
|
||||||
|
BlockingQueue<Stanza> unacknowledgedStanzas) {
|
||||||
|
final int unacknowledgesStanzasQueueSize = unacknowledgedStanzas.size();
|
||||||
|
List<Stanza> localUnacknowledgesStanzas = new ArrayList<>(unacknowledgesStanzasQueueSize);
|
||||||
|
localUnacknowledgesStanzas.addAll(unacknowledgedStanzas);
|
||||||
|
int droppedElements = elements.size() - overflowElementNum - 1;
|
||||||
|
|
||||||
|
String message = "The queue size " + unacknowledgesStanzasQueueSize + " is not able to fit another "
|
||||||
|
+ droppedElements + " potential stanzas type top-level stream-elements.";
|
||||||
|
return new UnacknowledgedQueueFullException(message, overflowElementNum, droppedElements, elements,
|
||||||
|
localUnacknowledgesStanzas);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1511,7 +1511,16 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
|
||||||
private void drainWriterQueueToUnacknowledgedStanzas() {
|
private void drainWriterQueueToUnacknowledgedStanzas() {
|
||||||
List<Element> elements = new ArrayList<>(queue.size());
|
List<Element> elements = new ArrayList<>(queue.size());
|
||||||
queue.drainTo(elements);
|
queue.drainTo(elements);
|
||||||
for (Element element : elements) {
|
for (int i = 0; i < elements.size(); i++) {
|
||||||
|
Element element = elements.get(i);
|
||||||
|
// If the unacknowledgedStanza queue is full, then bail out with a warning message. See SMACK-844.
|
||||||
|
if (unacknowledgedStanzas.remainingCapacity() == 0) {
|
||||||
|
StreamManagementException.UnacknowledgedQueueFullException exception = StreamManagementException.UnacknowledgedQueueFullException
|
||||||
|
.newWith(i, elements, unacknowledgedStanzas);
|
||||||
|
LOGGER.log(Level.WARNING,
|
||||||
|
"Some stanzas may be lost as not all could be drained to the unacknowledged stanzas queue", exception);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (element instanceof Stanza) {
|
if (element instanceof Stanza) {
|
||||||
unacknowledgedStanzas.add((Stanza) element);
|
unacknowledgedStanzas.add((Stanza) element);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue