@Override
public boolean authenticate(final Socket toBeAuthenticated, final Set<CancelableTask> relatedHolePunchingTasks,
final CancelableTask ownTask, final Object sharedLock) throws IOException {
boolean result = false;
logger.info("Trying to authenticate socket: {}", toBeAuthenticated); //$NON-NLS-1$
final MessageWriter messageWriter = new MessageWriter(toBeAuthenticated.getOutputStream());
final MessageReader messageReader = this.hpUtil.getCustomHolePunchingMessageReader();
// sending authentication message
final Message authenticationMessage = MessageStaticFactory.newSTUNMessageInstance(STUNMessageClass.REQUEST,
STUNMessageMethod.AUTHENTICATE);
authenticationMessage.addAttribute(new Token(this.authentikationToken));
logger.info("Sending AuthenticationMessage: {}", authenticationMessage); //$NON-NLS-1$
messageWriter.writeMessage(authenticationMessage);
// receiving authentication acknowledgment message
final Message reveivedAckMessage = messageReader.readSTUNMessage(toBeAuthenticated.getInputStream());
if (this.hpUtil.isAuthenticationAcknowledgmentMessage(reveivedAckMessage)) {
synchronized (sharedLock) {
// sending second authentication acknowledgment message
final Message secondAckMessage = reveivedAckMessage.buildSuccessResponse();
messageWriter.writeMessage(secondAckMessage);
logger.debug("Authentication successfull, stopping hole punching threads"); //$NON-NLS-1$
this.hpUtil.stopCancableTasksExceptOwnTask(relatedHolePunchingTasks, ownTask);
result = true;
}
}