mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 20:42:06 +01:00
Use static threadPool and start working on async file transfer method
This commit is contained in:
parent
71486ab68e
commit
f26f3bb503
7 changed files with 65 additions and 6 deletions
|
@ -25,6 +25,7 @@ import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
||||||
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||||
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
|
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
|
||||||
import org.jivesoftware.smackx.jingle.Role;
|
import org.jivesoftware.smackx.jingle.Role;
|
||||||
import org.jivesoftware.smackx.jingle.element.Jingle;
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
||||||
|
@ -162,7 +163,7 @@ public class IncomingJingleFileOffer extends JingleFileTransferSession implement
|
||||||
public void onSessionInitiated(BytestreamSession bytestreamSession) {
|
public void onSessionInitiated(BytestreamSession bytestreamSession) {
|
||||||
LOGGER.log(Level.INFO, "Bytestream initiated. Start receiving.");
|
LOGGER.log(Level.INFO, "Bytestream initiated. Start receiving.");
|
||||||
receivingThread = new ReceiveTask(bytestreamSession, file, target);
|
receivingThread = new ReceiveTask(bytestreamSession, file, target);
|
||||||
queued.add(threadPool.submit(receivingThread));
|
queued.add(JingleManager.getThreadPool().submit(receivingThread));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -25,6 +25,7 @@ import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Manager;
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.SmackFuture;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
|
@ -82,6 +83,12 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
|
||||||
offer.send(file);
|
offer.send(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SmackFuture<?> asyncSendFile(FullJid recipient, File file) {
|
||||||
|
OutgoingJingleFileOffer offer = new OutgoingJingleFileOffer(connection(), recipient);
|
||||||
|
JingleManager.getInstanceFor(connection()).registerJingleSessionHandler(recipient, offer.getSessionId(), offer);
|
||||||
|
return offer.sendAsync(file);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IQ handleJingleRequest(Jingle jingle) {
|
public IQ handleJingleRequest(Jingle jingle) {
|
||||||
FullJid fullJid = jingle.getFrom().asFullJidOrThrow();
|
FullJid fullJid = jingle.getFrom().asFullJidOrThrow();
|
||||||
|
|
|
@ -21,9 +21,11 @@ import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.SmackException;
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.SmackFuture;
|
||||||
import org.jivesoftware.smack.XMPPConnection;
|
import org.jivesoftware.smack.XMPPConnection;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
||||||
import org.jivesoftware.smackx.jingle.JingleManager;
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
||||||
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
|
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
|
||||||
|
@ -75,6 +77,13 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
|
||||||
initiateFileOffer(transfer, JingleContent.Creator.initiator, contentName);
|
initiateFileOffer(transfer, JingleContent.Creator.initiator, contentName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SmackFuture<?> sendAsync(File file) {
|
||||||
|
source = file;
|
||||||
|
String contentName = "jft-" + StringUtils.randomString(20);
|
||||||
|
JingleFileTransfer transfer = JingleFileTransferManager.fileTransferFromFile(file);
|
||||||
|
return null; //TODO
|
||||||
|
}
|
||||||
|
|
||||||
public void initiateFileOffer(JingleFileTransfer file, JingleContent.Creator creator, String name) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
|
public void initiateFileOffer(JingleFileTransfer file, JingleContent.Creator creator, String name) throws InterruptedException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, SmackException.NoResponseException {
|
||||||
if (state != State.fresh) {
|
if (state != State.fresh) {
|
||||||
throw new IllegalStateException("This session is not fresh.");
|
throw new IllegalStateException("This session is not fresh.");
|
||||||
|
@ -113,7 +122,7 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
|
||||||
@Override
|
@Override
|
||||||
public void onSessionInitiated(final BytestreamSession session) {
|
public void onSessionInitiated(final BytestreamSession session) {
|
||||||
sendingThread = new SendTask(session, source);
|
sendingThread = new SendTask(session, source);
|
||||||
queued.add(threadPool.submit(sendingThread));
|
queued.add(JingleManager.getThreadPool().submit(sendingThread));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -138,7 +147,7 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
|
||||||
final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection)
|
final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection)
|
||||||
.getTransportManager(transportReplace);
|
.getTransportManager(transportReplace);
|
||||||
|
|
||||||
queued.add(threadPool.submit(new Runnable() {
|
queued.add(JingleManager.getThreadPool().submit(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -19,6 +19,8 @@ package org.jivesoftware.smackx.jingle;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@ -44,6 +46,12 @@ public final class JingleManager extends Manager {
|
||||||
|
|
||||||
private static final Map<XMPPConnection, JingleManager> INSTANCES = new WeakHashMap<>();
|
private static final Map<XMPPConnection, JingleManager> INSTANCES = new WeakHashMap<>();
|
||||||
|
|
||||||
|
private static final ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||||
|
|
||||||
|
public static ExecutorService getThreadPool() {
|
||||||
|
return threadPool;
|
||||||
|
}
|
||||||
|
|
||||||
public static synchronized JingleManager getInstanceFor(XMPPConnection connection) {
|
public static synchronized JingleManager getInstanceFor(XMPPConnection connection) {
|
||||||
JingleManager jingleManager = INSTANCES.get(connection);
|
JingleManager jingleManager = INSTANCES.get(connection);
|
||||||
if (jingleManager == null) {
|
if (jingleManager == null) {
|
||||||
|
|
|
@ -19,8 +19,6 @@ package org.jivesoftware.smackx.jingle;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@ -48,7 +46,6 @@ public abstract class JingleSession implements JingleSessionHandler {
|
||||||
|
|
||||||
protected final List<JingleContent> contents = new ArrayList<>();
|
protected final List<JingleContent> contents = new ArrayList<>();
|
||||||
|
|
||||||
protected static ExecutorService threadPool = Executors.newSingleThreadExecutor();
|
|
||||||
protected ArrayList<Future<?>> queued = new ArrayList<>();
|
protected ArrayList<Future<?>> queued = new ArrayList<>();
|
||||||
protected JingleTransportSession<?> transportSession;
|
protected JingleTransportSession<?> transportSession;
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2017 Paul Schaub
|
||||||
|
*
|
||||||
|
* 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.smackx.jingle_filetransfer;
|
package org.jivesoftware.smackx.jingle_filetransfer;
|
||||||
|
|
||||||
import static junit.framework.TestCase.fail;
|
import static junit.framework.TestCase.fail;
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2017 Paul Schaub
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO describe me.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smackx.jingle_filetransfer;
|
Loading…
Reference in a new issue