├── LessonOne ├── client │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── scopes │ │ │ └── scope_settings.xml │ │ ├── encodings.xml │ │ ├── vcs.xml │ │ ├── modules.xml │ │ └── client.iml │ └── nodejs │ │ ├── .idea │ │ ├── .name │ │ ├── misc.xml │ │ ├── scopes │ │ │ └── scope_settings.xml │ │ ├── encodings.xml │ │ ├── vcs.xml │ │ ├── modules.xml │ │ ├── jsLibraryMappings.xml │ │ └── nodejs.iml │ │ ├── node_modules │ │ ├── mqtt │ │ │ ├── .npmignore │ │ │ ├── index.js │ │ │ ├── node_modules │ │ │ │ └── readable-stream │ │ │ │ │ ├── duplex.js │ │ │ │ │ ├── transform.js │ │ │ │ │ ├── writable.js │ │ │ │ │ ├── passthrough.js │ │ │ │ │ ├── readable.js │ │ │ │ │ ├── examples │ │ │ │ │ ├── typer-fsr.js │ │ │ │ │ ├── typer.js │ │ │ │ │ └── CAPSLOCKTYPER.JS │ │ │ │ │ ├── test │ │ │ │ │ ├── fixtures │ │ │ │ │ │ └── x1024.txt │ │ │ │ │ ├── simple │ │ │ │ │ │ ├── test-stream2-finish-pipe.js │ │ │ │ │ │ ├── test-stream2-compatibility.js │ │ │ │ │ │ ├── test-stream2-read-sync-stack.js │ │ │ │ │ │ ├── test-stream2-readable-legacy-drain.js │ │ │ │ │ │ ├── test-stream2-unpipe-drain.js │ │ │ │ │ │ ├── test-stream2-readable-non-empty-end.js │ │ │ │ │ │ ├── test-stream2-large-read-stall.js │ │ │ │ │ │ ├── test-stream2-unpipe-leak.js │ │ │ │ │ │ ├── test-stream2-pipe-error-handling.js │ │ │ │ │ │ ├── test-stream2-readable-from-list.js │ │ │ │ │ │ ├── test-stream2-push.js │ │ │ │ │ │ └── test-stream2-readable-empty-buffer-no-eof.js │ │ │ │ │ └── common.js │ │ │ │ │ ├── LICENSE │ │ │ │ │ └── lib │ │ │ │ │ ├── _stream_passthrough.js │ │ │ │ │ └── _stream_duplex.js │ │ │ ├── test │ │ │ │ ├── connection.js │ │ │ │ ├── helpers │ │ │ │ │ ├── public-key.pem │ │ │ │ │ ├── private-csr.pem │ │ │ │ │ ├── public-cert.pem │ │ │ │ │ ├── server.js │ │ │ │ │ └── private-key.pem │ │ │ │ ├── util.js │ │ │ │ ├── old │ │ │ │ │ ├── tester.js │ │ │ │ │ ├── smoke.test.js │ │ │ │ │ ├── tls.test.js │ │ │ │ │ ├── mqtt.client.test.js │ │ │ │ │ ├── connection.test.js │ │ │ │ │ └── qos.test.js │ │ │ │ ├── server.js │ │ │ │ ├── integration.js │ │ │ │ └── mqtt.js │ │ │ ├── examples │ │ │ │ ├── client │ │ │ │ │ ├── evict.js │ │ │ │ │ ├── tls-client.js │ │ │ │ │ └── client_test.js │ │ │ │ └── server │ │ │ │ │ ├── broadcast.js │ │ │ │ │ ├── tls.js │ │ │ │ │ ├── redis.js │ │ │ │ │ └── orig.js │ │ │ ├── bin │ │ │ │ ├── mqtt_pub │ │ │ │ └── mqtt_sub │ │ │ ├── LICENSE │ │ │ ├── lib │ │ │ │ ├── protocol.js │ │ │ │ ├── server.js │ │ │ │ ├── connection.js │ │ │ │ └── mqtt.js │ │ │ ├── README.md │ │ │ └── package.json │ │ └── .bin │ │ │ ├── mqtt_pub.cmd │ │ │ ├── mqtt_sub.cmd │ │ │ ├── mqtt_pub │ │ │ └── mqtt_sub │ │ ├── mqttClientPub.js │ │ └── mqttClientSub.js └── server │ ├── .idea │ ├── .name │ ├── misc.xml │ ├── scopes │ │ └── scope_settings.xml │ ├── encodings.xml │ ├── vcs.xml │ ├── jsLibraryMappings.xml │ ├── modules.xml │ └── server.iml │ ├── node_modules │ ├── mqtt │ │ ├── .npmignore │ │ ├── index.js │ │ ├── node_modules │ │ │ └── readable-stream │ │ │ │ ├── duplex.js │ │ │ │ ├── writable.js │ │ │ │ ├── transform.js │ │ │ │ ├── passthrough.js │ │ │ │ ├── readable.js │ │ │ │ ├── examples │ │ │ │ ├── typer-fsr.js │ │ │ │ ├── typer.js │ │ │ │ └── CAPSLOCKTYPER.JS │ │ │ │ ├── test │ │ │ │ ├── fixtures │ │ │ │ │ └── x1024.txt │ │ │ │ ├── simple │ │ │ │ │ ├── test-stream2-finish-pipe.js │ │ │ │ │ ├── test-stream2-compatibility.js │ │ │ │ │ ├── test-stream2-read-sync-stack.js │ │ │ │ │ ├── test-stream2-readable-legacy-drain.js │ │ │ │ │ ├── test-stream2-unpipe-drain.js │ │ │ │ │ ├── test-stream2-readable-non-empty-end.js │ │ │ │ │ ├── test-stream2-large-read-stall.js │ │ │ │ │ ├── test-stream2-unpipe-leak.js │ │ │ │ │ ├── test-stream2-pipe-error-handling.js │ │ │ │ │ ├── test-stream2-readable-from-list.js │ │ │ │ │ ├── test-stream2-push.js │ │ │ │ │ └── test-stream2-readable-empty-buffer-no-eof.js │ │ │ │ └── common.js │ │ │ │ ├── LICENSE │ │ │ │ └── lib │ │ │ │ ├── _stream_passthrough.js │ │ │ │ └── _stream_duplex.js │ │ ├── test │ │ │ ├── connection.js │ │ │ ├── helpers │ │ │ │ ├── public-key.pem │ │ │ │ ├── private-csr.pem │ │ │ │ ├── public-cert.pem │ │ │ │ ├── server.js │ │ │ │ └── private-key.pem │ │ │ ├── util.js │ │ │ ├── old │ │ │ │ ├── tester.js │ │ │ │ ├── smoke.test.js │ │ │ │ ├── tls.test.js │ │ │ │ ├── mqtt.client.test.js │ │ │ │ ├── connection.test.js │ │ │ │ └── qos.test.js │ │ │ ├── server.js │ │ │ ├── integration.js │ │ │ └── mqtt.js │ │ ├── examples │ │ │ ├── client │ │ │ │ ├── evict.js │ │ │ │ ├── tls-client.js │ │ │ │ └── client_test.js │ │ │ └── server │ │ │ │ ├── broadcast.js │ │ │ │ ├── tls.js │ │ │ │ ├── redis.js │ │ │ │ └── orig.js │ │ ├── bin │ │ │ ├── mqtt_pub │ │ │ └── mqtt_sub │ │ ├── LICENSE │ │ ├── lib │ │ │ ├── protocol.js │ │ │ ├── server.js │ │ │ ├── connection.js │ │ │ └── mqtt.js │ │ ├── README.md │ │ └── package.json │ └── .bin │ │ ├── mqtt_pub.cmd │ │ ├── mqtt_sub.cmd │ │ ├── mqtt_pub │ │ └── mqtt_sub │ └── mqttServer.js └── README.md /LessonOne/client/.idea/.name: -------------------------------------------------------------------------------- 1 | client -------------------------------------------------------------------------------- /LessonOne/server/.idea/.name: -------------------------------------------------------------------------------- 1 | server -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/.name: -------------------------------------------------------------------------------- 1 | nodejs -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mqttlesson 2 | ========== 3 | 4 | MQTT LESSON Example -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | certs/* 3 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | certs/* 3 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/mqtt'); 2 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/mqtt'); 2 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/duplex.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_duplex.js") 2 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/writable.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_writable.js") 2 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/duplex.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_duplex.js") 2 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/transform.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_transform.js") 2 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/transform.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_transform.js") 2 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/writable.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_writable.js") 2 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/passthrough.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_passthrough.js") 2 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/passthrough.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./lib/_stream_passthrough.js") 2 | -------------------------------------------------------------------------------- /LessonOne/client/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LessonOne/server/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/.bin/mqtt_pub.cmd: -------------------------------------------------------------------------------- 1 | @IF EXIST "%~dp0\node.exe" ( 2 | "%~dp0\node.exe" "%~dp0\..\mqtt\bin\mqtt_pub" %* 3 | ) ELSE ( 4 | node "%~dp0\..\mqtt\bin\mqtt_pub" %* 5 | ) -------------------------------------------------------------------------------- /LessonOne/server/node_modules/.bin/mqtt_sub.cmd: -------------------------------------------------------------------------------- 1 | @IF EXIST "%~dp0\node.exe" ( 2 | "%~dp0\node.exe" "%~dp0\..\mqtt\bin\mqtt_sub" %* 3 | ) ELSE ( 4 | node "%~dp0\..\mqtt\bin\mqtt_sub" %* 5 | ) -------------------------------------------------------------------------------- /LessonOne/client/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/.bin/mqtt_pub.cmd: -------------------------------------------------------------------------------- 1 | @IF EXIST "%~dp0\node.exe" ( 2 | "%~dp0\node.exe" "%~dp0\..\mqtt\bin\mqtt_pub" %* 3 | ) ELSE ( 4 | node "%~dp0\..\mqtt\bin\mqtt_pub" %* 5 | ) -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/.bin/mqtt_sub.cmd: -------------------------------------------------------------------------------- 1 | @IF EXIST "%~dp0\node.exe" ( 2 | "%~dp0\node.exe" "%~dp0\..\mqtt\bin\mqtt_sub" %* 3 | ) ELSE ( 4 | node "%~dp0\..\mqtt\bin\mqtt_sub" %* 5 | ) -------------------------------------------------------------------------------- /LessonOne/server/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /LessonOne/client/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LessonOne/server/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LessonOne/client/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LessonOne/server/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LessonOne/server/.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/connection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing requires 3 | */ 4 | 5 | var should = require('should'); 6 | 7 | describe('Connection', function() { 8 | describe('parsing', require('./connection.parse.js')); 9 | describe('transmission', require('./connection.transmit.js')); 10 | }); 11 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/connection.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing requires 3 | */ 4 | 5 | var should = require('should'); 6 | 7 | describe('Connection', function() { 8 | describe('parsing', require('./connection.parse.js')); 9 | describe('transmission', require('./connection.transmit.js')); 10 | }); 11 | -------------------------------------------------------------------------------- /LessonOne/client/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LessonOne/server/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LessonOne/client/.idea/client.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/.bin/mqtt_pub: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | basedir=`dirname "$0"` 3 | 4 | case `uname` in 5 | *CYGWIN*) basedir=`cygpath -w "$basedir"`;; 6 | esac 7 | 8 | if [ -x "$basedir/node" ]; then 9 | "$basedir/node" "$basedir/../mqtt/bin/mqtt_pub" "$@" 10 | ret=$? 11 | else 12 | node "$basedir/../mqtt/bin/mqtt_pub" "$@" 13 | ret=$? 14 | fi 15 | exit $ret 16 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/.bin/mqtt_sub: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | basedir=`dirname "$0"` 3 | 4 | case `uname` in 5 | *CYGWIN*) basedir=`cygpath -w "$basedir"`;; 6 | esac 7 | 8 | if [ -x "$basedir/node" ]; then 9 | "$basedir/node" "$basedir/../mqtt/bin/mqtt_sub" "$@" 10 | ret=$? 11 | else 12 | node "$basedir/../mqtt/bin/mqtt_sub" "$@" 13 | ret=$? 14 | fi 15 | exit $ret 16 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/.bin/mqtt_pub: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | basedir=`dirname "$0"` 3 | 4 | case `uname` in 5 | *CYGWIN*) basedir=`cygpath -w "$basedir"`;; 6 | esac 7 | 8 | if [ -x "$basedir/node" ]; then 9 | "$basedir/node" "$basedir/../mqtt/bin/mqtt_pub" "$@" 10 | ret=$? 11 | else 12 | node "$basedir/../mqtt/bin/mqtt_pub" "$@" 13 | ret=$? 14 | fi 15 | exit $ret 16 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/.bin/mqtt_sub: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | basedir=`dirname "$0"` 3 | 4 | case `uname` in 5 | *CYGWIN*) basedir=`cygpath -w "$basedir"`;; 6 | esac 7 | 8 | if [ -x "$basedir/node" ]; then 9 | "$basedir/node" "$basedir/../mqtt/bin/mqtt_sub" "$@" 10 | ret=$? 11 | else 12 | node "$basedir/../mqtt/bin/mqtt_sub" "$@" 13 | ret=$? 14 | fi 15 | exit $ret 16 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/jsLibraryMappings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/readable.js: -------------------------------------------------------------------------------- 1 | exports = module.exports = require('./lib/_stream_readable.js'); 2 | exports.Readable = exports; 3 | exports.Writable = require('./lib/_stream_writable.js'); 4 | exports.Duplex = require('./lib/_stream_duplex.js'); 5 | exports.Transform = require('./lib/_stream_transform.js'); 6 | exports.PassThrough = require('./lib/_stream_passthrough.js'); 7 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/readable.js: -------------------------------------------------------------------------------- 1 | exports = module.exports = require('./lib/_stream_readable.js'); 2 | exports.Readable = exports; 3 | exports.Writable = require('./lib/_stream_writable.js'); 4 | exports.Duplex = require('./lib/_stream_duplex.js'); 5 | exports.Transform = require('./lib/_stream_transform.js'); 6 | exports.PassThrough = require('./lib/_stream_passthrough.js'); 7 | -------------------------------------------------------------------------------- /LessonOne/server/.idea/server.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/.idea/nodejs.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/client/evict.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..'); 2 | 3 | mqtt.createClient(1883, "127.0.0.1", function(err,client) { 4 | if ( client ) { 5 | console.log("Client isn't null"); 6 | client.connect({ keepalive: 10, client: "evict_test_client" }); 7 | client.on("connack", function(packet) { 8 | console.log( JSON.stringify(packet) ); 9 | }); 10 | } else { 11 | console.log("Error " + JSON.stringify(err)); 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/examples/typer-fsr.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var FSReadable = require('../fs.js'); 3 | var rst = new FSReadable(__filename); 4 | 5 | rst.on('end', function() { 6 | process.stdin.pause(); 7 | }); 8 | 9 | process.stdin.setRawMode(true); 10 | process.stdin.on('data', function() { 11 | var c = rst.read(3); 12 | if (!c) return; 13 | process.stdout.write(c); 14 | }); 15 | process.stdin.resume(); 16 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/examples/typer-fsr.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var FSReadable = require('../fs.js'); 3 | var rst = new FSReadable(__filename); 4 | 5 | rst.on('end', function() { 6 | process.stdin.pause(); 7 | }); 8 | 9 | process.stdin.setRawMode(true); 10 | process.stdin.on('data', function() { 11 | var c = rst.read(3); 12 | if (!c) return; 13 | process.stdout.write(c); 14 | }); 15 | process.stdin.resume(); 16 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/client/evict.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..'); 2 | 3 | mqtt.createClient(1883, "127.0.0.1", function(err,client) { 4 | if ( client ) { 5 | console.log("Client isn't null"); 6 | client.connect({ keepalive: 10, client: "evict_test_client" }); 7 | client.on("connack", function(packet) { 8 | console.log( JSON.stringify(packet) ); 9 | }); 10 | } else { 11 | console.log("Error " + JSON.stringify(err)); 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/bin/mqtt_pub: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var mqtt = require('../lib/mqtt'); 4 | 5 | var argv = process.argv; 6 | 7 | for (var i = 2; i <= 5; i++) { 8 | if(!argv[i]) process.exit(-1); 9 | } 10 | 11 | var port = argv[2] 12 | , host = argv[3] 13 | , topic = argv[4] 14 | , payload = argv[5]; 15 | 16 | var client = mqtt.createClient(port, host); 17 | 18 | client.on('connect', function() { 19 | client.publish(topic, payload); 20 | client.end(); 21 | }); 22 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/bin/mqtt_pub: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var mqtt = require('../lib/mqtt'); 4 | 5 | var argv = process.argv; 6 | 7 | for (var i = 2; i <= 5; i++) { 8 | if(!argv[i]) process.exit(-1); 9 | } 10 | 11 | var port = argv[2] 12 | , host = argv[3] 13 | , topic = argv[4] 14 | , payload = argv[5]; 15 | 16 | var client = mqtt.createClient(port, host); 17 | 18 | client.on('connect', function() { 19 | client.publish(topic, payload); 20 | client.end(); 21 | }); 22 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/bin/mqtt_sub: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var mqtt = require('../lib/mqtt'); 4 | 5 | var argv = process.argv; 6 | 7 | for (var i = 2; i <= 4; i++) { 8 | if(!argv[i]) process.exit(-1); 9 | } 10 | 11 | var port = argv[2] 12 | , host = argv[3] 13 | , topic = argv[4]; 14 | 15 | var c = mqtt.createClient(port, host); 16 | 17 | c.on('connect', function() { 18 | c.subscribe(topic); 19 | c.on('message', function(topic, message) { 20 | console.log(topic + ' ' + message); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/mqttClientPub.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created with JetBrains WebStorm. 3 | * User: youxiachai 4 | * Date: 13-4-25 5 | * Time: 上午2:01 6 | * To change this template use File | Settings | File Templates. 7 | */ 8 | 9 | var mqtt = require('mqtt'); 10 | 11 | //主题 12 | var topic = "mqtt/lessonOne"; 13 | var port = 1883; 14 | var host = 'localhost'; 15 | var client = mqtt.createClient(port, host); 16 | client.on('connect', function() { 17 | //对相关主题进行消息发布 18 | client.publish(topic, '大家好!'); 19 | client.end(); 20 | }); -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/bin/mqtt_sub: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var mqtt = require('../lib/mqtt'); 4 | 5 | var argv = process.argv; 6 | 7 | for (var i = 2; i <= 4; i++) { 8 | if(!argv[i]) process.exit(-1); 9 | } 10 | 11 | var port = argv[2] 12 | , host = argv[3] 13 | , topic = argv[4]; 14 | 15 | var c = mqtt.createClient(port, host); 16 | 17 | c.on('connect', function() { 18 | c.subscribe(topic); 19 | c.on('message', function(topic, message) { 20 | console.log(topic + ' ' + message); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/helpers/public-key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN PUBLIC KEY----- 2 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA23Mhmap/uHdrE72bOjZW 3 | GIX8EXdjgT98NOQXLbs+CyxL+KkU4x78jHIz+ED+j9tBvQFj6wfmzXTbSQ0fv26a 4 | 1ptYxoOY6Lxu+Yda1UJxiGfeaFtEVZw5F1Od9Dy5FhdkZ6i6mOWF+sqRAKhIAUqH 5 | U7NTto3HgKMwCNJrinK2swBU0nftGvKGx8y4cB4bz7QSjeqZjJqmWm6u2vwDcdZX 6 | rlHZUULBnmEobwj1fYXdStgoevjGXE6CMPyPY59hnGkYcjph7EnbxpHTGWxZ/NL5 7 | MooVXHLye2A8YvRlcve+yzzSYBwGX0V+wnO5+fggc5iylfU4QzQzumUyZDD/I1P0 8 | HwIDAQAB 9 | -----END PUBLIC KEY----- 10 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/helpers/public-key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN PUBLIC KEY----- 2 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA23Mhmap/uHdrE72bOjZW 3 | GIX8EXdjgT98NOQXLbs+CyxL+KkU4x78jHIz+ED+j9tBvQFj6wfmzXTbSQ0fv26a 4 | 1ptYxoOY6Lxu+Yda1UJxiGfeaFtEVZw5F1Od9Dy5FhdkZ6i6mOWF+sqRAKhIAUqH 5 | U7NTto3HgKMwCNJrinK2swBU0nftGvKGx8y4cB4bz7QSjeqZjJqmWm6u2vwDcdZX 6 | rlHZUULBnmEobwj1fYXdStgoevjGXE6CMPyPY59hnGkYcjph7EnbxpHTGWxZ/NL5 7 | MooVXHLye2A8YvRlcve+yzzSYBwGX0V+wnO5+fggc5iylfU4QzQzumUyZDD/I1P0 8 | HwIDAQAB 9 | -----END PUBLIC KEY----- 10 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/client/tls-client.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..'); 2 | 3 | mqtt.createSecureClient(process.argv[2], process.argv[3], "private-key.pem", "public-cert.pem", function(err,client) { 4 | if (client) { 5 | console.log("Client isn't null"); 6 | client.connect({ keepalive: 60, client: "tls_test_client" }); 7 | client.on("connack", function(packet) { 8 | console.log( JSON.stringify(packet) ); 9 | }); 10 | } else { 11 | console.log("Error " + JSON.stringify(err)); 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/client/tls-client.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..'); 2 | 3 | mqtt.createSecureClient(process.argv[2], process.argv[3], "private-key.pem", "public-cert.pem", function(err,client) { 4 | if (client) { 5 | console.log("Client isn't null"); 6 | client.connect({ keepalive: 60, client: "tls_test_client" }); 7 | client.on("connack", function(packet) { 8 | console.log( JSON.stringify(packet) ); 9 | }); 10 | } else { 11 | console.log("Error " + JSON.stringify(err)); 12 | } 13 | }); 14 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/examples/typer.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var fst = fs.createReadStream(__filename); 3 | var Readable = require('../readable.js'); 4 | var rst = new Readable(); 5 | rst.wrap(fst); 6 | 7 | rst.on('end', function() { 8 | process.stdin.pause(); 9 | }); 10 | 11 | process.stdin.setRawMode(true); 12 | process.stdin.on('data', function() { 13 | var c = rst.read(3); 14 | if (!c) return setTimeout(process.exit, 500) 15 | process.stdout.write(c); 16 | }); 17 | process.stdin.resume(); 18 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/examples/typer.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var fst = fs.createReadStream(__filename); 3 | var Readable = require('../readable.js'); 4 | var rst = new Readable(); 5 | rst.wrap(fst); 6 | 7 | rst.on('end', function() { 8 | process.stdin.pause(); 9 | }); 10 | 11 | process.stdin.setRawMode(true); 12 | process.stdin.on('data', function() { 13 | var c = rst.read(3); 14 | if (!c) return setTimeout(process.exit, 500) 15 | process.stdout.write(c); 16 | }); 17 | process.stdin.resume(); 18 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/mqttClientSub.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created with JetBrains WebStorm. 3 | * User: youxiachai 4 | * Date: 13-4-25 5 | * Time: 上午2:27 6 | * To change this template use File | Settings | File Templates. 7 | */ 8 | var mqtt = require('mqtt'); 9 | 10 | var port = 1883; 11 | var host = 'localhost'; 12 | var clientSub = mqtt.createClient(port, host); 13 | //主题订阅 14 | var topic = "mqtt/lessonOne"; 15 | //与服务进行连接 16 | clientSub.on('connect', function() { 17 | //订阅服务器主题 18 | clientSub.subscribe(topic); 19 | //接受该主题下的发布信息 20 | clientSub.on('message', function(topic, message) { 21 | console.log(topic + ' ' + message); 22 | //断开连接 23 | clientSub.end(); 24 | }); 25 | 26 | }); -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/util.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Requires 3 | */ 4 | 5 | var util = require('util') 6 | , Stream = require('stream').Transform; 7 | 8 | if (!Stream) { 9 | Stream = require("readable-stream").Transform; 10 | } 11 | 12 | /** 13 | * Export TestStream 14 | */ 15 | 16 | var TestStream = module.exports.TestStream = 17 | function TestStream() { 18 | if (!this instanceof TestStream) return new TestStream(); 19 | 20 | Stream.call(this); 21 | }; 22 | util.inherits(TestStream, Stream); 23 | 24 | TestStream.prototype._transform = function(buffer, encoding, callback) { 25 | if (!Buffer.isBuffer(buffer)) { 26 | buffer = new Buffer(buffer, encoding); 27 | } 28 | setTimeout(function () { 29 | callback(null, buffer); 30 | }, 10); 31 | }; 32 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/util.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Requires 3 | */ 4 | 5 | var util = require('util') 6 | , Stream = require('stream').Transform; 7 | 8 | if (!Stream) { 9 | Stream = require("readable-stream").Transform; 10 | } 11 | 12 | /** 13 | * Export TestStream 14 | */ 15 | 16 | var TestStream = module.exports.TestStream = 17 | function TestStream() { 18 | if (!this instanceof TestStream) return new TestStream(); 19 | 20 | Stream.call(this); 21 | }; 22 | util.inherits(TestStream, Stream); 23 | 24 | TestStream.prototype._transform = function(buffer, encoding, callback) { 25 | if (!Buffer.isBuffer(buffer)) { 26 | buffer = new Buffer(buffer, encoding); 27 | } 28 | setTimeout(function () { 29 | callback(null, buffer); 30 | }, 10); 31 | }; 32 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/old/tester.js: -------------------------------------------------------------------------------- 1 | var EventEmitter = require('events').EventEmitter, 2 | util = require('util'), 3 | assert = require('assert'); 4 | 5 | /* 6 | * Generates a mock stream object used for testing 7 | */ 8 | var Tester = function () { 9 | this.open = true; 10 | EventEmitter.call(this); 11 | }; 12 | 13 | util.inherits(Tester, EventEmitter); 14 | 15 | Tester.prototype.write = function (data) { 16 | assert.ok(this.open, "Not allowed to write, stream is closed"); 17 | this.emit('data', data); 18 | }; 19 | 20 | Tester.prototype.end = function () { 21 | this.open = false; 22 | }; 23 | 24 | /* 25 | * used to fake events like close and error in tests 26 | */ 27 | Tester.prototype.do_emit = function (evt, data) { 28 | this.emit(evt, data); 29 | }; 30 | 31 | module.exports = function () { 32 | return new Tester(); 33 | }; 34 | 35 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/old/tester.js: -------------------------------------------------------------------------------- 1 | var EventEmitter = require('events').EventEmitter, 2 | util = require('util'), 3 | assert = require('assert'); 4 | 5 | /* 6 | * Generates a mock stream object used for testing 7 | */ 8 | var Tester = function () { 9 | this.open = true; 10 | EventEmitter.call(this); 11 | }; 12 | 13 | util.inherits(Tester, EventEmitter); 14 | 15 | Tester.prototype.write = function (data) { 16 | assert.ok(this.open, "Not allowed to write, stream is closed"); 17 | this.emit('data', data); 18 | }; 19 | 20 | Tester.prototype.end = function () { 21 | this.open = false; 22 | }; 23 | 24 | /* 25 | * used to fake events like close and error in tests 26 | */ 27 | Tester.prototype.do_emit = function (evt, data) { 28 | this.emit(evt, data); 29 | }; 30 | 31 | module.exports = function () { 32 | return new Tester(); 33 | }; 34 | 35 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/old/smoke.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | /* 3 | * Just a simple syntax test 4 | * if you can not require then there is something major wrong 5 | */ 6 | var make_tester = require('./tester'); 7 | 8 | describe("Require all modules", function () { 9 | it("connection", function () { 10 | var O = require('../lib/connection'), 11 | o = new O(make_tester()); 12 | }); 13 | 14 | it("generate", function () { 15 | var c = require('../lib/generate'); 16 | }); 17 | 18 | it("mqtt", function () { 19 | var c = require('../lib/mqtt'); 20 | }); 21 | 22 | it("parse", function () { 23 | var c = require('../lib/parse'); 24 | }); 25 | 26 | it("protocol", function () { 27 | var c = require('../lib/protocol'); 28 | }); 29 | 30 | it("index", function () { 31 | var o = require('../index.js'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/old/smoke.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | /* 3 | * Just a simple syntax test 4 | * if you can not require then there is something major wrong 5 | */ 6 | var make_tester = require('./tester'); 7 | 8 | describe("Require all modules", function () { 9 | it("connection", function () { 10 | var O = require('../lib/connection'), 11 | o = new O(make_tester()); 12 | }); 13 | 14 | it("generate", function () { 15 | var c = require('../lib/generate'); 16 | }); 17 | 18 | it("mqtt", function () { 19 | var c = require('../lib/mqtt'); 20 | }); 21 | 22 | it("parse", function () { 23 | var c = require('../lib/parse'); 24 | }); 25 | 26 | it("protocol", function () { 27 | var c = require('../lib/protocol'); 28 | }); 29 | 30 | it("index", function () { 31 | var o = require('../index.js'); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/examples/CAPSLOCKTYPER.JS: -------------------------------------------------------------------------------- 1 | var Transform = require('../transform'); 2 | var inherits = require('util').inherits; 3 | 4 | // subclass 5 | function MyStream () { 6 | Transform.call(this, { 7 | lowWaterMark: 0, 8 | encoding: 'utf8' 9 | }); 10 | } 11 | inherits(MyStream, Transform); 12 | 13 | MyStream.prototype._transform = function (chunk, outputFn, callback) { 14 | outputFn(new Buffer(String(chunk).toUpperCase())); 15 | callback(); 16 | }; 17 | 18 | // use it! 19 | var s = new MyStream(); 20 | process.stdin.resume(); 21 | process.stdin.pipe(s).pipe(process.stdout); 22 | if (process.stdin.setRawMode) 23 | process.stdin.setRawMode(true); 24 | process.stdin.on('data', function (c) { 25 | c = c.toString(); 26 | if (c === '\u0003' || c === '\u0004') { 27 | process.stdin.pause(); 28 | s.end(); 29 | } 30 | if (c === '\r') 31 | process.stdout.write('\n'); 32 | }); 33 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/examples/CAPSLOCKTYPER.JS: -------------------------------------------------------------------------------- 1 | var Transform = require('../transform'); 2 | var inherits = require('util').inherits; 3 | 4 | // subclass 5 | function MyStream () { 6 | Transform.call(this, { 7 | lowWaterMark: 0, 8 | encoding: 'utf8' 9 | }); 10 | } 11 | inherits(MyStream, Transform); 12 | 13 | MyStream.prototype._transform = function (chunk, outputFn, callback) { 14 | outputFn(new Buffer(String(chunk).toUpperCase())); 15 | callback(); 16 | }; 17 | 18 | // use it! 19 | var s = new MyStream(); 20 | process.stdin.resume(); 21 | process.stdin.pipe(s).pipe(process.stdout); 22 | if (process.stdin.setRawMode) 23 | process.stdin.setRawMode(true); 24 | process.stdin.on('data', function (c) { 25 | c = c.toString(); 26 | if (c === '\u0003' || c === '\u0004') { 27 | process.stdin.pause(); 28 | s.end(); 29 | } 30 | if (c === '\r') 31 | process.stdout.write('\n'); 32 | }); 33 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing requires 3 | */ 4 | 5 | var should = require('should') 6 | , mqtt = require('../lib/mqtt'); 7 | 8 | /** 9 | * Units under test 10 | */ 11 | 12 | var server = require('../lib/server'); 13 | 14 | describe('MqttServer', function() { 15 | it('should emit MqttServerClients', function(done) { 16 | var s = new server.MqttServer(); 17 | s.listen(9877); 18 | 19 | s.on('client', function(client) { 20 | client.should.be.instanceOf(server.MqttServerClient); 21 | done(); 22 | }); 23 | 24 | mqtt.createClient(9877); 25 | }); 26 | 27 | it("should bind the stream's error in the clients", function (done) { 28 | var s = new server.MqttServer(); 29 | s.listen(9878); 30 | 31 | s.on('client', function(client) { 32 | client.on("error", function () { done(); }); 33 | client.stream.emit("error", new Error("bad idea!")); 34 | }); 35 | 36 | mqtt.createClient(9878); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing requires 3 | */ 4 | 5 | var should = require('should') 6 | , mqtt = require('../lib/mqtt'); 7 | 8 | /** 9 | * Units under test 10 | */ 11 | 12 | var server = require('../lib/server'); 13 | 14 | describe('MqttServer', function() { 15 | it('should emit MqttServerClients', function(done) { 16 | var s = new server.MqttServer(); 17 | s.listen(9877); 18 | 19 | s.on('client', function(client) { 20 | client.should.be.instanceOf(server.MqttServerClient); 21 | done(); 22 | }); 23 | 24 | mqtt.createClient(9877); 25 | }); 26 | 27 | it("should bind the stream's error in the clients", function (done) { 28 | var s = new server.MqttServer(); 29 | s.listen(9878); 30 | 31 | s.on('client', function(client) { 32 | client.on("error", function () { done(); }); 33 | client.stream.emit("error", new Error("bad idea!")); 34 | }); 35 | 36 | mqtt.createClient(9878); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/old/tls.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it, before */ 2 | 3 | 4 | var assert = require('assert'), 5 | should = require('should'); 6 | 7 | var servers = require('./helpers/server'), 8 | mqtt = require('..'); 9 | 10 | var KEY = __dirname + '/helpers/private-key.pem'; 11 | var CERT = __dirname + '/helpers/public-cert.pem'; 12 | 13 | var PORT = (process.env.PORT || 1883) + 1; //port collides with other tests so +1 14 | 15 | describe.skip("SecureClient", function () { 16 | before(function () { 17 | this.server = servers.init_secure_server(PORT); 18 | }); 19 | 20 | it("should connect", function (done) { 21 | mqtt.createSecureClient(PORT, 'localhost', KEY, CERT, function (err, client) { 22 | should.not.exist(err); 23 | client.connect({keepalive: 1000}); 24 | client.on('connack', function (packet) { 25 | done(); 26 | }); 27 | client.on('error', function (err) { 28 | should.not.exist(err); 29 | done(); 30 | }); 31 | }); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/old/tls.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it, before */ 2 | 3 | 4 | var assert = require('assert'), 5 | should = require('should'); 6 | 7 | var servers = require('./helpers/server'), 8 | mqtt = require('..'); 9 | 10 | var KEY = __dirname + '/helpers/private-key.pem'; 11 | var CERT = __dirname + '/helpers/public-cert.pem'; 12 | 13 | var PORT = (process.env.PORT || 1883) + 1; //port collides with other tests so +1 14 | 15 | describe.skip("SecureClient", function () { 16 | before(function () { 17 | this.server = servers.init_secure_server(PORT); 18 | }); 19 | 20 | it("should connect", function (done) { 21 | mqtt.createSecureClient(PORT, 'localhost', KEY, CERT, function (err, client) { 22 | should.not.exist(err); 23 | client.connect({keepalive: 1000}); 24 | client.on('connack', function (packet) { 25 | done(); 26 | }); 27 | client.on('error', function (err) { 28 | should.not.exist(err); 29 | done(); 30 | }); 31 | }); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/helpers/private-csr.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx 3 | ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN 4 | AQEBBQADggEPADCCAQoCggEBANtzIZmqf7h3axO9mzo2VhiF/BF3Y4E/fDTkFy27 5 | PgssS/ipFOMe/IxyM/hA/o/bQb0BY+sH5s1020kNH79umtabWMaDmOi8bvmHWtVC 6 | cYhn3mhbRFWcORdTnfQ8uRYXZGeoupjlhfrKkQCoSAFKh1OzU7aNx4CjMAjSa4py 7 | trMAVNJ37RryhsfMuHAeG8+0Eo3qmYyaplpurtr8A3HWV65R2VFCwZ5hKG8I9X2F 8 | 3UrYKHr4xlxOgjD8j2OfYZxpGHI6YexJ28aR0xlsWfzS+TKKFVxy8ntgPGL0ZXL3 9 | vss80mAcBl9FfsJzufn4IHOYspX1OEM0M7plMmQw/yNT9B8CAwEAAaAAMA0GCSqG 10 | SIb3DQEBBQUAA4IBAQBsONiE5HTjfR1pDrWPIhbLqMO3AqmuB5AwpQm8kAaM2Oz1 11 | DI/a8bHYyODMiyWUPTtwLMQWcJpAG2ZhE18gLqFwXZR1XSOxY1yF+uZ7Ls3hwzbq 12 | 9A6O254B5wXBnXkVbzZwFshV5HWiZwVivF5GDyLRsMoS2EtUHoDEP4YIRK0kPL9H 13 | m3BB334KlWTc8NNXFFG62OL7q2fa8xRHlN8SYfeUjy79eEoBdHv5wL/ZN/YBCDNJ 14 | 2zrYUvbOmfoq1e+6AczZ6xAHHeneUQuaOF225aMwHHZTiP2TlIeFXwBvzV1BWIJv 15 | dOaHX/f3NamKoGvwYyIR1FrI2FpXTJLRE/eu7TFD 16 | -----END CERTIFICATE REQUEST----- 17 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/helpers/private-csr.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIICijCCAXICAQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx 3 | ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN 4 | AQEBBQADggEPADCCAQoCggEBANtzIZmqf7h3axO9mzo2VhiF/BF3Y4E/fDTkFy27 5 | PgssS/ipFOMe/IxyM/hA/o/bQb0BY+sH5s1020kNH79umtabWMaDmOi8bvmHWtVC 6 | cYhn3mhbRFWcORdTnfQ8uRYXZGeoupjlhfrKkQCoSAFKh1OzU7aNx4CjMAjSa4py 7 | trMAVNJ37RryhsfMuHAeG8+0Eo3qmYyaplpurtr8A3HWV65R2VFCwZ5hKG8I9X2F 8 | 3UrYKHr4xlxOgjD8j2OfYZxpGHI6YexJ28aR0xlsWfzS+TKKFVxy8ntgPGL0ZXL3 9 | vss80mAcBl9FfsJzufn4IHOYspX1OEM0M7plMmQw/yNT9B8CAwEAAaAAMA0GCSqG 10 | SIb3DQEBBQUAA4IBAQBsONiE5HTjfR1pDrWPIhbLqMO3AqmuB5AwpQm8kAaM2Oz1 11 | DI/a8bHYyODMiyWUPTtwLMQWcJpAG2ZhE18gLqFwXZR1XSOxY1yF+uZ7Ls3hwzbq 12 | 9A6O254B5wXBnXkVbzZwFshV5HWiZwVivF5GDyLRsMoS2EtUHoDEP4YIRK0kPL9H 13 | m3BB334KlWTc8NNXFFG62OL7q2fa8xRHlN8SYfeUjy79eEoBdHv5wL/ZN/YBCDNJ 14 | 2zrYUvbOmfoq1e+6AczZ6xAHHeneUQuaOF225aMwHHZTiP2TlIeFXwBvzV1BWIJv 15 | dOaHX/f3NamKoGvwYyIR1FrI2FpXTJLRE/eu7TFD 16 | -----END CERTIFICATE REQUEST----- 17 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/fixtures/x1024.txt: -------------------------------------------------------------------------------- 1 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/fixtures/x1024.txt: -------------------------------------------------------------------------------- 1 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/old/mqtt.client.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | 3 | var assert = require('assert'), 4 | should = require('should'); 5 | 6 | var mqtt = require('../lib/mqtt'), 7 | Connection = require('../../lib/connection'); 8 | 9 | describe.skip("Mqtt.createClient", function () { 10 | 11 | it("should return Connection", function () { 12 | var client = mqtt.createClient(); 13 | client.should.be.instanceOf(Connection); 14 | }); 15 | 16 | it("should callback error if not able to connect", function (done) { 17 | //use a non existing IP and default port 18 | var client = mqtt.createClient('127.0.0.3', false, function (err) { 19 | should.exist(err); 20 | err.should.be.instanceOf(Error); 21 | done(); 22 | }); 23 | }); 24 | 25 | it("should callback with client if success", function (done) { 26 | var client = mqtt.createClient(function (err, c) { 27 | should.not.exist(err, "make sure you have a running server on the defaultPort"); 28 | should.exist(c); 29 | c.should.be.instanceOf(Connection); 30 | done(); 31 | }); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/old/mqtt.client.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it */ 2 | 3 | var assert = require('assert'), 4 | should = require('should'); 5 | 6 | var mqtt = require('../lib/mqtt'), 7 | Connection = require('../../lib/connection'); 8 | 9 | describe.skip("Mqtt.createClient", function () { 10 | 11 | it("should return Connection", function () { 12 | var client = mqtt.createClient(); 13 | client.should.be.instanceOf(Connection); 14 | }); 15 | 16 | it("should callback error if not able to connect", function (done) { 17 | //use a non existing IP and default port 18 | var client = mqtt.createClient('127.0.0.3', false, function (err) { 19 | should.exist(err); 20 | err.should.be.instanceOf(Error); 21 | done(); 22 | }); 23 | }); 24 | 25 | it("should callback with client if success", function (done) { 26 | var client = mqtt.createClient(function (err, c) { 27 | should.not.exist(err, "make sure you have a running server on the defaultPort"); 28 | should.exist(c); 29 | c.should.be.instanceOf(Connection); 30 | done(); 31 | }); 32 | }); 33 | 34 | }); 35 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 by Adam Rudd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 by Adam Rudd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/client/client_test.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..'); 2 | 3 | mqtt.createClient(1883, 'localhost', function(err, client) { 4 | if (err) { 5 | console.dir(err); 6 | return process.exit(-1); 7 | } 8 | 9 | var events = ['connack', 'puback', 'pubrec', 'pubcomp']; 10 | 11 | for (var i = 0; i < events.length; i++) { 12 | client.on(events[i], function(packet) { 13 | console.dir(packet); 14 | }); 15 | }; 16 | 17 | client.connect({keepalive: 1000}); 18 | 19 | client.on('connack', function(packet) { 20 | setInterval(function() { 21 | client.publish({ 22 | topic: 'test0' 23 | , payload: 'test' 24 | , qos: 0 25 | }); 26 | 27 | client.publish({ 28 | topic: 'test1' 29 | , payload: 'test' 30 | , qos: 1 31 | , messageId: 1 32 | }); 33 | client.publish({ 34 | topic: 'test2' 35 | , payload: 'test' 36 | , qos: 2 37 | , messageId: 2 38 | }); 39 | }, 10000); 40 | 41 | setInterval(function() { 42 | client.pingreq(); 43 | }, 1000); 44 | }); 45 | 46 | client.on('pubrec', function(packet) { 47 | client.pubrel({messageId: 2}); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/helpers/public-cert.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDBjCCAe4CCQDkrq1PMPtmfzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB 3 | VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 4 | cyBQdHkgTHRkMB4XDTEzMDEyNTEwMzEyOVoXDTEzMDIyNDEwMzEyOVowRTELMAkG 5 | A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 6 | IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB 7 | ANtzIZmqf7h3axO9mzo2VhiF/BF3Y4E/fDTkFy27PgssS/ipFOMe/IxyM/hA/o/b 8 | Qb0BY+sH5s1020kNH79umtabWMaDmOi8bvmHWtVCcYhn3mhbRFWcORdTnfQ8uRYX 9 | ZGeoupjlhfrKkQCoSAFKh1OzU7aNx4CjMAjSa4pytrMAVNJ37RryhsfMuHAeG8+0 10 | Eo3qmYyaplpurtr8A3HWV65R2VFCwZ5hKG8I9X2F3UrYKHr4xlxOgjD8j2OfYZxp 11 | GHI6YexJ28aR0xlsWfzS+TKKFVxy8ntgPGL0ZXL3vss80mAcBl9FfsJzufn4IHOY 12 | spX1OEM0M7plMmQw/yNT9B8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAeHAwoKYl 13 | 6g9lUEwBDqm6ZxjgoYQi6V3loCjBcTr5OrMkLvvZrA55xsse0NRH40I/pvCaAZAZ 14 | EEna0fr5GPYi+y+I8EoU2W/+ehSqRAU8Fkdm0eR5MjyLWYOwd3ClUND8EpUNNSKH 15 | Xw9k9EQmyKsDxVsKWoJoO9rfFkUjooz07jGPCud18QCBs5i5ThbnQ9UP+26D8z5k 16 | 1Dii69LIcLXA3Vtm6R5fT57zNusfx8bqA9yy7UThYaXIazNMWNxiJRXfv0J4zFdD 17 | RQ+SFdJ3p5jurPkc3oRWWPbn/Lpf0E5XlYTJImXT1WmWnQSaNtME4P+3kEL5x+v/ 18 | u8zTLbobG4x0rQ== 19 | -----END CERTIFICATE----- 20 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/helpers/public-cert.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIDBjCCAe4CCQDkrq1PMPtmfzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB 3 | VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0 4 | cyBQdHkgTHRkMB4XDTEzMDEyNTEwMzEyOVoXDTEzMDIyNDEwMzEyOVowRTELMAkG 5 | A1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0 6 | IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB 7 | ANtzIZmqf7h3axO9mzo2VhiF/BF3Y4E/fDTkFy27PgssS/ipFOMe/IxyM/hA/o/b 8 | Qb0BY+sH5s1020kNH79umtabWMaDmOi8bvmHWtVCcYhn3mhbRFWcORdTnfQ8uRYX 9 | ZGeoupjlhfrKkQCoSAFKh1OzU7aNx4CjMAjSa4pytrMAVNJ37RryhsfMuHAeG8+0 10 | Eo3qmYyaplpurtr8A3HWV65R2VFCwZ5hKG8I9X2F3UrYKHr4xlxOgjD8j2OfYZxp 11 | GHI6YexJ28aR0xlsWfzS+TKKFVxy8ntgPGL0ZXL3vss80mAcBl9FfsJzufn4IHOY 12 | spX1OEM0M7plMmQw/yNT9B8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAeHAwoKYl 13 | 6g9lUEwBDqm6ZxjgoYQi6V3loCjBcTr5OrMkLvvZrA55xsse0NRH40I/pvCaAZAZ 14 | EEna0fr5GPYi+y+I8EoU2W/+ehSqRAU8Fkdm0eR5MjyLWYOwd3ClUND8EpUNNSKH 15 | Xw9k9EQmyKsDxVsKWoJoO9rfFkUjooz07jGPCud18QCBs5i5ThbnQ9UP+26D8z5k 16 | 1Dii69LIcLXA3Vtm6R5fT57zNusfx8bqA9yy7UThYaXIazNMWNxiJRXfv0J4zFdD 17 | RQ+SFdJ3p5jurPkc3oRWWPbn/Lpf0E5XlYTJImXT1WmWnQSaNtME4P+3kEL5x+v/ 18 | u8zTLbobG4x0rQ== 19 | -----END CERTIFICATE----- 20 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/client/client_test.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..'); 2 | 3 | mqtt.createClient(1883, 'localhost', function(err, client) { 4 | if (err) { 5 | console.dir(err); 6 | return process.exit(-1); 7 | } 8 | 9 | var events = ['connack', 'puback', 'pubrec', 'pubcomp']; 10 | 11 | for (var i = 0; i < events.length; i++) { 12 | client.on(events[i], function(packet) { 13 | console.dir(packet); 14 | }); 15 | }; 16 | 17 | client.connect({keepalive: 1000}); 18 | 19 | client.on('connack', function(packet) { 20 | setInterval(function() { 21 | client.publish({ 22 | topic: 'test0' 23 | , payload: 'test' 24 | , qos: 0 25 | }); 26 | 27 | client.publish({ 28 | topic: 'test1' 29 | , payload: 'test' 30 | , qos: 1 31 | , messageId: 1 32 | }); 33 | client.publish({ 34 | topic: 'test2' 35 | , payload: 'test' 36 | , qos: 2 37 | , messageId: 2 38 | }); 39 | }, 10000); 40 | 41 | setInterval(function() { 42 | client.pingreq(); 43 | }, 1000); 44 | }); 45 | 46 | client.on('pubrec', function(packet) { 47 | client.pubrel({messageId: 2}); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/lib/protocol.js: -------------------------------------------------------------------------------- 1 | /* Protocol - protocol constants */ 2 | 3 | /* Command code => mnemonic */ 4 | module.exports.types = { 5 | 0: 'reserved', 6 | 1: 'connect', 7 | 2: 'connack', 8 | 3: 'publish', 9 | 4: 'puback', 10 | 5: 'pubrec', 11 | 6: 'pubrel', 12 | 7: 'pubcomp', 13 | 8: 'subscribe', 14 | 9: 'suback', 15 | 10: 'unsubscribe', 16 | 11: 'unsuback', 17 | 12: 'pingreq', 18 | 13: 'pingresp', 19 | 14: 'disconnect', 20 | 15: 'reserved' 21 | }; 22 | 23 | /* Mnemonic => Command code */ 24 | module.exports.codes = {} 25 | for(var k in module.exports.types) { 26 | var v = module.exports.types[k]; 27 | module.exports.codes[v] = k; 28 | } 29 | 30 | /* Header */ 31 | module.exports.CMD_SHIFT = 4; 32 | module.exports.CMD_MASK = 0xF0; 33 | module.exports.DUP_MASK = 0x08; 34 | module.exports.QOS_MASK = 0x03; 35 | module.exports.QOS_SHIFT = 1; 36 | module.exports.RETAIN_MASK = 0x01; 37 | 38 | /* Length */ 39 | module.exports.LENGTH_MASK = 0x7F; 40 | module.exports.LENGTH_FIN_MASK = 0x80; 41 | 42 | /* Connect */ 43 | module.exports.USERNAME_MASK = 0x80; 44 | module.exports.PASSWORD_MASK = 0x40; 45 | module.exports.WILL_RETAIN_MASK = 0x20; 46 | module.exports.WILL_QOS_MASK = 0x18; 47 | module.exports.WILL_QOS_SHIFT = 3; 48 | module.exports.WILL_FLAG_MASK = 0x04; 49 | module.exports.CLEAN_SESSION_MASK = 0x02; 50 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/lib/protocol.js: -------------------------------------------------------------------------------- 1 | /* Protocol - protocol constants */ 2 | 3 | /* Command code => mnemonic */ 4 | module.exports.types = { 5 | 0: 'reserved', 6 | 1: 'connect', 7 | 2: 'connack', 8 | 3: 'publish', 9 | 4: 'puback', 10 | 5: 'pubrec', 11 | 6: 'pubrel', 12 | 7: 'pubcomp', 13 | 8: 'subscribe', 14 | 9: 'suback', 15 | 10: 'unsubscribe', 16 | 11: 'unsuback', 17 | 12: 'pingreq', 18 | 13: 'pingresp', 19 | 14: 'disconnect', 20 | 15: 'reserved' 21 | }; 22 | 23 | /* Mnemonic => Command code */ 24 | module.exports.codes = {} 25 | for(var k in module.exports.types) { 26 | var v = module.exports.types[k]; 27 | module.exports.codes[v] = k; 28 | } 29 | 30 | /* Header */ 31 | module.exports.CMD_SHIFT = 4; 32 | module.exports.CMD_MASK = 0xF0; 33 | module.exports.DUP_MASK = 0x08; 34 | module.exports.QOS_MASK = 0x03; 35 | module.exports.QOS_SHIFT = 1; 36 | module.exports.RETAIN_MASK = 0x01; 37 | 38 | /* Length */ 39 | module.exports.LENGTH_MASK = 0x7F; 40 | module.exports.LENGTH_FIN_MASK = 0x80; 41 | 42 | /* Connect */ 43 | module.exports.USERNAME_MASK = 0x80; 44 | module.exports.PASSWORD_MASK = 0x40; 45 | module.exports.WILL_RETAIN_MASK = 0x20; 46 | module.exports.WILL_QOS_MASK = 0x18; 47 | module.exports.WILL_QOS_SHIFT = 3; 48 | module.exports.WILL_FLAG_MASK = 0x04; 49 | module.exports.CLEAN_SESSION_MASK = 0x02; 50 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/server/broadcast.js: -------------------------------------------------------------------------------- 1 | /* broadcast.js - all published messages are relayed to all connected clients */ 2 | 3 | var mqtt = require('../..') 4 | , util = require('util'); 5 | 6 | mqtt.createServer(function(client) { 7 | var self = this; 8 | 9 | if (!self.clients) self.clients = {}; 10 | 11 | client.on('connect', function(packet) { 12 | client.connack({returnCode: 0}); 13 | client.id = packet.clientId; 14 | console.log("CONNECT: client id: " + client.id); 15 | self.clients[client.id] = client; 16 | }); 17 | 18 | client.on('publish', function(packet) { 19 | for (var k in self.clients) { 20 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 21 | } 22 | }); 23 | 24 | client.on('subscribe', function(packet) { 25 | var granted = []; 26 | for (var i = 0; i < packet.subscriptions.length; i++) { 27 | granted.push(packet.subscriptions[i].qos); 28 | } 29 | 30 | client.suback({granted: granted}); 31 | }); 32 | 33 | client.on('pingreq', function(packet) { 34 | client.pingresp(); 35 | }); 36 | 37 | client.on('disconnect', function(packet) { 38 | client.stream.end(); 39 | }); 40 | 41 | client.on('close', function(err) { 42 | delete self.clients[client.id]; 43 | }); 44 | 45 | client.on('error', function(err) { 46 | client.stream.end(); 47 | util.log('error!'); 48 | }); 49 | }).listen(process.argv[2] || 1883); 50 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/server/broadcast.js: -------------------------------------------------------------------------------- 1 | /* broadcast.js - all published messages are relayed to all connected clients */ 2 | 3 | var mqtt = require('../..') 4 | , util = require('util'); 5 | 6 | mqtt.createServer(function(client) { 7 | var self = this; 8 | 9 | if (!self.clients) self.clients = {}; 10 | 11 | client.on('connect', function(packet) { 12 | client.connack({returnCode: 0}); 13 | client.id = packet.clientId; 14 | console.log("CONNECT: client id: " + client.id); 15 | self.clients[client.id] = client; 16 | }); 17 | 18 | client.on('publish', function(packet) { 19 | for (var k in self.clients) { 20 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 21 | } 22 | }); 23 | 24 | client.on('subscribe', function(packet) { 25 | var granted = []; 26 | for (var i = 0; i < packet.subscriptions.length; i++) { 27 | granted.push(packet.subscriptions[i].qos); 28 | } 29 | 30 | client.suback({granted: granted}); 31 | }); 32 | 33 | client.on('pingreq', function(packet) { 34 | client.pingresp(); 35 | }); 36 | 37 | client.on('disconnect', function(packet) { 38 | client.stream.end(); 39 | }); 40 | 41 | client.on('close', function(err) { 42 | delete self.clients[client.id]; 43 | }); 44 | 45 | client.on('error', function(err) { 46 | client.stream.end(); 47 | util.log('error!'); 48 | }); 49 | }).listen(process.argv[2] || 1883); 50 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/server/tls.js: -------------------------------------------------------------------------------- 1 | /* broadcast.js - all published messages are relayed to all connected clients */ 2 | 3 | var mqtt = require('../..'); 4 | 5 | mqtt.createSecureServer("private-key.pem", "public-cert.pem", function(client) { 6 | var self = this; 7 | 8 | if (!self.clients) self.clients = {}; 9 | 10 | client.on('connect', function(packet) { 11 | client.connack({returnCode: 0}); 12 | client.id = packet.clientId; 13 | console.log("CONNECT: client id: " + client.id); 14 | self.clients[client.id] = client; 15 | }); 16 | 17 | client.on('publish', function(packet) { 18 | for (var k in self.clients) { 19 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 20 | } 21 | }); 22 | 23 | client.on('subscribe', function(packet) { 24 | var granted = []; 25 | for (var i = 0; i < packet.subscriptions.length; i++) { 26 | granted.push(packet.subscriptions[i].qos); 27 | } 28 | 29 | client.suback({granted: granted}); 30 | }); 31 | 32 | client.on('pingreq', function(packet) { 33 | client.pingresp(); 34 | }); 35 | 36 | client.on('disconnect', function(packet) { 37 | client.stream.end(); 38 | }); 39 | 40 | client.on('close', function(err) { 41 | delete self.clients[client.id]; 42 | }); 43 | 44 | client.on('error', function(err) { 45 | client.stream.end(); 46 | util.log('error!'); 47 | }); 48 | }).listen(process.argv[2] || 1883); 49 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Isaac Z. Schlueter ("Author") 2 | All rights reserved. 3 | 4 | The BSD License 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions 8 | are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in the 15 | documentation and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/server/tls.js: -------------------------------------------------------------------------------- 1 | /* broadcast.js - all published messages are relayed to all connected clients */ 2 | 3 | var mqtt = require('../..'); 4 | 5 | mqtt.createSecureServer("private-key.pem", "public-cert.pem", function(client) { 6 | var self = this; 7 | 8 | if (!self.clients) self.clients = {}; 9 | 10 | client.on('connect', function(packet) { 11 | client.connack({returnCode: 0}); 12 | client.id = packet.clientId; 13 | console.log("CONNECT: client id: " + client.id); 14 | self.clients[client.id] = client; 15 | }); 16 | 17 | client.on('publish', function(packet) { 18 | for (var k in self.clients) { 19 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 20 | } 21 | }); 22 | 23 | client.on('subscribe', function(packet) { 24 | var granted = []; 25 | for (var i = 0; i < packet.subscriptions.length; i++) { 26 | granted.push(packet.subscriptions[i].qos); 27 | } 28 | 29 | client.suback({granted: granted}); 30 | }); 31 | 32 | client.on('pingreq', function(packet) { 33 | client.pingresp(); 34 | }); 35 | 36 | client.on('disconnect', function(packet) { 37 | client.stream.end(); 38 | }); 39 | 40 | client.on('close', function(err) { 41 | delete self.clients[client.id]; 42 | }); 43 | 44 | client.on('error', function(err) { 45 | client.stream.end(); 46 | util.log('error!'); 47 | }); 48 | }).listen(process.argv[2] || 1883); 49 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Isaac Z. Schlueter ("Author") 2 | All rights reserved. 3 | 4 | The BSD License 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions 8 | are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | 13 | 2. Redistributions in binary form must reproduce the above copyright 14 | notice, this list of conditions and the following disclaimer in the 15 | documentation and/or other materials provided with the distribution. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 21 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/server/redis.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..') 2 | , redis = require('redis'); 3 | 4 | mqtt.createServer(function(err, client) { 5 | client.p = redis.createClient(null, null, {no_ready_check: true}); 6 | client.s = redis.createClient(null, null, {no_ready_check: true}); 7 | 8 | client.on('connect', function(packet) { 9 | client.connack({returnCode: 0}); 10 | }); 11 | 12 | client.on('subscribe', function(packet) { 13 | var granted = []; 14 | for (var i = 0; i < packet.subscriptions.length; i++) { 15 | var sub = packet.subscriptions[i] 16 | granted.push(sub.qos); 17 | 18 | client.s.psubscribe( 19 | sub.topic 20 | .replace(/\+/g, '[^/]') 21 | .replace(/\#/g, '*') 22 | ); 23 | } 24 | 25 | client.suback({messageId: packet.messageId, granted: granted}); 26 | }); 27 | client.on('publish', function(packet) { 28 | client.p.publish(packet.topic, packet.payload); 29 | }); 30 | client.on('pingreq', function(packet) { 31 | client.pingresp(); 32 | }); 33 | client.on('close', function() { 34 | client.p.end(); 35 | client.s.end(); 36 | }); 37 | client.s.on('pmessage', function(pattern, channel, message) { 38 | client.publish({topic: channel, payload: message}); 39 | }); 40 | client.on('disconnect', function(packet) { 41 | client.stream.end(); 42 | }); 43 | client.on('error', function(e) { 44 | client.stream.end(); 45 | console.log(e); 46 | }); 47 | }).listen(process.argv[2] || 1883); 48 | 49 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/server/redis.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..') 2 | , redis = require('redis'); 3 | 4 | mqtt.createServer(function(err, client) { 5 | client.p = redis.createClient(null, null, {no_ready_check: true}); 6 | client.s = redis.createClient(null, null, {no_ready_check: true}); 7 | 8 | client.on('connect', function(packet) { 9 | client.connack({returnCode: 0}); 10 | }); 11 | 12 | client.on('subscribe', function(packet) { 13 | var granted = []; 14 | for (var i = 0; i < packet.subscriptions.length; i++) { 15 | var sub = packet.subscriptions[i] 16 | granted.push(sub.qos); 17 | 18 | client.s.psubscribe( 19 | sub.topic 20 | .replace(/\+/g, '[^/]') 21 | .replace(/\#/g, '*') 22 | ); 23 | } 24 | 25 | client.suback({messageId: packet.messageId, granted: granted}); 26 | }); 27 | client.on('publish', function(packet) { 28 | client.p.publish(packet.topic, packet.payload); 29 | }); 30 | client.on('pingreq', function(packet) { 31 | client.pingresp(); 32 | }); 33 | client.on('close', function() { 34 | client.p.end(); 35 | client.s.end(); 36 | }); 37 | client.s.on('pmessage', function(pattern, channel, message) { 38 | client.publish({topic: channel, payload: message}); 39 | }); 40 | client.on('disconnect', function(packet) { 41 | client.stream.end(); 42 | }); 43 | client.on('error', function(e) { 44 | client.stream.end(); 45 | console.log(e); 46 | }); 47 | }).listen(process.argv[2] || 1883); 48 | 49 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/old/connection.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it, beforeEach */ 2 | 3 | var assert = require('assert'), 4 | should = require('should'); 5 | 6 | var protocol = require('../../lib/protocol'), 7 | Connection = require('../../lib/connection'), 8 | make_tester = require('./tester'); 9 | 10 | var tester = make_tester(); 11 | var connection = new Connection(tester); 12 | 13 | function make_protocol_type_case(type) { 14 | return it('#' + type, function () { 15 | var method = connection[type]; 16 | method.should.be.a('function'); 17 | }); 18 | } 19 | 20 | 21 | describe.skip("Connection", function () { 22 | 23 | describe("should have functions for", function () { 24 | var typenum; 25 | for (typenum in protocol.types) { 26 | if (protocol.types.hasOwnProperty(typenum)) { 27 | make_protocol_type_case.call(this, protocol.types[typenum]); 28 | } 29 | } 30 | }); 31 | 32 | 33 | describe("should emit on stream event", function () { 34 | beforeEach(function () { 35 | this.tester = make_tester(); 36 | this.connection = new Connection(this.tester); 37 | }); 38 | 39 | it("close", function (done) { 40 | this.connection.on('close', done); 41 | this.tester.do_emit('close'); 42 | }); 43 | 44 | it("error", function (done) { 45 | this.connection.on('error', function (err) { 46 | should.exist(err); 47 | err.should.be.instanceOf(Error); 48 | done(); 49 | }); 50 | this.tester.do_emit('error', new Error("fake test one")); 51 | }); 52 | 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/old/connection.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it, beforeEach */ 2 | 3 | var assert = require('assert'), 4 | should = require('should'); 5 | 6 | var protocol = require('../../lib/protocol'), 7 | Connection = require('../../lib/connection'), 8 | make_tester = require('./tester'); 9 | 10 | var tester = make_tester(); 11 | var connection = new Connection(tester); 12 | 13 | function make_protocol_type_case(type) { 14 | return it('#' + type, function () { 15 | var method = connection[type]; 16 | method.should.be.a('function'); 17 | }); 18 | } 19 | 20 | 21 | describe.skip("Connection", function () { 22 | 23 | describe("should have functions for", function () { 24 | var typenum; 25 | for (typenum in protocol.types) { 26 | if (protocol.types.hasOwnProperty(typenum)) { 27 | make_protocol_type_case.call(this, protocol.types[typenum]); 28 | } 29 | } 30 | }); 31 | 32 | 33 | describe("should emit on stream event", function () { 34 | beforeEach(function () { 35 | this.tester = make_tester(); 36 | this.connection = new Connection(this.tester); 37 | }); 38 | 39 | it("close", function (done) { 40 | this.connection.on('close', done); 41 | this.tester.do_emit('close'); 42 | }); 43 | 44 | it("error", function (done) { 45 | this.connection.on('error', function (err) { 46 | should.exist(err); 47 | err.should.be.instanceOf(Error); 48 | done(); 49 | }); 50 | this.tester.do_emit('error', new Error("fake test one")); 51 | }); 52 | 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/integration.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing requires 3 | */ 4 | 5 | var should = require('should'), 6 | child_process = require('child_process'); 7 | 8 | var exec = child_process.exec 9 | , spawn = child_process.spawn; 10 | 11 | /** 12 | * Units under test 13 | */ 14 | 15 | var mqtt = require('../lib/mqtt'); 16 | 17 | /** 18 | * Check if we have mosquitto stuff 19 | */ 20 | exec('mosquitto_sub', function (err) { 21 | if (/not found/.test(err.message)) { 22 | throw new Error('Tests require mosquitto and clients'); 23 | } 24 | }); 25 | 26 | /** 27 | * Tests 28 | */ 29 | 30 | describe.skip('MqttClient', function() { 31 | describe('subscribing', function() { 32 | it('should receive a message event', function(done) { 33 | var c = mqtt.createClient(); 34 | c.on('connect', function() { 35 | c.subscribe('topic'); 36 | c.on('message', function(topic, message) { 37 | topic.should.equal('topic'); 38 | message.should.equal('test'); 39 | done(); 40 | }); 41 | exec('mosquitto_pub -t topic -m test'); 42 | }); 43 | }); 44 | }); 45 | 46 | describe('publishing', function() { 47 | it('should receive payload text', function(done) { 48 | var c = mqtt.createClient() 49 | , sub = spawn('mosquitto_sub', ['-t', 'topic']); 50 | 51 | this.timeout(5000); 52 | sub.stdout.setEncoding('utf8'); 53 | 54 | sub.stdout.on('data', function(data) { 55 | if (/test/.test(data)) { 56 | done(); 57 | } 58 | }); 59 | 60 | c.on('connect', function() { 61 | c.publish('topic', 'test'); 62 | c.end(); 63 | }); 64 | }); 65 | }); 66 | }); 67 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/integration.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing requires 3 | */ 4 | 5 | var should = require('should'), 6 | child_process = require('child_process'); 7 | 8 | var exec = child_process.exec 9 | , spawn = child_process.spawn; 10 | 11 | /** 12 | * Units under test 13 | */ 14 | 15 | var mqtt = require('../lib/mqtt'); 16 | 17 | /** 18 | * Check if we have mosquitto stuff 19 | */ 20 | exec('mosquitto_sub', function (err) { 21 | if (/not found/.test(err.message)) { 22 | throw new Error('Tests require mosquitto and clients'); 23 | } 24 | }); 25 | 26 | /** 27 | * Tests 28 | */ 29 | 30 | describe.skip('MqttClient', function() { 31 | describe('subscribing', function() { 32 | it('should receive a message event', function(done) { 33 | var c = mqtt.createClient(); 34 | c.on('connect', function() { 35 | c.subscribe('topic'); 36 | c.on('message', function(topic, message) { 37 | topic.should.equal('topic'); 38 | message.should.equal('test'); 39 | done(); 40 | }); 41 | exec('mosquitto_pub -t topic -m test'); 42 | }); 43 | }); 44 | }); 45 | 46 | describe('publishing', function() { 47 | it('should receive payload text', function(done) { 48 | var c = mqtt.createClient() 49 | , sub = spawn('mosquitto_sub', ['-t', 'topic']); 50 | 51 | this.timeout(5000); 52 | sub.stdout.setEncoding('utf8'); 53 | 54 | sub.stdout.on('data', function(data) { 55 | if (/test/.test(data)) { 56 | done(); 57 | } 58 | }); 59 | 60 | c.on('connect', function() { 61 | c.publish('topic', 'test'); 62 | c.end(); 63 | }); 64 | }); 65 | }); 66 | }); 67 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/helpers/server.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../../lib/mqtt'); 2 | 3 | var KEY = __dirname + '/private-key.pem'; 4 | var CERT = __dirname + '/public-cert.pem'; 5 | 6 | module.exports.init_server = function (PORT) { 7 | var server = mqtt.createServer(function (client) { 8 | /*var i, events = ['connect', 'publish', 'pubrel', 'subscribe', 'disconnect']; 9 | 10 | for (i = 0; i < events.length; i++) { 11 | client.on(events[i], function (packet) { 12 | //console.dir(packet); 13 | }); 14 | } 15 | */ 16 | 17 | client.on('connect', function (packet) { 18 | client.connack(0); 19 | }); 20 | 21 | client.on('publish', function (packet) { 22 | switch (packet.qos) { 23 | case 1: 24 | client.puback({messageId: packet.messageId}); 25 | break; 26 | case 2: 27 | client.pubrec({messageId: packet.messageId}); 28 | break; 29 | default: 30 | //console.log('errors? QOS=', packet.qos); 31 | break; 32 | } 33 | 34 | }); 35 | 36 | client.on('pubrel', function (packet) { 37 | client.pubcomp({messageId: packet.messageId}); 38 | }); 39 | 40 | client.on('pingreq', function (packet) { 41 | client.pingresp(); 42 | }); 43 | 44 | client.on('disconnect', function (packet) { 45 | client.stream.end(); 46 | }); 47 | }); 48 | server.listen(PORT); 49 | return server; 50 | }; 51 | 52 | module.exports.init_secure_server = function (PORT) { 53 | var server = mqtt.createSecureServer(KEY, CERT, function (client) { 54 | client.on('connect', function (packet) { 55 | client.connack(0); 56 | }); 57 | }); 58 | server.listen(PORT); 59 | return server; 60 | }; 61 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/helpers/server.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../../lib/mqtt'); 2 | 3 | var KEY = __dirname + '/private-key.pem'; 4 | var CERT = __dirname + '/public-cert.pem'; 5 | 6 | module.exports.init_server = function (PORT) { 7 | var server = mqtt.createServer(function (client) { 8 | /*var i, events = ['connect', 'publish', 'pubrel', 'subscribe', 'disconnect']; 9 | 10 | for (i = 0; i < events.length; i++) { 11 | client.on(events[i], function (packet) { 12 | //console.dir(packet); 13 | }); 14 | } 15 | */ 16 | 17 | client.on('connect', function (packet) { 18 | client.connack(0); 19 | }); 20 | 21 | client.on('publish', function (packet) { 22 | switch (packet.qos) { 23 | case 1: 24 | client.puback({messageId: packet.messageId}); 25 | break; 26 | case 2: 27 | client.pubrec({messageId: packet.messageId}); 28 | break; 29 | default: 30 | //console.log('errors? QOS=', packet.qos); 31 | break; 32 | } 33 | 34 | }); 35 | 36 | client.on('pubrel', function (packet) { 37 | client.pubcomp({messageId: packet.messageId}); 38 | }); 39 | 40 | client.on('pingreq', function (packet) { 41 | client.pingresp(); 42 | }); 43 | 44 | client.on('disconnect', function (packet) { 45 | client.stream.end(); 46 | }); 47 | }); 48 | server.listen(PORT); 49 | return server; 50 | }; 51 | 52 | module.exports.init_secure_server = function (PORT) { 53 | var server = mqtt.createSecureServer(KEY, CERT, function (client) { 54 | client.on('connect', function (packet) { 55 | client.connack(0); 56 | }); 57 | }); 58 | server.listen(PORT); 59 | return server; 60 | }; 61 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/helpers/private-key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpAIBAAKCAQEA23Mhmap/uHdrE72bOjZWGIX8EXdjgT98NOQXLbs+CyxL+KkU 3 | 4x78jHIz+ED+j9tBvQFj6wfmzXTbSQ0fv26a1ptYxoOY6Lxu+Yda1UJxiGfeaFtE 4 | VZw5F1Od9Dy5FhdkZ6i6mOWF+sqRAKhIAUqHU7NTto3HgKMwCNJrinK2swBU0nft 5 | GvKGx8y4cB4bz7QSjeqZjJqmWm6u2vwDcdZXrlHZUULBnmEobwj1fYXdStgoevjG 6 | XE6CMPyPY59hnGkYcjph7EnbxpHTGWxZ/NL5MooVXHLye2A8YvRlcve+yzzSYBwG 7 | X0V+wnO5+fggc5iylfU4QzQzumUyZDD/I1P0HwIDAQABAoIBAQDNgNdqS5wnZs1D 8 | Qz/mF5QwiugugxsPoh/yd9as4LeNRwIt7ki9F/twmlHInTTGCpFZKcAkDNY6eMAR 9 | fNTKNA2UAw3zeLDs4ekai4KoSvx+vKYuG6m2cgGUsp0sZuD8qxM/b2auX+JDpQZ9 10 | Exm6+8wWucwfHE5DTI5i9In4sMweeuiEUYnndTzElkvnP/44h1fGSU1iGUKn/ftc 11 | P4X+3SU68KMT3kUsEBavtmSdyeG/lSFEjm73FwVIRZ+PfbQX2hDD+mmseAXGFKi1 12 | HudtQkEzTvYR+QAgvtjNgt/0qxFtPdj7Y+iRkCZQSJToAw8z6vwUn1qNCADauGMI 13 | X6KIm8XBAoGBAPiwMLYpIqp1rksINbqpbVqjtqsoejQuPYeEF7OXHbH9il7pWrQF 14 | wLbogo3YXX+a66RreVMhsUeq7+pIf/sK2lT73gDpFfvZnJG1ww94QkHBEPso0bN9 15 | pcGgceIK7KRRAiAl5Mjw6pZZNnIBxlIFaSbBqQau74NfdaalMBF2wi+3AoGBAOHm 16 | 3ttFtVjVlb2fHoiGNZCZDv3gnsQXZlCxS+rQ4XEmEWKHAH4T3+Kzmo8jWoX+DGGD 17 | 6UkxWHv7e+KrYIZDi7Dd2HFV0gHN6d1SNdPix3vN114bNOrbfqxuEVT5PdFHSuel 18 | 5d3ix+3U+tpHamwb88eyeq6Q3t5Lcl3gIRGLzo7ZAoGBAKVuLzk+K/1Qw1zOXU+K 19 | nWAKP92j04caq3uWd13UTMC2dHGmsdvHZ+dEzHQnVisol1CM3exbIV8XavliuR/6 20 | nDqkQY5Bf4pFvE2Bp/yGdyzejblF8hmAn98qKBfCRKEZ8lwIWSUCfkr9laZJX+/4 21 | AXbypMn5XQL7YXw1rsAvTAYJAoGAV4ZL8kkf6jtWuRFdkyfsuQmUdWkCGpe2XK1U 22 | 7LXhoyVMtw/3cOHibMOJrsvT1vaHdYDWcjVcQy084qXj0CF7jhtmMQM/StOtOMMR 23 | d/b1s1Idj6ia6CQDAGvk6zdmbB9jNj1gwoeLTuqmBsyEvz5VRZoxTlFzCE3TEew0 24 | 48d3UIECgYBMxnLByVQA3pQWWIZZyqt+HgJAphYPdpnPalblQAbuCksKTZ/QKDkW 25 | dzih1PQROVrYrX7VwJ3/I8gXIuvKVtN1NKOS3a0JtbJQhpH4YbRwyQskXWYP8oYa 26 | MjBGPymNDhZh0zoGWzst5uR3NpdNV+7yNYPvyxzVNjlPjtAUqIxjBg== 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/helpers/private-key.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpAIBAAKCAQEA23Mhmap/uHdrE72bOjZWGIX8EXdjgT98NOQXLbs+CyxL+KkU 3 | 4x78jHIz+ED+j9tBvQFj6wfmzXTbSQ0fv26a1ptYxoOY6Lxu+Yda1UJxiGfeaFtE 4 | VZw5F1Od9Dy5FhdkZ6i6mOWF+sqRAKhIAUqHU7NTto3HgKMwCNJrinK2swBU0nft 5 | GvKGx8y4cB4bz7QSjeqZjJqmWm6u2vwDcdZXrlHZUULBnmEobwj1fYXdStgoevjG 6 | XE6CMPyPY59hnGkYcjph7EnbxpHTGWxZ/NL5MooVXHLye2A8YvRlcve+yzzSYBwG 7 | X0V+wnO5+fggc5iylfU4QzQzumUyZDD/I1P0HwIDAQABAoIBAQDNgNdqS5wnZs1D 8 | Qz/mF5QwiugugxsPoh/yd9as4LeNRwIt7ki9F/twmlHInTTGCpFZKcAkDNY6eMAR 9 | fNTKNA2UAw3zeLDs4ekai4KoSvx+vKYuG6m2cgGUsp0sZuD8qxM/b2auX+JDpQZ9 10 | Exm6+8wWucwfHE5DTI5i9In4sMweeuiEUYnndTzElkvnP/44h1fGSU1iGUKn/ftc 11 | P4X+3SU68KMT3kUsEBavtmSdyeG/lSFEjm73FwVIRZ+PfbQX2hDD+mmseAXGFKi1 12 | HudtQkEzTvYR+QAgvtjNgt/0qxFtPdj7Y+iRkCZQSJToAw8z6vwUn1qNCADauGMI 13 | X6KIm8XBAoGBAPiwMLYpIqp1rksINbqpbVqjtqsoejQuPYeEF7OXHbH9il7pWrQF 14 | wLbogo3YXX+a66RreVMhsUeq7+pIf/sK2lT73gDpFfvZnJG1ww94QkHBEPso0bN9 15 | pcGgceIK7KRRAiAl5Mjw6pZZNnIBxlIFaSbBqQau74NfdaalMBF2wi+3AoGBAOHm 16 | 3ttFtVjVlb2fHoiGNZCZDv3gnsQXZlCxS+rQ4XEmEWKHAH4T3+Kzmo8jWoX+DGGD 17 | 6UkxWHv7e+KrYIZDi7Dd2HFV0gHN6d1SNdPix3vN114bNOrbfqxuEVT5PdFHSuel 18 | 5d3ix+3U+tpHamwb88eyeq6Q3t5Lcl3gIRGLzo7ZAoGBAKVuLzk+K/1Qw1zOXU+K 19 | nWAKP92j04caq3uWd13UTMC2dHGmsdvHZ+dEzHQnVisol1CM3exbIV8XavliuR/6 20 | nDqkQY5Bf4pFvE2Bp/yGdyzejblF8hmAn98qKBfCRKEZ8lwIWSUCfkr9laZJX+/4 21 | AXbypMn5XQL7YXw1rsAvTAYJAoGAV4ZL8kkf6jtWuRFdkyfsuQmUdWkCGpe2XK1U 22 | 7LXhoyVMtw/3cOHibMOJrsvT1vaHdYDWcjVcQy084qXj0CF7jhtmMQM/StOtOMMR 23 | d/b1s1Idj6ia6CQDAGvk6zdmbB9jNj1gwoeLTuqmBsyEvz5VRZoxTlFzCE3TEew0 24 | 48d3UIECgYBMxnLByVQA3pQWWIZZyqt+HgJAphYPdpnPalblQAbuCksKTZ/QKDkW 25 | dzih1PQROVrYrX7VwJ3/I8gXIuvKVtN1NKOS3a0JtbJQhpH4YbRwyQskXWYP8oYa 26 | MjBGPymNDhZh0zoGWzst5uR3NpdNV+7yNYPvyxzVNjlPjtAUqIxjBg== 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/lib/_stream_passthrough.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | // a passthrough stream. 23 | // basically just the most minimal sort of Transform stream. 24 | // Every written chunk gets output as-is. 25 | 26 | module.exports = PassThrough; 27 | 28 | var Transform = require('./_stream_transform'); 29 | var util = require('util'); 30 | util.inherits(PassThrough, Transform); 31 | 32 | function PassThrough(options) { 33 | if (!(this instanceof PassThrough)) 34 | return new PassThrough(options); 35 | 36 | Transform.call(this, options); 37 | } 38 | 39 | PassThrough.prototype._transform = function(chunk, encoding, cb) { 40 | cb(null, chunk); 41 | }; 42 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/lib/_stream_passthrough.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | // a passthrough stream. 23 | // basically just the most minimal sort of Transform stream. 24 | // Every written chunk gets output as-is. 25 | 26 | module.exports = PassThrough; 27 | 28 | var Transform = require('./_stream_transform'); 29 | var util = require('util'); 30 | util.inherits(PassThrough, Transform); 31 | 32 | function PassThrough(options) { 33 | if (!(this instanceof PassThrough)) 34 | return new PassThrough(options); 35 | 36 | Transform.call(this, options); 37 | } 38 | 39 | PassThrough.prototype._transform = function(chunk, encoding, cb) { 40 | cb(null, chunk); 41 | }; 42 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-finish-pipe.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common.js'); 23 | var stream = require('../../readable'); 24 | var Buffer = require('buffer').Buffer; 25 | 26 | var r = new stream.Readable(); 27 | r._read = function(size) { 28 | r.push(new Buffer(size)); 29 | }; 30 | 31 | var w = new stream.Writable(); 32 | w._write = function(data, encoding, cb) { 33 | cb(null); 34 | }; 35 | 36 | r.pipe(w); 37 | 38 | // This might sound unrealistic, but it happens in net.js. When 39 | // `socket.allowHalfOpen === false`, EOF will cause `.destroySoon()` call which 40 | // ends the writable side of net.Socket. 41 | w.end(); 42 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-finish-pipe.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common.js'); 23 | var stream = require('../../readable'); 24 | var Buffer = require('buffer').Buffer; 25 | 26 | var r = new stream.Readable(); 27 | r._read = function(size) { 28 | r.push(new Buffer(size)); 29 | }; 30 | 31 | var w = new stream.Writable(); 32 | w._write = function(data, encoding, cb) { 33 | cb(null); 34 | }; 35 | 36 | r.pipe(w); 37 | 38 | // This might sound unrealistic, but it happens in net.js. When 39 | // `socket.allowHalfOpen === false`, EOF will cause `.destroySoon()` call which 40 | // ends the writable side of net.Socket. 41 | w.end(); 42 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/old/qos.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it, before, after */ 2 | 3 | var assert = require('assert'), 4 | should = require('should'); 5 | 6 | var servers = require('./helpers/server'); 7 | 8 | var mqtt = require('../lib/mqtt'), 9 | HOST = 'localhost', 10 | PORT = (process.env.PORT || 1883) + 2; 11 | 12 | 13 | 14 | 15 | describe.skip("Client-Server QOS Test", function () { 16 | before(function (done) { 17 | var self = this; 18 | this.server = servers.init_server(PORT); 19 | mqtt.createClient(PORT, HOST, function (err, client) { 20 | should.not.exist(err); 21 | self.client = client; 22 | done(); 23 | }); 24 | }); 25 | 26 | describe("Client", function () { 27 | it("Client should connect", function (done) { 28 | this.client.connect({keepalive: 1000}); 29 | this.client.on('connack', function (packet) { 30 | done(); 31 | }); 32 | }); 33 | 34 | it("qos 0", function (done) { 35 | this.client.publish({ 36 | topic: 'test0', 37 | payload: 'test', 38 | qos: 0 39 | }); 40 | done(); 41 | }); 42 | 43 | 44 | it("qos 1", function (done) { 45 | this.client.once('puback', function (packet) { 46 | should.exist(packet); 47 | packet.should.have.property('messageId'); 48 | done(); 49 | }); 50 | this.client.publish({ 51 | topic: 'test1', 52 | payload: 'test', 53 | qos: 1 54 | }); 55 | }); 56 | 57 | it("qos 2", function (done) { 58 | this.client.once('pubrec', function (packet) { 59 | should.exist(packet); 60 | packet.should.have.property('messageId'); 61 | done(); 62 | }); 63 | this.client.publish({ 64 | topic: 'test2', 65 | payload: 'test', 66 | qos: 2 67 | }); 68 | }); 69 | });//describe client 70 | }); 71 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/old/qos.test.js: -------------------------------------------------------------------------------- 1 | /*global describe, it, before, after */ 2 | 3 | var assert = require('assert'), 4 | should = require('should'); 5 | 6 | var servers = require('./helpers/server'); 7 | 8 | var mqtt = require('../lib/mqtt'), 9 | HOST = 'localhost', 10 | PORT = (process.env.PORT || 1883) + 2; 11 | 12 | 13 | 14 | 15 | describe.skip("Client-Server QOS Test", function () { 16 | before(function (done) { 17 | var self = this; 18 | this.server = servers.init_server(PORT); 19 | mqtt.createClient(PORT, HOST, function (err, client) { 20 | should.not.exist(err); 21 | self.client = client; 22 | done(); 23 | }); 24 | }); 25 | 26 | describe("Client", function () { 27 | it("Client should connect", function (done) { 28 | this.client.connect({keepalive: 1000}); 29 | this.client.on('connack', function (packet) { 30 | done(); 31 | }); 32 | }); 33 | 34 | it("qos 0", function (done) { 35 | this.client.publish({ 36 | topic: 'test0', 37 | payload: 'test', 38 | qos: 0 39 | }); 40 | done(); 41 | }); 42 | 43 | 44 | it("qos 1", function (done) { 45 | this.client.once('puback', function (packet) { 46 | should.exist(packet); 47 | packet.should.have.property('messageId'); 48 | done(); 49 | }); 50 | this.client.publish({ 51 | topic: 'test1', 52 | payload: 'test', 53 | qos: 1 54 | }); 55 | }); 56 | 57 | it("qos 2", function (done) { 58 | this.client.once('pubrec', function (packet) { 59 | should.exist(packet); 60 | packet.should.have.property('messageId'); 61 | done(); 62 | }); 63 | this.client.publish({ 64 | topic: 'test2', 65 | payload: 'test', 66 | qos: 2 67 | }); 68 | }); 69 | });//describe client 70 | }); 71 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-compatibility.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | var common = require('../common.js'); 24 | var R = require('../../lib/_stream_readable'); 25 | var assert = require('assert'); 26 | 27 | var util = require('util'); 28 | var EE = require('events').EventEmitter; 29 | 30 | var ondataCalled = 0; 31 | 32 | function TestReader() { 33 | R.apply(this); 34 | this._buffer = new Buffer(100); 35 | this._buffer.fill('x'); 36 | 37 | this.on('data', function() { 38 | ondataCalled++; 39 | }); 40 | } 41 | 42 | util.inherits(TestReader, R); 43 | 44 | TestReader.prototype._read = function(n) { 45 | this.push(this._buffer); 46 | this._buffer = new Buffer(0); 47 | }; 48 | 49 | var reader = new TestReader(); 50 | assert.equal(ondataCalled, 1); 51 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/examples/server/orig.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..') 2 | , util = require('util'); 3 | 4 | mqtt.createServer(function(client) { 5 | var self = this; 6 | 7 | if (!self.clients) self.clients = {}; 8 | 9 | client.on('connect', function(packet) { 10 | self.clients[packet.client] = client; 11 | client.id = packet.clientId; 12 | console.log("CONNECT: client id: " + client.id); 13 | client.subscriptions = []; 14 | client.connack({returnCode: 0}); 15 | }); 16 | 17 | client.on('subscribe', function(packet) { 18 | var granted = []; 19 | 20 | for (var i = 0; i < packet.subscriptions.length; i++) { 21 | var qos = packet.subscriptions[i].qos 22 | , topic = packet.subscriptions[i].topic 23 | , reg = new RegExp(topic.replace('+', '[^\/]+').replace('#', '.+$')); 24 | 25 | granted.push(qos); 26 | client.subscriptions.push(reg); 27 | } 28 | 29 | client.suback({messageId: packet.messageId, granted: granted}); 30 | }); 31 | 32 | client.on('publish', function(packet) { 33 | for (var k in self.clients) { 34 | var c = self.clients[k] 35 | , publish = false; 36 | 37 | for (var i = 0; i < c.subscriptions.length; i++) { 38 | var s = c.subscriptions[i]; 39 | 40 | if (s.test(packet.topic)) { 41 | publish = true; 42 | } 43 | } 44 | 45 | if (publish) { 46 | c.publish({topic: packet.topic, payload: packet.payload}); 47 | } 48 | } 49 | }); 50 | 51 | client.on('pingreq', function(packet) { 52 | console.log('Ping from client ' + client.id); 53 | client.pingresp(); 54 | }); 55 | 56 | client.on('disconnect', function(packet) { 57 | client.stream.end(); 58 | }); 59 | 60 | client.on('close', function(packet) { 61 | delete self.clients[client.id]; 62 | }); 63 | 64 | client.on('error', function(e) { 65 | client.stream.end(); 66 | console.log(e); 67 | }); 68 | }).listen(process.argv[2] || 1883); 69 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/examples/server/orig.js: -------------------------------------------------------------------------------- 1 | var mqtt = require('../..') 2 | , util = require('util'); 3 | 4 | mqtt.createServer(function(client) { 5 | var self = this; 6 | 7 | if (!self.clients) self.clients = {}; 8 | 9 | client.on('connect', function(packet) { 10 | self.clients[packet.client] = client; 11 | client.id = packet.clientId; 12 | console.log("CONNECT: client id: " + client.id); 13 | client.subscriptions = []; 14 | client.connack({returnCode: 0}); 15 | }); 16 | 17 | client.on('subscribe', function(packet) { 18 | var granted = []; 19 | 20 | for (var i = 0; i < packet.subscriptions.length; i++) { 21 | var qos = packet.subscriptions[i].qos 22 | , topic = packet.subscriptions[i].topic 23 | , reg = new RegExp(topic.replace('+', '[^\/]+').replace('#', '.+$')); 24 | 25 | granted.push(qos); 26 | client.subscriptions.push(reg); 27 | } 28 | 29 | client.suback({messageId: packet.messageId, granted: granted}); 30 | }); 31 | 32 | client.on('publish', function(packet) { 33 | for (var k in self.clients) { 34 | var c = self.clients[k] 35 | , publish = false; 36 | 37 | for (var i = 0; i < c.subscriptions.length; i++) { 38 | var s = c.subscriptions[i]; 39 | 40 | if (s.test(packet.topic)) { 41 | publish = true; 42 | } 43 | } 44 | 45 | if (publish) { 46 | c.publish({topic: packet.topic, payload: packet.payload}); 47 | } 48 | } 49 | }); 50 | 51 | client.on('pingreq', function(packet) { 52 | console.log('Ping from client ' + client.id); 53 | client.pingresp(); 54 | }); 55 | 56 | client.on('disconnect', function(packet) { 57 | client.stream.end(); 58 | }); 59 | 60 | client.on('close', function(packet) { 61 | delete self.clients[client.id]; 62 | }); 63 | 64 | client.on('error', function(e) { 65 | client.stream.end(); 66 | console.log(e); 67 | }); 68 | }).listen(process.argv[2] || 1883); 69 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-compatibility.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | var common = require('../common.js'); 24 | var R = require('../../lib/_stream_readable'); 25 | var assert = require('assert'); 26 | 27 | var util = require('util'); 28 | var EE = require('events').EventEmitter; 29 | 30 | var ondataCalled = 0; 31 | 32 | function TestReader() { 33 | R.apply(this); 34 | this._buffer = new Buffer(100); 35 | this._buffer.fill('x'); 36 | 37 | this.on('data', function() { 38 | ondataCalled++; 39 | }); 40 | } 41 | 42 | util.inherits(TestReader, R); 43 | 44 | TestReader.prototype._read = function(n) { 45 | this.push(this._buffer); 46 | this._buffer = new Buffer(0); 47 | }; 48 | 49 | var reader = new TestReader(); 50 | assert.equal(ondataCalled, 1); 51 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/lib/server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Requires 3 | */ 4 | 5 | var fs = require('fs') 6 | , net = require('net') 7 | , tls = require('tls') 8 | , util = require('util') 9 | , Connection = require('./connection'); 10 | 11 | /** 12 | * MqttServer 13 | * 14 | * @param listener 15 | */ 16 | var MqttServer = module.exports.MqttServer = 17 | function Server(listener) { 18 | if (!(this instanceof Server)) return new Server(listener); 19 | 20 | var self = this; 21 | 22 | net.Server.call(self); 23 | 24 | if (listener) { 25 | self.on('client', listener); 26 | } 27 | 28 | self.on('connection', function(socket) { 29 | self.emit('client', new MqttServerClient(socket, self)); 30 | }); 31 | 32 | return this; 33 | } 34 | util.inherits(MqttServer, net.Server); 35 | 36 | /** 37 | * MqttSecureServer 38 | * 39 | * @param privateKeyPath 40 | * @param publicCertPath 41 | * @param listener 42 | */ 43 | var MqttSecureServer = module.exports.MqttSecureServer = 44 | function SecureServer(keyPath, certPath, listener) { 45 | if (!(this instanceof SecureServer)) { 46 | return new SecureServer(listener); 47 | } 48 | var self = this; 49 | 50 | tls.Server.call(self, { 51 | key: fs.readFileSync(keyPath), 52 | cert: fs.readFileSync(certPath) 53 | }); 54 | 55 | if (listener) { 56 | self.on('client', listener); 57 | } 58 | 59 | self.on('secureConnection', function(clearTextStream) { 60 | self.emit('client', 61 | new MqttServerClient(clearTextStream, self)); 62 | }); 63 | 64 | return this; 65 | } 66 | util.inherits(MqttSecureServer, tls.Server); 67 | 68 | /** 69 | * MqttServerClient - wrapper around Connection 70 | * Exists if we want to extend server functionality later 71 | * 72 | * @param stream 73 | * @param server 74 | */ 75 | 76 | var MqttServerClient = module.exports.MqttServerClient = 77 | function MqttServerClient(stream, server) { 78 | Connection.call(this, stream, server); 79 | this.stream.on('error', this.emit.bind(this, 'error')); 80 | }; 81 | util.inherits(MqttServerClient, Connection); 82 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/lib/server.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Requires 3 | */ 4 | 5 | var fs = require('fs') 6 | , net = require('net') 7 | , tls = require('tls') 8 | , util = require('util') 9 | , Connection = require('./connection'); 10 | 11 | /** 12 | * MqttServer 13 | * 14 | * @param listener 15 | */ 16 | var MqttServer = module.exports.MqttServer = 17 | function Server(listener) { 18 | if (!(this instanceof Server)) return new Server(listener); 19 | 20 | var self = this; 21 | 22 | net.Server.call(self); 23 | 24 | if (listener) { 25 | self.on('client', listener); 26 | } 27 | 28 | self.on('connection', function(socket) { 29 | self.emit('client', new MqttServerClient(socket, self)); 30 | }); 31 | 32 | return this; 33 | } 34 | util.inherits(MqttServer, net.Server); 35 | 36 | /** 37 | * MqttSecureServer 38 | * 39 | * @param privateKeyPath 40 | * @param publicCertPath 41 | * @param listener 42 | */ 43 | var MqttSecureServer = module.exports.MqttSecureServer = 44 | function SecureServer(keyPath, certPath, listener) { 45 | if (!(this instanceof SecureServer)) { 46 | return new SecureServer(listener); 47 | } 48 | var self = this; 49 | 50 | tls.Server.call(self, { 51 | key: fs.readFileSync(keyPath), 52 | cert: fs.readFileSync(certPath) 53 | }); 54 | 55 | if (listener) { 56 | self.on('client', listener); 57 | } 58 | 59 | self.on('secureConnection', function(clearTextStream) { 60 | self.emit('client', 61 | new MqttServerClient(clearTextStream, self)); 62 | }); 63 | 64 | return this; 65 | } 66 | util.inherits(MqttSecureServer, tls.Server); 67 | 68 | /** 69 | * MqttServerClient - wrapper around Connection 70 | * Exists if we want to extend server functionality later 71 | * 72 | * @param stream 73 | * @param server 74 | */ 75 | 76 | var MqttServerClient = module.exports.MqttServerClient = 77 | function MqttServerClient(stream, server) { 78 | Connection.call(this, stream, server); 79 | this.stream.on('error', this.emit.bind(this, 'error')); 80 | }; 81 | util.inherits(MqttServerClient, Connection); 82 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-read-sync-stack.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | var Readable = require('../../readable').Readable; 25 | var r = new Readable(); 26 | var N = 256 * 1024; 27 | 28 | // Go ahead and allow the pathological case for this test. 29 | // Yes, it's an infinite loop, that's the point. 30 | process.maxTickDepth = N + 2; 31 | 32 | var reads = 0; 33 | r._read = function(n) { 34 | var chunk = reads++ === N ? null : new Buffer(1); 35 | r.push(chunk); 36 | }; 37 | 38 | r.on('readable', function onReadable() { 39 | if (!(r._readableState.length % 256)) 40 | console.error('readable', r._readableState.length); 41 | r.read(N * 2); 42 | }); 43 | 44 | var ended = false; 45 | r.on('end', function onEnd() { 46 | ended = true; 47 | }); 48 | 49 | r.read(0); 50 | 51 | process.on('exit', function() { 52 | assert(ended); 53 | console.log('ok'); 54 | }); 55 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-read-sync-stack.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | var Readable = require('../../readable').Readable; 25 | var r = new Readable(); 26 | var N = 256 * 1024; 27 | 28 | // Go ahead and allow the pathological case for this test. 29 | // Yes, it's an infinite loop, that's the point. 30 | process.maxTickDepth = N + 2; 31 | 32 | var reads = 0; 33 | r._read = function(n) { 34 | var chunk = reads++ === N ? null : new Buffer(1); 35 | r.push(chunk); 36 | }; 37 | 38 | r.on('readable', function onReadable() { 39 | if (!(r._readableState.length % 256)) 40 | console.error('readable', r._readableState.length); 41 | r.read(N * 2); 42 | }); 43 | 44 | var ended = false; 45 | r.on('end', function onEnd() { 46 | ended = true; 47 | }); 48 | 49 | r.read(0); 50 | 51 | process.on('exit', function() { 52 | assert(ended); 53 | console.log('ok'); 54 | }); 55 | -------------------------------------------------------------------------------- /LessonOne/server/mqttServer.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created with JetBrains WebStorm. 3 | * User: youxiachai 4 | * Date: 13-4-25 5 | * Time: 上午1:58 6 | * To change this template use File | Settings | File Templates. 7 | */ 8 | var mqtt = require('mqtt') 9 | , util = require('util'); 10 | 11 | mqtt.createServer(function(client) { 12 | var self = this; 13 | //客户端集合 14 | if (!self.clients) self.clients = {}; 15 | 16 | //监听连接请求 17 | client.on('connect', function(packet) { 18 | client.connack({returnCode: 0}); 19 | client.id = packet.clientId; 20 | client.subscriptions = []; 21 | console.log("CONNECT: client id: " + client.id + "packet" + JSON.stringify(packet)); 22 | self.clients[client.id] = client; 23 | 24 | }); 25 | 26 | client.on('publish', function(packet) { 27 | //发布者本身不接受发布信息 28 | delete self.clients[client.id]; 29 | //对所有连接进行一次广播 30 | for (var k in self.clients) { 31 | console.log("publish ->"+k); 32 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 33 | } 34 | }); 35 | 36 | client.on('subscribe', function(packet) { 37 | var granted = []; 38 | for (var i = 0; i < packet.subscriptions.length; i++) { 39 | //消息Qos 级别 40 | granted.push(packet.subscriptions[i].qos); 41 | 42 | } 43 | 44 | console.log('subscribe->' + JSON.stringify({granted: granted}) + JSON.stringify(packet)); 45 | client.suback({granted:granted,messageId:packet.messageId}); 46 | 47 | }); 48 | 49 | client.on('pingreq', function(packet) { 50 | util.log('pingreq!'+JSON.stringify(packet)); 51 | client.pingresp(); 52 | }); 53 | 54 | client.on('unsubscribe', function(packet){ 55 | console.log('unsubscribe!' + JSON.stringify(packet)); 56 | client.unsubscribe(); 57 | }); 58 | 59 | client.on('disconnect', function(packet) { 60 | delete self.clients[client.id]; 61 | util.log('disconnect!'); 62 | client.stream.end(); 63 | }); 64 | 65 | client.on('close', function(err) { 66 | delete self.clients[client.id]; 67 | }); 68 | 69 | client.on('error', function(err) { 70 | delete self.clients[client.id]; 71 | client.stream.end(); 72 | console.log(err); 73 | }); 74 | }).listen(1883); -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-legacy-drain.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | 25 | var Stream = require('../../readable'); 26 | var Readable = Stream.Readable; 27 | 28 | var r = new Readable(); 29 | var N = 256; 30 | var reads = 0; 31 | r._read = function(n) { 32 | return r.push(++reads === N ? null : new Buffer(1)); 33 | }; 34 | 35 | var rended = false; 36 | r.on('end', function() { 37 | rended = true; 38 | }); 39 | 40 | var w = new Stream(); 41 | w.writable = true; 42 | var writes = 0; 43 | var buffered = 0; 44 | w.write = function(c) { 45 | writes += c.length; 46 | buffered += c.length; 47 | process.nextTick(drain); 48 | return false; 49 | }; 50 | 51 | function drain() { 52 | assert(buffered <= 2); 53 | buffered = 0; 54 | w.emit('drain'); 55 | } 56 | 57 | 58 | var wended = false; 59 | w.end = function() { 60 | wended = true; 61 | }; 62 | 63 | // Just for kicks, let's mess with the drain count. 64 | // This verifies that even if it gets negative in the 65 | // pipe() cleanup function, we'll still function properly. 66 | r.on('readable', function() { 67 | w.emit('drain'); 68 | }); 69 | 70 | r.pipe(w); 71 | process.on('exit', function() { 72 | assert(rended); 73 | assert(wended); 74 | console.error('ok'); 75 | }); 76 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/test/mqtt.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing includes 3 | */ 4 | 5 | var should = require('should') 6 | , net = require('net'); 7 | 8 | /** 9 | * Unit under test 10 | */ 11 | 12 | var mqtt = require('../lib/mqtt'); 13 | 14 | describe('mqtt', function() { 15 | describe('#createClient', function() { 16 | it('should return an MqttClient', function() { 17 | var c = mqtt.createClient(); 18 | 19 | c.should.be.instanceOf(mqtt.MqttClient); 20 | }); 21 | }); 22 | 23 | describe('#createSecureClient', function() { 24 | it('should return an MqttClient', function() { 25 | var c = mqtt.createClient(); 26 | 27 | c.should.be.instanceOf(mqtt.MqttClient); 28 | }); 29 | it('should throw on incorrect args'); 30 | }); 31 | 32 | describe('#createServer', function() { 33 | it('should return an MqttServer', function() { 34 | var s = mqtt.createServer(); 35 | 36 | s.should.be.instanceOf(mqtt.MqttServer); 37 | }); 38 | }); 39 | 40 | describe('#createSecureServer', function() { 41 | it('should return an MqttSecureServer', function() { 42 | var s = mqtt.createSecureServer( 43 | __dirname + '/helpers/private-key.pem', 44 | __dirname + '/helpers/public-cert.pem' 45 | ); 46 | s.should.be.instanceOf(mqtt.MqttSecureServer); 47 | }); 48 | }); 49 | 50 | describe('#createConnection', function() { 51 | before(function () { 52 | // Setup dummy server 53 | 54 | // If there's an error it's probably EADDRINUSE 55 | // Just use whatever's there already (likely mosquitto) 56 | this.server = new net.Server(); 57 | this.server.listen(1883); 58 | this.server.on('error', function(){}); 59 | }); 60 | it('should return an MqttConnection', function() { 61 | var c = mqtt.createConnection(); 62 | 63 | c.should.be.instanceOf(mqtt.MqttConnection); 64 | }); 65 | 66 | it('should fire callback on net connect', function(done) { 67 | mqtt.createConnection(done); 68 | }); 69 | it('should bind stream close to connection', function(done) { 70 | var c = mqtt.createConnection(); 71 | c.once('connected', function() { 72 | c.once('close', function() { done() }); 73 | c.stream.end(); 74 | }); 75 | }); 76 | it('should bind stream error to conn', function(done) { 77 | var c = mqtt.createConnection(); 78 | c.once('error', function() { done() }); 79 | c.stream.emit('error', new Error('Bad idea!')); 80 | }); 81 | }); 82 | }); 83 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-legacy-drain.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | 25 | var Stream = require('../../readable'); 26 | var Readable = Stream.Readable; 27 | 28 | var r = new Readable(); 29 | var N = 256; 30 | var reads = 0; 31 | r._read = function(n) { 32 | return r.push(++reads === N ? null : new Buffer(1)); 33 | }; 34 | 35 | var rended = false; 36 | r.on('end', function() { 37 | rended = true; 38 | }); 39 | 40 | var w = new Stream(); 41 | w.writable = true; 42 | var writes = 0; 43 | var buffered = 0; 44 | w.write = function(c) { 45 | writes += c.length; 46 | buffered += c.length; 47 | process.nextTick(drain); 48 | return false; 49 | }; 50 | 51 | function drain() { 52 | assert(buffered <= 2); 53 | buffered = 0; 54 | w.emit('drain'); 55 | } 56 | 57 | 58 | var wended = false; 59 | w.end = function() { 60 | wended = true; 61 | }; 62 | 63 | // Just for kicks, let's mess with the drain count. 64 | // This verifies that even if it gets negative in the 65 | // pipe() cleanup function, we'll still function properly. 66 | r.on('readable', function() { 67 | w.emit('drain'); 68 | }); 69 | 70 | r.pipe(w); 71 | process.on('exit', function() { 72 | assert(rended); 73 | assert(wended); 74 | console.error('ok'); 75 | }); 76 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/test/mqtt.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing includes 3 | */ 4 | 5 | var should = require('should') 6 | , net = require('net'); 7 | 8 | /** 9 | * Unit under test 10 | */ 11 | 12 | var mqtt = require('../lib/mqtt'); 13 | 14 | describe('mqtt', function() { 15 | describe('#createClient', function() { 16 | it('should return an MqttClient', function() { 17 | var c = mqtt.createClient(); 18 | 19 | c.should.be.instanceOf(mqtt.MqttClient); 20 | }); 21 | }); 22 | 23 | describe('#createSecureClient', function() { 24 | it('should return an MqttClient', function() { 25 | var c = mqtt.createClient(); 26 | 27 | c.should.be.instanceOf(mqtt.MqttClient); 28 | }); 29 | it('should throw on incorrect args'); 30 | }); 31 | 32 | describe('#createServer', function() { 33 | it('should return an MqttServer', function() { 34 | var s = mqtt.createServer(); 35 | 36 | s.should.be.instanceOf(mqtt.MqttServer); 37 | }); 38 | }); 39 | 40 | describe('#createSecureServer', function() { 41 | it('should return an MqttSecureServer', function() { 42 | var s = mqtt.createSecureServer( 43 | __dirname + '/helpers/private-key.pem', 44 | __dirname + '/helpers/public-cert.pem' 45 | ); 46 | s.should.be.instanceOf(mqtt.MqttSecureServer); 47 | }); 48 | }); 49 | 50 | describe('#createConnection', function() { 51 | before(function () { 52 | // Setup dummy server 53 | 54 | // If there's an error it's probably EADDRINUSE 55 | // Just use whatever's there already (likely mosquitto) 56 | this.server = new net.Server(); 57 | this.server.listen(1883); 58 | this.server.on('error', function(){}); 59 | }); 60 | it('should return an MqttConnection', function() { 61 | var c = mqtt.createConnection(); 62 | 63 | c.should.be.instanceOf(mqtt.MqttConnection); 64 | }); 65 | 66 | it('should fire callback on net connect', function(done) { 67 | mqtt.createConnection(done); 68 | }); 69 | it('should bind stream close to connection', function(done) { 70 | var c = mqtt.createConnection(); 71 | c.once('connected', function() { 72 | c.once('close', function() { done() }); 73 | c.stream.end(); 74 | }); 75 | }); 76 | it('should bind stream error to conn', function(done) { 77 | var c = mqtt.createConnection(); 78 | c.once('error', function() { done() }); 79 | c.stream.emit('error', new Error('Bad idea!')); 80 | }); 81 | }); 82 | }); 83 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/README.md: -------------------------------------------------------------------------------- 1 | # mqtt.js 2 | 3 | ## Important note for existing users 4 | 5 | v0.2.0 has brough some API breaking changes to mqtt.js. Please 6 | consult the [migration guide](http://github.com/adamvr/MQTT.js/wiki/migration) for information 7 | or open an issue if you need any help. 8 | 9 | ## Introduction 10 | 11 | mqtt.js is a library for the MQTT protocol, written in javascript. 12 | 13 | 14 | ## Installation 15 | 16 | npm install mqtt 17 | 18 | ## Documentation 19 | 20 | Detailed documentation can be found in [the wiki](http://github.com/adamvr/MQTT.js/wiki) 21 | 22 | ## Client API usage 23 | 24 | A basic publish client, the basis for `bin/mqtt_pub`: 25 | 26 | var mqtt = require('mqtt'); 27 | var argv = process.argv; 28 | 29 | for (var i = 2; i <= 5; i++) { 30 | if(!argv[i]) process.exit(-1); 31 | } 32 | 33 | var port = argv[2], 34 | host = argv[3], 35 | topic = argv[4], 36 | payload = argv[5]; 37 | 38 | var client = mqtt.createClient(port, host) 39 | client.on('connect', function() { 40 | client.publish(topic, payload); 41 | client.end(); 42 | }); 43 | 44 | ## Server API usage 45 | 46 | A broadcast server example, included in `examples/broadcast.js`: 47 | 48 | var mqtt = require('mqtt'); 49 | 50 | mqtt.createServer(function(client) { 51 | var self = this; 52 | 53 | if (!self.clients) self.clients = {}; 54 | 55 | client.on('connect', function(packet) { 56 | client.connack({returnCode: 0}); 57 | client.id = packet.client; 58 | self.clients[client.id] = client; 59 | }); 60 | 61 | client.on('publish', function(packet) { 62 | for (var k in self.clients) { 63 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 64 | } 65 | }); 66 | 67 | client.on('subscribe', function(packet) { 68 | var granted = []; 69 | for (var i = 0; i < packet.subscriptions.length; i++) { 70 | granted.push(packet.subscriptions[i].qos); 71 | } 72 | 73 | client.suback({granted: granted}); 74 | }); 75 | 76 | client.on('pingreq', function(packet) { 77 | client.pingresp(); 78 | }); 79 | 80 | client.on('disconnect', function(packet) { 81 | client.stream.end(); 82 | }); 83 | 84 | client.on('close', function(err) { 85 | delete self.clients[client.id]; 86 | }); 87 | 88 | client.on('error', function(err) { 89 | client.stream.end(); 90 | console.log('error!'); 91 | }); 92 | }).listen(1883); 93 | 94 | 95 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/README.md: -------------------------------------------------------------------------------- 1 | # mqtt.js 2 | 3 | ## Important note for existing users 4 | 5 | v0.2.0 has brough some API breaking changes to mqtt.js. Please 6 | consult the [migration guide](http://github.com/adamvr/MQTT.js/wiki/migration) for information 7 | or open an issue if you need any help. 8 | 9 | ## Introduction 10 | 11 | mqtt.js is a library for the MQTT protocol, written in javascript. 12 | 13 | 14 | ## Installation 15 | 16 | npm install mqtt 17 | 18 | ## Documentation 19 | 20 | Detailed documentation can be found in [the wiki](http://github.com/adamvr/MQTT.js/wiki) 21 | 22 | ## Client API usage 23 | 24 | A basic publish client, the basis for `bin/mqtt_pub`: 25 | 26 | var mqtt = require('mqtt'); 27 | var argv = process.argv; 28 | 29 | for (var i = 2; i <= 5; i++) { 30 | if(!argv[i]) process.exit(-1); 31 | } 32 | 33 | var port = argv[2], 34 | host = argv[3], 35 | topic = argv[4], 36 | payload = argv[5]; 37 | 38 | var client = mqtt.createClient(port, host) 39 | client.on('connect', function() { 40 | client.publish(topic, payload); 41 | client.end(); 42 | }); 43 | 44 | ## Server API usage 45 | 46 | A broadcast server example, included in `examples/broadcast.js`: 47 | 48 | var mqtt = require('mqtt'); 49 | 50 | mqtt.createServer(function(client) { 51 | var self = this; 52 | 53 | if (!self.clients) self.clients = {}; 54 | 55 | client.on('connect', function(packet) { 56 | client.connack({returnCode: 0}); 57 | client.id = packet.client; 58 | self.clients[client.id] = client; 59 | }); 60 | 61 | client.on('publish', function(packet) { 62 | for (var k in self.clients) { 63 | self.clients[k].publish({topic: packet.topic, payload: packet.payload}); 64 | } 65 | }); 66 | 67 | client.on('subscribe', function(packet) { 68 | var granted = []; 69 | for (var i = 0; i < packet.subscriptions.length; i++) { 70 | granted.push(packet.subscriptions[i].qos); 71 | } 72 | 73 | client.suback({granted: granted}); 74 | }); 75 | 76 | client.on('pingreq', function(packet) { 77 | client.pingresp(); 78 | }); 79 | 80 | client.on('disconnect', function(packet) { 81 | client.stream.end(); 82 | }); 83 | 84 | client.on('close', function(err) { 85 | delete self.clients[client.id]; 86 | }); 87 | 88 | client.on('error', function(err) { 89 | client.stream.end(); 90 | console.log('error!'); 91 | }); 92 | }).listen(1883); 93 | 94 | 95 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-unpipe-drain.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | var common = require('../common.js'); 24 | var assert = require('assert'); 25 | var stream = require('../../readable'); 26 | var crypto = require('crypto'); 27 | 28 | var util = require('util'); 29 | 30 | function TestWriter() { 31 | stream.Writable.call(this); 32 | } 33 | util.inherits(TestWriter, stream.Writable); 34 | 35 | TestWriter.prototype._write = function (buffer, encoding, callback) { 36 | console.log('write called'); 37 | // super slow write stream (callback never called) 38 | }; 39 | 40 | var dest = new TestWriter(); 41 | 42 | function TestReader(id) { 43 | stream.Readable.call(this); 44 | this.reads = 0; 45 | } 46 | util.inherits(TestReader, stream.Readable); 47 | 48 | TestReader.prototype._read = function (size) { 49 | this.reads += 1; 50 | this.push(crypto.randomBytes(size)); 51 | }; 52 | 53 | var src1 = new TestReader(); 54 | var src2 = new TestReader(); 55 | 56 | src1.pipe(dest); 57 | 58 | src1.once('readable', function () { 59 | process.nextTick(function () { 60 | 61 | src2.pipe(dest); 62 | 63 | src2.once('readable', function () { 64 | process.nextTick(function () { 65 | 66 | src1.unpipe(dest); 67 | }); 68 | }); 69 | }); 70 | }); 71 | 72 | 73 | process.on('exit', function () { 74 | assert.equal(src1.reads, 2); 75 | assert.equal(src2.reads, 2); 76 | }); 77 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-unpipe-drain.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | var common = require('../common.js'); 24 | var assert = require('assert'); 25 | var stream = require('../../readable'); 26 | var crypto = require('crypto'); 27 | 28 | var util = require('util'); 29 | 30 | function TestWriter() { 31 | stream.Writable.call(this); 32 | } 33 | util.inherits(TestWriter, stream.Writable); 34 | 35 | TestWriter.prototype._write = function (buffer, encoding, callback) { 36 | console.log('write called'); 37 | // super slow write stream (callback never called) 38 | }; 39 | 40 | var dest = new TestWriter(); 41 | 42 | function TestReader(id) { 43 | stream.Readable.call(this); 44 | this.reads = 0; 45 | } 46 | util.inherits(TestReader, stream.Readable); 47 | 48 | TestReader.prototype._read = function (size) { 49 | this.reads += 1; 50 | this.push(crypto.randomBytes(size)); 51 | }; 52 | 53 | var src1 = new TestReader(); 54 | var src2 = new TestReader(); 55 | 56 | src1.pipe(dest); 57 | 58 | src1.once('readable', function () { 59 | process.nextTick(function () { 60 | 61 | src2.pipe(dest); 62 | 63 | src2.once('readable', function () { 64 | process.nextTick(function () { 65 | 66 | src1.unpipe(dest); 67 | }); 68 | }); 69 | }); 70 | }); 71 | 72 | 73 | process.on('exit', function () { 74 | assert.equal(src1.reads, 2); 75 | assert.equal(src2.reads, 2); 76 | }); 77 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-non-empty-end.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var assert = require('assert'); 23 | var common = require('../common.js'); 24 | var Readable = require('../../lib/_stream_readable'); 25 | 26 | var len = 0; 27 | var chunks = new Array(10); 28 | for (var i = 1; i <= 10; i++) { 29 | chunks[i-1] = new Buffer(i); 30 | len += i; 31 | } 32 | 33 | var test = new Readable(); 34 | var n = 0; 35 | test._read = function(size) { 36 | var chunk = chunks[n++]; 37 | setTimeout(function() { 38 | test.push(chunk); 39 | }); 40 | }; 41 | 42 | test.on('end', thrower); 43 | function thrower() { 44 | throw new Error('this should not happen!'); 45 | } 46 | 47 | var bytesread = 0; 48 | test.on('readable', function() { 49 | var b = len - bytesread - 1; 50 | var res = test.read(b); 51 | if (res) { 52 | bytesread += res.length; 53 | console.error('br=%d len=%d', bytesread, len); 54 | setTimeout(next); 55 | } 56 | test.read(0); 57 | }); 58 | test.read(0); 59 | 60 | function next() { 61 | // now let's make 'end' happen 62 | test.removeListener('end', thrower); 63 | 64 | var endEmitted = false; 65 | process.on('exit', function() { 66 | assert(endEmitted, 'end should be emitted by now'); 67 | }); 68 | test.on('end', function() { 69 | endEmitted = true; 70 | }); 71 | 72 | // one to get the last byte 73 | var r = test.read(); 74 | assert(r); 75 | assert.equal(r.length, 1); 76 | r = test.read(); 77 | assert.equal(r, null); 78 | } 79 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-non-empty-end.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var assert = require('assert'); 23 | var common = require('../common.js'); 24 | var Readable = require('../../lib/_stream_readable'); 25 | 26 | var len = 0; 27 | var chunks = new Array(10); 28 | for (var i = 1; i <= 10; i++) { 29 | chunks[i-1] = new Buffer(i); 30 | len += i; 31 | } 32 | 33 | var test = new Readable(); 34 | var n = 0; 35 | test._read = function(size) { 36 | var chunk = chunks[n++]; 37 | setTimeout(function() { 38 | test.push(chunk); 39 | }); 40 | }; 41 | 42 | test.on('end', thrower); 43 | function thrower() { 44 | throw new Error('this should not happen!'); 45 | } 46 | 47 | var bytesread = 0; 48 | test.on('readable', function() { 49 | var b = len - bytesread - 1; 50 | var res = test.read(b); 51 | if (res) { 52 | bytesread += res.length; 53 | console.error('br=%d len=%d', bytesread, len); 54 | setTimeout(next); 55 | } 56 | test.read(0); 57 | }); 58 | test.read(0); 59 | 60 | function next() { 61 | // now let's make 'end' happen 62 | test.removeListener('end', thrower); 63 | 64 | var endEmitted = false; 65 | process.on('exit', function() { 66 | assert(endEmitted, 'end should be emitted by now'); 67 | }); 68 | test.on('end', function() { 69 | endEmitted = true; 70 | }); 71 | 72 | // one to get the last byte 73 | var r = test.read(); 74 | assert(r); 75 | assert.equal(r.length, 1); 76 | r = test.read(); 77 | assert.equal(r, null); 78 | } 79 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/lib/_stream_duplex.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | // a duplex stream is just a stream that is both readable and writable. 23 | // Since JS doesn't have multiple prototypal inheritance, this class 24 | // prototypally inherits from Readable, and then parasitically from 25 | // Writable. 26 | 27 | module.exports = Duplex; 28 | var util = require('util'); 29 | var Readable = require('./_stream_readable'); 30 | var Writable = require('./_stream_writable'); 31 | 32 | util.inherits(Duplex, Readable); 33 | 34 | Object.keys(Writable.prototype).forEach(function(method) { 35 | if (!Duplex.prototype[method]) 36 | Duplex.prototype[method] = Writable.prototype[method]; 37 | }); 38 | 39 | function Duplex(options) { 40 | if (!(this instanceof Duplex)) 41 | return new Duplex(options); 42 | 43 | Readable.call(this, options); 44 | Writable.call(this, options); 45 | 46 | if (options && options.readable === false) 47 | this.readable = false; 48 | 49 | if (options && options.writable === false) 50 | this.writable = false; 51 | 52 | this.allowHalfOpen = true; 53 | if (options && options.allowHalfOpen === false) 54 | this.allowHalfOpen = false; 55 | 56 | this.once('end', onend); 57 | } 58 | 59 | // the no-half-open enforcer 60 | function onend() { 61 | // if we allow half-open state, or if the writable side ended, 62 | // then we're ok. 63 | if (this.allowHalfOpen || this._writableState.ended) 64 | return; 65 | 66 | // no more data can be written. 67 | // But allow more writes to happen in this tick. 68 | process.nextTick(this.end.bind(this)); 69 | } 70 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/lib/_stream_duplex.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | // a duplex stream is just a stream that is both readable and writable. 23 | // Since JS doesn't have multiple prototypal inheritance, this class 24 | // prototypally inherits from Readable, and then parasitically from 25 | // Writable. 26 | 27 | module.exports = Duplex; 28 | var util = require('util'); 29 | var Readable = require('./_stream_readable'); 30 | var Writable = require('./_stream_writable'); 31 | 32 | util.inherits(Duplex, Readable); 33 | 34 | Object.keys(Writable.prototype).forEach(function(method) { 35 | if (!Duplex.prototype[method]) 36 | Duplex.prototype[method] = Writable.prototype[method]; 37 | }); 38 | 39 | function Duplex(options) { 40 | if (!(this instanceof Duplex)) 41 | return new Duplex(options); 42 | 43 | Readable.call(this, options); 44 | Writable.call(this, options); 45 | 46 | if (options && options.readable === false) 47 | this.readable = false; 48 | 49 | if (options && options.writable === false) 50 | this.writable = false; 51 | 52 | this.allowHalfOpen = true; 53 | if (options && options.allowHalfOpen === false) 54 | this.allowHalfOpen = false; 55 | 56 | this.once('end', onend); 57 | } 58 | 59 | // the no-half-open enforcer 60 | function onend() { 61 | // if we allow half-open state, or if the writable side ended, 62 | // then we're ok. 63 | if (this.allowHalfOpen || this._writableState.ended) 64 | return; 65 | 66 | // no more data can be written. 67 | // But allow more writes to happen in this tick. 68 | process.nextTick(this.end.bind(this)); 69 | } 70 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-large-read-stall.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common.js'); 23 | var assert = require('assert'); 24 | 25 | // If everything aligns so that you do a read(n) of exactly the 26 | // remaining buffer, then make sure that 'end' still emits. 27 | 28 | var READSIZE = 100; 29 | var PUSHSIZE = 20; 30 | var PUSHCOUNT = 1000; 31 | var HWM = 50; 32 | 33 | var Readable = require('../../readable').Readable; 34 | var r = new Readable({ 35 | highWaterMark: HWM 36 | }); 37 | var rs = r._readableState; 38 | 39 | r._read = push; 40 | 41 | r.on('readable', function() { 42 | console.error('>> readable'); 43 | do { 44 | console.error(' > read(%d)', READSIZE); 45 | var ret = r.read(READSIZE); 46 | console.error(' < %j (%d remain)', ret && ret.length, rs.length); 47 | } while (ret && ret.length === READSIZE); 48 | 49 | console.error('<< after read()', 50 | ret && ret.length, 51 | rs.needReadable, 52 | rs.length); 53 | }); 54 | 55 | var endEmitted = false; 56 | r.on('end', function() { 57 | endEmitted = true; 58 | console.error('end'); 59 | }); 60 | 61 | var pushes = 0; 62 | function push() { 63 | if (pushes > PUSHCOUNT) 64 | return; 65 | 66 | if (pushes++ === PUSHCOUNT) { 67 | console.error(' push(EOF)'); 68 | return r.push(null); 69 | } 70 | 71 | console.error(' push #%d', pushes); 72 | if (r.push(new Buffer(PUSHSIZE))) 73 | setTimeout(push); 74 | } 75 | 76 | // start the flow 77 | var ret = r.read(0); 78 | 79 | process.on('exit', function() { 80 | assert.equal(pushes, PUSHCOUNT + 1); 81 | assert(endEmitted); 82 | }); 83 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-large-read-stall.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common.js'); 23 | var assert = require('assert'); 24 | 25 | // If everything aligns so that you do a read(n) of exactly the 26 | // remaining buffer, then make sure that 'end' still emits. 27 | 28 | var READSIZE = 100; 29 | var PUSHSIZE = 20; 30 | var PUSHCOUNT = 1000; 31 | var HWM = 50; 32 | 33 | var Readable = require('../../readable').Readable; 34 | var r = new Readable({ 35 | highWaterMark: HWM 36 | }); 37 | var rs = r._readableState; 38 | 39 | r._read = push; 40 | 41 | r.on('readable', function() { 42 | console.error('>> readable'); 43 | do { 44 | console.error(' > read(%d)', READSIZE); 45 | var ret = r.read(READSIZE); 46 | console.error(' < %j (%d remain)', ret && ret.length, rs.length); 47 | } while (ret && ret.length === READSIZE); 48 | 49 | console.error('<< after read()', 50 | ret && ret.length, 51 | rs.needReadable, 52 | rs.length); 53 | }); 54 | 55 | var endEmitted = false; 56 | r.on('end', function() { 57 | endEmitted = true; 58 | console.error('end'); 59 | }); 60 | 61 | var pushes = 0; 62 | function push() { 63 | if (pushes > PUSHCOUNT) 64 | return; 65 | 66 | if (pushes++ === PUSHCOUNT) { 67 | console.error(' push(EOF)'); 68 | return r.push(null); 69 | } 70 | 71 | console.error(' push #%d', pushes); 72 | if (r.push(new Buffer(PUSHSIZE))) 73 | setTimeout(push); 74 | } 75 | 76 | // start the flow 77 | var ret = r.read(0); 78 | 79 | process.on('exit', function() { 80 | assert.equal(pushes, PUSHCOUNT + 1); 81 | assert(endEmitted); 82 | }); 83 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-unpipe-leak.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | var common = require('../common.js'); 24 | var assert = require('assert'); 25 | var stream = require('../../readable'); 26 | 27 | var chunk = new Buffer('hallo'); 28 | 29 | var util = require('util'); 30 | 31 | function TestWriter() { 32 | stream.Writable.call(this); 33 | } 34 | util.inherits(TestWriter, stream.Writable); 35 | 36 | TestWriter.prototype._write = function(buffer, encoding, callback) { 37 | callback(null); 38 | }; 39 | 40 | var dest = new TestWriter(); 41 | 42 | // Set this high so that we'd trigger a nextTick warning 43 | // and/or RangeError if we do maybeReadMore wrong. 44 | function TestReader() { 45 | stream.Readable.call(this, { highWaterMark: 0x10000 }); 46 | } 47 | util.inherits(TestReader, stream.Readable); 48 | 49 | TestReader.prototype._read = function(size) { 50 | this.push(chunk); 51 | }; 52 | 53 | var src = new TestReader(); 54 | 55 | for (var i = 0; i < 10; i++) { 56 | src.pipe(dest); 57 | src.unpipe(dest); 58 | } 59 | 60 | assert.equal(src.listeners('end').length, 0); 61 | assert.equal(src.listeners('readable').length, 0); 62 | 63 | assert.equal(dest.listeners('unpipe').length, 0); 64 | assert.equal(dest.listeners('drain').length, 0); 65 | assert.equal(dest.listeners('error').length, 0); 66 | assert.equal(dest.listeners('close').length, 0); 67 | assert.equal(dest.listeners('finish').length, 0); 68 | 69 | console.error(src._readableState); 70 | process.on('exit', function() { 71 | assert(src._readableState.length >= src._readableState.highWaterMark); 72 | src._readableState.buffer.length = 0; 73 | console.error(src._readableState); 74 | }); 75 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-unpipe-leak.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | var common = require('../common.js'); 24 | var assert = require('assert'); 25 | var stream = require('../../readable'); 26 | 27 | var chunk = new Buffer('hallo'); 28 | 29 | var util = require('util'); 30 | 31 | function TestWriter() { 32 | stream.Writable.call(this); 33 | } 34 | util.inherits(TestWriter, stream.Writable); 35 | 36 | TestWriter.prototype._write = function(buffer, encoding, callback) { 37 | callback(null); 38 | }; 39 | 40 | var dest = new TestWriter(); 41 | 42 | // Set this high so that we'd trigger a nextTick warning 43 | // and/or RangeError if we do maybeReadMore wrong. 44 | function TestReader() { 45 | stream.Readable.call(this, { highWaterMark: 0x10000 }); 46 | } 47 | util.inherits(TestReader, stream.Readable); 48 | 49 | TestReader.prototype._read = function(size) { 50 | this.push(chunk); 51 | }; 52 | 53 | var src = new TestReader(); 54 | 55 | for (var i = 0; i < 10; i++) { 56 | src.pipe(dest); 57 | src.unpipe(dest); 58 | } 59 | 60 | assert.equal(src.listeners('end').length, 0); 61 | assert.equal(src.listeners('readable').length, 0); 62 | 63 | assert.equal(dest.listeners('unpipe').length, 0); 64 | assert.equal(dest.listeners('drain').length, 0); 65 | assert.equal(dest.listeners('error').length, 0); 66 | assert.equal(dest.listeners('close').length, 0); 67 | assert.equal(dest.listeners('finish').length, 0); 68 | 69 | console.error(src._readableState); 70 | process.on('exit', function() { 71 | assert(src._readableState.length >= src._readableState.highWaterMark); 72 | src._readableState.buffer.length = 0; 73 | console.error(src._readableState); 74 | }); 75 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": { 3 | "name": "Adam Rudd", 4 | "email": "adam.rudd@uqconnect.edu.au" 5 | }, 6 | "name": "mqtt", 7 | "description": "A library for the MQTT protocol", 8 | "version": "0.2.6", 9 | "keywords": [ 10 | "mqtt", 11 | "publish/subscribe", 12 | "publish", 13 | "subscribe" 14 | ], 15 | "repository": { 16 | "type": "git", 17 | "url": "git://github.com/adamvr/MQTT.js.git" 18 | }, 19 | "main": "index.js", 20 | "scripts": { 21 | "test": "mocha" 22 | }, 23 | "bin": { 24 | "mqtt_pub": "./bin/mqtt_pub", 25 | "mqtt_sub": "./bin/mqtt_sub" 26 | }, 27 | "engines": { 28 | "node": ">=0.8.6" 29 | }, 30 | "dependencies": { 31 | "readable-stream": "~1.0.2" 32 | }, 33 | "devDependencies": { 34 | "mocha": "*", 35 | "should": "*" 36 | }, 37 | "readme": "# mqtt.js\n\n## Important note for existing users\n\nv0.2.0 has brough some API breaking changes to mqtt.js. Please\nconsult the [migration guide](http://github.com/adamvr/MQTT.js/wiki/migration) for information\nor open an issue if you need any help.\n\n## Introduction\n\nmqtt.js is a library for the MQTT protocol, written in javascript.\n\n\n## Installation\n\n npm install mqtt\n\n## Documentation\n\nDetailed documentation can be found in [the wiki](http://github.com/adamvr/MQTT.js/wiki)\n\n## Client API usage\n\nA basic publish client, the basis for `bin/mqtt_pub`:\n\n var mqtt = require('mqtt');\n var argv = process.argv;\n\n for (var i = 2; i <= 5; i++) {\n if(!argv[i]) process.exit(-1);\n }\n\n var port = argv[2],\n host = argv[3],\n topic = argv[4],\n payload = argv[5];\n\n var client = mqtt.createClient(port, host)\n client.on('connect', function() {\n client.publish(topic, payload);\n client.end();\n });\n\n## Server API usage\n\nA broadcast server example, included in `examples/broadcast.js`:\n\n var mqtt = require('mqtt');\n\n mqtt.createServer(function(client) {\n var self = this;\n\n if (!self.clients) self.clients = {};\n\n client.on('connect', function(packet) {\n client.connack({returnCode: 0});\n client.id = packet.client;\n self.clients[client.id] = client;\n });\n\n client.on('publish', function(packet) {\n for (var k in self.clients) {\n self.clients[k].publish({topic: packet.topic, payload: packet.payload});\n }\n });\n\n client.on('subscribe', function(packet) {\n var granted = [];\n for (var i = 0; i < packet.subscriptions.length; i++) {\n granted.push(packet.subscriptions[i].qos);\n }\n\n client.suback({granted: granted});\n });\n\n client.on('pingreq', function(packet) {\n client.pingresp();\n });\n\n client.on('disconnect', function(packet) {\n client.stream.end();\n });\n\n client.on('close', function(err) {\n delete self.clients[client.id];\n });\n\n client.on('error', function(err) {\n client.stream.end();\n console.log('error!');\n });\n }).listen(1883);\n\n\n", 38 | "readmeFilename": "README.md", 39 | "_id": "mqtt@0.2.6", 40 | "_from": "mqtt@" 41 | } 42 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": { 3 | "name": "Adam Rudd", 4 | "email": "adam.rudd@uqconnect.edu.au" 5 | }, 6 | "name": "mqtt", 7 | "description": "A library for the MQTT protocol", 8 | "version": "0.2.6", 9 | "keywords": [ 10 | "mqtt", 11 | "publish/subscribe", 12 | "publish", 13 | "subscribe" 14 | ], 15 | "repository": { 16 | "type": "git", 17 | "url": "git://github.com/adamvr/MQTT.js.git" 18 | }, 19 | "main": "index.js", 20 | "scripts": { 21 | "test": "mocha" 22 | }, 23 | "bin": { 24 | "mqtt_pub": "./bin/mqtt_pub", 25 | "mqtt_sub": "./bin/mqtt_sub" 26 | }, 27 | "engines": { 28 | "node": ">=0.8.6" 29 | }, 30 | "dependencies": { 31 | "readable-stream": "~1.0.2" 32 | }, 33 | "devDependencies": { 34 | "mocha": "*", 35 | "should": "*" 36 | }, 37 | "readme": "# mqtt.js\n\n## Important note for existing users\n\nv0.2.0 has brough some API breaking changes to mqtt.js. Please\nconsult the [migration guide](http://github.com/adamvr/MQTT.js/wiki/migration) for information\nor open an issue if you need any help.\n\n## Introduction\n\nmqtt.js is a library for the MQTT protocol, written in javascript.\n\n\n## Installation\n\n npm install mqtt\n\n## Documentation\n\nDetailed documentation can be found in [the wiki](http://github.com/adamvr/MQTT.js/wiki)\n\n## Client API usage\n\nA basic publish client, the basis for `bin/mqtt_pub`:\n\n var mqtt = require('mqtt');\n var argv = process.argv;\n\n for (var i = 2; i <= 5; i++) {\n if(!argv[i]) process.exit(-1);\n }\n\n var port = argv[2],\n host = argv[3],\n topic = argv[4],\n payload = argv[5];\n\n var client = mqtt.createClient(port, host)\n client.on('connect', function() {\n client.publish(topic, payload);\n client.end();\n });\n\n## Server API usage\n\nA broadcast server example, included in `examples/broadcast.js`:\n\n var mqtt = require('mqtt');\n\n mqtt.createServer(function(client) {\n var self = this;\n\n if (!self.clients) self.clients = {};\n\n client.on('connect', function(packet) {\n client.connack({returnCode: 0});\n client.id = packet.client;\n self.clients[client.id] = client;\n });\n\n client.on('publish', function(packet) {\n for (var k in self.clients) {\n self.clients[k].publish({topic: packet.topic, payload: packet.payload});\n }\n });\n\n client.on('subscribe', function(packet) {\n var granted = [];\n for (var i = 0; i < packet.subscriptions.length; i++) {\n granted.push(packet.subscriptions[i].qos);\n }\n\n client.suback({granted: granted});\n });\n\n client.on('pingreq', function(packet) {\n client.pingresp();\n });\n\n client.on('disconnect', function(packet) {\n client.stream.end();\n });\n\n client.on('close', function(err) {\n delete self.clients[client.id];\n });\n\n client.on('error', function(err) {\n client.stream.end();\n console.log('error!');\n });\n }).listen(1883);\n\n\n", 38 | "readmeFilename": "README.md", 39 | "_id": "mqtt@0.2.6", 40 | "_from": "mqtt@" 41 | } 42 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/lib/connection.js: -------------------------------------------------------------------------------- 1 | var events = require('events') 2 | , util = require('util') 3 | , protocol = require('./protocol') 4 | , generate = require('./generate') 5 | , parse = require('./parse'); 6 | 7 | var Connection = module.exports = 8 | function Connection(stream, server) { 9 | this.server = server; 10 | 11 | if (!require("stream").Readable) { 12 | // we are in node < 0.10 13 | // let's wrap the stream 14 | var wrapper = new require("readable-stream").Readable(); 15 | wrapper.wrap(stream); 16 | this.stream = wrapper; 17 | stream.on("connect", 18 | this.stream.emit.bind(this.stream, "connect")); 19 | } else { 20 | this.stream = stream; 21 | } 22 | 23 | this.buffer = null; 24 | this.packet = {}; 25 | this.skip = false; 26 | var that = this; 27 | 28 | // we might be using the wrapper here 29 | this.stream.on('readable', function () { 30 | if (!that.skip) { 31 | that.parse(); 32 | } 33 | that.skip = false; 34 | }); 35 | 36 | events.EventEmitter.call(this); 37 | }; 38 | util.inherits(Connection, events.EventEmitter); 39 | 40 | Connection.prototype.parse = function() { 41 | var byte = null, bytes = []; 42 | 43 | // Fresh packet - parse the header 44 | if (!this.packet.cmd) { 45 | byte = this.stream.read(1); 46 | if (byte === null) { 47 | return; 48 | } 49 | parse.header(byte, this.packet); 50 | } 51 | 52 | if (!this.packet.length) { 53 | var tmp = {mul: 1, length: 0}; 54 | byte = this.stream.read(1); 55 | 56 | if (byte === null) { 57 | return; 58 | } 59 | 60 | bytes.push(byte); 61 | var pos = 1; 62 | 63 | while (pos++ < 4) { 64 | 65 | tmp.length += 66 | tmp.mul * (byte[0] & protocol.LENGTH_MASK); 67 | tmp.mul *= 0x80; 68 | 69 | if ((byte[0] & protocol.LENGTH_FIN_MASK) === 0) { 70 | break; 71 | } 72 | 73 | byte = this.stream.read(1); 74 | if(byte === null) { 75 | this.skip = true; 76 | this.stream.unshift(Buffer.concat(bytes)); 77 | return; 78 | } 79 | bytes.push(byte); 80 | } 81 | 82 | this.packet.length = tmp.length; 83 | } 84 | 85 | // Do we have a payload? 86 | if (this.packet.length > 0) { 87 | var payload = this.stream.read(this.packet.length); 88 | 89 | // Do we have enough data to complete the payload? 90 | if (payload === null) { 91 | // Nope, wait for more data 92 | return; 93 | } 94 | } 95 | 96 | // Finally we can parse the payload 97 | parse[this.packet.cmd]( 98 | payload, 99 | this.packet 100 | ); 101 | 102 | // Emit packet and reset connection state 103 | this.emit(this.packet.cmd, this.packet); 104 | this.packet = {}; 105 | 106 | // there might be one more message 107 | // to parse. 108 | this.parse(); 109 | }; 110 | 111 | for (var k in protocol.types) { 112 | var v = protocol.types[k]; 113 | 114 | Connection.prototype[v] = function(type) { 115 | return function(opts) { 116 | var p = generate[type](opts); 117 | if (p instanceof Error) { 118 | this.emit('error', p) 119 | } else { 120 | this.stream.write(p); 121 | } 122 | } 123 | }(v); 124 | } 125 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/lib/connection.js: -------------------------------------------------------------------------------- 1 | var events = require('events') 2 | , util = require('util') 3 | , protocol = require('./protocol') 4 | , generate = require('./generate') 5 | , parse = require('./parse'); 6 | 7 | var Connection = module.exports = 8 | function Connection(stream, server) { 9 | this.server = server; 10 | 11 | if (!require("stream").Readable) { 12 | // we are in node < 0.10 13 | // let's wrap the stream 14 | var wrapper = new require("readable-stream").Readable(); 15 | wrapper.wrap(stream); 16 | this.stream = wrapper; 17 | stream.on("connect", 18 | this.stream.emit.bind(this.stream, "connect")); 19 | } else { 20 | this.stream = stream; 21 | } 22 | 23 | this.buffer = null; 24 | this.packet = {}; 25 | this.skip = false; 26 | var that = this; 27 | 28 | // we might be using the wrapper here 29 | this.stream.on('readable', function () { 30 | if (!that.skip) { 31 | that.parse(); 32 | } 33 | that.skip = false; 34 | }); 35 | 36 | events.EventEmitter.call(this); 37 | }; 38 | util.inherits(Connection, events.EventEmitter); 39 | 40 | Connection.prototype.parse = function() { 41 | var byte = null, bytes = []; 42 | 43 | // Fresh packet - parse the header 44 | if (!this.packet.cmd) { 45 | byte = this.stream.read(1); 46 | if (byte === null) { 47 | return; 48 | } 49 | parse.header(byte, this.packet); 50 | } 51 | 52 | if (!this.packet.length) { 53 | var tmp = {mul: 1, length: 0}; 54 | byte = this.stream.read(1); 55 | 56 | if (byte === null) { 57 | return; 58 | } 59 | 60 | bytes.push(byte); 61 | var pos = 1; 62 | 63 | while (pos++ < 4) { 64 | 65 | tmp.length += 66 | tmp.mul * (byte[0] & protocol.LENGTH_MASK); 67 | tmp.mul *= 0x80; 68 | 69 | if ((byte[0] & protocol.LENGTH_FIN_MASK) === 0) { 70 | break; 71 | } 72 | 73 | byte = this.stream.read(1); 74 | if(byte === null) { 75 | this.skip = true; 76 | this.stream.unshift(Buffer.concat(bytes)); 77 | return; 78 | } 79 | bytes.push(byte); 80 | } 81 | 82 | this.packet.length = tmp.length; 83 | } 84 | 85 | // Do we have a payload? 86 | if (this.packet.length > 0) { 87 | var payload = this.stream.read(this.packet.length); 88 | 89 | // Do we have enough data to complete the payload? 90 | if (payload === null) { 91 | // Nope, wait for more data 92 | return; 93 | } 94 | } 95 | 96 | // Finally we can parse the payload 97 | parse[this.packet.cmd]( 98 | payload, 99 | this.packet 100 | ); 101 | 102 | // Emit packet and reset connection state 103 | this.emit(this.packet.cmd, this.packet); 104 | this.packet = {}; 105 | 106 | // there might be one more message 107 | // to parse. 108 | this.parse(); 109 | }; 110 | 111 | for (var k in protocol.types) { 112 | var v = protocol.types[k]; 113 | 114 | Connection.prototype[v] = function(type) { 115 | return function(opts) { 116 | var p = generate[type](opts); 117 | if (p instanceof Error) { 118 | this.emit('error', p) 119 | } else { 120 | this.stream.write(p); 121 | } 122 | } 123 | }(v); 124 | } 125 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-pipe-error-handling.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | var stream = require('../../readable'); 25 | 26 | (function testErrorListenerCatches() { 27 | var count = 1000; 28 | 29 | var source = new stream.Readable(); 30 | source._read = function(n) { 31 | n = Math.min(count, n); 32 | count -= n; 33 | source.push(new Buffer(n)); 34 | }; 35 | 36 | var unpipedDest; 37 | source.unpipe = function(dest) { 38 | unpipedDest = dest; 39 | stream.Readable.prototype.unpipe.call(this, dest); 40 | }; 41 | 42 | var dest = new stream.Writable(); 43 | dest._write = function(chunk, encoding, cb) { 44 | cb(); 45 | }; 46 | 47 | source.pipe(dest); 48 | 49 | var gotErr = null; 50 | dest.on('error', function(err) { 51 | gotErr = err; 52 | }); 53 | 54 | var unpipedSource; 55 | dest.on('unpipe', function(src) { 56 | unpipedSource = src; 57 | }); 58 | 59 | var err = new Error('This stream turned into bacon.'); 60 | dest.emit('error', err); 61 | assert.strictEqual(gotErr, err); 62 | assert.strictEqual(unpipedSource, source); 63 | assert.strictEqual(unpipedDest, dest); 64 | })(); 65 | 66 | (function testErrorWithoutListenerThrows() { 67 | var count = 1000; 68 | 69 | var source = new stream.Readable(); 70 | source._read = function(n) { 71 | n = Math.min(count, n); 72 | count -= n; 73 | source.push(new Buffer(n)); 74 | }; 75 | 76 | var unpipedDest; 77 | source.unpipe = function(dest) { 78 | unpipedDest = dest; 79 | stream.Readable.prototype.unpipe.call(this, dest); 80 | }; 81 | 82 | var dest = new stream.Writable(); 83 | dest._write = function(chunk, encoding, cb) { 84 | cb(); 85 | }; 86 | 87 | source.pipe(dest); 88 | 89 | var unpipedSource; 90 | dest.on('unpipe', function(src) { 91 | unpipedSource = src; 92 | }); 93 | 94 | var err = new Error('This stream turned into bacon.'); 95 | 96 | var gotErr = null; 97 | try { 98 | dest.emit('error', err); 99 | } catch (e) { 100 | gotErr = e; 101 | } 102 | assert.strictEqual(gotErr, err); 103 | assert.strictEqual(unpipedSource, source); 104 | assert.strictEqual(unpipedDest, dest); 105 | })(); 106 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-pipe-error-handling.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | var stream = require('../../readable'); 25 | 26 | (function testErrorListenerCatches() { 27 | var count = 1000; 28 | 29 | var source = new stream.Readable(); 30 | source._read = function(n) { 31 | n = Math.min(count, n); 32 | count -= n; 33 | source.push(new Buffer(n)); 34 | }; 35 | 36 | var unpipedDest; 37 | source.unpipe = function(dest) { 38 | unpipedDest = dest; 39 | stream.Readable.prototype.unpipe.call(this, dest); 40 | }; 41 | 42 | var dest = new stream.Writable(); 43 | dest._write = function(chunk, encoding, cb) { 44 | cb(); 45 | }; 46 | 47 | source.pipe(dest); 48 | 49 | var gotErr = null; 50 | dest.on('error', function(err) { 51 | gotErr = err; 52 | }); 53 | 54 | var unpipedSource; 55 | dest.on('unpipe', function(src) { 56 | unpipedSource = src; 57 | }); 58 | 59 | var err = new Error('This stream turned into bacon.'); 60 | dest.emit('error', err); 61 | assert.strictEqual(gotErr, err); 62 | assert.strictEqual(unpipedSource, source); 63 | assert.strictEqual(unpipedDest, dest); 64 | })(); 65 | 66 | (function testErrorWithoutListenerThrows() { 67 | var count = 1000; 68 | 69 | var source = new stream.Readable(); 70 | source._read = function(n) { 71 | n = Math.min(count, n); 72 | count -= n; 73 | source.push(new Buffer(n)); 74 | }; 75 | 76 | var unpipedDest; 77 | source.unpipe = function(dest) { 78 | unpipedDest = dest; 79 | stream.Readable.prototype.unpipe.call(this, dest); 80 | }; 81 | 82 | var dest = new stream.Writable(); 83 | dest._write = function(chunk, encoding, cb) { 84 | cb(); 85 | }; 86 | 87 | source.pipe(dest); 88 | 89 | var unpipedSource; 90 | dest.on('unpipe', function(src) { 91 | unpipedSource = src; 92 | }); 93 | 94 | var err = new Error('This stream turned into bacon.'); 95 | 96 | var gotErr = null; 97 | try { 98 | dest.emit('error', err); 99 | } catch (e) { 100 | gotErr = e; 101 | } 102 | assert.strictEqual(gotErr, err); 103 | assert.strictEqual(unpipedSource, source); 104 | assert.strictEqual(unpipedDest, dest); 105 | })(); 106 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-from-list.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var assert = require('assert'); 23 | var common = require('../common.js'); 24 | var fromList = require('../../lib/_stream_readable')._fromList; 25 | 26 | // tiny node-tap lookalike. 27 | var tests = []; 28 | var count = 0; 29 | 30 | function test(name, fn) { 31 | count++; 32 | tests.push([name, fn]); 33 | } 34 | 35 | function run() { 36 | var next = tests.shift(); 37 | if (!next) 38 | return console.error('ok'); 39 | 40 | var name = next[0]; 41 | var fn = next[1]; 42 | console.log('# %s', name); 43 | fn({ 44 | same: assert.deepEqual, 45 | equal: assert.equal, 46 | end: function () { 47 | count--; 48 | run(); 49 | } 50 | }); 51 | } 52 | 53 | // ensure all tests have run 54 | process.on("exit", function () { 55 | assert.equal(count, 0); 56 | }); 57 | 58 | process.nextTick(run); 59 | 60 | 61 | 62 | test('buffers', function(t) { 63 | // have a length 64 | var len = 16; 65 | var list = [ new Buffer('foog'), 66 | new Buffer('bark'), 67 | new Buffer('bazy'), 68 | new Buffer('kuel') ]; 69 | 70 | // read more than the first element. 71 | var ret = fromList(6, { buffer: list, length: 16 }); 72 | t.equal(ret.toString(), 'foogba'); 73 | 74 | // read exactly the first element. 75 | ret = fromList(2, { buffer: list, length: 10 }); 76 | t.equal(ret.toString(), 'rk'); 77 | 78 | // read less than the first element. 79 | ret = fromList(2, { buffer: list, length: 8 }); 80 | t.equal(ret.toString(), 'ba'); 81 | 82 | // read more than we have. 83 | ret = fromList(100, { buffer: list, length: 6 }); 84 | t.equal(ret.toString(), 'zykuel'); 85 | 86 | // all consumed. 87 | t.same(list, []); 88 | 89 | t.end(); 90 | }); 91 | 92 | test('strings', function(t) { 93 | // have a length 94 | var len = 16; 95 | var list = [ 'foog', 96 | 'bark', 97 | 'bazy', 98 | 'kuel' ]; 99 | 100 | // read more than the first element. 101 | var ret = fromList(6, { buffer: list, length: 16, decoder: true }); 102 | t.equal(ret, 'foogba'); 103 | 104 | // read exactly the first element. 105 | ret = fromList(2, { buffer: list, length: 10, decoder: true }); 106 | t.equal(ret, 'rk'); 107 | 108 | // read less than the first element. 109 | ret = fromList(2, { buffer: list, length: 8, decoder: true }); 110 | t.equal(ret, 'ba'); 111 | 112 | // read more than we have. 113 | ret = fromList(100, { buffer: list, length: 6, decoder: true }); 114 | t.equal(ret, 'zykuel'); 115 | 116 | // all consumed. 117 | t.same(list, []); 118 | 119 | t.end(); 120 | }); 121 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-from-list.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var assert = require('assert'); 23 | var common = require('../common.js'); 24 | var fromList = require('../../lib/_stream_readable')._fromList; 25 | 26 | // tiny node-tap lookalike. 27 | var tests = []; 28 | var count = 0; 29 | 30 | function test(name, fn) { 31 | count++; 32 | tests.push([name, fn]); 33 | } 34 | 35 | function run() { 36 | var next = tests.shift(); 37 | if (!next) 38 | return console.error('ok'); 39 | 40 | var name = next[0]; 41 | var fn = next[1]; 42 | console.log('# %s', name); 43 | fn({ 44 | same: assert.deepEqual, 45 | equal: assert.equal, 46 | end: function () { 47 | count--; 48 | run(); 49 | } 50 | }); 51 | } 52 | 53 | // ensure all tests have run 54 | process.on("exit", function () { 55 | assert.equal(count, 0); 56 | }); 57 | 58 | process.nextTick(run); 59 | 60 | 61 | 62 | test('buffers', function(t) { 63 | // have a length 64 | var len = 16; 65 | var list = [ new Buffer('foog'), 66 | new Buffer('bark'), 67 | new Buffer('bazy'), 68 | new Buffer('kuel') ]; 69 | 70 | // read more than the first element. 71 | var ret = fromList(6, { buffer: list, length: 16 }); 72 | t.equal(ret.toString(), 'foogba'); 73 | 74 | // read exactly the first element. 75 | ret = fromList(2, { buffer: list, length: 10 }); 76 | t.equal(ret.toString(), 'rk'); 77 | 78 | // read less than the first element. 79 | ret = fromList(2, { buffer: list, length: 8 }); 80 | t.equal(ret.toString(), 'ba'); 81 | 82 | // read more than we have. 83 | ret = fromList(100, { buffer: list, length: 6 }); 84 | t.equal(ret.toString(), 'zykuel'); 85 | 86 | // all consumed. 87 | t.same(list, []); 88 | 89 | t.end(); 90 | }); 91 | 92 | test('strings', function(t) { 93 | // have a length 94 | var len = 16; 95 | var list = [ 'foog', 96 | 'bark', 97 | 'bazy', 98 | 'kuel' ]; 99 | 100 | // read more than the first element. 101 | var ret = fromList(6, { buffer: list, length: 16, decoder: true }); 102 | t.equal(ret, 'foogba'); 103 | 104 | // read exactly the first element. 105 | ret = fromList(2, { buffer: list, length: 10, decoder: true }); 106 | t.equal(ret, 'rk'); 107 | 108 | // read less than the first element. 109 | ret = fromList(2, { buffer: list, length: 8, decoder: true }); 110 | t.equal(ret, 'ba'); 111 | 112 | // read more than we have. 113 | ret = fromList(100, { buffer: list, length: 6, decoder: true }); 114 | t.equal(ret, 'zykuel'); 115 | 116 | // all consumed. 117 | t.same(list, []); 118 | 119 | t.end(); 120 | }); 121 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-push.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common.js'); 23 | var stream = require('../../readable'); 24 | var Readable = stream.Readable; 25 | var Writable = stream.Writable; 26 | var assert = require('assert'); 27 | 28 | var util = require('util'); 29 | var EE = require('events').EventEmitter; 30 | 31 | 32 | // a mock thing a bit like the net.Socket/tcp_wrap.handle interaction 33 | 34 | var stream = new Readable({ 35 | highWaterMark: 16, 36 | encoding: 'utf8' 37 | }); 38 | 39 | var source = new EE; 40 | 41 | stream._read = function() { 42 | console.error('stream._read'); 43 | readStart(); 44 | }; 45 | 46 | var ended = false; 47 | stream.on('end', function() { 48 | ended = true; 49 | }); 50 | 51 | source.on('data', function(chunk) { 52 | var ret = stream.push(chunk); 53 | console.error('data', stream._readableState.length); 54 | if (!ret) 55 | readStop(); 56 | }); 57 | 58 | source.on('end', function() { 59 | stream.push(null); 60 | }); 61 | 62 | var reading = false; 63 | 64 | function readStart() { 65 | console.error('readStart'); 66 | reading = true; 67 | } 68 | 69 | function readStop() { 70 | console.error('readStop'); 71 | reading = false; 72 | process.nextTick(function() { 73 | var r = stream.read(); 74 | if (r !== null) 75 | writer.write(r); 76 | }); 77 | } 78 | 79 | var writer = new Writable({ 80 | decodeStrings: false 81 | }); 82 | 83 | var written = []; 84 | 85 | var expectWritten = 86 | [ 'asdfgasdfgasdfgasdfg', 87 | 'asdfgasdfgasdfgasdfg', 88 | 'asdfgasdfgasdfgasdfg', 89 | 'asdfgasdfgasdfgasdfg', 90 | 'asdfgasdfgasdfgasdfg', 91 | 'asdfgasdfgasdfgasdfg' ]; 92 | 93 | writer._write = function(chunk, encoding, cb) { 94 | console.error('WRITE %s', chunk); 95 | written.push(chunk); 96 | process.nextTick(cb); 97 | }; 98 | 99 | writer.on('finish', finish); 100 | 101 | 102 | // now emit some chunks. 103 | 104 | var chunk = "asdfg"; 105 | 106 | var set = 0; 107 | readStart(); 108 | data(); 109 | function data() { 110 | assert(reading); 111 | source.emit('data', chunk); 112 | assert(reading); 113 | source.emit('data', chunk); 114 | assert(reading); 115 | source.emit('data', chunk); 116 | assert(reading); 117 | source.emit('data', chunk); 118 | assert(!reading); 119 | if (set++ < 5) 120 | setTimeout(data, 10); 121 | else 122 | end(); 123 | } 124 | 125 | function finish() { 126 | console.error('finish'); 127 | assert.deepEqual(written, expectWritten); 128 | console.log('ok'); 129 | } 130 | 131 | function end() { 132 | source.emit('end'); 133 | assert(!reading); 134 | writer.end(stream.read()); 135 | setTimeout(function() { 136 | assert(ended); 137 | }); 138 | } 139 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-push.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common.js'); 23 | var stream = require('../../readable'); 24 | var Readable = stream.Readable; 25 | var Writable = stream.Writable; 26 | var assert = require('assert'); 27 | 28 | var util = require('util'); 29 | var EE = require('events').EventEmitter; 30 | 31 | 32 | // a mock thing a bit like the net.Socket/tcp_wrap.handle interaction 33 | 34 | var stream = new Readable({ 35 | highWaterMark: 16, 36 | encoding: 'utf8' 37 | }); 38 | 39 | var source = new EE; 40 | 41 | stream._read = function() { 42 | console.error('stream._read'); 43 | readStart(); 44 | }; 45 | 46 | var ended = false; 47 | stream.on('end', function() { 48 | ended = true; 49 | }); 50 | 51 | source.on('data', function(chunk) { 52 | var ret = stream.push(chunk); 53 | console.error('data', stream._readableState.length); 54 | if (!ret) 55 | readStop(); 56 | }); 57 | 58 | source.on('end', function() { 59 | stream.push(null); 60 | }); 61 | 62 | var reading = false; 63 | 64 | function readStart() { 65 | console.error('readStart'); 66 | reading = true; 67 | } 68 | 69 | function readStop() { 70 | console.error('readStop'); 71 | reading = false; 72 | process.nextTick(function() { 73 | var r = stream.read(); 74 | if (r !== null) 75 | writer.write(r); 76 | }); 77 | } 78 | 79 | var writer = new Writable({ 80 | decodeStrings: false 81 | }); 82 | 83 | var written = []; 84 | 85 | var expectWritten = 86 | [ 'asdfgasdfgasdfgasdfg', 87 | 'asdfgasdfgasdfgasdfg', 88 | 'asdfgasdfgasdfgasdfg', 89 | 'asdfgasdfgasdfgasdfg', 90 | 'asdfgasdfgasdfgasdfg', 91 | 'asdfgasdfgasdfgasdfg' ]; 92 | 93 | writer._write = function(chunk, encoding, cb) { 94 | console.error('WRITE %s', chunk); 95 | written.push(chunk); 96 | process.nextTick(cb); 97 | }; 98 | 99 | writer.on('finish', finish); 100 | 101 | 102 | // now emit some chunks. 103 | 104 | var chunk = "asdfg"; 105 | 106 | var set = 0; 107 | readStart(); 108 | data(); 109 | function data() { 110 | assert(reading); 111 | source.emit('data', chunk); 112 | assert(reading); 113 | source.emit('data', chunk); 114 | assert(reading); 115 | source.emit('data', chunk); 116 | assert(reading); 117 | source.emit('data', chunk); 118 | assert(!reading); 119 | if (set++ < 5) 120 | setTimeout(data, 10); 121 | else 122 | end(); 123 | } 124 | 125 | function finish() { 126 | console.error('finish'); 127 | assert.deepEqual(written, expectWritten); 128 | console.log('ok'); 129 | } 130 | 131 | function end() { 132 | source.emit('end'); 133 | assert(!reading); 134 | writer.end(stream.read()); 135 | setTimeout(function() { 136 | assert(ended); 137 | }); 138 | } 139 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-empty-buffer-no-eof.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | 25 | var Readable = require('../../readable').Readable; 26 | 27 | test1(); 28 | if (!/^v0\.[0-8]\./.test(process.version)) 29 | test2(); 30 | 31 | function test1() { 32 | var r = new Readable(); 33 | 34 | // should not end when we get a Buffer(0) or '' as the _read result 35 | // that just means that there is *temporarily* no data, but to go 36 | // ahead and try again later. 37 | // 38 | // note that this is very unusual. it only works for crypto streams 39 | // because the other side of the stream will call read(0) to cycle 40 | // data through openssl. that's why we set the timeouts to call 41 | // r.read(0) again later, otherwise there is no more work being done 42 | // and the process just exits. 43 | 44 | var buf = new Buffer(5); 45 | buf.fill('x'); 46 | var reads = 5; 47 | r._read = function(n) { 48 | switch (reads--) { 49 | case 0: 50 | return r.push(null); // EOF 51 | case 1: 52 | return r.push(buf); 53 | case 2: 54 | setTimeout(r.read.bind(r, 0), 10); 55 | return r.push(new Buffer(0)); // Not-EOF! 56 | case 3: 57 | setTimeout(r.read.bind(r, 0), 10); 58 | return process.nextTick(function() { 59 | return r.push(new Buffer(0)); 60 | }); 61 | case 4: 62 | setTimeout(r.read.bind(r, 0), 10); 63 | return setTimeout(function() { 64 | return r.push(new Buffer(0)); 65 | }); 66 | case 5: 67 | return setTimeout(function() { 68 | return r.push(buf); 69 | }); 70 | default: 71 | throw new Error('unreachable'); 72 | } 73 | }; 74 | 75 | var results = []; 76 | function flow() { 77 | var chunk; 78 | while (null !== (chunk = r.read())) 79 | results.push(chunk + ''); 80 | } 81 | r.on('readable', flow); 82 | r.on('end', function() { 83 | results.push('EOF'); 84 | }); 85 | flow(); 86 | 87 | process.on('exit', function() { 88 | assert.deepEqual(results, [ 'xxxxx', 'xxxxx', 'EOF' ]); 89 | console.log('ok'); 90 | }); 91 | } 92 | 93 | function test2() { 94 | var r = new Readable({ encoding: 'base64' }); 95 | var reads = 5; 96 | r._read = function(n) { 97 | if (!reads--) 98 | return r.push(null); // EOF 99 | else 100 | return r.push(new Buffer('x')); 101 | }; 102 | 103 | var results = []; 104 | function flow() { 105 | var chunk; 106 | while (null !== (chunk = r.read())) 107 | results.push(chunk + ''); 108 | } 109 | r.on('readable', flow); 110 | r.on('end', function() { 111 | results.push('EOF'); 112 | }); 113 | flow(); 114 | 115 | process.on('exit', function() { 116 | assert.deepEqual(results, [ 'eHh4', 'eHg=', 'EOF' ]); 117 | console.log('ok'); 118 | }); 119 | } 120 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/simple/test-stream2-readable-empty-buffer-no-eof.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var common = require('../common'); 23 | var assert = require('assert'); 24 | 25 | var Readable = require('../../readable').Readable; 26 | 27 | test1(); 28 | if (!/^v0\.[0-8]\./.test(process.version)) 29 | test2(); 30 | 31 | function test1() { 32 | var r = new Readable(); 33 | 34 | // should not end when we get a Buffer(0) or '' as the _read result 35 | // that just means that there is *temporarily* no data, but to go 36 | // ahead and try again later. 37 | // 38 | // note that this is very unusual. it only works for crypto streams 39 | // because the other side of the stream will call read(0) to cycle 40 | // data through openssl. that's why we set the timeouts to call 41 | // r.read(0) again later, otherwise there is no more work being done 42 | // and the process just exits. 43 | 44 | var buf = new Buffer(5); 45 | buf.fill('x'); 46 | var reads = 5; 47 | r._read = function(n) { 48 | switch (reads--) { 49 | case 0: 50 | return r.push(null); // EOF 51 | case 1: 52 | return r.push(buf); 53 | case 2: 54 | setTimeout(r.read.bind(r, 0), 10); 55 | return r.push(new Buffer(0)); // Not-EOF! 56 | case 3: 57 | setTimeout(r.read.bind(r, 0), 10); 58 | return process.nextTick(function() { 59 | return r.push(new Buffer(0)); 60 | }); 61 | case 4: 62 | setTimeout(r.read.bind(r, 0), 10); 63 | return setTimeout(function() { 64 | return r.push(new Buffer(0)); 65 | }); 66 | case 5: 67 | return setTimeout(function() { 68 | return r.push(buf); 69 | }); 70 | default: 71 | throw new Error('unreachable'); 72 | } 73 | }; 74 | 75 | var results = []; 76 | function flow() { 77 | var chunk; 78 | while (null !== (chunk = r.read())) 79 | results.push(chunk + ''); 80 | } 81 | r.on('readable', flow); 82 | r.on('end', function() { 83 | results.push('EOF'); 84 | }); 85 | flow(); 86 | 87 | process.on('exit', function() { 88 | assert.deepEqual(results, [ 'xxxxx', 'xxxxx', 'EOF' ]); 89 | console.log('ok'); 90 | }); 91 | } 92 | 93 | function test2() { 94 | var r = new Readable({ encoding: 'base64' }); 95 | var reads = 5; 96 | r._read = function(n) { 97 | if (!reads--) 98 | return r.push(null); // EOF 99 | else 100 | return r.push(new Buffer('x')); 101 | }; 102 | 103 | var results = []; 104 | function flow() { 105 | var chunk; 106 | while (null !== (chunk = r.read())) 107 | results.push(chunk + ''); 108 | } 109 | r.on('readable', flow); 110 | r.on('end', function() { 111 | results.push('EOF'); 112 | }); 113 | flow(); 114 | 115 | process.on('exit', function() { 116 | assert.deepEqual(results, [ 'eHh4', 'eHg=', 'EOF' ]); 117 | console.log('ok'); 118 | }); 119 | } 120 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/lib/mqtt.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Adam Rudd. 3 | * See LICENSE for more information 4 | */ 5 | 6 | var net = require('net') 7 | , MqttServer = require('./server').MqttServer 8 | , MqttSecureServer = require('./server').MqttSecureServer 9 | , MqttClient = require('./client') 10 | , MqttConnection = require('./connection'); 11 | 12 | var defaultPort = 1883 13 | , defaultHost = 'localhost'; 14 | 15 | /** 16 | * createClient - create an MQTT client 17 | * 18 | * @param [port] - broker port 19 | * @param [host] - broker host 20 | * @param [opts] - see MqttClient#constructor 21 | * @api public 22 | */ 23 | module.exports.createClient = function(port, host, opts) { 24 | var net_client, mqtt_client; 25 | 26 | if ('undefined' === typeof port) { 27 | // createClient() 28 | port = defaultPort; 29 | host = defaultHost; 30 | opts = {}; 31 | } else if ('object' === typeof port) { 32 | // createClient({}) 33 | opts = port; 34 | port = defaultPort; 35 | host = defaultHost; 36 | } else if ('object' === typeof host) { 37 | // createClient(1883, {}) 38 | opts = host; 39 | host = defaultHost; 40 | } 41 | 42 | net_client = net.createConnection(port, host); 43 | mqtt_client = new MqttClient(net_client, opts); 44 | 45 | return mqtt_client; 46 | }; 47 | 48 | /** 49 | * createSecureClient - create a tls secured MQTT client 50 | * 51 | * @param [port] 52 | * @param [host] 53 | * @param opts 54 | * @api public 55 | */ 56 | module.exports.createSecureClient = function(port, host, opts) { 57 | var tls_client, mqtt_client; 58 | 59 | if ('object' === typeof port) { 60 | opts = port; 61 | port = defaultPort; 62 | host = defaultHost; 63 | } else if ('object' === typeof host) { 64 | opts = host; 65 | host = defaultHost; 66 | } else if ('object' !== typeof opts) { 67 | throw new Error('Invalid options'); 68 | } 69 | 70 | var keyPath = opts.keyPath 71 | , certPath = opts.certPath; 72 | 73 | tls_client = tls.connect(port, host, { 74 | key: fs.readFileSync(keyPath), 75 | cert: fs.readFileSync(certPath) 76 | }); 77 | 78 | tls_client.on('secureConnect', function() { 79 | if (!tls_client.authorized) { 80 | throw new Error('TLS not authorized'); 81 | } 82 | }); 83 | 84 | mqtt_client = new MqttClient(tls_client, opts); 85 | return mqtt_client; 86 | } 87 | 88 | /** 89 | * createServer - create an MQTT server 90 | * 91 | * @param listener - called on new client connections 92 | */ 93 | 94 | module.exports.createServer = function(listener) { 95 | return new MqttServer(listener); 96 | }; 97 | 98 | /** 99 | * createSecureServer - create a tls secured MQTT server 100 | * 101 | * @param keyPath - path to private key 102 | * @param certPath - path to public cert 103 | * @param listener - called on new client conns 104 | */ 105 | 106 | module.exports.createSecureServer = 107 | function(keyPath, certPath, listener) { 108 | return new MqttSecureServer(keyPath, certPath, listener); 109 | }; 110 | 111 | /** 112 | * createConnection - create a bare MQTT connection 113 | * 114 | * @param [port] 115 | * @param [host] 116 | * @param [callback] 117 | */ 118 | module.exports.createConnection = function(port, host, callback) { 119 | var net_client, mqtt_conn; 120 | if ('undefined' === typeof port) { 121 | // createConnection(); 122 | port = defaultPort; 123 | host = defaultHost; 124 | callback = function(){}; 125 | } else if ('function' === typeof port) { 126 | // createConnection(function(){}); 127 | callback = port; 128 | port = defaultPort; 129 | host = defaultHost; 130 | } else if ('function' === typeof host) { 131 | // createConnection(1883, function(){}); 132 | callback = host; 133 | host = defaultHost; 134 | } else if ('function' !== typeof callback) { 135 | // createConnection(1883, 'localhost'); 136 | callback = function(){}; 137 | } 138 | 139 | net_client = net.createConnection(port, host); 140 | mqtt_conn = new MqttConnection(net_client); 141 | 142 | // Echo net errors 143 | mqtt_conn.stream.on('error', mqtt_conn.emit.bind(mqtt_conn, 'error')); 144 | mqtt_conn.stream.on('close', mqtt_conn.emit.bind(mqtt_conn, 'close')); 145 | 146 | net_client.on('connect', function() { 147 | mqtt_conn.emit('connected'); 148 | }); 149 | 150 | mqtt_conn.once('connected', function() { 151 | callback(null, mqtt_conn); 152 | }); 153 | 154 | mqtt_conn.once('error', function(err) { 155 | callback(err); 156 | }); 157 | 158 | return mqtt_conn; 159 | }; 160 | 161 | // Expose MqttClient 162 | module.exports.MqttClient = MqttClient; 163 | 164 | // Expose servers 165 | module.exports.MqttServer = MqttServer; 166 | module.exports.MqttSecureServer = MqttSecureServer; 167 | 168 | // Expose Connection 169 | module.exports.MqttConnection = MqttConnection; 170 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/lib/mqtt.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011 Adam Rudd. 3 | * See LICENSE for more information 4 | */ 5 | 6 | var net = require('net') 7 | , MqttServer = require('./server').MqttServer 8 | , MqttSecureServer = require('./server').MqttSecureServer 9 | , MqttClient = require('./client') 10 | , MqttConnection = require('./connection'); 11 | 12 | var defaultPort = 1883 13 | , defaultHost = 'localhost'; 14 | 15 | /** 16 | * createClient - create an MQTT client 17 | * 18 | * @param [port] - broker port 19 | * @param [host] - broker host 20 | * @param [opts] - see MqttClient#constructor 21 | * @api public 22 | */ 23 | module.exports.createClient = function(port, host, opts) { 24 | var net_client, mqtt_client; 25 | 26 | if ('undefined' === typeof port) { 27 | // createClient() 28 | port = defaultPort; 29 | host = defaultHost; 30 | opts = {}; 31 | } else if ('object' === typeof port) { 32 | // createClient({}) 33 | opts = port; 34 | port = defaultPort; 35 | host = defaultHost; 36 | } else if ('object' === typeof host) { 37 | // createClient(1883, {}) 38 | opts = host; 39 | host = defaultHost; 40 | } 41 | 42 | net_client = net.createConnection(port, host); 43 | mqtt_client = new MqttClient(net_client, opts); 44 | 45 | return mqtt_client; 46 | }; 47 | 48 | /** 49 | * createSecureClient - create a tls secured MQTT client 50 | * 51 | * @param [port] 52 | * @param [host] 53 | * @param opts 54 | * @api public 55 | */ 56 | module.exports.createSecureClient = function(port, host, opts) { 57 | var tls_client, mqtt_client; 58 | 59 | if ('object' === typeof port) { 60 | opts = port; 61 | port = defaultPort; 62 | host = defaultHost; 63 | } else if ('object' === typeof host) { 64 | opts = host; 65 | host = defaultHost; 66 | } else if ('object' !== typeof opts) { 67 | throw new Error('Invalid options'); 68 | } 69 | 70 | var keyPath = opts.keyPath 71 | , certPath = opts.certPath; 72 | 73 | tls_client = tls.connect(port, host, { 74 | key: fs.readFileSync(keyPath), 75 | cert: fs.readFileSync(certPath) 76 | }); 77 | 78 | tls_client.on('secureConnect', function() { 79 | if (!tls_client.authorized) { 80 | throw new Error('TLS not authorized'); 81 | } 82 | }); 83 | 84 | mqtt_client = new MqttClient(tls_client, opts); 85 | return mqtt_client; 86 | } 87 | 88 | /** 89 | * createServer - create an MQTT server 90 | * 91 | * @param listener - called on new client connections 92 | */ 93 | 94 | module.exports.createServer = function(listener) { 95 | return new MqttServer(listener); 96 | }; 97 | 98 | /** 99 | * createSecureServer - create a tls secured MQTT server 100 | * 101 | * @param keyPath - path to private key 102 | * @param certPath - path to public cert 103 | * @param listener - called on new client conns 104 | */ 105 | 106 | module.exports.createSecureServer = 107 | function(keyPath, certPath, listener) { 108 | return new MqttSecureServer(keyPath, certPath, listener); 109 | }; 110 | 111 | /** 112 | * createConnection - create a bare MQTT connection 113 | * 114 | * @param [port] 115 | * @param [host] 116 | * @param [callback] 117 | */ 118 | module.exports.createConnection = function(port, host, callback) { 119 | var net_client, mqtt_conn; 120 | if ('undefined' === typeof port) { 121 | // createConnection(); 122 | port = defaultPort; 123 | host = defaultHost; 124 | callback = function(){}; 125 | } else if ('function' === typeof port) { 126 | // createConnection(function(){}); 127 | callback = port; 128 | port = defaultPort; 129 | host = defaultHost; 130 | } else if ('function' === typeof host) { 131 | // createConnection(1883, function(){}); 132 | callback = host; 133 | host = defaultHost; 134 | } else if ('function' !== typeof callback) { 135 | // createConnection(1883, 'localhost'); 136 | callback = function(){}; 137 | } 138 | 139 | net_client = net.createConnection(port, host); 140 | mqtt_conn = new MqttConnection(net_client); 141 | 142 | // Echo net errors 143 | mqtt_conn.stream.on('error', mqtt_conn.emit.bind(mqtt_conn, 'error')); 144 | mqtt_conn.stream.on('close', mqtt_conn.emit.bind(mqtt_conn, 'close')); 145 | 146 | net_client.on('connect', function() { 147 | mqtt_conn.emit('connected'); 148 | }); 149 | 150 | mqtt_conn.once('connected', function() { 151 | callback(null, mqtt_conn); 152 | }); 153 | 154 | mqtt_conn.once('error', function(err) { 155 | callback(err); 156 | }); 157 | 158 | return mqtt_conn; 159 | }; 160 | 161 | // Expose MqttClient 162 | module.exports.MqttClient = MqttClient; 163 | 164 | // Expose servers 165 | module.exports.MqttServer = MqttServer; 166 | module.exports.MqttSecureServer = MqttSecureServer; 167 | 168 | // Expose Connection 169 | module.exports.MqttConnection = MqttConnection; 170 | -------------------------------------------------------------------------------- /LessonOne/server/node_modules/mqtt/node_modules/readable-stream/test/common.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var path = require('path'); 23 | var assert = require('assert'); 24 | 25 | exports.testDir = path.dirname(__filename); 26 | exports.fixturesDir = path.join(exports.testDir, 'fixtures'); 27 | exports.libDir = path.join(exports.testDir, '../lib'); 28 | exports.tmpDir = path.join(exports.testDir, 'tmp'); 29 | exports.PORT = 12346; 30 | 31 | if (process.platform === 'win32') { 32 | exports.PIPE = '\\\\.\\pipe\\libuv-test'; 33 | } else { 34 | exports.PIPE = exports.tmpDir + '/test.sock'; 35 | } 36 | 37 | var util = require('util'); 38 | for (var i in util) exports[i] = util[i]; 39 | //for (var i in exports) global[i] = exports[i]; 40 | 41 | function protoCtrChain(o) { 42 | var result = []; 43 | for (; o; o = o.__proto__) { result.push(o.constructor); } 44 | return result.join(); 45 | } 46 | 47 | exports.indirectInstanceOf = function(obj, cls) { 48 | if (obj instanceof cls) { return true; } 49 | var clsChain = protoCtrChain(cls.prototype); 50 | var objChain = protoCtrChain(obj); 51 | return objChain.slice(-clsChain.length) === clsChain; 52 | }; 53 | 54 | 55 | exports.ddCommand = function(filename, kilobytes) { 56 | if (process.platform === 'win32') { 57 | var p = path.resolve(exports.fixturesDir, 'create-file.js'); 58 | return '"' + process.argv[0] + '" "' + p + '" "' + 59 | filename + '" ' + (kilobytes * 1024); 60 | } else { 61 | return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes; 62 | } 63 | }; 64 | 65 | 66 | exports.spawnPwd = function(options) { 67 | var spawn = require('child_process').spawn; 68 | 69 | if (process.platform === 'win32') { 70 | return spawn('cmd.exe', ['/c', 'cd'], options); 71 | } else { 72 | return spawn('pwd', [], options); 73 | } 74 | }; 75 | 76 | 77 | // Turn this off if the test should not check for global leaks. 78 | exports.globalCheck = true; 79 | 80 | process.on('exit', function() { 81 | if (!exports.globalCheck) return; 82 | var knownGlobals = [setTimeout, 83 | setInterval, 84 | global.setImmediate, 85 | clearTimeout, 86 | clearInterval, 87 | global.clearImmediate, 88 | console, 89 | Buffer, 90 | process, 91 | global]; 92 | 93 | if (global.errno) { 94 | knownGlobals.push(errno); 95 | } 96 | 97 | if (global.gc) { 98 | knownGlobals.push(gc); 99 | } 100 | 101 | if (global.DTRACE_HTTP_SERVER_RESPONSE) { 102 | knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); 103 | knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); 104 | knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); 105 | knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); 106 | knownGlobals.push(DTRACE_NET_STREAM_END); 107 | knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); 108 | knownGlobals.push(DTRACE_NET_SOCKET_READ); 109 | knownGlobals.push(DTRACE_NET_SOCKET_WRITE); 110 | } 111 | if (global.COUNTER_NET_SERVER_CONNECTION) { 112 | knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); 113 | knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); 114 | knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); 115 | knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); 116 | knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); 117 | knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); 118 | } 119 | 120 | if (global.ArrayBuffer) { 121 | knownGlobals.push(ArrayBuffer); 122 | knownGlobals.push(Int8Array); 123 | knownGlobals.push(Uint8Array); 124 | knownGlobals.push(Uint8ClampedArray); 125 | knownGlobals.push(Int16Array); 126 | knownGlobals.push(Uint16Array); 127 | knownGlobals.push(Int32Array); 128 | knownGlobals.push(Uint32Array); 129 | knownGlobals.push(Float32Array); 130 | knownGlobals.push(Float64Array); 131 | knownGlobals.push(DataView); 132 | } 133 | 134 | for (var x in global) { 135 | var found = false; 136 | 137 | for (var y in knownGlobals) { 138 | if (global[x] === knownGlobals[y]) { 139 | found = true; 140 | break; 141 | } 142 | } 143 | 144 | if (!found) { 145 | console.error('Unknown global: %s', x); 146 | assert.ok(false, 'Unknown global found'); 147 | } 148 | } 149 | }); 150 | 151 | 152 | var mustCallChecks = []; 153 | 154 | 155 | function runCallChecks() { 156 | var failed = mustCallChecks.filter(function(context) { 157 | return context.actual !== context.expected; 158 | }); 159 | 160 | failed.forEach(function(context) { 161 | console.log('Mismatched %s function calls. Expected %d, actual %d.', 162 | context.name, 163 | context.expected, 164 | context.actual); 165 | console.log(context.stack.split('\n').slice(2).join('\n')); 166 | }); 167 | 168 | if (failed.length) process.exit(1); 169 | } 170 | 171 | 172 | exports.mustCall = function(fn, expected) { 173 | if (typeof expected !== 'number') expected = 1; 174 | 175 | var context = { 176 | expected: expected, 177 | actual: 0, 178 | stack: (new Error).stack, 179 | name: fn.name || '' 180 | }; 181 | 182 | // add the exit listener only once to avoid listener leak warnings 183 | if (mustCallChecks.length === 0) process.on('exit', runCallChecks); 184 | 185 | mustCallChecks.push(context); 186 | 187 | return function() { 188 | context.actual++; 189 | return fn.apply(this, arguments); 190 | }; 191 | }; 192 | -------------------------------------------------------------------------------- /LessonOne/client/nodejs/node_modules/mqtt/node_modules/readable-stream/test/common.js: -------------------------------------------------------------------------------- 1 | // Copyright Joyent, Inc. and other Node contributors. 2 | // 3 | // Permission is hereby granted, free of charge, to any person obtaining a 4 | // copy of this software and associated documentation files (the 5 | // "Software"), to deal in the Software without restriction, including 6 | // without limitation the rights to use, copy, modify, merge, publish, 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit 8 | // persons to whom the Software is furnished to do so, subject to the 9 | // following conditions: 10 | // 11 | // The above copyright notice and this permission notice shall be included 12 | // in all copies or substantial portions of the Software. 13 | // 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | var path = require('path'); 23 | var assert = require('assert'); 24 | 25 | exports.testDir = path.dirname(__filename); 26 | exports.fixturesDir = path.join(exports.testDir, 'fixtures'); 27 | exports.libDir = path.join(exports.testDir, '../lib'); 28 | exports.tmpDir = path.join(exports.testDir, 'tmp'); 29 | exports.PORT = 12346; 30 | 31 | if (process.platform === 'win32') { 32 | exports.PIPE = '\\\\.\\pipe\\libuv-test'; 33 | } else { 34 | exports.PIPE = exports.tmpDir + '/test.sock'; 35 | } 36 | 37 | var util = require('util'); 38 | for (var i in util) exports[i] = util[i]; 39 | //for (var i in exports) global[i] = exports[i]; 40 | 41 | function protoCtrChain(o) { 42 | var result = []; 43 | for (; o; o = o.__proto__) { result.push(o.constructor); } 44 | return result.join(); 45 | } 46 | 47 | exports.indirectInstanceOf = function(obj, cls) { 48 | if (obj instanceof cls) { return true; } 49 | var clsChain = protoCtrChain(cls.prototype); 50 | var objChain = protoCtrChain(obj); 51 | return objChain.slice(-clsChain.length) === clsChain; 52 | }; 53 | 54 | 55 | exports.ddCommand = function(filename, kilobytes) { 56 | if (process.platform === 'win32') { 57 | var p = path.resolve(exports.fixturesDir, 'create-file.js'); 58 | return '"' + process.argv[0] + '" "' + p + '" "' + 59 | filename + '" ' + (kilobytes * 1024); 60 | } else { 61 | return 'dd if=/dev/zero of="' + filename + '" bs=1024 count=' + kilobytes; 62 | } 63 | }; 64 | 65 | 66 | exports.spawnPwd = function(options) { 67 | var spawn = require('child_process').spawn; 68 | 69 | if (process.platform === 'win32') { 70 | return spawn('cmd.exe', ['/c', 'cd'], options); 71 | } else { 72 | return spawn('pwd', [], options); 73 | } 74 | }; 75 | 76 | 77 | // Turn this off if the test should not check for global leaks. 78 | exports.globalCheck = true; 79 | 80 | process.on('exit', function() { 81 | if (!exports.globalCheck) return; 82 | var knownGlobals = [setTimeout, 83 | setInterval, 84 | global.setImmediate, 85 | clearTimeout, 86 | clearInterval, 87 | global.clearImmediate, 88 | console, 89 | Buffer, 90 | process, 91 | global]; 92 | 93 | if (global.errno) { 94 | knownGlobals.push(errno); 95 | } 96 | 97 | if (global.gc) { 98 | knownGlobals.push(gc); 99 | } 100 | 101 | if (global.DTRACE_HTTP_SERVER_RESPONSE) { 102 | knownGlobals.push(DTRACE_HTTP_SERVER_RESPONSE); 103 | knownGlobals.push(DTRACE_HTTP_SERVER_REQUEST); 104 | knownGlobals.push(DTRACE_HTTP_CLIENT_RESPONSE); 105 | knownGlobals.push(DTRACE_HTTP_CLIENT_REQUEST); 106 | knownGlobals.push(DTRACE_NET_STREAM_END); 107 | knownGlobals.push(DTRACE_NET_SERVER_CONNECTION); 108 | knownGlobals.push(DTRACE_NET_SOCKET_READ); 109 | knownGlobals.push(DTRACE_NET_SOCKET_WRITE); 110 | } 111 | if (global.COUNTER_NET_SERVER_CONNECTION) { 112 | knownGlobals.push(COUNTER_NET_SERVER_CONNECTION); 113 | knownGlobals.push(COUNTER_NET_SERVER_CONNECTION_CLOSE); 114 | knownGlobals.push(COUNTER_HTTP_SERVER_REQUEST); 115 | knownGlobals.push(COUNTER_HTTP_SERVER_RESPONSE); 116 | knownGlobals.push(COUNTER_HTTP_CLIENT_REQUEST); 117 | knownGlobals.push(COUNTER_HTTP_CLIENT_RESPONSE); 118 | } 119 | 120 | if (global.ArrayBuffer) { 121 | knownGlobals.push(ArrayBuffer); 122 | knownGlobals.push(Int8Array); 123 | knownGlobals.push(Uint8Array); 124 | knownGlobals.push(Uint8ClampedArray); 125 | knownGlobals.push(Int16Array); 126 | knownGlobals.push(Uint16Array); 127 | knownGlobals.push(Int32Array); 128 | knownGlobals.push(Uint32Array); 129 | knownGlobals.push(Float32Array); 130 | knownGlobals.push(Float64Array); 131 | knownGlobals.push(DataView); 132 | } 133 | 134 | for (var x in global) { 135 | var found = false; 136 | 137 | for (var y in knownGlobals) { 138 | if (global[x] === knownGlobals[y]) { 139 | found = true; 140 | break; 141 | } 142 | } 143 | 144 | if (!found) { 145 | console.error('Unknown global: %s', x); 146 | assert.ok(false, 'Unknown global found'); 147 | } 148 | } 149 | }); 150 | 151 | 152 | var mustCallChecks = []; 153 | 154 | 155 | function runCallChecks() { 156 | var failed = mustCallChecks.filter(function(context) { 157 | return context.actual !== context.expected; 158 | }); 159 | 160 | failed.forEach(function(context) { 161 | console.log('Mismatched %s function calls. Expected %d, actual %d.', 162 | context.name, 163 | context.expected, 164 | context.actual); 165 | console.log(context.stack.split('\n').slice(2).join('\n')); 166 | }); 167 | 168 | if (failed.length) process.exit(1); 169 | } 170 | 171 | 172 | exports.mustCall = function(fn, expected) { 173 | if (typeof expected !== 'number') expected = 1; 174 | 175 | var context = { 176 | expected: expected, 177 | actual: 0, 178 | stack: (new Error).stack, 179 | name: fn.name || '' 180 | }; 181 | 182 | // add the exit listener only once to avoid listener leak warnings 183 | if (mustCallChecks.length === 0) process.on('exit', runCallChecks); 184 | 185 | mustCallChecks.push(context); 186 | 187 | return function() { 188 | context.actual++; 189 | return fn.apply(this, arguments); 190 | }; 191 | }; 192 | --------------------------------------------------------------------------------