当前位置: 首页>>代码示例>>Java>>正文


Java ErrorCode类代码示例

本文整理汇总了Java中de.javawi.jstun.attribute.ErrorCode的典型用法代码示例。如果您正苦于以下问题:Java ErrorCode类的具体用法?Java ErrorCode怎么用?Java ErrorCode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ErrorCode类属于de.javawi.jstun.attribute包,在下文中一共展示了ErrorCode类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: bindingCommunicationInitialSocket

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean bindingCommunicationInitialSocket() throws UtilityException, IOException, MessageHeaderParsingException, MessageAttributeParsingException {
	MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
	sendMH.generateTransactionID();
	ChangeRequest changeRequest = new ChangeRequest();
	sendMH.addMessageAttribute(changeRequest);
	byte[] data = sendMH.getBytes();
	
	DatagramPacket send = new DatagramPacket(data, data.length, InetAddress.getByName(stunServer), port);
	initialSocket.send(send);
	LOGGER.debug("Binding Request sent.");

	MessageHeader receiveMH = new MessageHeader();
	while (!(receiveMH.equalTransactionID(sendMH))) {
		DatagramPacket receive = new DatagramPacket(new byte[200], 200);
		initialSocket.receive(receive);
		receiveMH = MessageHeader.parseHeader(receive.getData());
		receiveMH.parseAttributes(receive.getData());
	}
	ma = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
	ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
	if (ec != null) {
		LOGGER.debug("Message header contains an Errorcode message attribute.");
		return true;
	}
	if (ma == null) {
		LOGGER.debug("Response does not contain a Mapped Address message attribute.");
		return true;
	}
	return false;
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:31,代码来源:BindingLifetimeTest.java

示例2: bindingCommunicationInitialSocket

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean bindingCommunicationInitialSocket(final DatagramSocket socket) throws UtilityException, IOException, MessageHeaderParsingException, MessageAttributeParsingException {
    MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
    sendMH.setTransactionID(mUniqueId);
    ChangeRequest changeRequest = new ChangeRequest();
    sendMH.addMessageAttribute(changeRequest);
    byte[] data = sendMH.getBytes();

    DatagramPacket send = new DatagramPacket(data, data.length, InetAddress.getByName(mStunServer), mPort);
    socket.send(send);
    if (DEBUG) {
        Log.i(TAG, "Binding Request sent.");
    }

    final byte[] buf = new byte[256];
    MessageHeader receiveMH = new MessageHeader();
    while (!(receiveMH.equalTransactionID(sendMH))) {
        DatagramPacket receive = new DatagramPacket(buf, buf.length);
        socket.receive(receive);
        receiveMH = MessageHeader.parseHeader(receive.getData());
        receiveMH.parseAttributes(receive.getData());
    }

    mMappedAddress = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
    ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
    if (ec != null) {
        if (DEBUG) {
            Log.e(TAG, "Message header contains an ErrorCode message attribute. ErrorCode=" + ec);
        }
        return false;
    }
    if (mMappedAddress == null) {
        if (DEBUG) {
            Log.e(TAG, "Response does not contain a Mapped Address message attribute.");
        }
        return false;
    }

    if (DEBUG) {
        Log.i(TAG, "Address: " + mMappedAddress.getAddress().toString());
        Log.i(TAG, "Port: " + mMappedAddress.getPort());
    }
    return true;
}
 
开发者ID:DeviceConnect,项目名称:DeviceConnect-Android,代码行数:44,代码来源:StunClient.java

示例3: test1

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean test1() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException {
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		try {
			// Test 1 including response
			socketTest1 = new DatagramSocket(new InetSocketAddress(sourceIaddress, sourcePort));
			socketTest1.setReuseAddress(true);
			socketTest1.connect(InetAddress.getByName(stunServer), stunServerPort);
			socketTest1.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			sendMH.addMessageAttribute(changeRequest);
			
			byte[] data = sendMH.getBytes();
			DatagramPacket send = new DatagramPacket(data, data.length);
			socketTest1.send(send);
			LOGGER.debug("Test 1: Binding Request sent.");
		
			MessageHeader receiveMH = new MessageHeader();
			while (!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				socketTest1.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			
			ma = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
			ca = (ChangedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ChangedAddress);
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return false;
			}
			if ((ma == null) || (ca == null)) {
				di.setError(700, "The server is sending an incomplete response (Mapped Address and Changed Address message attributes are missing). The client should not retry.");
				LOGGER.debug("Response does not contain a Mapped Address or Changed Address message attribute.");
				return false;
			} else {
				di.setPublicIP(ma.getAddress().getInetAddress());
				di.setPublicPort(ma.getPort());
				if ((ma.getPort() == socketTest1.getLocalPort()) && (ma.getAddress().getInetAddress().equals(socketTest1.getLocalAddress()))) {
					LOGGER.debug("Node is not natted.");
					nodeNatted = false;
				} else {
					LOGGER.debug("Node is natted.");
				}
				return true;
			}
		} catch (SocketTimeoutException ste) {
			if (timeSinceFirstTransmission < 7900) {
				LOGGER.debug("Test 1: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
				int timeoutAddValue = (timeSinceFirstTransmission * 2);
				if (timeoutAddValue > 1600) timeoutAddValue = 1600;
				timeout = timeoutAddValue;
			} else {
				// node is not capable of udp communication
				LOGGER.debug("Test 1: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
				di.setBlockedUDP();
				LOGGER.debug("Node is not capable of UDP communication.");
				return false;
			}
		} 
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:71,代码来源:DiscoveryTest.java

示例4: test2

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean test2() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException {
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		try {
			// Test 2 including response
			DatagramSocket sendSocket = new DatagramSocket(new InetSocketAddress(sourceIaddress, sourcePort));
			sendSocket.connect(InetAddress.getByName(stunServer), stunServerPort);
			sendSocket.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			changeRequest.setChangeIP();
			changeRequest.setChangePort();
			sendMH.addMessageAttribute(changeRequest);
				 
			byte[] data = sendMH.getBytes(); 
			DatagramPacket send = new DatagramPacket(data, data.length);
			sendSocket.send(send);
			LOGGER.debug("Test 2: Binding Request sent.");
			
			int localPort = sendSocket.getLocalPort();
			InetAddress localAddress = sendSocket.getLocalAddress();
			
			sendSocket.close();
			
			DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
			receiveSocket.connect(ca.getAddress().getInetAddress(), ca.getPort());
			receiveSocket.setSoTimeout(timeout);
			
			MessageHeader receiveMH = new MessageHeader();
			while(!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				receiveSocket.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return false;
			}
			if (!nodeNatted) {
				di.setOpenAccess();
				LOGGER.debug("Node has open access to the Internet (or, at least the node is behind a full-cone NAT without translation).");
			} else {
				di.setFullCone();
				LOGGER.debug("Node is behind a full-cone NAT.");
			}
			return false;
		} catch (SocketTimeoutException ste) {
			if (timeSinceFirstTransmission < 7900) {
				LOGGER.debug("Test 2: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
				int timeoutAddValue = (timeSinceFirstTransmission * 2);
				if (timeoutAddValue > 1600) timeoutAddValue = 1600;
				timeout = timeoutAddValue;
			} else {
				LOGGER.debug("Test 2: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
				if (!nodeNatted) {
					di.setSymmetricUDPFirewall();
					LOGGER.debug("Node is behind a symmetric UDP firewall.");
					return false;
				} else {
					// not is natted
					// redo test 1 with address and port as offered in the changed-address message attribute
					return true;
				}
			}
		}
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:76,代码来源:DiscoveryTest.java

示例5: test1Redo

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean test1Redo() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException{
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		// redo test 1 with address and port as offered in the changed-address message attribute
		try {
			// Test 1 with changed port and address values
			socketTest1.connect(ca.getAddress().getInetAddress(), ca.getPort());
			socketTest1.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			sendMH.addMessageAttribute(changeRequest);
			
			byte[] data = sendMH.getBytes();
			DatagramPacket send = new DatagramPacket(data, data.length);
			socketTest1.send(send);
			LOGGER.debug("Test 1 redo with changed address: Binding Request sent.");
			
			MessageHeader receiveMH = new MessageHeader();
			while (!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				socketTest1.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			MappedAddress ma2 = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return false;
			}
			if (ma2 == null) {
				di.setError(700, "The server is sending an incomplete response (Mapped Address message attribute is missing). The client should not retry.");
				LOGGER.debug("Response does not contain a Mapped Address message attribute.");
				return false;
			} else {
				if ((ma.getPort() != ma2.getPort()) || (!(ma.getAddress().getInetAddress().equals(ma2.getAddress().getInetAddress())))) {
					di.setSymmetric();
					LOGGER.debug("Node is behind a symmetric NAT.");
					return false;
				}
			}
			return true;
		} catch (SocketTimeoutException ste2) {
			if (timeSinceFirstTransmission < 7900) {
				LOGGER.debug("Test 1 redo with changed address: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
				int timeoutAddValue = (timeSinceFirstTransmission * 2);
				if (timeoutAddValue > 1600) timeoutAddValue = 1600;
				timeout = timeoutAddValue;
			} else {
				LOGGER.debug("Test 1 redo with changed address: Socket timeout while receiving the response.  Maximum retry limit exceed. Give up.");
				return false;
			}
		}
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:62,代码来源:DiscoveryTest.java

示例6: test3

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private void test3() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException {
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		try {
			// Test 3 including response
			DatagramSocket sendSocket = new DatagramSocket(new InetSocketAddress(sourceIaddress, sourcePort));
			sendSocket.connect(InetAddress.getByName(stunServer), stunServerPort);
			sendSocket.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			changeRequest.setChangePort();
			sendMH.addMessageAttribute(changeRequest);
			
			byte[] data = sendMH.getBytes();
			DatagramPacket send = new DatagramPacket(data, data.length);
			sendSocket.send(send);
			LOGGER.debug("Test 3: Binding Request sent.");
			
			int localPort = sendSocket.getLocalPort();
			InetAddress localAddress = sendSocket.getLocalAddress();
			
			sendSocket.close();
			
			DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
			receiveSocket.connect(InetAddress.getByName(stunServer), ca.getPort());
			receiveSocket.setSoTimeout(timeout);
			
			MessageHeader receiveMH = new MessageHeader();
			while (!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				receiveSocket.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return;
			}
			if (nodeNatted) {
				di.setRestrictedCone();
				LOGGER.debug("Node is behind a restricted NAT.");
				return;
			}
		} catch (SocketTimeoutException ste) {
			if (timeSinceFirstTransmission < 7900) {
				LOGGER.debug("Test 3: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
				int timeoutAddValue = (timeSinceFirstTransmission * 2);
				if (timeoutAddValue > 1600) timeoutAddValue = 1600;
				timeout = timeoutAddValue;
			} else {
				LOGGER.debug("Test 3: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
				di.setPortRestrictedCone();
				LOGGER.debug("Node is behind a port restricted NAT.");
				return;
			}
		}
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:66,代码来源:DiscoveryTest.java

示例7: test1

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean test1() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException {
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		try {
			// Test 1 including response
			socketTest1 = new DatagramSocket(new InetSocketAddress(sourceIaddress, sourcePort));
			socketTest1.setReuseAddress(true);
			socketTest1.connect(InetAddress.getByName(stunServer), stunServerPort);
			socketTest1.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			sendMH.addMessageAttribute(changeRequest);
			
			byte[] data = sendMH.getBytes();
			DatagramPacket send = new DatagramPacket(data, data.length);
			socketTest1.send(send);
			LOGGER.debug("Test 1: Binding Request sent.");
		
			MessageHeader receiveMH = new MessageHeader();
			while (!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				socketTest1.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			ma = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
			ca = (ChangedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ChangedAddress);
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return false;
			}
			if ((ma == null) || (ca == null)) {
				di.setError(700, "The server is sending an incomplete response (Mapped Address and Changed Address message attributes are missing). The client should not retry.");
				LOGGER.debug("Response does not contain a Mapped Address or Changed Address message attribute.");
				return false;
			} else {
				di.setPublicIP(ma.getAddress().getInetAddress());
				di.setPublicPort(ma.getPort());
				if ((ma.getPort() == socketTest1.getLocalPort()) && (ma.getAddress().getInetAddress().equals(socketTest1.getLocalAddress()))) {
					LOGGER.debug("Node is not natted.");
					nodeNatted = false;
				} else {
					LOGGER.debug("Node is natted.");
				}
				return true;
			}
		} catch (SocketTimeoutException ste) {
			if (timeSinceFirstTransmission < 300) {
				LOGGER.debug("Test 1: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
			} else {
				// node is not capable of udp communication
				LOGGER.debug("Test 1: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
				di.setBlockedUDP();
				LOGGER.debug("Node is not capable of UDP communication.");
				return false;
			}
		} 
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:67,代码来源:FastDiscoveryTest.java

示例8: test2

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private int test2() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException {	
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		try {
			// Test 2 including response
			DatagramSocket sendSocket = new DatagramSocket(new InetSocketAddress(sourceIaddress, sourcePort));
			sendSocket.connect(InetAddress.getByName(stunServer), stunServerPort);
			sendSocket.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			changeRequest.setChangeIP();
			changeRequest.setChangePort();
			sendMH.addMessageAttribute(changeRequest);
				 
			byte[] data = sendMH.getBytes(); 
			DatagramPacket send = new DatagramPacket(data, data.length);
			sendSocket.send(send);
			LOGGER.debug("Test 2: Binding Request sent.");
			
			int localPort = sendSocket.getLocalPort();
			InetAddress localAddress = sendSocket.getLocalAddress();
			
			sendSocket.close();
			
			DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
			receiveSocket.connect(ca.getAddress().getInetAddress(), ca.getPort());
			receiveSocket.setSoTimeout(timeout);
			
			MessageHeader receiveMH = new MessageHeader();
			while(!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				receiveSocket.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return ERROR;
			}
			return CONNECTION_ESTABLISHED_NO_ERROR;
		} catch (SocketTimeoutException ste) {
			if (timeSinceFirstTransmission < 300) {
				LOGGER.debug("Test 2: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
			} else {
				LOGGER.debug("Test 2: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
				return CONNECTION_TIMEOUT; 
			}
		}
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:58,代码来源:FastDiscoveryTest.java

示例9: test1Redo

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private boolean test1Redo() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageHeaderParsingException{
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		// redo test 1 with address and port as offered in the changed-address message attribute
		try {
			// Test 1 with changed port and address values
			socketTest1.connect(ca.getAddress().getInetAddress(), ca.getPort());
			socketTest1.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			sendMH.addMessageAttribute(changeRequest);
			
			byte[] data = sendMH.getBytes();
			DatagramPacket send = new DatagramPacket(data, data.length);
			socketTest1.send(send);
			LOGGER.debug("Test 1 redo with changed address: Binding Request sent.");
			
			MessageHeader receiveMH = new MessageHeader();
			while (!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				socketTest1.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			MappedAddress ma2 = (MappedAddress) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return false;
			}
			if (ma2 == null) {
				di.setError(700, "The server is sending an incomplete response (Mapped Address message attribute is missing). The client should not retry.");
				LOGGER.debug("Response does not contain a Mapped Address message attribute.");
				return false;
			} else {
				if ((ma.getPort() != ma2.getPort()) || (!(ma.getAddress().getInetAddress().equals(ma2.getAddress().getInetAddress())))) {
					di.setSymmetric();
					LOGGER.debug("Node is behind a symmetric NAT.");
					return false;
				}
			}
			return true;
		} catch (SocketTimeoutException ste2) {
			if (timeSinceFirstTransmission < 300) {
				LOGGER.debug("Test 1 redo with changed address: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
			} else {
				LOGGER.debug("Test 1 redo with changed address: Socket timeout while receiving the response.  Maximum retry limit exceed. Give up.");
				return false;
			}
		}
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:59,代码来源:FastDiscoveryTest.java

示例10: test3

import de.javawi.jstun.attribute.ErrorCode; //导入依赖的package包/类
private int test3() throws UtilityException, SocketException, UnknownHostException, IOException, MessageAttributeParsingException, MessageAttributeException, MessageHeaderParsingException {
	int timeSinceFirstTransmission = 0;
	int timeout = timeoutInitValue;
	while (true) {
		try {
			// Test 3 including response
			DatagramSocket sendSocket = new DatagramSocket(new InetSocketAddress(sourceIaddress, sourcePort));
			sendSocket.connect(InetAddress.getByName(stunServer), stunServerPort);
			sendSocket.setSoTimeout(timeout);
			
			MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
			sendMH.generateTransactionID();
			
			ChangeRequest changeRequest = new ChangeRequest();
			changeRequest.setChangePort();
			sendMH.addMessageAttribute(changeRequest);
			
			byte[] data = sendMH.getBytes();
			DatagramPacket send = new DatagramPacket(data, data.length);
			sendSocket.send(send);
			LOGGER.debug("Test 3: Binding Request sent.");
			
			int localPort = sendSocket.getLocalPort();
			InetAddress localAddress = sendSocket.getLocalAddress();
			
			sendSocket.close();
			
			DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
			receiveSocket.connect(InetAddress.getByName(stunServer), ca.getPort());
			receiveSocket.setSoTimeout(timeout);
			
			MessageHeader receiveMH = new MessageHeader();
			while (!(receiveMH.equalTransactionID(sendMH))) {
				DatagramPacket receive = new DatagramPacket(new byte[200], 200);
				receiveSocket.receive(receive);
				receiveMH = MessageHeader.parseHeader(receive.getData());
				receiveMH.parseAttributes(receive.getData());
			}
			ErrorCode ec = (ErrorCode) receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
			if (ec != null) {
				di.setError(ec.getResponseCode(), ec.getReason());
				LOGGER.debug("Message header contains an Errorcode message attribute.");
				return ERROR;
			}
			return CONNECTION_ESTABLISHED_NO_ERROR;
		} catch (SocketTimeoutException ste) {
			if (timeSinceFirstTransmission < 300) {
				LOGGER.debug("Test 3: Socket timeout while receiving the response.");
				timeSinceFirstTransmission += timeout;
			} else {
				LOGGER.debug("Test 3: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
				return CONNECTION_TIMEOUT;
			}
		}
	}
}
 
开发者ID:tking,项目名称:JSTUN,代码行数:57,代码来源:FastDiscoveryTest.java


注:本文中的de.javawi.jstun.attribute.ErrorCode类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。