MiniDNS / minidns

DNS library for Android and Java SE
Other
220 stars 61 forks source link

Allow overriding socket creation for UDP and TCP-based lookups #60

Closed suzukieng closed 7 years ago

suzukieng commented 7 years ago

It is often useful to be able to inject a non-default Socket instance via an override or a SocketFactory for testing purposes, or to provide a custom implementation.

suzukieng commented 7 years ago

Some background info: I'm working on a project where I'm overriding the default Socket implementation and tunnel the Socket traffic through another communication channel.

The rationale behind not using a SocketFactory (and providing it in the ctor of NetworkDataSource) is that with the override approach we can also inject a custom DatagramSocket. For some weird reason javax.net has a SocketFactory but no DatagramSocketFactory. Other libraries such as Apache Commons provide their own factory classes (see: https://commons.apache.org/proper/commons-net/javadocs/api-1.4.1/org/apache/commons/net/DatagramSocketFactory.html) but I opted for the override approach to keep things simple, and not extend the API for a feature that will probably be seldomly used.

Flowdalic commented 7 years ago

Looks sensible. Could you squash the commits?

suzukieng commented 7 years ago

@Flowdalic Sure, done.

suzukieng commented 7 years ago

Thanks a lot @Flowdalic for the lightning-quick merge.

suzukieng commented 7 years ago

If you're wondering why I asked for this, see: https://blog.classycode.com/sockets-over-nfc-on-android-c294b6c58bbf