summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-07-27 15:01:45 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-01-12 11:31:04 (GMT)
commitb4cf2bb8d7b69d95b4a10d610ad259998d2aee5b (patch)
treeb942953236a7c712eed6ce4a5f261019691c0dca /src/com/isode/stroke/network/StaticDomainNameResolver.java
parentc168fcd0c2468ec939b8d164175e9c5776a63147 (diff)
downloadstroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.zip
stroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.tar.bz2
Make Networks equivalent with Swiften.
Adds ProxyProviders, DomainNameResolvers and DummyConnection. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for ChainedConnector, Connector and HostAddress. Test also added for ComponentConnector, which needed bits of Network. Five assertions are commented in ConnectorTest, which fails and will be updated after review. Change-Id: I8a62841eb2f9c109bc3a94865b7a003b33493e11
Diffstat (limited to 'src/com/isode/stroke/network/StaticDomainNameResolver.java')
-rw-r--r--src/com/isode/stroke/network/StaticDomainNameResolver.java187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/com/isode/stroke/network/StaticDomainNameResolver.java b/src/com/isode/stroke/network/StaticDomainNameResolver.java
new file mode 100644
index 0000000..14817bb
--- /dev/null
+++ b/src/com/isode/stroke/network/StaticDomainNameResolver.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * 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.network;
+
+import com.isode.stroke.network.HostAddress;
+import com.isode.stroke.network.HostAddressPort;
+import com.isode.stroke.network.DomainNameResolver;
+import com.isode.stroke.network.DomainNameServiceQuery;
+import com.isode.stroke.network.DomainNameAddressQuery;
+import com.isode.stroke.eventloop.EventLoop;
+import com.isode.stroke.eventloop.EventOwner;
+import com.isode.stroke.eventloop.Event;
+import java.util.Vector;
+import java.util.Map;
+import java.util.HashMap;
+
+public class StaticDomainNameResolver extends DomainNameResolver {
+
+ private EventLoop eventLoop;
+ private boolean isResponsive;
+ private Map<String, Vector<HostAddress> > addresses = new HashMap<String, Vector<HostAddress> >();
+ private Vector<Pair> services = new Vector<Pair>();
+ private EventOwner owner;
+
+ class ServiceQuery extends DomainNameServiceQuery {
+
+ public ServiceQuery(final String service, StaticDomainNameResolver resolver, EventLoop eventLoop, EventOwner owner) {
+ this.eventLoop = eventLoop;
+ this.service = service;
+ this.resolver = resolver;
+ this.owner = owner;
+ }
+
+ public void run() {
+ if (!resolver.getIsResponsive()) {
+ return;
+ }
+ final Vector<DomainNameServiceQuery.Result> results = new Vector<DomainNameServiceQuery.Result>();
+ for(StaticDomainNameResolver.Pair i : resolver.getServices()) {
+ if(i.node.equals(service)) {
+ results.add(i.queryResult);
+ }
+ }
+ eventLoop.postEvent(new Event.Callback() {
+ @Override
+ public void run() {
+ emitOnResult(results);
+ }
+ }, owner);
+ }
+
+ public void emitOnResult(Vector<DomainNameServiceQuery.Result> results) {
+ onResult.emit(results);
+ }
+
+ public EventLoop eventLoop;
+ public String service = "";
+ public StaticDomainNameResolver resolver;
+ public EventOwner owner;
+ };
+
+ class AddressQuery extends DomainNameAddressQuery {
+
+ public AddressQuery(final String host, StaticDomainNameResolver resolver, EventLoop eventLoop, EventOwner owner) {
+ this.eventLoop = eventLoop;
+ this.host = host;
+ this.resolver = resolver;
+ this.owner = owner;
+ }
+
+ public void run() {
+ if (!resolver.getIsResponsive()) {
+ return;
+ }
+ if (resolver.getAddresses().containsKey(host)) {
+ eventLoop.postEvent(new Event.Callback() {
+ @Override
+ public void run() {
+ emitOnResult(resolver.getAddresses().get(host), null);
+ }
+ });
+ }
+ else {
+ eventLoop.postEvent(new Event.Callback() {
+ @Override
+ public void run() {
+ emitOnResult(new Vector<HostAddress>(), new DomainNameResolveError());
+ }
+ });
+ }
+ }
+
+ public void emitOnResult(Vector<HostAddress> results, DomainNameResolveError error) {
+ onResult.emit(results, error);
+ }
+
+ public EventLoop eventLoop;
+ public String host = "";
+ public StaticDomainNameResolver resolver;
+ public EventOwner owner;
+ };
+
+ private class Pair {
+ public String node;
+ public DomainNameServiceQuery.Result queryResult;
+
+ public Pair(String j, DomainNameServiceQuery.Result n) {node = j; queryResult = n;}
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof Pair)) return false;
+ Pair o1 = (Pair) o;
+ return queryResult.equals(o1.queryResult) && node.equals(o1.node);
+ }
+ }
+
+ class StaticDomainNameResolverEventOwner implements EventOwner {
+
+ };
+
+ public StaticDomainNameResolver(EventLoop eventLoop) {
+ this.eventLoop = eventLoop;
+ isResponsive = true;
+ owner = new StaticDomainNameResolverEventOwner();
+ }
+
+ public void addAddress(final String domain, final HostAddress address) {
+ Vector<HostAddress> vec = new Vector<HostAddress>();
+ vec.add(address);
+ if(!(addresses.containsKey(domain))) {
+ addresses.put(domain, vec);
+ }
+ else {
+ addresses.get(domain).add(address);
+ }
+ }
+
+ public void addService(final String service, final DomainNameServiceQuery.Result result) {
+ services.add(new Pair(service, result));
+ }
+
+ public void addXMPPClientService(final String domain, final HostAddressPort address) {
+ int hostid = 0;
+ String hostname = "host-" + Integer.toString(hostid);
+ hostid++;
+
+ addService("_xmpp-client._tcp." + domain, new ServiceQuery.Result(hostname, address.getPort(), 0, 0));
+ addAddress(hostname, address.getAddress());
+ }
+
+ public void addXMPPClientService(final String domain, final String hostname, int port) {
+ addService("_xmpp-client._tcp." + domain, new ServiceQuery.Result(hostname, port, 0, 0));
+ }
+
+ public Map<String, Vector<HostAddress> > getAddresses() {
+ return addresses;
+ }
+
+ public Vector<Pair> getServices() {
+ return services;
+ }
+
+ public boolean getIsResponsive() {
+ return isResponsive;
+ }
+
+ public void setIsResponsive(boolean b) {
+ isResponsive = b;
+ }
+
+ public DomainNameServiceQuery createServiceQuery(final String serviceLookupPrefix, final String domain) {
+ return new ServiceQuery(serviceLookupPrefix + domain, this, eventLoop, owner);
+ }
+
+ public DomainNameAddressQuery createAddressQuery(final String name) {
+ return new AddressQuery(name, this, eventLoop, owner);
+ }
+} \ No newline at end of file