summaryrefslogtreecommitdiffstats
blob: d4a1ae401fc8b907098987002186811f3f42a224 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */
/*
* Copyright (c) 2014-2015 Isode Limited.
* All rights reserved.v3.
* 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.parser.payloadparsers;

import com.isode.stroke.parser.GenericPayloadParser;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.elements.JingleS5BTransportPayload;
import com.isode.stroke.base.NotNull;
import com.isode.stroke.jid.JID;
import com.isode.stroke.network.HostAddressPort;
import com.isode.stroke.network.HostAddress;
import java.util.logging.Logger;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class JingleS5BTransportMethodPayloadParser extends GenericPayloadParser<JingleS5BTransportPayload> {

	private int level = 0;
	private Logger logger_ = Logger.getLogger(this.getClass().getName());

	public JingleS5BTransportMethodPayloadParser() {
		super(new JingleS5BTransportPayload());
		this.level = 0;
	}

	private JingleS5BTransportPayload.Candidate.Type stringToType(String str) {
		if (str.equals("direct")) {
			return JingleS5BTransportPayload.Candidate.Type.DirectType;
		} else if (str.equals("assisted")) {
			return JingleS5BTransportPayload.Candidate.Type.AssistedType;
		} else if (str.equals("tunnel")) {
			return JingleS5BTransportPayload.Candidate.Type.TunnelType;
		} else if (str.equals("proxy")) {
			return JingleS5BTransportPayload.Candidate.Type.ProxyType;
		} else {
			logger_.warning("Unknown candidate type; falling back to default!");
			return JingleS5BTransportPayload.Candidate.Type.DirectType;
		}
	}
	/**
	* @param element, NotNull.
	* @param ns.
	* @param attributes.
	*/
	@Override
	public void handleStartElement(String element, String ns, AttributeMap attributes) {

		if (level == 0) {
			if(attributes.getAttributeValue("sid") != null) {
				getPayloadInternal().setSessionID(attributes.getAttributeValue("sid"));
			} else {
				getPayloadInternal().setSessionID("");
			}

			String mode = "";
			if(attributes.getAttributeValue("mode") != null) {
				mode = attributes.getAttributeValue("mode");
			} else {
				mode = "tcp";
			}

			if (mode.equals("tcp")) {
				getPayloadInternal().setMode(JingleS5BTransportPayload.Mode.TCPMode);
			} else if(mode.equals("udp")) {
				getPayloadInternal().setMode(JingleS5BTransportPayload.Mode.UDPMode);
			} else {
				logger_.warning("Unknown S5B mode; falling back to default!");
				getPayloadInternal().setMode(JingleS5BTransportPayload.Mode.TCPMode);
			}

			if(attributes.getAttributeValue("dstaddr") != null) {
				getPayloadInternal().setDstAddr(attributes.getAttributeValue("dstaddr"));
			} else {
				getPayloadInternal().setDstAddr("");
			}

		} else if (level == 1) {
			if (element.equals("candidate")) {
				JingleS5BTransportPayload.Candidate candidate = new JingleS5BTransportPayload.Candidate();
				candidate.cid = "";
				if(attributes.getAttributeValue("cid") != null) {
					candidate.cid = attributes.getAttributeValue("cid");
				}

				int port = -1;
				try {
					if(attributes.getAttributeValue("port") != null) {
						port = Integer.parseInt(attributes.getAttributeValue("port"));
					}
				} catch (NumberFormatException e) {

				}
				
				try {
					candidate.hostPort = new HostAddressPort(new HostAddress(InetAddress.getByName("")), port);
					if(attributes.getAttributeValue("host") != null) {
						candidate.hostPort = new HostAddressPort(new HostAddress(InetAddress.getByName(attributes.getAttributeValue("host"))), port);
					}
				} catch (UnknownHostException e) {

				}

				candidate.jid = new JID("");
				if(attributes.getAttributeValue("jid") != null) {
					candidate.jid = new JID(attributes.getAttributeValue("jid"));
				}

				int priority = -1;
				try {
					if(attributes.getAttributeValue("priority") != null) {
						priority = Integer.parseInt(attributes.getAttributeValue("priority"));
					}
				} catch (NumberFormatException e) {

				}
				candidate.priority = priority;

				candidate.type = stringToType("direct");
				if(attributes.getAttributeValue("type") != null) {
					candidate.type = stringToType(attributes.getAttributeValue("type"));
				}
				getPayloadInternal().addCandidate(candidate);

			} else if (element.equals("candidate-used")) {
				if(attributes.getAttributeValue("cid") != null) {
					getPayloadInternal().setCandidateUsed(attributes.getAttributeValue("cid"));
				} else {
					getPayloadInternal().setCandidateUsed("");
				}
			} else if (element.equals("candidate-error")) {
				getPayloadInternal().setCandidateError(true);
			} else if (element.equals("activated")) {
				if(attributes.getAttributeValue("cid") != null) {
					getPayloadInternal().setActivated(attributes.getAttributeValue("cid"));
				} else {
					getPayloadInternal().setActivated("");
				}
			} else if (element.equals("proxy-error")) {
				getPayloadInternal().setProxyError(true);
			}
		}

		++level;
	}

	/**
	* @param element, NotNull.
	* @param ns.
	*/
	@Override
	public void handleEndElement(String element, String ns) {
		--level;
	}

	/**
	* @param data, NotNull.
	*/
	@Override
	public void handleCharacterData(String data) {

	}
}