1package eu.siacs.conversations.xmpp.jingle;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.List;
8import java.util.Map.Entry;
9
10import android.graphics.BitmapFactory;
11import android.util.Log;
12
13import eu.siacs.conversations.entities.Account;
14import eu.siacs.conversations.entities.Conversation;
15import eu.siacs.conversations.entities.Message;
16import eu.siacs.conversations.services.XmppConnectionService;
17import eu.siacs.conversations.xml.Element;
18import eu.siacs.conversations.xmpp.OnIqPacketReceived;
19import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
20import eu.siacs.conversations.xmpp.jingle.stanzas.JinglePacket;
21import eu.siacs.conversations.xmpp.jingle.stanzas.Reason;
22import eu.siacs.conversations.xmpp.stanzas.IqPacket;
23
24public class JingleConnection {
25
26 private final String[] extensions = {"webp","jpeg","jpg","png"};
27 private final String[] cryptoExtensions = {"pgp","gpg"};
28
29 private JingleConnectionManager mJingleConnectionManager;
30 private XmppConnectionService mXmppConnectionService;
31
32 public static final int STATUS_INITIATED = 0;
33 public static final int STATUS_ACCEPTED = 1;
34 public static final int STATUS_TERMINATED = 2;
35 public static final int STATUS_CANCELED = 3;
36 public static final int STATUS_FINISHED = 4;
37 public static final int STATUS_TRANSMITTING = 5;
38 public static final int STATUS_FAILED = 99;
39
40 private int ibbBlockSize = 4096;
41
42 private int status = -1;
43 private Message message;
44 private String sessionId;
45 private Account account;
46 private String initiator;
47 private String responder;
48 private List<JingleCandidate> candidates = new ArrayList<JingleCandidate>();
49 private HashMap<String, JingleSocks5Transport> connections = new HashMap<String, JingleSocks5Transport>();
50
51 private String transportId;
52 private Element fileOffer;
53 private JingleFile file = null;
54
55 private String contentName;
56 private String contentCreator;
57
58 private boolean receivedCandidate = false;
59 private boolean sentCandidate = false;
60
61 private boolean acceptedAutomatically = false;
62
63 private JingleTransport transport = null;
64
65 private OnIqPacketReceived responseListener = new OnIqPacketReceived() {
66
67 @Override
68 public void onIqPacketReceived(Account account, IqPacket packet) {
69 if (packet.getType() == IqPacket.TYPE_ERROR) {
70 if (initiator.equals(account.getFullJid())) {
71 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
72 }
73 status = STATUS_FAILED;
74 }
75 }
76 };
77
78 final OnFileTransmitted onFileTransmitted = new OnFileTransmitted() {
79
80 @Override
81 public void onFileTransmitted(JingleFile file) {
82 if (responder.equals(account.getFullJid())) {
83 sendSuccess();
84 if (acceptedAutomatically) {
85 message.markUnread();
86 JingleConnection.this.mXmppConnectionService.updateUi(message.getConversation(), true);
87 }
88 BitmapFactory.Options options = new BitmapFactory.Options();
89 options.inJustDecodeBounds = true;
90 BitmapFactory.decodeFile(file.getAbsolutePath(),options);
91 int imageHeight = options.outHeight;
92 int imageWidth = options.outWidth;
93 message.setBody(""+file.getSize()+","+imageWidth+","+imageHeight);
94 mXmppConnectionService.databaseBackend.createMessage(message);
95 mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVED);
96 }
97 Log.d("xmppService","sucessfully transmitted file:"+file.getName()+" encryption:"+message.getEncryption());
98 }
99 };
100
101 private OnProxyActivated onProxyActivated = new OnProxyActivated() {
102
103 @Override
104 public void success() {
105 if (initiator.equals(account.getFullJid())) {
106 Log.d("xmppService","we were initiating. sending file");
107 transport.send(file,onFileTransmitted);
108 } else {
109 transport.receive(file,onFileTransmitted);
110 Log.d("xmppService","we were responding. receiving file");
111 }
112 }
113
114 @Override
115 public void failed() {
116 Log.d("xmppService","proxy activation failed");
117 }
118 };
119
120 public JingleConnection(JingleConnectionManager mJingleConnectionManager) {
121 this.mJingleConnectionManager = mJingleConnectionManager;
122 this.mXmppConnectionService = mJingleConnectionManager.getXmppConnectionService();
123 }
124
125 public String getSessionId() {
126 return this.sessionId;
127 }
128
129 public String getAccountJid() {
130 return this.account.getFullJid();
131 }
132
133 public String getCounterPart() {
134 return this.message.getCounterpart();
135 }
136
137 public void deliverPacket(JinglePacket packet) {
138 boolean returnResult = true;
139 if (packet.isAction("session-terminate")) {
140 Reason reason = packet.getReason();
141 if (reason!=null) {
142 if (reason.hasChild("cancel")) {
143 this.receiveCancel();
144 } else if (reason.hasChild("success")) {
145 this.receiveSuccess();
146 } else {
147 this.receiveCancel();
148 }
149 } else {
150 this.receiveCancel();
151 }
152 } else if (packet.isAction("session-accept")) {
153 returnResult = receiveAccept(packet);
154 } else if (packet.isAction("transport-info")) {
155 returnResult = receiveTransportInfo(packet);
156 } else if (packet.isAction("transport-replace")) {
157 if (packet.getJingleContent().hasIbbTransport()) {
158 returnResult = this.receiveFallbackToIbb(packet);
159 } else {
160 returnResult = false;
161 Log.d("xmppService","trying to fallback to something unknown"+packet.toString());
162 }
163 } else if (packet.isAction("transport-accept")) {
164 returnResult = this.receiveTransportAccept(packet);
165 } else {
166 Log.d("xmppService","packet arrived in connection. action was "+packet.getAction());
167 returnResult = false;
168 }
169 IqPacket response;
170 if (returnResult) {
171 response = packet.generateRespone(IqPacket.TYPE_RESULT);
172
173 } else {
174 response = packet.generateRespone(IqPacket.TYPE_ERROR);
175 }
176 account.getXmppConnection().sendIqPacket(response, null);
177 }
178
179 public void init(Message message) {
180 this.contentCreator = "initiator";
181 this.contentName = this.mJingleConnectionManager.nextRandomId();
182 this.message = message;
183 this.account = message.getConversation().getAccount();
184 this.initiator = this.account.getFullJid();
185 this.responder = this.message.getCounterpart();
186 this.sessionId = this.mJingleConnectionManager.nextRandomId();
187 if (this.candidates.size() > 0) {
188 this.sendInitRequest();
189 } else {
190 this.mJingleConnectionManager.getPrimaryCandidate(account, new OnPrimaryCandidateFound() {
191
192 @Override
193 public void onPrimaryCandidateFound(boolean success, final JingleCandidate candidate) {
194 if (success) {
195 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(JingleConnection.this, candidate);
196 connections.put(candidate.getCid(), socksConnection);
197 socksConnection.connect(new OnTransportConnected() {
198
199 @Override
200 public void failed() {
201 Log.d("xmppService","connection to our own primary candidete failed");
202 sendInitRequest();
203 }
204
205 @Override
206 public void established() {
207 Log.d("xmppService","succesfully connected to our own primary candidate");
208 mergeCandidate(candidate);
209 sendInitRequest();
210 }
211 });
212 mergeCandidate(candidate);
213 } else {
214 Log.d("xmppService","no primary candidate of our own was found");
215 sendInitRequest();
216 }
217 }
218 });
219 }
220
221 }
222
223 public void init(Account account, JinglePacket packet) {
224 this.status = STATUS_INITIATED;
225 Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().split("/")[0], false);
226 this.message = new Message(conversation, "", Message.ENCRYPTION_NONE);
227 this.message.setType(Message.TYPE_IMAGE);
228 this.message.setStatus(Message.STATUS_RECEIVED_OFFER);
229 this.message.setJingleConnection(this);
230 String[] fromParts = packet.getFrom().split("/");
231 this.message.setPresence(fromParts[1]);
232 this.account = account;
233 this.initiator = packet.getFrom();
234 this.responder = this.account.getFullJid();
235 this.sessionId = packet.getSessionId();
236 Content content = packet.getJingleContent();
237 this.contentCreator = content.getAttribute("creator");
238 this.contentName = content.getAttribute("name");
239 this.transportId = content.getTransportId();
240 this.mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
241 this.fileOffer = packet.getJingleContent().getFileOffer();
242 if (fileOffer!=null) {
243 Element fileSize = fileOffer.findChild("size");
244 Element fileNameElement = fileOffer.findChild("name");
245 if (fileNameElement!=null) {
246 boolean supportedFile = false;
247 String[] filename = fileNameElement.getContent().toLowerCase().split("\\.");
248 if (Arrays.asList(this.extensions).contains(filename[filename.length - 1])) {
249 supportedFile = true;
250 } else if (Arrays.asList(this.cryptoExtensions).contains(filename[filename.length - 1])) {
251 if (filename.length == 3) {
252 if (Arrays.asList(this.extensions).contains(filename[filename.length -2])) {
253 supportedFile = true;
254 this.message.setEncryption(Message.ENCRYPTION_PGP);
255 }
256 }
257 }
258 if (supportedFile) {
259 long size = Long.parseLong(fileSize.getContent());
260 message.setBody(""+size);
261 conversation.getMessages().add(message);
262 if (size<=this.mJingleConnectionManager.getAutoAcceptFileSize()) {
263 Log.d("xmppService","auto accepting file from "+packet.getFrom());
264 this.acceptedAutomatically = true;
265 this.sendAccept();
266 } else {
267 message.markUnread();
268 Log.d("xmppService","not auto accepting new file offer with size: "+size+" allowed size:"+this.mJingleConnectionManager.getAutoAcceptFileSize());
269 this.mXmppConnectionService.updateUi(conversation, true);
270 }
271 this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message,false);
272 this.file.setExpectedSize(size);
273 } else {
274 this.sendCancel();
275 }
276 } else {
277 this.sendCancel();
278 }
279 } else {
280 this.sendCancel();
281 }
282 }
283
284 private void sendInitRequest() {
285 JinglePacket packet = this.bootstrapPacket("session-initiate");
286 Content content = new Content(this.contentCreator,this.contentName);
287 if (message.getType() == Message.TYPE_IMAGE) {
288 content.setTransportId(this.transportId);
289 this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message,false);
290 content.setFileOffer(this.file);
291 this.transportId = this.mJingleConnectionManager.nextRandomId();
292 content.setTransportId(this.transportId);
293 content.socks5transport().setChildren(getCandidatesAsElements());
294 packet.setContent(content);
295 this.sendJinglePacket(packet);
296 this.status = STATUS_INITIATED;
297 }
298 }
299
300 private List<Element> getCandidatesAsElements() {
301 List<Element> elements = new ArrayList<Element>();
302 for(JingleCandidate c : this.candidates) {
303 elements.add(c.toElement());
304 }
305 return elements;
306 }
307
308 private void sendAccept() {
309 status = STATUS_ACCEPTED;
310 mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVING);
311 this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() {
312
313 @Override
314 public void onPrimaryCandidateFound(boolean success,final JingleCandidate candidate) {
315 final JinglePacket packet = bootstrapPacket("session-accept");
316 final Content content = new Content(contentCreator,contentName);
317 content.setFileOffer(fileOffer);
318 content.setTransportId(transportId);
319 if ((success)&&(!equalCandidateExists(candidate))) {
320 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(JingleConnection.this, candidate);
321 connections.put(candidate.getCid(), socksConnection);
322 socksConnection.connect(new OnTransportConnected() {
323
324 @Override
325 public void failed() {
326 Log.d("xmppService","connection to our own primary candidate failed");
327 content.socks5transport().setChildren(getCandidatesAsElements());
328 packet.setContent(content);
329 sendJinglePacket(packet);
330 connectNextCandidate();
331 }
332
333 @Override
334 public void established() {
335 Log.d("xmppService","connected to primary candidate");
336 mergeCandidate(candidate);
337 content.socks5transport().setChildren(getCandidatesAsElements());
338 packet.setContent(content);
339 sendJinglePacket(packet);
340 connectNextCandidate();
341 }
342 });
343 } else {
344 Log.d("xmppService","did not find a primary candidate for ourself");
345 content.socks5transport().setChildren(getCandidatesAsElements());
346 packet.setContent(content);
347 sendJinglePacket(packet);
348 connectNextCandidate();
349 }
350 }
351 });
352
353 }
354
355 private JinglePacket bootstrapPacket(String action) {
356 JinglePacket packet = new JinglePacket();
357 packet.setAction(action);
358 packet.setFrom(account.getFullJid());
359 packet.setTo(this.message.getCounterpart());
360 packet.setSessionId(this.sessionId);
361 packet.setInitiator(this.initiator);
362 return packet;
363 }
364
365 private void sendJinglePacket(JinglePacket packet) {
366 //Log.d("xmppService",packet.toString());
367 account.getXmppConnection().sendIqPacket(packet,responseListener);
368 }
369
370 private boolean receiveAccept(JinglePacket packet) {
371 Content content = packet.getJingleContent();
372 mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
373 this.status = STATUS_ACCEPTED;
374 mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
375 this.connectNextCandidate();
376 return true;
377 }
378
379 private boolean receiveTransportInfo(JinglePacket packet) {
380 Content content = packet.getJingleContent();
381 if (content.hasSocks5Transport()) {
382 if (content.socks5transport().hasChild("activated")) {
383 if ((this.transport!=null)&&(this.transport instanceof JingleSocks5Transport)) {
384 onProxyActivated.success();
385 } else {
386 String cid = content.socks5transport().findChild("activated").getAttribute("cid");
387 Log.d("xmppService","received proxy activated ("+cid+")prior to choosing our own transport");
388 JingleSocks5Transport connection = this.connections.get(cid);
389 if (connection!=null) {
390 connection.setActivated(true);
391 } else {
392 Log.d("xmppService","activated connection not found");
393 this.sendCancel();
394 }
395 }
396 return true;
397 } else if (content.socks5transport().hasChild("proxy-error")) {
398 onProxyActivated.failed();
399 return true;
400 } else if (content.socks5transport().hasChild("candidate-error")) {
401 Log.d("xmppService","received candidate error");
402 this.receivedCandidate = true;
403 if ((status == STATUS_ACCEPTED)&&(this.sentCandidate)) {
404 this.connect();
405 }
406 return true;
407 } else if (content.socks5transport().hasChild("candidate-used")){
408 String cid = content.socks5transport().findChild("candidate-used").getAttribute("cid");
409 if (cid!=null) {
410 Log.d("xmppService","candidate used by counterpart:"+cid);
411 JingleCandidate candidate = getCandidate(cid);
412 candidate.flagAsUsedByCounterpart();
413 this.receivedCandidate = true;
414 if ((status == STATUS_ACCEPTED)&&(this.sentCandidate)) {
415 this.connect();
416 } else {
417 Log.d("xmppService","ignoring because file is already in transmission or we havent sent our candidate yet");
418 }
419 return true;
420 } else {
421 return false;
422 }
423 } else {
424 return false;
425 }
426 } else {
427 return true;
428 }
429 }
430
431 private void connect() {
432 final JingleSocks5Transport connection = chooseConnection();
433 this.transport = connection;
434 if (connection==null) {
435 Log.d("xmppService","could not find suitable candidate");
436 this.disconnect();
437 if (this.initiator.equals(account.getFullJid())) {
438 this.sendFallbackToIbb();
439 }
440 } else {
441 this.status = STATUS_TRANSMITTING;
442 if (connection.needsActivation()) {
443 if (connection.getCandidate().isOurs()) {
444 Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy. going to activate");
445 IqPacket activation = new IqPacket(IqPacket.TYPE_SET);
446 activation.setTo(connection.getCandidate().getJid());
447 activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId());
448 activation.query().addChild("activate").setContent(this.getCounterPart());
449 this.account.getXmppConnection().sendIqPacket(activation, new OnIqPacketReceived() {
450
451 @Override
452 public void onIqPacketReceived(Account account, IqPacket packet) {
453 if (packet.getType()==IqPacket.TYPE_ERROR) {
454 onProxyActivated.failed();
455 } else {
456 onProxyActivated.success();
457 sendProxyActivated(connection.getCandidate().getCid());
458 }
459 }
460 });
461 } else {
462 Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was a proxy. waiting for other party to activate");
463 }
464 } else {
465 if (initiator.equals(account.getFullJid())) {
466 Log.d("xmppService","we were initiating. sending file");
467 connection.send(file,onFileTransmitted);
468 } else {
469 Log.d("xmppService","we were responding. receiving file");
470 connection.receive(file,onFileTransmitted);
471 }
472 }
473 }
474 }
475
476 private JingleSocks5Transport chooseConnection() {
477 JingleSocks5Transport connection = null;
478 Iterator<Entry<String, JingleSocks5Transport>> it = this.connections.entrySet().iterator();
479 while (it.hasNext()) {
480 Entry<String, JingleSocks5Transport> pairs = it.next();
481 JingleSocks5Transport currentConnection = pairs.getValue();
482 //Log.d("xmppService","comparing candidate: "+currentConnection.getCandidate().toString());
483 if (currentConnection.isEstablished()&&(currentConnection.getCandidate().isUsedByCounterpart()||(!currentConnection.getCandidate().isOurs()))) {
484 //Log.d("xmppService","is usable");
485 if (connection==null) {
486 connection = currentConnection;
487 } else {
488 if (connection.getCandidate().getPriority()<currentConnection.getCandidate().getPriority()) {
489 connection = currentConnection;
490 } else if (connection.getCandidate().getPriority()==currentConnection.getCandidate().getPriority()) {
491 //Log.d("xmppService","found two candidates with same priority");
492 if (initiator.equals(account.getFullJid())) {
493 if (currentConnection.getCandidate().isOurs()) {
494 connection = currentConnection;
495 }
496 } else {
497 if (!currentConnection.getCandidate().isOurs()) {
498 connection = currentConnection;
499 }
500 }
501 }
502 }
503 }
504 it.remove();
505 }
506 return connection;
507 }
508
509 private void sendSuccess() {
510 JinglePacket packet = bootstrapPacket("session-terminate");
511 Reason reason = new Reason();
512 reason.addChild("success");
513 packet.setReason(reason);
514 this.sendJinglePacket(packet);
515 this.disconnect();
516 this.status = STATUS_FINISHED;
517 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_RECIEVED);
518 this.mJingleConnectionManager.finishConnection(this);
519 }
520
521 private void sendFallbackToIbb() {
522 JinglePacket packet = this.bootstrapPacket("transport-replace");
523 Content content = new Content(this.contentCreator,this.contentName);
524 this.transportId = this.mJingleConnectionManager.nextRandomId();
525 content.setTransportId(this.transportId);
526 content.ibbTransport().setAttribute("block-size",""+this.ibbBlockSize);
527 packet.setContent(content);
528 this.sendJinglePacket(packet);
529 }
530
531 private boolean receiveFallbackToIbb(JinglePacket packet) {
532 String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
533 if (receivedBlockSize!=null) {
534 int bs = Integer.parseInt(receivedBlockSize);
535 if (bs>this.ibbBlockSize) {
536 this.ibbBlockSize = bs;
537 }
538 }
539 this.transportId = packet.getJingleContent().getTransportId();
540 this.transport = new JingleInbandTransport(this.account,this.responder,this.transportId,this.ibbBlockSize);
541 this.transport.receive(file, onFileTransmitted);
542 JinglePacket answer = bootstrapPacket("transport-accept");
543 Content content = new Content("initiator", "a-file-offer");
544 content.setTransportId(this.transportId);
545 content.ibbTransport().setAttribute("block-size", ""+this.ibbBlockSize);
546 answer.setContent(content);
547 this.sendJinglePacket(answer);
548 return true;
549 }
550
551 private boolean receiveTransportAccept(JinglePacket packet) {
552 if (packet.getJingleContent().hasIbbTransport()) {
553 String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
554 if (receivedBlockSize!=null) {
555 int bs = Integer.parseInt(receivedBlockSize);
556 if (bs>this.ibbBlockSize) {
557 this.ibbBlockSize = bs;
558 }
559 }
560 this.transport = new JingleInbandTransport(this.account,this.responder,this.transportId,this.ibbBlockSize);
561 this.transport.connect(new OnTransportConnected() {
562
563 @Override
564 public void failed() {
565 Log.d("xmppService","ibb open failed");
566 }
567
568 @Override
569 public void established() {
570 JingleConnection.this.transport.send(file, onFileTransmitted);
571 }
572 });
573 return true;
574 } else {
575 return false;
576 }
577 }
578
579 private void receiveSuccess() {
580 this.status = STATUS_FINISHED;
581 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND);
582 this.disconnect();
583 this.mJingleConnectionManager.finishConnection(this);
584 }
585
586 private void receiveCancel() {
587 this.disconnect();
588 this.status = STATUS_CANCELED;
589 this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_REJECTED);
590 this.mJingleConnectionManager.finishConnection(this);
591 }
592
593 private void sendCancel() {
594 JinglePacket packet = bootstrapPacket("session-terminate");
595 Reason reason = new Reason();
596 reason.addChild("cancel");
597 packet.setReason(reason);
598 this.sendJinglePacket(packet);
599 }
600
601 private void connectNextCandidate() {
602 for(JingleCandidate candidate : this.candidates) {
603 if ((!connections.containsKey(candidate.getCid())&&(!candidate.isOurs()))) {
604 this.connectWithCandidate(candidate);
605 return;
606 }
607 }
608 this.sendCandidateError();
609 }
610
611 private void connectWithCandidate(final JingleCandidate candidate) {
612 final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this,candidate);
613 connections.put(candidate.getCid(), socksConnection);
614 socksConnection.connect(new OnTransportConnected() {
615
616 @Override
617 public void failed() {
618 Log.d("xmppService", "connection failed with "+candidate.getHost()+":"+candidate.getPort());
619 connectNextCandidate();
620 }
621
622 @Override
623 public void established() {
624 Log.d("xmppService", "established connection with "+candidate.getHost()+":"+candidate.getPort());
625 sendCandidateUsed(candidate.getCid());
626 }
627 });
628 }
629
630 private void disconnect() {
631 Iterator<Entry<String, JingleSocks5Transport>> it = this.connections.entrySet().iterator();
632 while (it.hasNext()) {
633 Entry<String, JingleSocks5Transport> pairs = it.next();
634 pairs.getValue().disconnect();
635 it.remove();
636 }
637 }
638
639 private void sendProxyActivated(String cid) {
640 JinglePacket packet = bootstrapPacket("transport-info");
641 Content content = new Content(this.contentCreator,this.contentName);
642 content.setTransportId(this.transportId);
643 content.socks5transport().addChild("activated").setAttribute("cid", cid);
644 packet.setContent(content);
645 this.sendJinglePacket(packet);
646 }
647
648 private void sendCandidateUsed(final String cid) {
649 JinglePacket packet = bootstrapPacket("transport-info");
650 Content content = new Content(this.contentCreator,this.contentName);
651 content.setTransportId(this.transportId);
652 content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
653 packet.setContent(content);
654 this.sentCandidate = true;
655 if ((receivedCandidate)&&(status == STATUS_ACCEPTED)) {
656 connect();
657 }
658 this.sendJinglePacket(packet);
659 }
660
661 private void sendCandidateError() {
662 JinglePacket packet = bootstrapPacket("transport-info");
663 Content content = new Content(this.contentCreator,this.contentName);
664 content.setTransportId(this.transportId);
665 content.socks5transport().addChild("candidate-error");
666 packet.setContent(content);
667 this.sentCandidate = true;
668 if ((receivedCandidate)&&(status == STATUS_ACCEPTED)) {
669 connect();
670 }
671 this.sendJinglePacket(packet);
672 }
673
674 public String getInitiator() {
675 return this.initiator;
676 }
677
678 public String getResponder() {
679 return this.responder;
680 }
681
682 public int getStatus() {
683 return this.status;
684 }
685
686 private boolean equalCandidateExists(JingleCandidate candidate) {
687 for(JingleCandidate c : this.candidates) {
688 if (c.equalValues(candidate)) {
689 return true;
690 }
691 }
692 return false;
693 }
694
695 private void mergeCandidate(JingleCandidate candidate) {
696 for(JingleCandidate c : this.candidates) {
697 if (c.equals(candidate)) {
698 return;
699 }
700 }
701 this.candidates.add(candidate);
702 }
703
704 private void mergeCandidates(List<JingleCandidate> candidates) {
705 for(JingleCandidate c : candidates) {
706 mergeCandidate(c);
707 }
708 }
709
710 private JingleCandidate getCandidate(String cid) {
711 for(JingleCandidate c : this.candidates) {
712 if (c.getCid().equals(cid)) {
713 return c;
714 }
715 }
716 return null;
717 }
718
719 interface OnProxyActivated {
720 public void success();
721 public void failed();
722 }
723
724 public boolean hasTransportId(String sid) {
725 return sid.equals(this.transportId);
726 }
727
728 public JingleTransport getTransport() {
729 return this.transport;
730 }
731
732 public void accept() {
733 if (status==STATUS_INITIATED) {
734 new Thread(new Runnable() {
735
736 @Override
737 public void run() {
738 sendAccept();
739 }
740 }).start();
741 } else {
742 Log.d("xmppService","status ("+status+") was not ok");
743 }
744 }
745}