JingleConnection.java

  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.Locale;
  9import java.util.Map.Entry;
 10
 11import android.graphics.BitmapFactory;
 12import android.util.Log;
 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","otr"};
 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.getAbsolutePath());
 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(Locale.US).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							if (filename[filename.length - 1].equals("otr")) {
255								Log.d("xmppService","receiving otr file");
256								this.message.setEncryption(Message.ENCRYPTION_OTR);
257							} else {
258								this.message.setEncryption(Message.ENCRYPTION_PGP);
259							}
260						}
261					}
262				}
263				if (supportedFile) {
264					long size = Long.parseLong(fileSize.getContent());
265					message.setBody(""+size);
266					conversation.getMessages().add(message);
267					if (size<=this.mJingleConnectionManager.getAutoAcceptFileSize()) {
268						Log.d("xmppService","auto accepting file from "+packet.getFrom());
269						this.acceptedAutomatically = true;
270						this.sendAccept();
271					} else {
272						message.markUnread();
273						Log.d("xmppService","not auto accepting new file offer with size: "+size+" allowed size:"+this.mJingleConnectionManager.getAutoAcceptFileSize());
274						this.mXmppConnectionService.updateUi(conversation, true);
275					}
276					this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message,false);
277					if (message.getEncryption() == Message.ENCRYPTION_OTR) {
278						this.file.setKey(conversation.getSymmetricKey());
279					}
280					this.file.setExpectedSize(size);
281				} else {
282					this.sendCancel();
283				}
284			} else {
285				this.sendCancel();
286			}
287		} else {
288			this.sendCancel();
289		}
290	}
291	
292	private void sendInitRequest() {
293		JinglePacket packet = this.bootstrapPacket("session-initiate");
294		Content content = new Content(this.contentCreator,this.contentName);
295		if (message.getType() == Message.TYPE_IMAGE) {
296			content.setTransportId(this.transportId);
297			this.file = this.mXmppConnectionService.getFileBackend().getJingleFile(message,false);
298			if (message.getEncryption() == Message.ENCRYPTION_OTR) {
299				Conversation conversation = this.message.getConversation();
300				this.mXmppConnectionService.renewSymmetricKey(conversation);
301				content.setFileOffer(this.file, true);
302				this.file.setKey(conversation.getSymmetricKey());
303			} else {
304				content.setFileOffer(this.file,false);
305			}
306			this.transportId = this.mJingleConnectionManager.nextRandomId();
307			content.setTransportId(this.transportId);
308			content.socks5transport().setChildren(getCandidatesAsElements());
309			packet.setContent(content);
310			this.sendJinglePacket(packet);
311			this.status = STATUS_INITIATED;
312		}
313	}
314	
315	private List<Element> getCandidatesAsElements() {
316		List<Element> elements = new ArrayList<Element>();
317		for(JingleCandidate c : this.candidates) {
318			elements.add(c.toElement());
319		}
320		return elements;
321	}
322	
323	private void sendAccept() {
324		status = STATUS_ACCEPTED;
325		mXmppConnectionService.markMessage(message, Message.STATUS_RECIEVING);
326		this.mJingleConnectionManager.getPrimaryCandidate(this.account, new OnPrimaryCandidateFound() {
327			
328			@Override
329			public void onPrimaryCandidateFound(boolean success,final JingleCandidate candidate) {
330				final JinglePacket packet = bootstrapPacket("session-accept");
331				final Content content = new Content(contentCreator,contentName);
332				content.setFileOffer(fileOffer);
333				content.setTransportId(transportId);
334				if ((success)&&(!equalCandidateExists(candidate))) {
335					final JingleSocks5Transport socksConnection = new JingleSocks5Transport(JingleConnection.this, candidate);
336					connections.put(candidate.getCid(), socksConnection);
337					socksConnection.connect(new OnTransportConnected() {
338						
339						@Override
340						public void failed() {
341							Log.d("xmppService","connection to our own primary candidate failed");
342							content.socks5transport().setChildren(getCandidatesAsElements());
343							packet.setContent(content);
344							sendJinglePacket(packet);
345							connectNextCandidate();
346						}
347						
348						@Override
349						public void established() {
350							Log.d("xmppService","connected to primary candidate");
351							mergeCandidate(candidate);
352							content.socks5transport().setChildren(getCandidatesAsElements());
353							packet.setContent(content);
354							sendJinglePacket(packet);
355							connectNextCandidate();
356						}
357					});
358				} else {
359					Log.d("xmppService","did not find a primary candidate for ourself");
360					content.socks5transport().setChildren(getCandidatesAsElements());
361					packet.setContent(content);
362					sendJinglePacket(packet);
363					connectNextCandidate();
364				}
365			}
366		});
367		
368	}
369	
370	private JinglePacket bootstrapPacket(String action) {
371		JinglePacket packet = new JinglePacket();
372		packet.setAction(action);
373		packet.setFrom(account.getFullJid());
374		packet.setTo(this.message.getCounterpart());
375		packet.setSessionId(this.sessionId);
376		packet.setInitiator(this.initiator);
377		return packet;
378	}
379	
380	private void sendJinglePacket(JinglePacket packet) {
381		//Log.d("xmppService",packet.toString());
382		account.getXmppConnection().sendIqPacket(packet,responseListener);
383	}
384	
385	private boolean receiveAccept(JinglePacket packet) {
386		Content content = packet.getJingleContent();
387		mergeCandidates(JingleCandidate.parse(content.socks5transport().getChildren()));
388		this.status = STATUS_ACCEPTED;
389		mXmppConnectionService.markMessage(message, Message.STATUS_UNSEND);
390		this.connectNextCandidate();
391		return true;
392	}
393
394	private boolean receiveTransportInfo(JinglePacket packet) {
395		Content content = packet.getJingleContent();
396		if (content.hasSocks5Transport()) {
397			if (content.socks5transport().hasChild("activated")) {
398				if ((this.transport!=null)&&(this.transport instanceof JingleSocks5Transport)) {
399					onProxyActivated.success();
400				} else {
401					String cid = content.socks5transport().findChild("activated").getAttribute("cid");
402					Log.d("xmppService","received proxy activated ("+cid+")prior to choosing our own transport");
403					JingleSocks5Transport connection = this.connections.get(cid);
404					if (connection!=null) {
405						connection.setActivated(true);
406					} else {
407						Log.d("xmppService","activated connection not found");
408						this.sendCancel();
409					}
410				}
411				return true;
412			} else if (content.socks5transport().hasChild("proxy-error")) {
413				onProxyActivated.failed();
414				return true;
415			} else if (content.socks5transport().hasChild("candidate-error")) {
416				Log.d("xmppService","received candidate error");
417				this.receivedCandidate = true;
418				if ((status == STATUS_ACCEPTED)&&(this.sentCandidate)) {
419					this.connect();
420				}
421				return true;
422			} else if (content.socks5transport().hasChild("candidate-used")){
423				String cid = content.socks5transport().findChild("candidate-used").getAttribute("cid");
424				if (cid!=null) {
425					Log.d("xmppService","candidate used by counterpart:"+cid);
426					JingleCandidate candidate = getCandidate(cid);
427					candidate.flagAsUsedByCounterpart();
428					this.receivedCandidate = true;
429					if ((status == STATUS_ACCEPTED)&&(this.sentCandidate)) {
430						this.connect();
431					} else {
432						Log.d("xmppService","ignoring because file is already in transmission or we havent sent our candidate yet");
433					}
434					return true;
435				} else {
436					return false;
437				}
438			} else {
439				return false;
440			}
441		} else {
442			return true;
443		}
444	}
445
446	private void connect() {
447		final JingleSocks5Transport connection = chooseConnection();
448		this.transport = connection;
449		if (connection==null) {
450			Log.d("xmppService","could not find suitable candidate");
451			this.disconnect();
452			if (this.initiator.equals(account.getFullJid())) {
453				this.sendFallbackToIbb();
454			}
455		} else {
456			this.status = STATUS_TRANSMITTING;
457			if (connection.needsActivation()) {
458				if (connection.getCandidate().isOurs()) {
459					Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was our proxy. going to activate");
460					IqPacket activation = new IqPacket(IqPacket.TYPE_SET);
461					activation.setTo(connection.getCandidate().getJid());
462					activation.query("http://jabber.org/protocol/bytestreams").setAttribute("sid", this.getSessionId());
463					activation.query().addChild("activate").setContent(this.getCounterPart());
464					this.account.getXmppConnection().sendIqPacket(activation, new OnIqPacketReceived() {
465						
466						@Override
467						public void onIqPacketReceived(Account account, IqPacket packet) {
468							if (packet.getType()==IqPacket.TYPE_ERROR) {
469								onProxyActivated.failed();
470							} else {
471								onProxyActivated.success();
472								sendProxyActivated(connection.getCandidate().getCid());
473							}
474						}
475					});
476				} else {
477					Log.d("xmppService","candidate "+connection.getCandidate().getCid()+" was a proxy. waiting for other party to activate");
478				}
479			} else {
480				if (initiator.equals(account.getFullJid())) {
481					Log.d("xmppService","we were initiating. sending file");
482					connection.send(file,onFileTransmitted);
483				} else {
484					Log.d("xmppService","we were responding. receiving file");
485					connection.receive(file,onFileTransmitted);
486				}
487			}
488		}
489	}
490	
491	private JingleSocks5Transport chooseConnection() {
492		JingleSocks5Transport connection = null;
493		Iterator<Entry<String, JingleSocks5Transport>> it = this.connections.entrySet().iterator();
494	    while (it.hasNext()) {
495	    	Entry<String, JingleSocks5Transport> pairs = it.next();
496	    	JingleSocks5Transport currentConnection = pairs.getValue();
497	    	//Log.d("xmppService","comparing candidate: "+currentConnection.getCandidate().toString());
498	        if (currentConnection.isEstablished()&&(currentConnection.getCandidate().isUsedByCounterpart()||(!currentConnection.getCandidate().isOurs()))) {
499	        	//Log.d("xmppService","is usable");
500	        	if (connection==null) {
501	        		connection = currentConnection;
502	        	} else {
503	        		if (connection.getCandidate().getPriority()<currentConnection.getCandidate().getPriority()) {
504	        			connection = currentConnection;
505	        		} else if (connection.getCandidate().getPriority()==currentConnection.getCandidate().getPriority()) {
506	        			//Log.d("xmppService","found two candidates with same priority");
507	        			if (initiator.equals(account.getFullJid())) {
508	        				if (currentConnection.getCandidate().isOurs()) {
509	        					connection = currentConnection;
510	        				}
511	        			} else {
512	        				if (!currentConnection.getCandidate().isOurs()) {
513	        					connection = currentConnection;
514	        				}
515	        			}
516	        		}
517	        	}
518	        }
519	        it.remove();
520	    }
521		return connection;
522	}
523
524	private void sendSuccess() {
525		JinglePacket packet = bootstrapPacket("session-terminate");
526		Reason reason = new Reason();
527		reason.addChild("success");
528		packet.setReason(reason);
529		this.sendJinglePacket(packet);
530		this.disconnect();
531		this.status = STATUS_FINISHED;
532		this.mXmppConnectionService.markMessage(this.message, Message.STATUS_RECIEVED);
533		this.mJingleConnectionManager.finishConnection(this);
534	}
535	
536	private void sendFallbackToIbb() {
537		JinglePacket packet = this.bootstrapPacket("transport-replace");
538		Content content = new Content(this.contentCreator,this.contentName);
539		this.transportId = this.mJingleConnectionManager.nextRandomId();
540		content.setTransportId(this.transportId);
541		content.ibbTransport().setAttribute("block-size",""+this.ibbBlockSize);
542		packet.setContent(content);
543		this.sendJinglePacket(packet);
544	}
545	
546	private boolean receiveFallbackToIbb(JinglePacket packet) {
547		String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
548		if (receivedBlockSize!=null) {
549			int bs = Integer.parseInt(receivedBlockSize);
550			if (bs>this.ibbBlockSize) {
551				this.ibbBlockSize = bs;
552			}
553		}
554		this.transportId = packet.getJingleContent().getTransportId();
555		this.transport = new JingleInbandTransport(this.account,this.responder,this.transportId,this.ibbBlockSize);
556		this.transport.receive(file, onFileTransmitted);
557		JinglePacket answer = bootstrapPacket("transport-accept");
558		Content content = new Content("initiator", "a-file-offer");
559		content.setTransportId(this.transportId);
560		content.ibbTransport().setAttribute("block-size", ""+this.ibbBlockSize);
561		answer.setContent(content);
562		this.sendJinglePacket(answer);
563		return true;
564	}
565	
566	private boolean receiveTransportAccept(JinglePacket packet) {
567		if (packet.getJingleContent().hasIbbTransport()) {
568			String receivedBlockSize = packet.getJingleContent().ibbTransport().getAttribute("block-size");
569			if (receivedBlockSize!=null) {
570				int bs = Integer.parseInt(receivedBlockSize);
571				if (bs>this.ibbBlockSize) {
572					this.ibbBlockSize = bs;
573				}
574			}
575			this.transport = new JingleInbandTransport(this.account,this.responder,this.transportId,this.ibbBlockSize);
576			this.transport.connect(new OnTransportConnected() {
577				
578				@Override
579				public void failed() {
580					Log.d("xmppService","ibb open failed");
581				}
582				
583				@Override
584				public void established() {
585					JingleConnection.this.transport.send(file, onFileTransmitted);
586				}
587			});
588			return true;
589		} else {
590			return false;
591		}
592	}
593	
594	private void receiveSuccess() {
595		this.status = STATUS_FINISHED;
596		this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND);
597		this.disconnect();
598		this.mJingleConnectionManager.finishConnection(this);
599	}
600	
601	private void receiveCancel() {
602		this.disconnect();
603		this.status = STATUS_CANCELED;
604		this.mXmppConnectionService.markMessage(this.message, Message.STATUS_SEND_REJECTED);
605		this.mJingleConnectionManager.finishConnection(this);
606	}
607	
608	private void sendCancel() {
609		JinglePacket packet = bootstrapPacket("session-terminate");
610		Reason reason = new Reason();
611		reason.addChild("cancel");
612		packet.setReason(reason);
613		this.sendJinglePacket(packet);
614	}
615
616	private void connectNextCandidate() {
617		for(JingleCandidate candidate : this.candidates) {
618			if ((!connections.containsKey(candidate.getCid())&&(!candidate.isOurs()))) {
619				this.connectWithCandidate(candidate);
620				return;
621			}
622		}
623		this.sendCandidateError();
624	}
625	
626	private void connectWithCandidate(final JingleCandidate candidate) {
627		final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this,candidate);
628		connections.put(candidate.getCid(), socksConnection);
629		socksConnection.connect(new OnTransportConnected() {
630			
631			@Override
632			public void failed() {
633				Log.d("xmppService", "connection failed with "+candidate.getHost()+":"+candidate.getPort());
634				connectNextCandidate();
635			}
636			
637			@Override
638			public void established() {
639				Log.d("xmppService", "established connection with "+candidate.getHost()+":"+candidate.getPort());
640				sendCandidateUsed(candidate.getCid());
641			}
642		});
643	}
644
645	private void disconnect() {
646		Iterator<Entry<String, JingleSocks5Transport>> it = this.connections.entrySet().iterator();
647	    while (it.hasNext()) {
648	        Entry<String, JingleSocks5Transport> pairs = it.next();
649	        pairs.getValue().disconnect();
650	        it.remove();
651	    }
652	}
653	
654	private void sendProxyActivated(String cid) {
655		JinglePacket packet = bootstrapPacket("transport-info");
656		Content content = new Content(this.contentCreator,this.contentName);
657		content.setTransportId(this.transportId);
658		content.socks5transport().addChild("activated").setAttribute("cid", cid);
659		packet.setContent(content);
660		this.sendJinglePacket(packet);
661	}
662	
663	private void sendCandidateUsed(final String cid) {
664		JinglePacket packet = bootstrapPacket("transport-info");
665		Content content = new Content(this.contentCreator,this.contentName);
666		content.setTransportId(this.transportId);
667		content.socks5transport().addChild("candidate-used").setAttribute("cid", cid);
668		packet.setContent(content);
669		this.sentCandidate = true;
670		if ((receivedCandidate)&&(status == STATUS_ACCEPTED)) {
671			connect();
672		}
673		this.sendJinglePacket(packet);
674	}
675	
676	private void sendCandidateError() {
677		JinglePacket packet = bootstrapPacket("transport-info");
678		Content content = new Content(this.contentCreator,this.contentName);
679		content.setTransportId(this.transportId);
680		content.socks5transport().addChild("candidate-error");
681		packet.setContent(content);
682		this.sentCandidate = true;
683		if ((receivedCandidate)&&(status == STATUS_ACCEPTED)) {
684			connect();
685		}
686		this.sendJinglePacket(packet);
687	}
688
689	public String getInitiator() {
690		return this.initiator;
691	}
692	
693	public String getResponder() {
694		return this.responder;
695	}
696	
697	public int getStatus() {
698		return this.status;
699	}
700	
701	private boolean equalCandidateExists(JingleCandidate candidate) {
702		for(JingleCandidate c : this.candidates) {
703			if (c.equalValues(candidate)) {
704				return true;
705			}
706		}
707		return false;
708	}
709	
710	private void mergeCandidate(JingleCandidate candidate) {
711		for(JingleCandidate c : this.candidates) {
712			if (c.equals(candidate)) {
713				return;
714			}
715		}
716		this.candidates.add(candidate);
717	}
718	
719	private void mergeCandidates(List<JingleCandidate> candidates) {
720		for(JingleCandidate c : candidates) {
721			mergeCandidate(c);
722		}
723	}
724	
725	private JingleCandidate getCandidate(String cid) {
726		for(JingleCandidate c : this.candidates) {
727			if (c.getCid().equals(cid)) {
728				return c;
729			}
730		}
731		return null;
732	}
733	
734	interface OnProxyActivated {
735		public void success();
736		public void failed();
737	}
738
739	public boolean hasTransportId(String sid) {
740		return sid.equals(this.transportId);
741	}
742	
743	public JingleTransport getTransport() {
744		return this.transport;
745	}
746
747	public void accept() {
748		if (status==STATUS_INITIATED) {
749			new Thread(new Runnable() {
750				
751				@Override
752				public void run() {
753					sendAccept();
754				}
755			}).start();
756		} else {
757			Log.d("xmppService","status ("+status+") was not ok");
758		}
759	}
760}