Use static threadPool and start working on async file transfer method

This commit is contained in:
vanitasvitae 2017-07-02 15:42:16 +02:00
parent 71486ab68e
commit f26f3bb503
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
7 changed files with 65 additions and 6 deletions

View File

@ -25,6 +25,7 @@ import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
import org.jivesoftware.smackx.jingle.Role;
import org.jivesoftware.smackx.jingle.element.Jingle;
@ -162,7 +163,7 @@ public class IncomingJingleFileOffer extends JingleFileTransferSession implement
public void onSessionInitiated(BytestreamSession bytestreamSession) {
LOGGER.log(Level.INFO, "Bytestream initiated. Start receiving.");
receivingThread = new ReceiveTask(bytestreamSession, file, target);
queued.add(threadPool.submit(receivingThread));
queued.add(JingleManager.getThreadPool().submit(receivingThread));
}
@Override

View File

@ -25,6 +25,7 @@ import java.util.logging.Logger;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackFuture;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
@ -82,6 +83,12 @@ public final class JingleFileTransferManager extends Manager implements JingleHa
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
public IQ handleJingleRequest(Jingle jingle) {
FullJid fullJid = jingle.getFrom().asFullJidOrThrow();

View File

@ -21,9 +21,11 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackFuture;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleTransportMethodManager;
@ -75,6 +77,13 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
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 {
if (state != State.fresh) {
throw new IllegalStateException("This session is not fresh.");
@ -113,7 +122,7 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
@Override
public void onSessionInitiated(final BytestreamSession session) {
sendingThread = new SendTask(session, source);
queued.add(threadPool.submit(sendingThread));
queued.add(JingleManager.getThreadPool().submit(sendingThread));
}
@Override
@ -138,7 +147,7 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection)
.getTransportManager(transportReplace);
queued.add(threadPool.submit(new Runnable() {
queued.add(JingleManager.getThreadPool().submit(new Runnable() {
@Override
public void run() {
try {

View File

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.jingle;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
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 ExecutorService threadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
public static ExecutorService getThreadPool() {
return threadPool;
}
public static synchronized JingleManager getInstanceFor(XMPPConnection connection) {
JingleManager jingleManager = INSTANCES.get(connection);
if (jingleManager == null) {

View File

@ -19,8 +19,6 @@ package org.jivesoftware.smackx.jingle;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Logger;
@ -48,7 +46,6 @@ public abstract class JingleSession implements JingleSessionHandler {
protected final List<JingleContent> contents = new ArrayList<>();
protected static ExecutorService threadPool = Executors.newSingleThreadExecutor();
protected ArrayList<Future<?>> queued = new ArrayList<>();
protected JingleTransportSession<?> transportSession;

View File

@ -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;
import static junit.framework.TestCase.fail;

View File

@ -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;