1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

Progress on Transports

This commit is contained in:
vanitasvitae 2017-07-21 17:58:57 +02:00
parent f4a910cfa5
commit c448d07234
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
10 changed files with 218 additions and 46 deletions

View file

@ -22,7 +22,7 @@ import org.jivesoftware.smackx.jingle3.element.JingleContentTransportElement;
*/
public class Content {
private Session session;
private Session parent;
private JingleContentElement.Creator creator;
private String name;
private String disposition;
@ -117,28 +117,49 @@ public class Content {
return senders;
}
public Description getDescription() {
public void setParent(Session session) {
if (this.parent != session) {
this.parent = session;
}
}
public Session getParent() {
return parent;
}
public Description<?> getDescription() {
return description;
}
public void setDescription(Description<?> description) {
if (this.description != description) {
this.description = description;
description.setParent(this);
}
}
public Transport<?> getTransport() {
return transport;
}
public void setTransport(Transport<?> transport) {
this.transport = transport;
}
public void setSession(Session session) {
if (this.session != session) {
this.session = session;
if (this.transport != transport) {
this.transport = transport;
transport.setParent(this);
}
}
public Security getSecurity() {
public Security<?> getSecurity() {
return security;
}
public void setSecurity(Security<?> security) {
if (this.security != security) {
this.security = security;
security.setParent(this);
}
}
public static String randomName() {
return "cont-" + StringUtils.randomString(16);
}

View file

@ -7,5 +7,17 @@ import org.jivesoftware.smackx.jingle3.element.JingleContentDescriptionElement;
*/
public abstract class Description<D extends JingleContentDescriptionElement> {
private Content parent;
public abstract D getElement();
public void setParent(Content parent) {
if (this.parent != parent) {
this.parent = parent;
}
}
public Content getParent() {
return parent;
}
}

View file

@ -9,7 +9,19 @@ import org.jivesoftware.smackx.jingle3.element.JingleContentSecurityInfoElement;
*/
public abstract class Security<D extends JingleContentSecurityElement> {
private Content parent;
public abstract D getElement();
public abstract JingleElement handleSecurityInfo(JingleContentSecurityInfoElement element);
public void setParent(Content parent) {
if (this.parent != parent) {
this.parent = parent;
}
}
public Content getParent() {
return parent;
}
}

View file

@ -57,7 +57,7 @@ public class Session {
void addContent(Content content) {
contents.put(content.getName(), content);
content.setSession(this);
content.setParent(this);
}
void addContent(JingleContentElement content)

View file

@ -1,5 +1,8 @@
package org.jivesoftware.smackx.jingle3.internal;
import java.util.ArrayList;
import java.util.List;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportElement;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportInfoElement;
@ -12,8 +15,45 @@ import org.jxmpp.jid.FullJid;
*/
public abstract class Transport<D extends JingleContentTransportElement> {
private Content parent;
private final ArrayList<TransportCandidate<?>> candidates = new ArrayList<>();
private Transport peersProposal;
private boolean isPeersProposal;
public abstract D getElement();
public void addCandidate(TransportCandidate<?> candidate) {
// Insert sorted by priority descending
// Empty list -> insert
if (candidates.isEmpty()) {
candidates.add(candidate);
candidate.setParent(this);
return;
}
// Find appropriate index
for (int i = 0; i < candidates.size(); i++) {
TransportCandidate<?> c = candidates.get(i);
// list already contains element -> return
if (c == candidate) {
return;
}
//Found the index
if (c.getPriority() <= candidate.getPriority()) {
candidates.add(i, candidate);
candidate.setParent(this);
}
}
}
public List<TransportCandidate<?>> getCandidates() {
return candidates;
}
public abstract String getNamespace();
public abstract void establishIncomingBytestreamSession(FullJid peer,
@ -26,7 +66,24 @@ public abstract class Transport<D extends JingleContentTransportElement> {
BytestreamSessionEstablishedListener listener,
XMPPConnection connection);
public abstract void setPeersProposal(Transport<?> peersProposal);
public void setPeersProposal(Transport peersProposal) {
this.peersProposal = peersProposal;
peersProposal.isPeersProposal = true;
}
public boolean isPeersProposal() {
return isPeersProposal;
}
public abstract void handleTransportInfo(JingleContentTransportInfoElement info);
public void setParent(Content parent) {
if (this.parent != parent) {
this.parent = parent;
}
}
public Content getParent() {
return parent;
}
}

View file

@ -0,0 +1,32 @@
package org.jivesoftware.smackx.jingle3.internal;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportCandidateElement;
/**
* Created by vanitas on 21.07.17.
*/
public abstract class TransportCandidate<E extends JingleContentTransportCandidateElement> {
private Transport<?> parent;
private int priority;
public void setParent(Transport<?> transport) {
if (parent != transport) {
parent = transport;
}
}
public Transport<?> getParent() {
return parent;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public abstract E getElement();
}

View file

@ -10,6 +10,7 @@ import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportInfoElement;
import org.jivesoftware.smackx.jingle3.internal.Transport;
import org.jivesoftware.smackx.jingle3.internal.TransportCandidate;
import org.jivesoftware.smackx.jingle3.transport.BytestreamSessionEstablishedListener;
import org.jivesoftware.smackx.jingle3.transport.jingle_ibb.element.JingleIBBTransportElement;
@ -26,8 +27,6 @@ public class JingleIBBTransport extends Transport<JingleIBBTransportElement> {
private final String streamId;
private final Short blockSize;
private JingleIBBTransport peersProposal;
public JingleIBBTransport(String streamId, Short blockSize) {
this.streamId = streamId;
this.blockSize = blockSize;
@ -82,12 +81,13 @@ public class JingleIBBTransport extends Transport<JingleIBBTransportElement> {
}
@Override
public void setPeersProposal(Transport<?> peersProposal) {
this.peersProposal = (JingleIBBTransport) peersProposal;
public void addCandidate(TransportCandidate<?> candidate) {
// Sorry, we don't want any candidates.
}
@Override
public void handleTransportInfo(JingleContentTransportInfoElement info) {
// Nothing to do.
}
}

View file

@ -1,13 +1,14 @@
package org.jivesoftware.smackx.jingle3.transport.jingle_s5b;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportInfoElement;
import org.jivesoftware.smackx.jingle3.internal.Transport;
import org.jivesoftware.smackx.jingle3.internal.TransportCandidate;
import org.jivesoftware.smackx.jingle3.transport.BytestreamSessionEstablishedListener;
import org.jivesoftware.smackx.jingle3.transport.jingle_s5b.elements.JingleS5BTransportCandidateElement;
import org.jivesoftware.smackx.jingle3.transport.jingle_s5b.elements.JingleS5BTransportElement;
@ -27,11 +28,8 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
private String dstAddr;
private Bytestream.Mode mode;
private HashMap<String, JingleS5BTransportCandidate> candidates = new HashMap<>();
private String peerDstAddr;
private Bytestream.Mode peerMode;
private HashMap<String, JingleS5BTransportCandidate> peerCandidates;
private JingleS5BTransport peersProposal;
private JingleS5BTransportCandidate ourChoice, theirChoice;
private JingleS5BTransportCandidate nominee;
@ -41,9 +39,9 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
this.dstAddr = dstAddr;
this.mode = mode;
for (JingleS5BTransportCandidate c : (candidates != null ?
for (TransportCandidate<JingleS5BTransportCandidateElement> c : (candidates != null ?
candidates : Collections.<JingleS5BTransportCandidate>emptySet())) {
this.candidates.put(c.getCandidateId(), c);
addCandidate(c);
}
}
@ -54,14 +52,21 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
.setDestinationAddress(dstAddr)
.setMode(mode);
for (JingleS5BTransportCandidate candidate : candidates.values()) {
builder.addTransportCandidate(
(JingleS5BTransportCandidateElement) candidate.getElement());
for (TransportCandidate candidate : getCandidates()) {
builder.addTransportCandidate((JingleS5BTransportCandidateElement) candidate.getElement());
}
return builder.build();
}
public String getDstAddr() {
return dstAddr;
}
public Bytestream.Mode getMode() {
return mode;
}
@Override
public String getNamespace() {
return NAMESPACE;
@ -69,7 +74,7 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
@Override
public void establishIncomingBytestreamSession(FullJid peer, String transportSessionId, BytestreamSessionEstablishedListener listener, XMPPConnection connection) {
Socks5Proxy.getSocks5Proxy().addTransfer(dstAddr);
}
@Override
@ -77,14 +82,6 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
}
@Override
public void setPeersProposal(Transport<?> peersProposal) {
JingleS5BTransport transport = (JingleS5BTransport) peersProposal;
peerCandidates = transport.candidates;
peerDstAddr = transport.dstAddr;
peerMode = transport.mode;
}
@Override
public void handleTransportInfo(JingleContentTransportInfoElement info) {
switch (info.getElementName()) {
@ -112,7 +109,6 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
private void handleCandidateUsed(JingleS5BTransportInfoElement info) {
String candidateId = ((JingleS5BTransportInfoElement.CandidateUsed) info).getCandidateId();
theirChoice = candidates.get(candidateId);
if (theirChoice == null) {
/*

View file

@ -1,7 +1,12 @@
package org.jivesoftware.smackx.jingle3.transport.jingle_s5b;
import java.util.ArrayList;
import org.jivesoftware.smackx.jingle3.adapter.JingleTransportAdapter;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportCandidateElement;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportElement;
import org.jivesoftware.smackx.jingle3.transport.jingle_s5b.elements.JingleS5BTransportCandidateElement;
import org.jivesoftware.smackx.jingle3.transport.jingle_s5b.elements.JingleS5BTransportElement;
/**
* Created by vanitas on 19.07.17.
@ -10,7 +15,14 @@ public class JingleS5BTransportAdapter implements JingleTransportAdapter<JingleS
@Override
public JingleS5BTransport transportFromElement(JingleContentTransportElement element) {
JingleS5BTransport transport = new JingleS5BTransport();
JingleS5BTransportElement s5b = (JingleS5BTransportElement) element;
ArrayList<JingleS5BTransportCandidate> candidates = new ArrayList<>();
for (JingleContentTransportCandidateElement e : element.getCandidates()) {
candidates.add(JingleS5BTransportCandidate.fromElement((JingleS5BTransportCandidateElement) e));
}
return new JingleS5BTransport(s5b.getSid(), s5b.getDestinationAddress(), s5b.getMode(), candidates);
}
@Override

View file

@ -1,27 +1,42 @@
package org.jivesoftware.smackx.jingle3.transport.jingle_s5b;
import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Client;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5ClientForInitiator;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportCandidateElement;
import org.jivesoftware.smackx.jingle3.internal.Session;
import org.jivesoftware.smackx.jingle3.internal.TransportCandidate;
import org.jivesoftware.smackx.jingle3.transport.jingle_s5b.elements.JingleS5BTransportCandidateElement;
/**
* Created by vanitas on 19.07.17.
*/
public class JingleS5BTransportCandidate {
public class JingleS5BTransportCandidate extends TransportCandidate<JingleS5BTransportCandidateElement> {
private final String candidateId;
private final Bytestream.StreamHost streamHost;
private final int priority;
private final JingleS5BTransportCandidateElement.Type type;
private Socket socket;
public JingleS5BTransportCandidate(String candidateId,
Bytestream.StreamHost streamHost,
int priority,
JingleS5BTransportCandidateElement.Type type) {
this.candidateId = candidateId;
this.streamHost = streamHost;
this.priority = priority;
this.type = type;
setPriority(priority);
}
public static JingleS5BTransportCandidate fromElement(JingleS5BTransportCandidateElement element) {
return new JingleS5BTransportCandidate(element.getCandidateId(), element.getStreamHost(), element.getPriority(), element.getType());
}
public String getCandidateId() {
@ -32,15 +47,30 @@ public class JingleS5BTransportCandidate {
return streamHost;
}
public int getPriority() {
return priority;
}
public JingleS5BTransportCandidateElement.Type getType() {
return type;
}
public JingleContentTransportCandidateElement getElement() {
return new JingleS5BTransportCandidateElement(candidateId, streamHost.getAddress(), streamHost.getJID(), streamHost.getPort(), priority, type);
public JingleS5BTransportCandidateElement getElement() {
return new JingleS5BTransportCandidateElement(
getCandidateId(), getStreamHost().getAddress(),
getStreamHost().getJID(), getStreamHost().getPort(),
getPriority(), getType());
}
public Socket connect(int timeout) throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
Socks5Client client;
if (getParent().isPeersProposal()) {
client = new Socks5Client(getStreamHost(), ((JingleS5BTransport) getParent()).getDstAddr());
}
else {
Session session = getParent().getParent().getParent();
client = new Socks5ClientForInitiator(getStreamHost(), ((JingleS5BTransport) getParent()).getDstAddr(),
session.getJingleManager().getConnection(), session.getSessionId(), session.getPeer());
}
this.socket = client.getSocket(timeout);
return socket;
}
}