CandidateAttributeParser implements SdpParser<CandidateAttribute> { @Override public CandidateAttribute parse(String sdp) throws SdpException { try { String[] values = sdp.trim().substring(12).split(" "); int index = 0; String foundation = values[index++]; short componentId = Short.parseShort(values[index++]); String protocol = values[index++]; long priority = Long.parseLong(values[index++]); String address = values[index++]; int port = Integer.parseInt(values[index++]); index++; String type = values[index++]; if(!CandidateAttribute.isCandidateTypeValid(type)) { throw new IllegalArgumentException("Unrecognized candidate type: " + type); } String relatedAddress = null; int relatedPort = 0; if(!CandidateAttribute.TYP_HOST.equals(type)) { index++; relatedAddress = values[index++]; index++; relatedPort = Integer.parseInt(values[index++]); } String tcptype = null; if(protocol.equalsIgnoreCase("tcp")) { index++; tcptype = values[index++]; } index++; int generation = 0; if(index == values.length - 1) { generation = Integer.parseInt(values[index]); } CandidateAttribute candidate = new CandidateAttribute(); candidate.setFoundation(foundation); candidate.setComponentId(componentId); candidate.setProtocol(protocol); candidate.setPriority(priority); candidate.setAddress(address); candidate.setPort(port); candidate.setCandidateType(type); candidate.setRelatedAddress(relatedAddress); candidate.setRelatedPort(relatedPort); candidate.setTcpType(tcptype); candidate.setGeneration(generation); return candidate; } catch (Exception e) { throw new SdpException(PARSE_ERROR + sdp, e); } }  @Override boolean canParse(String sdp); @Override CandidateAttribute parse(String sdp); @Override void parse(CandidateAttribute field, String sdp);  }
@Test public void testParseHostCandidate() throws SdpException { String line = "a=candidate:1995739850 1 udp 2113937151 192.168.1.65 54550 typ host generation 0\n\r"; CandidateAttribute candidate = parser.parse(line); Assert.assertEquals("1995739850", candidate.getFoundation()); Assert.assertEquals(1, candidate.getComponentId()); Assert.assertEquals("udp", candidate.getProtocol()); Assert.assertEquals(2113937151, candidate.getPriority()); Assert.assertEquals("192.168.1.65", candidate.getAddress()); Assert.assertEquals(54550, candidate.getPort()); Assert.assertEquals("host", candidate.getCandidateType()); Assert.assertEquals(0, candidate.getGeneration()); Assert.assertNull(candidate.getRelatedAddress()); Assert.assertEquals(0, candidate.getRelatedPort()); }
@Test public void testParseSrflxCandidate() throws SdpException { String line = "a=candidate:2162486046 1 udp 1845501695 85.241.121.60 60495 typ srflx raddr 192.168.1.65 rport 54550 generation 0\n\r"; CandidateAttribute candidate = parser.parse(line); Assert.assertEquals("2162486046", candidate.getFoundation()); Assert.assertEquals(1, candidate.getComponentId()); Assert.assertEquals("udp", candidate.getProtocol()); Assert.assertEquals(1845501695, candidate.getPriority()); Assert.assertEquals("85.241.121.60", candidate.getAddress()); Assert.assertEquals(60495, candidate.getPort()); Assert.assertEquals("srflx", candidate.getCandidateType()); Assert.assertEquals("192.168.1.65", candidate.getRelatedAddress()); Assert.assertEquals(54550, candidate.getRelatedPort()); Assert.assertEquals(0, candidate.getGeneration()); }
@Test public void testParseOverwrite() throws SdpException { String line1 = "a=candidate:2162486046 1 udp 1845501695 85.241.121.60 60495 typ srflx raddr 192.168.1.65 rport 54550 generation 0\n\r"; String line2 = "a=candidate:1995739850 1 udp 2113937151 192.168.1.65 54550 typ host generation 0\n\r"; CandidateAttribute candidate = parser.parse(line1); parser.parse(candidate, line2); Assert.assertEquals("1995739850", candidate.getFoundation()); Assert.assertEquals(1, candidate.getComponentId()); Assert.assertEquals("udp", candidate.getProtocol()); Assert.assertEquals(2113937151, candidate.getPriority()); Assert.assertEquals("192.168.1.65", candidate.getAddress()); Assert.assertEquals(54550, candidate.getPort()); Assert.assertEquals("host", candidate.getCandidateType()); Assert.assertEquals(0, candidate.getGeneration()); Assert.assertNull(candidate.getRelatedAddress()); Assert.assertEquals(0, candidate.getRelatedPort()); }