diff options
author | Alex Clayton <alex.clayton@isode.com> | 2016-02-22 16:05:37 (GMT) |
---|---|---|
committer | Alex Clayton <alex.clayton@isode.com> | 2016-02-29 12:10:44 (GMT) |
commit | d636d68c84229c82ff746c7697d2014ff4dd4477 (patch) | |
tree | a534ffdb9696c68d21d1cec6624023795ef683d7 /src/com/isode/stroke/network/PlatformDomainNameAddressQuery.java | |
parent | 2de569d23468c94fdcf1adc336a580b053423fd7 (diff) | |
download | stroke-d636d68c84229c82ff746c7697d2014ff4dd4477.zip stroke-d636d68c84229c82ff746c7697d2014ff4dd4477.tar.bz2 |
Finish porting on Network Package
As per PortingProgress.txt finsh porting all the classes I can from the network
package. This involved some updates as the tests and code had changed since
they existing classes had been imported.
I have added notes for the classes I did not port in PortingProgress explaining
why they were not ported.
Test-information:
All unit tests pass.
Change-Id: Ibb52ae409f1da9b72a4c1e590cd22835a1be95eb
Diffstat (limited to 'src/com/isode/stroke/network/PlatformDomainNameAddressQuery.java')
-rw-r--r-- | src/com/isode/stroke/network/PlatformDomainNameAddressQuery.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/com/isode/stroke/network/PlatformDomainNameAddressQuery.java b/src/com/isode/stroke/network/PlatformDomainNameAddressQuery.java new file mode 100644 index 0000000..24deb4d --- /dev/null +++ b/src/com/isode/stroke/network/PlatformDomainNameAddressQuery.java @@ -0,0 +1,86 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.network; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.isode.stroke.eventloop.Event.Callback; +import com.isode.stroke.eventloop.EventLoop; + +public class PlatformDomainNameAddressQuery extends DomainNameAddressQuery { + + private final String host_; + private final EventLoop eventLoop_; + + public PlatformDomainNameAddressQuery(String host,EventLoop eventLoop) { + host_ = host; + eventLoop_ = eventLoop; + } + + private class QueryRunnable implements Runnable { + + private final List<HostAddress> results_ = + Collections.synchronizedList(new ArrayList<HostAddress>()); + + @Override + public void run() { + try { + InetAddress[] inetAddresses = InetAddress.getAllByName(host_); + for (InetAddress address : inetAddresses) { + HostAddress result = new HostAddress(address); + results_.add(result); + } + } catch (UnknownHostException e) { + emitError(); + } + emitResults(); + } + + private void emitError() { + eventLoop_.postEvent(new Callback() { + + @Override + public void run() { + onResult.emit(new ArrayList<HostAddress>(),new DomainNameResolveError()); + } + + }); + } + + private void emitResults() { + eventLoop_.postEvent(new Callback() { + + @Override + public void run() { + // For thread safety emit a copy of the results + List<HostAddress> resultCopy = new ArrayList<HostAddress>(); + synchronized (results_) { + resultCopy.addAll(results_); + } + onResult.emit(results_,null); + } + + }); + } + + } + + @Override + public void run() { + Thread queryThread = new Thread(new QueryRunnable()); + queryThread.setDaemon(true); + queryThread.run(); + } + +} |