From 31ba3cde17e8a164164319bfbfff6c65191e97e7 Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 14 May 2009 20:34:14 +0200 Subject: [PATCH] Rename TCP classes to sit in node hierarchy. --- node.html | 48 ++++++++++++++++++++----------- src/net.cc | 4 +-- src/node.cc | 6 ++-- test/test-pingpong.js | 4 +-- test/test_http.js => test_http.js | 4 +-- 5 files changed, 41 insertions(+), 25 deletions(-) rename test/test_http.js => test_http.js (83%) diff --git a/node.html b/node.html index fc72e5ec9df..2cfaa6760ab 100644 --- a/node.html +++ b/node.html @@ -30,7 +30,7 @@ body { } #toc a { color: #777; } -h1, h2, h3 { color: inherit; } +h1, h2, h3 { color: #aaf; } h1 { margin: 2em 0; @@ -42,7 +42,6 @@ h1 { h1 a { color: inherit; } h2 { - margin: 2em 0; font-size: 45px; line-height: inherit; font-weight: bold; @@ -58,13 +57,18 @@ h3 { pre, code { font-family: monospace; font-size: 13pt; + color: #eae; +} + +pre { + padding-left: 2em; } dl { } dt { - color: #f55; + color: #aaf; font-weight: bold; } @@ -76,6 +80,11 @@ dd { a { color: #cd5; text-decoration: none; } a:hover { text-decoration: underline; } +.highlight { + background: #733; + padding: 0.2em 0; +} + node.js @@ -104,10 +113,11 @@ a:hover { text-decoration: underline; }

Node

-

Node is a purely evented I/O framework for V8 javascript. For example, this is a -simple web server which responds with "Hello World" after waiting two -seconds: +

Node is a purely asynchronous I/O framework for V8 javascript. + +

This is an example of a web server written with Node which responds with +"Hello World" after waiting two seconds:

 new node.http.Server(function (msg) {
   setTimeout(function () {
@@ -351,7 +361,8 @@ msg.sendHeader( 200
 
 

Modules

-

Node has simple module loading. Here is an example of loading a module: +

Node has simple module loading. Here is an example. This is the file +foo.js:

 include("mjsunit");
 
@@ -362,14 +373,17 @@ function onLoad () {
 

Here the module mjsunit has provided the function assertEquals(). -

The file mjsunit.js must be in the same directory for this -to work. The include() function will take all the exported -objects from the file and put them into the global namespace. Because file -loading does not happen instantaniously, and because Node has a policy of -never blocking, the callback onLoad() is provided to notify the -user when all the exported functions are completely loaded. +

The module file, mjsunit.js, must be in the same directory +as foo.js for include() to work. The +include() function will insert all the exported objects from the +module into the global namespace. -

To export an object, add to the special object exports. +

Because file loading does not happen instantaneously, and because Node +has a policy of never blocking, the callback onLoad() is +provided to notify the user when all the exported functions are completely +loaded. + +

To export an object, add to the special object exports. Let's look at how mjsunit.js does this

@@ -381,7 +395,7 @@ function deepEquals (a, b) {
   // ...
 }
 
-exports.assertEquals = function (expected, found, name_opt) {
+exports.assertEquals = function (expected, found, name_opt) {
   if (!deepEquals(found, expected)) {
     fail(expected, found, name_opt);
   }
@@ -393,7 +407,7 @@ exported and remain private to the module.
 

In addition to include() a module can use require(). Instead of loading the exported objects into the global namespace, it will return a namespace object. Again, the members of -the namespace object can only be guarenteed to exist after the +the namespace object can only be guaranteed to exist after the onLoad() callback is made. For example:

 var mjsunit = require("mjsunit");
diff --git a/src/net.cc b/src/net.cc
index f4f7320b3ee..fb1defe0064 100644
--- a/src/net.cc
+++ b/src/net.cc
@@ -57,7 +57,7 @@ Connection::Initialize (v8::Handle target)
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "fullClose", v8FullClose);
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "forceClose", v8ForceClose);
 
-  target->Set(String::NewSymbol("TCPConnection"), constructor_template->GetFunction());
+  target->Set(String::NewSymbol("Connection"), constructor_template->GetFunction());
 }
 
 Connection::Connection (Handle handle, Handle protocol_class)
@@ -357,7 +357,7 @@ Acceptor::Initialize (Handle target)
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "listen", v8Listen);
   NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", v8Close);
 
-  target->Set(String::NewSymbol("TCPServer"), constructor_template->GetFunction());
+  target->Set(String::NewSymbol("Server"), constructor_template->GetFunction());
 }
 
 Acceptor::Acceptor (Handle handle, Handle protocol_class,  Handle options) 
diff --git a/src/node.cc b/src/node.cc
index 44a374c68b1..5790ea12b3c 100644
--- a/src/node.cc
+++ b/src/node.cc
@@ -271,8 +271,10 @@ main (int argc, char *argv[])
 
   File::Initialize(g);
 
-  Acceptor::Initialize(g);
-  Connection::Initialize(g);
+  Local tcp = Object::New();
+  node->Set(String::New("tcp"), tcp);
+  Acceptor::Initialize(tcp);
+  Connection::Initialize(tcp);
 
   Local http = Object::New();
   node->Set(String::New("http"), http);
diff --git a/test/test-pingpong.js b/test/test-pingpong.js
index e23c49861b5..99ad0bb66dc 100644
--- a/test/test-pingpong.js
+++ b/test/test-pingpong.js
@@ -57,9 +57,9 @@ function Pinger (socket) {
 }
 
 function onLoad() {
-  var server = new TCPServer(Ponger);
+  var server = new node.tcp.Server(Ponger);
   server.listen(port);
 
-  var client = new TCPConnection(Pinger);
+  var client = new node.tcp.Connection(Pinger);
   client.connect(port);
 }
diff --git a/test/test_http.js b/test_http.js
similarity index 83%
rename from test/test_http.js
rename to test_http.js
index 58d5c860614..86598a41376 100644
--- a/test/test_http.js
+++ b/test_http.js
@@ -1,8 +1,8 @@
 p({hello: "world"});
 new node.http.Server(function (msg) {
-  setTimeout(function () {
+//  setTimeout(function () {
     msg.sendHeader(200, [["Content-Type", "text/plain"]]);
     msg.sendBody("Hello World");
     msg.finish();
-  }, 2000);
+//  }, 1);
 }).listen(8000, "localhost");