From 133246e38a48c7fa6e0ca02d9bff7e0b6dc16405 Mon Sep 17 00:00:00 2001 From: Oussama El Bacha Date: Thu, 9 Jun 2016 18:14:28 +0000 Subject: [PATCH] No longer throwing exception to module and just returns false. README and .npmignore modifications --- .npmignore | 2 + README.md | 99 ++++++++++++++++++- .../portavailable/PortAvailableModule.java | 39 +++----- main-tf.js | 30 ------ main.js | 4 - 5 files changed, 115 insertions(+), 59 deletions(-) delete mode 100644 main-tf.js diff --git a/.npmignore b/.npmignore index f53e780..0d502ab 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,8 @@ node_modules/ build/ +android/build/ +.git/ *.pbxuser !default.pbxuser *.mode1v3 diff --git a/README.md b/README.md index d6782c6..156672f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,99 @@ # react-native-portavailable -A React Native Package for checking port availability on native android udp & tcp +> A React Native Package for checking tcp and udp port availability on native android. + +This is an [implementation](http://svn.apache.org/viewvc/camel/trunk/components/camel-test/src/main/java/org/apache/camel/test/AvailablePortFinder.java?view=markup#l130) coming from the Apache [camel](http://camel.apache.org/) project. + +###### Support +This package is only supported on **android** devices, since i'm not much of an iOS Developer. The source code is pretty much straightforward so feel free to submit a PR of an iOS port i'll be happy to merge ! :) + +### Install + npm i -S react-native-portavailable + +#### Android + + + - Add the following line to the bottom of your project's `settings.gradle` file. + + `project(':react-native-portavailable').projectDir = new File(settingsDir, '../node_modules/react-native-portavailable/android')` + + - Change the `include` line of your project's `settings.gradle` to include the `:react-native-portavailable` project. + + `include ':react-native-portavailable', ':app'` + + - Open your app's `build.gradle` file and add the following line to the `dependencies` block. + + `compile project(":react-native-portavailable")` + + - In your app's `MainActivity.java` file, add `new PortAvailableReactModule()` to the return statement of the `getPackages()` function. + +``` +... + new MainReactPackage(), + new PortAvailableReactModule() +... +``` + + - Then in the same file add the import statement : + `import com.odinvt.portavailable.PortAvailableReactModule;` + + +### Usage + + import PortAvailable from 'react-native-portavailable' + +#### API + +The calls to `PortAvailable` methods are static so you can go ahead and use it directly. + +##### Methods + +###### `PortAvailable.check(port)` Checks if the port 'port' is available + + + - This will return a `Promise` instance that resolves to a `boolean` of the availability of the port. We're handling sockets opened and closed on the native side so we do need to expect an async execution. There is no need to use a `catch` method on the Promise object since all the exceptions are caught on the native side and the Promise just resolves to false. + +Example : + +```javascript + +PortAvailable.check(48500).then(available => { + // 'available' is true if the port 48500 is available +}) + +``` + +###### `PortAvailable.checkRange(min_port, max_port, stop = 0)` Checks which of the ports between 'min_port' and 'max_port' (included) are available + + - This will return a `Promise` instance that resolves to an `array` of the available ports. + - The `stop` parameter is optional and tells the function at how much available ports it should stop checking and resolve. `stop = 0` is the default value and it tells the function to just check all the ports until `max_port`. + +Example : + +```javascript + +PortAvailable.checkRange(48500, 48600, 5).then(ports => { + // 'ports' is an array of the available ports + // for example : ports = [48500, 48501, 48502, 48503, 48505] if 48504 is not available +}) + +``` + +###### `PortAvailable.getPorts()` Returns the ports discovered by the last `checkRange` method call + + - This will return an `array` of the available ports. + - The preferred way of getting the discovered ports is inside the `PortAvailable.checkRange` `then` callback. This method is provided so you can access the discovered ports anywhere on your application **but only if** you're sure that the `PortAvailable.getPorts()` call is made after the `PortAvailable.checkRange` Promise resolves . + +Example : + +```javascript + +let open_ports = PortAvailable.getPorts(); +// open_ports = [48500, 48501, 48502, 48503, 48505] + +``` + +### Babel + +This component uses ES6. So if you're using `Webpack` you should launch `babel` on `main.js` and output to `main-tf.js` if for some reason the `npm postinstall` script didn't execute. + + "postinstall": "babel main.js --out-file main-tf.js" diff --git a/android/src/main/java/com/odinvt/portavailable/PortAvailableModule.java b/android/src/main/java/com/odinvt/portavailable/PortAvailableModule.java index c8b5060..9231f00 100644 --- a/android/src/main/java/com/odinvt/portavailable/PortAvailableModule.java +++ b/android/src/main/java/com/odinvt/portavailable/PortAvailableModule.java @@ -27,40 +27,32 @@ public String getName() { @ReactMethod public void available(int port, Promise promise) { boolean available; - try { - available = check(port); + available = check(port); - promise.resolve(available); - } catch (Exception e) { - promise.reject(e); - } + promise.resolve(available); } @ReactMethod public void range(int min_port, int max_port, int stop, Promise promise) { WritableArray ports = new WritableNativeArray(); - try { - int found = 0; - for(int i = min_port; i <= max_port; i++) { - if(stop > 0 && found == stop) { - break; - } - if(check(i)) { - ports.pushInt(i); - found++; - } + int found = 0; + for(int i = min_port; i <= max_port; i++) { + if(stop > 0 && found == stop) { + break; + } + if(check(i)) { + ports.pushInt(i); + found++; } - - promise.resolve(ports); - } catch (Exception e) { - promise.reject(e); } + + promise.resolve(ports); } - public boolean check(int port) throws IllegalArgumentException, IOException { + public boolean check(int port) { if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { - throw new IllegalArgumentException("Invalid start port: " + port); + return false; } ServerSocket ss = null; @@ -73,7 +65,7 @@ public boolean check(int port) throws IllegalArgumentException, IOException { ds.setReuseAddress(true); result = true; } catch (IOException e) { - throw new IOException(e); + /*goes to the return false statement after closing the sockets*/ } finally { if (ds != null) { ds.close(); @@ -84,7 +76,6 @@ public boolean check(int port) throws IllegalArgumentException, IOException { ss.close(); } catch (IOException e) { /* should not be thrown */ - throw new IOException(e); } } } diff --git a/main-tf.js b/main-tf.js deleted file mode 100644 index c2a9490..0000000 --- a/main-tf.js +++ /dev/null @@ -1,30 +0,0 @@ -Object.defineProperty(exports,"__esModule",{value:true});var _reactNative=require('react-native');function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var -RNPortAvailable=_reactNative.NativeModules.RNPortAvailable;var - -PortAvailable=function PortAvailable(){_classCallCheck(this,PortAvailable);};PortAvailable. - -available_ports=[];PortAvailable. - -check=function(port){ -return new Promise(function(resolve,reject){ -RNPortAvailable.available(port).then(function(available){ -resolve(available);}). -catch(function(err){ -reject(err);});});};PortAvailable. - - - - -checkRange=function(min_port,max_port){var stop=arguments.length<=2||arguments[2]===undefined?0:arguments[2]; -return new Promise(function(resolve,reject){ -RNPortAvailable.range(min_port,max_port,stop).then(function(availablePorts){ -PortAvailable.available_ports=availablePorts; -resolve(availablePorts);}). -catch(function(err){ -reject(err);});});};PortAvailable. - - - - -getPorts=function(){ -return PortAvailable.available_ports;};exports.default=PortAvailable; diff --git a/main.js b/main.js index 8af5783..760c8a9 100644 --- a/main.js +++ b/main.js @@ -9,8 +9,6 @@ export default class PortAvailable { return new Promise((resolve, reject) => { RNPortAvailable.available(port).then((available) => { resolve(available); - }).catch(err => { - reject(err); }) }) } @@ -20,8 +18,6 @@ export default class PortAvailable { RNPortAvailable.range(min_port, max_port, stop).then((availablePorts) => { PortAvailable.available_ports = availablePorts; resolve(availablePorts); - }).catch(err => { - reject(err); }) }) }