Skip to content

Commit

Permalink
Updated to NAN 1.6.x
Browse files Browse the repository at this point in the history
sockopt API

• convenience methods for `get/setsockopt()`

an interface to nanomsg socket options by way of:

`socket.option(param)` or `socket.option()`

passing a param sets that, while no param gets that.

*a few caveats will be referenced in forthcoming docs

it’s the simple interface i wrote in nanomsg.iojs covering:

• tcpnodelay
• linger
• sndbuf
• rcvbuf
• sndtimeo
• rcvtimeo
• reconn
• maxreconn
• sndprio
• rcvprio

test: socket.tcpnodelay(boolean)

document the api

opts initializer

also added some semicolons

add note on address strings

act like a getter, not a setter

delete var domain

test: sockoptapi.js semicolons

docs: update raw opt

use more nn.Err()

remove nn.Strerr() C++ function, use nn.Err()

maintain byte length check during nn_recv() operation

fixes #47 with a
necessary check removed in
c0336f19cd6376607
31f32911d1f31b09e6f7f8f

• if nn_recv() errors setting len to -1, node buffer library will
accordingly refuse to allocate (malloc or smalloc) an impossible size
in memory

test: set sockopts when starting the socket

unorthodox symbol fix: rvcprio()

opval

dry it out a bit

docs: rcvbuf and sndbuf unit is size in bytes

adding Makefile

after pulling down run git submodule update - -init:

when building the repo on unix for node/iojs versions just go:

nvm use $VERSION && make full

make full removes the build and node_modules directory first
then it rebuilds the project compiling sources
then it runs the test suite.

if you only want to rebuild for a different version just go:
make clean && make

we’ll probably want to uncomment the perf stuff once that’s setup

docs: the Makefile

docs: move contributing under test section

js: put sockopt API methods onto Socket prototype

js: line up the o's (ocd)

docs: also add size in bytes to top links

js: partial function application & test: RCVPRIO

• adds TODO: Issue #50 comment on line 19
• rewrite opts check for RAW & remove redundant error check
  - RAW option now more consistent with its description in docs
• partial function application: tighten up opts API methods
• test: RCVPRIO, ensure symbol test fails for the missing symbol

js: clean up, opt function context

minor bump

bump version
  • Loading branch information
reqshark authored and Nick Desaulniers committed Feb 24, 2015
1 parent c476019 commit 5fa3305
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ libtool
nanocat
perf
.libs
Makefile
v8
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.PHONY: clean check test perf bench full

ALL:
npm i

check:
npm t

test:
npm t

clean:
rm -fr build && rm -rf node_modules

#perf:
# node perf/local_lat.js tcp://127.0.0.1:5555 1 100000& node perf/remote_lat.js tcp://127.0.0.1:5555 1 100000
# node perf/local_thr.js tcp://127.0.0.1:5556 1 100000& node perf/remote_thr.js tcp://127.0.0.1:5556 1 100000#

#bench:
# node perf/local_lat.js tcp://127.0.0.1:5555 10 1000& node perf/remote_lat.js tcp://127.0.0.1:5555 10 1000
# node perf/local_thr.js tcp://127.0.0.1:5556 10 100000& node perf/remote_thr.js tcp://127.0.0.1:5556 10 100000

full:
rm -fr build && rm -rf node_modules
npm i && npm t
211 changes: 206 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

