summaryrefslogtreecommitdiffstats
blob: 5a812ff8ee7d08f48322774f79231969043e2498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright (c) 2010-2013 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */
/*
 * Copyright (c) 2015 Tarun Gupta.
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

package com.isode.stroke.filetransfer;

import com.isode.stroke.signals.Signal1;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.signals.Slot;
import com.isode.stroke.jid.JID;
import com.isode.stroke.queries.IQRouter;
import com.isode.stroke.elements.IBB;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.base.ByteArray;

public class IBBSendSession {

	private String id = "";
	private JID from = new JID();
	private JID to = new JID();
	private ReadBytestream bytestream;
	private IQRouter router;
	private int blockSize;
	private int sequenceNumber;
	private boolean active;
	private boolean waitingForData;

	public IBBSendSession(final String id, final JID from, final JID to, ReadBytestream bytestream, IQRouter router) {
		this.id = id; 
		this.from = from; 
		this.to = to; 
		this.bytestream = bytestream;
		this.router = router; 
		this.blockSize = 4096; 
		this.sequenceNumber = 0; 
		this.active = false; 
		this.waitingForData = false;
		bytestream.onDataAvailable.connect(new Slot() {
			@Override
			public void call() {
				handleDataAvailable();
			}
		});
	}

	public void start() {
		IBBRequest request = IBBRequest.create(from, to, IBB.createIBBOpen(id, (int)(blockSize)), router);
		request.onResponse.connect(new Slot2<IBB, ErrorPayload>() {
			@Override
			public void call(IBB b, ErrorPayload e) {
				handleIBBResponse(b, e);
			}
		});
		active = true;
		request.send();
	}

	public void stop() {
		if (active && router.isAvailable()) {
			IBBRequest.create(from, to, IBB.createIBBClose(id), router).send();
		}
		finish(null);
	}

	public JID getSender() {
		return from;
	}

	public JID getReceiver() {
		return to;
	}

	public void setBlockSize(int blockSize) {
		this.blockSize = blockSize;
	}

	public final Signal1<FileTransferError> onFinished = new Signal1<FileTransferError>();
	public final Signal1<Integer> onBytesSent = new Signal1<Integer>();

	private void handleIBBResponse(IBB ibb, ErrorPayload error) {
		if (error == null && active) {
			if (!bytestream.isFinished()) {
				sendMoreData();
			}
			else {
				finish(null);
			}
		}
		else {
			finish(new FileTransferError(FileTransferError.Type.PeerError));
		}		
	}
	private void finish(FileTransferError error) {
		active = false;
		onFinished.emit(error);
	}

	private void sendMoreData() {
		//try {
			ByteArray data = bytestream.read(blockSize);
			if (!data.isEmpty()) {
				waitingForData = false;
				IBBRequest request = IBBRequest.create(from, to, IBB.createIBBData(id, sequenceNumber, data), router);
				sequenceNumber++;
				request.onResponse.connect(new Slot2<IBB, ErrorPayload>() {
					@Override
					public void call(IBB b, ErrorPayload e) {
						handleIBBResponse(b, e);
					}
				});
				request.send();
				onBytesSent.emit(data.getSize());
			}
			else {
				waitingForData = true;
			}
		//}
		//catch (BytestreamException e) {
		//	finish(new FileTransferError(FileTransferError.Type.ReadError));
		//}		
	}

	private void handleDataAvailable() {
		if (waitingForData) {
			sendMoreData();
		}
	}
}