Skip to content

Commit

Permalink
created esl.Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Engler authored and Chad Engler committed Oct 1, 2012
1 parent eb5554e commit 5d35bb7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 31 deletions.
39 changes: 36 additions & 3 deletions README.md
Expand Up @@ -57,7 +57,7 @@ This library exposes 3 main classes:

* `esl.Connection`
* `esl.Event`
* `esl.Server` [Coming in `v0.0.2`]
* `esl.Server`
* `esl.Parser` (Used internally for parsing the raw socket stream; __not for public use__)

Which implement the [ESLconnection](http://wiki.freeswitch.org/wiki/Event_Socket_Library#ESLconnection_Object)
Expand Down Expand Up @@ -108,7 +108,7 @@ This changes the constructor call for an "Outbound" connection to look like:
var conn = new esl.Connection(socket[, readyCallback]);
```

The readyCallback is called once the connection is stored, and a `CHANNEL_DATA` event is received.
The readyCallback is called once the connection is stored, the `connect` command is issued, and a `CHANNEL_DATA` event is received.

#### `esl.Connection::sendRecv(command[, args][, body][, callback]);`

Expand Down Expand Up @@ -144,6 +144,11 @@ conn.on('ready', function() {
});
```

The `esl.Connection` object uses [`EventEmitter2`](https://github.com/hij1nx/EventEmitter2) to
send namespaced events. For example every event raises the `esl::event::<EVENT_NAME>` event, where
`<EVENT_NAME>` is the name of the event. Listening to `esl::event::*` will be called everytime an
event is fired.

Here is the event list in the form of `event_name(param1 {type1}, ..., paramN {typeN})`:

<dl>
Expand Down Expand Up @@ -205,6 +210,19 @@ Here is the event list in the form of `event_name(param1 {type1}, ..., paramN {t
<dd>Any Content-Type not parsed by the library is emmited on this channel, where CONTENT_TYPE is the Event's Content-Type header value</dd>
</dl>

And here are the events that will be emitted by `esl.Server`; it also uses EventEmitter2:

<dl>
<dt><code>connection::open(connection {esl.Connection})</code></dt>
<dd>FreeSWITCH has opened a connection to the server, however the connection object is not ready to be used yet</dd>

<dt><code>connection::ready(connection {esl.Connection})</code></dt>
<dd>A newly opened connection is now ready to be used</dd>

<dt><code>connection::close(connection {esl.Connection})</code></dt>
<dd>A connection has been closed</dd>
</dl>

### Library API

Since this library implements the [Event Socket Library](http://wiki.freeswitch.org/wiki/Event_Socket_Library)
Expand Down Expand Up @@ -259,4 +277,19 @@ reference they are listed below in the form `function_name(param1 {type1}, ...,
- `addHeader(name {string}, value {string})` (if `name` exists, overwrites value)
- `delHeader(name {string})`
- `firstHeader()`
- `nextHeader()`
- `nextHeader()`

#### `esl.Server`

- `Server([options {object}][, readyCb {function}])`
* `readyCb` is called once the server is listening for connections
* `options` defaults to the following:
```javascript
{
port: 8022,
host: '127.0.0.1',
server: null
//server overrides port/host options with the port/host of the passed server
//DO NOT PASS server TO THE LIBRARY UNTIL ITS listening CALLBACK HAS BEEN CALLED
}
```
7 changes: 7 additions & 0 deletions lib/esl.js
Expand Up @@ -48,6 +48,13 @@ esl.Event = require('./esl/event').Event;

esl.Connection = require('./esl/connection').Connection;

//
// ESLserver Object
// Custom object to manage multiple "Outbound" connections from FreeSWITCH
//

esl.Server = require('./esl/server').Server;

//
// ESLparser Object
// Custom object to parse ESL data into an ESLevent
Expand Down
19 changes: 3 additions & 16 deletions lib/esl/connection.js
Expand Up @@ -74,14 +74,16 @@ var Connection = exports.Connection = function() {
this.connecting = false;
this._onConnect();

this.send('connect');

this.once('esl::event::CHANNEL_DATA', function(evt) {
self.emit('ready');

if(self.readyCb && typeof self.readyCb === 'function')
self.readyCb();
});

this.socket.on('error', utile.proxy(this._onError, his));
this.socket.on('error', utile.proxy(this._onError, this));
}
//Invalid arguments passed
else { //0 args, or more than 4
Expand Down Expand Up @@ -567,22 +569,7 @@ Connection.prototype._onEvent = function(event, headers, body) {
case 'command/reply':
emit += '::command::reply';

//a bug in the response to connect
if(headers['Event-Name'] === 'CHANNEL_DATA') {
console.log('CHANNEL_DATA\n', event, headers, body);
/*body = headers;
headers = {};
['Content-Type','Reply-Text','Socket-Mode','Control'].forEach(function(name, i) {
headers[name] = body[name];
delete body[name];
});*/

//TODO: body needs to be str here, but is object
// log its contents to see how to handle it
console.log(headers, body);
event = new esl.Event(headers, body);

if(!this._inbound) {
this.channelData = event;
this.emit('esl::event::CHANNEL_DATA', event);
Expand Down
26 changes: 14 additions & 12 deletions lib/esl/server.js
Expand Up @@ -40,18 +40,20 @@ Server.prototype._onConnection = function(socket) {
var conn = new esl.Connection(socket),
id = this._generateId();

this.connections[id] = {
conn: conn,
socket: socket
};

conn.on('ready', utile.proxy(function(c, id) {
this.emit('connection::open', c);
}, this, conn, id));

conn.on('esl::end', utile.proxy(function(c, id) {
this.emit('connection::close', c);
}, this, conn, id));
this.connections[id] = conn;
this.connections[id]._id = id;

this.emit('connection::open', conn, id);

conn.on('ready', utile.proxy(function(id) {
this.emit('connection::ready', this.connections[id], id);
}, this, id));

conn.on('esl::end', utile.proxy(function(id) {
this.emit('connection::close', this.connections[id], id);

delete this.connections[id];
}, this, id));
};

Server.prototype._onListening = function() {
Expand Down

0 comments on commit 5d35bb7

Please sign in to comment.