[![Build status](https://ci.appveyor.com/api/projects/status/07j7o9juuktas2uk)](https://ci.appveyor.com/project/tcr/node-nanomsg) [![Build Status](https://travis-ci.org/tcr/node-nanomsg.svg)](https://travis-ci.org/tcr/node-nanomsg)

Install:
### install:

```
npm install nanomsg
```

check it out:
### check it out:

```js
var nano = require('nanomsg');
Expand All @@ -31,12 +31,213 @@ setTimeout(function () {
}, 100);
```

## contributing
# API

Issues and pull requests welcome!
### nano.socket(type, [options,])

Starts a new socket. The nanomsg socket can bind or connect to multiple heterogeneous endpoints as well as shutdown any of these established links.

#### `options`
* `'raw'` *(Boolean, default: `false`)*: determines the domain of the socket. `AF_SP`, the default, creates a standard full-blown SP socket. `AF_SP_RAW` family sockets operate over internal network protocols and interfaces. Raw sockets omit the end-to-end functionality found in `AF_SP` sockets and thus can be used to implement intermediary devices in SP topologies, see [nanomsg docs](http://nanomsg.org/v0.5/nn_socket.3.html) or consult your man page entry `socket(2)` for more info.
```js
//ex. starting raw sockets
nano.socket('bus', { raw: true } )
```
* `'tcpnodelay'` *(Boolean, default: `false`)*: see [`socket.tcpnodelay(boolean)`](https://github.com/nickdesaulniers/node-nanomsg#sockettcpnodelayboolean).
* `'linger'` *(Number, default: `1000`)*: see [`socket.linger(duration)`](https://github.com/nickdesaulniers/node-nanomsg#socketlingerduration).
* `'sndbuf'` *(Number, size in bytes, default: `128kB`)*: see [`socket.sndbuf(size)`](https://github.com/nickdesaulniers/node-nanomsg#socketsndbufsize).
* `'rcvbuf'` *(Number, size in bytes, default: `128kB`)*: see [`socket.rcvbuf(size)`](https://github.com/nickdesaulniers/node-nanomsg#socketrcvbufsize).
* `'sndtimeo'` *(Number, default: `-1`)*: see [`socket.sndtimeo(duration)`](https://github.com/nickdesaulniers/node-nanomsg#socketsndtimeoduration).
* `'rcvtimeo'` *(Number, default: `-1`)*: see [`socket.rcvtimeo(duration)`](https://github.com/nickdesaulniers/node-nanomsg#socketrcvtimeoduration).
* `'reconn'` *(Number, default: `100`)*: see [`socket.reconn(duration)`](https://github.com/nickdesaulniers/node-nanomsg#socketreconnduration).
* `'maxreconn'` *(Number, default: `0`)*: see [`socket.maxreconn(duration)`](https://github.com/nickdesaulniers/node-nanomsg#socketmaxreconnduration).
* `'sndprio'` *(Number, default: `0`)*: see [`socket.sndprio(priority)`](https://github.com/nickdesaulniers/node-nanomsg#socketsndpriopriority).
* `'rcvprio'` *(Number, default: `0`)*: see [`socket.rcvprio(priority)`](https://github.com/nickdesaulniers/node-nanomsg#socketrcvpriopriority).

### socket.shutdown(address)

*(Function, param: String)*: Removes an endpoint established by calls to `bind()` or `connect()`. The nanomsg library will try to deliver any outstanding outbound messages to the endpoint for the time specified by `linger`.

```js
socket.shutdown('tcp://127.0.0.1:5555')
```

### socket.bind(address)

*(Function, param: String)*: Adds a local endpoint to the socket. The endpoint can be then used by other applications to connect.

`bind()` (or `connect()`) may be called multiple times on the same socket thus allowing the socket to communicate with multiple heterogeneous endpoints.

```js
socket.bind('tcp://eth0:5555')
```

*<sub>recommend checking your machine's `ifconfig` first before using a named interface. `ipconfig` on windows.</sub>*

### socket.connect(address)

*(Function, param: String)*: Adds a remote endpoint to the socket. The nanomsg library would then try to connect to the specified remote endpoint.

`connect()` (as well as `bind()`) may be called multiple times on the same socket thus allowing the socket to communicate with multiple heterogeneous endpoints.

```js
socket.connect('tcp://127.0.0.1:5555')
```

*<sub>When connecting over remote TCP allow `100ms` or more depending on round trip time for the operation to complete.</sub>*

##### *[a note on address strings](docs/address_strings.markdown)*

### socket.close()

*(Function, param: Function)*: Closes the socket. Any buffered inbound messages that were not yet received by the application will be discarded. The nanomsg library will try to deliver any outstanding outbound messages for the time specified by `linger`.

### socket.tcpnodelay(boolean)

*(Function, param: Boolean, default: false)*: When set, disables Nagle’s algorithm. It also disables delaying of TCP acknowledgments. Using this option improves latency at the expense of throughput.

Pass no parameter for current tcp nodelay setting.

```js
//default
console.log(socket.tcpnodelay()) //tcp nodelay: off

socket.tcpnodelay(true) //disabling Nagle's algorithm

console.log(socket.tcpnodelay()) //tcp nodelay: on
```

### socket.linger(duration)

*(Function, param: Number, default: `1000`)*: Specifies how long the socket should try to send pending outbound messages after `socket.close()` or `socket.shutdown()` is called, in milliseconds.

Pass no parameter for the linger duration.

```js
socket.linger(5000)
console.log(socket.linger()) //5000
```

### socket.sndbuf(size)

*(Function, param: Number, size in bytes, default: `128kB`)*: Size of the send buffer, in bytes. To prevent blocking for messages larger than the buffer, exactly one message may be buffered in addition to the data in the send buffer.

Pass no parameter for the socket's send buffer size.

```js
socket.sndbuf(131072)
console.log(socket.sndbuf()) // 131072
```

### socket.rcvbuf(size)

*(Function, param: Number, size in bytes, default: `128kB`)*: Size of the receive buffer, in bytes. To prevent blocking for messages larger than the buffer, exactly one message may be buffered in addition to the data in the receive buffer.

Pass no parameter for the socket's receive buffer size.

```js
socket.rcvbuf(20480)
console.log(socket.rcvbuf()) // 20480
```

### socket.sndtimeo(duration)

*(Function, param: Number, default: `-1`)*: The timeout for send operation on the socket, in milliseconds.

Pass no parameter for the socket's send timeout.

```js
socket.sndtimeo(200)
console.log(socket.sndtimeo()) // 200
```

### socket.rcvtimeo(duration)

*(Function, param: Number, default: `-1`)*: The timeout for recv operation on the socket, in milliseconds.

Pass no parameter for the socket's recv timeout.

```js
socket.rcvtimeo(50)
console.log(socket.rcvtimeo()) // 50
```

### socket.reconn(duration)

*(Function, param: Number, default: `100`)*: For connection-based transports such as TCP, this option specifies how long to wait, in milliseconds, when connection is broken before trying to re-establish it. Note that actual reconnect interval may be randomized to some extent to prevent severe reconnection storms.

Pass no parameter for the socket's `reconnect` interval.

```js
socket.reconn(600)
console.log(socket.reconn()) // 600
```

### socket.maxreconn(duration)

*(Function, param: Number, default: `0`)*: <strong>Only to be used in addition to `socket.reconn()`.</strong> `maxreconn()` specifies maximum reconnection interval. On each reconnect attempt, the previous interval is doubled until `maxreconn` is reached. Value of zero means that no exponential backoff is performed and reconnect interval is based only on `reconn`. If `maxreconn` is less than `reconn`, it is ignored.

Pass no parameter for the socket's `maxreconn` interval.

```js
socket.maxreconn(60000)
console.log(socket.maxreconn()) // 60000
```

### socket.sndprio(priority)

*(Function, param: Number, default: `8`)*: Sets outbound priority for endpoints subsequently added to the socket.

This option has no effect on socket types that send messages to all the peers. However, if the socket type sends each message to a single peer (or a limited set of peers), peers with high priority take precedence over peers with low priority.

Highest priority is 1, lowest is 16. Pass no parameter for the socket's current outbound priority.

```js
socket.sndprio(2)
console.log(socket.sndprio()) // 2
```

### socket.rcvprio(priority)

*(Function, param: Number, default: `8`)*: Sets inbound priority for endpoints subsequently added to the socket.

This option has no effect on socket types that are not able to receive messages.

When receiving a message, messages from peer with higher priority are received before messages from peer with lower priority.

Highest priority is 1, lowest is 16. Pass no parameter for the socket's current inbound priority.

```js
socket.rcvprio(10)
console.log(socket.rcvprio()) // 10
```

# test

```bash
$ git clone https://github.com/nickdesaulniers/node-nanomsg.git nano
$ cd nano && git submodule update --init

# now you can build the project and run the test suite:
$ make && make check

# or perhaps you'd prefer to use the npm commands instead:
$ npm i
$ npm t

# let's say you switch to another version of node/iojs, you might want to run:
$ make clean && make && make check

# for the super deluxe make clean, rebuild, and test suite:
$ make full
```

Note: you must `git submodule update --init` to initialize the nanomsg repository.

# license
## contributing

Issues and pull requests welcome!

## license

MIT
8 changes: 8 additions & 0 deletions docs/address_strings.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
### socket addresses

*(Strings)*

Socket address strings consist of two parts as follows: `transport://address`. The transport specifies the underlying transport protocol to use. The meaning of the address part is specific to the underlying transport protocol.
* *TCP transport mechanism*: `'tcp://127.0.0.1:65000'` When binding a TCP socket, address of the form `tcp://interface:port` should be used. Port is the TCP port number to use. Interface is either: `IPv4` or `IPv6` address of a local network interface, or DNS name of the remote box. It is possible to use named interfaces like `eth0`. For more info see [nanomsg docs](http://nanomsg.org/v0.5/nn_tcp.7.html).
* *in-process transport mechanism*: `'inproc://bar'` The `inproc` transport allows messages between threads or modules inside a process. In-process address is an arbitrary case-sensitive string preceded by `inproc://` protocol specifier. All in-process addresses are visible from any module within the process. They are not visible from outside of the process. The overall buffer size for an inproc connection is determined by `rcvbuf` socket option on the receiving end of the connection. `sndbuf` is ignored. In addition to the buffer, one message of arbitrary size will fit into the buffer. That way, even messages larger than the buffer can be transfered via inproc connection.
* *inter-process transport mechanism*: `'ipc:///tmp/foo.ipc'` The `ipc` transport allows for sending messages between processes within a single box. The nanomsg implementation uses native IPC mechanism provided by the local operating system and the IPC addresses are thus OS-specific. On POSIX-compliant systems, UNIX domain sockets are used and IPC addresses are file references. Note that both relative (`ipc://test.ipc`) and absolute (`ipc:///tmp/test.ipc`) paths may be used. Also note that access rights on the IPC files must be set in such a way that the appropriate applications can actually use them. On Windows, named pipes are used for IPC. The Windows IPC address is an arbitrary case-insensitive string containing any character except for backslash: internally, address `ipc://test` means that named pipe `\\.\pipe\test` will be used.
Loading

0 comments on commit 5fa3305

Please sign in to comment.