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