楼主: Sky-Tiger

JPPF 4.0 raises the bar in the grid computing arena

[复制链接]
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
11#
 楼主| 发表于 2014-1-28 21:48 | 只看该作者
Save the file as net-watcher.js. Most of the code here is taken from previous examples in the book, so it should look pretty familiar. The novel parts to the net-watcher program begin inside the callback function given to createServer(). This callback function does three things:
• It reports that the connection has been established (both to the client with connection.write and to the console).
• It begins listening for changes to the target file, saving the returned watcher object. This callback sends change information to the client using connection.write.
• It listens for the connection’s close event so it can report that the subscriber has disconnected and stop watching the file, with watcher.close().
Finally, notice the callback passed into server.listen() at the end. Node invokes this function after it has successfully bound port 5432 and is ready to start receiving connections.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
12#
 楼主| 发表于 2014-1-28 21:49 | 只看该作者
Listening on Unix Sockets
To see how the net module uses Unix sockets, let’s modify the net-watcher program to use this kind of communication channel. Keep in mind that Unix sockets work only on Unix-like environments.
Open the net-watcher.js program and change the server.listen() section to this:
server.listen('/tmp/watcher.sock', function() {
  console.log('Listening for subscribers...');
});
Save the file as net-watcher-unix.js, then run the program as before: $ node --harmony net-watcher-unix.js target.txt
Listening for subscribers...
To connect a client, we now need nc instead of telnet. nc is short for netcat, a TCP/UDP socket utility program that also supports Unix sockets.
$ nc -U /tmp/watcher.sock
Now watching target.txt for changes...
Unix sockets can be faster than TCP sockets because they don’t require invoking network hardware. However, they’re local to the machine.
That concludes the basics of creating network socket servers in Node. We discovered how to create socket servers and connect to them using common client utility programs like telnet and nc. This framework will supply the backdrop for the rest of the examples in the chapter.
Next, we’ll beef up our service by transforming the data into a parsable format. This will put us in position to develop custom client applications.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
13#
 楼主| 发表于 2014-1-28 21:49 | 只看该作者
Implementing a Messaging Protocol
We’ve just explored how to create socket servers that listen for incoming connections in Node. So far, our example programs have sent plain-text messages that are meant to be read by a human. In this section, we’ll design and implement a better protocol.
A protocol is a set of rules that defines how endpoints in a system communi- cate. Any time you develop a networked application in Node, you’re working
Chapter 3. Networking with Sockets • 28
with one or more protocols. Here we’ll create a protocol based on passing JSON encoded messages over TCP.1
JSON is incredibly prevalent in Node.js programming and in JavaScript programming generally. We’ll use it extensively for data serialization and configuration throughout the book. JSON is significantly easier to program clients against than plain text, and it’s still human-readable.
We’ll implement client and server endpoints that use our new JSON-based protocol. This will give us opportunities to develop test cases and refactor our code into reusable modules.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
14#
 楼主| 发表于 2014-1-28 21:50 | 只看该作者
Serializing Messages with JSON
Let’s develop the message-passing protocol that uses JSON to serialize mes- sages. Each message is a JSON-serialized object, which is a hash of key-value pairs. Here’s an example JSON object with two key-value pairs:
{"key":"value","anotherKey":"anotherValue"}
The net-watcher service we’ve been developing in this chapter sends two kinds
of messages that we need to convert to JSON:
• When the connection is first established, the client receives the string
Now watching target.txt for changes...
• Whenever the target file changes, the client receives a string like this: File ’target.txt’ changed: Sat Jan 12 2013 12:35:52 GMT-0500 (EST)
We’ll encode the first kind of message this way:
{"type":"watching","file":"target.txt"}
The type field indicates that this is a watching message—the specified file is now
being watched.
The second type of message is encoded this way: {"type":"changed","file":"target.txt","timestamp":1358175733785}
Here the type field announces that the target file has changed. The timestamp field contains an integer value representing the number of milliseconds since midnight, January 1, 1970. This happens to be an easy time format to work with in JavaScript. For example, you can get the current time in this format with Date.now().
Implementing a Messaging Protocol • 29

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
15#
 楼主| 发表于 2014-1-28 21:50 | 只看该作者
Overview
The way we manipulate sequences in a procedural language is more closely related to the way a computer works than to the way humans think. Iteration is a step above the dreaded goto statement, and it’s intended to be easily translated into machine code more than it’s intended to be easy to use.
Filter-Map-Reduce gives us a more declarative way to do many sequence manipulations. Instead of writing code that reorders or alters the elements in a sequence by working its way iteratively through them, element by element, we can work at a higher level by using a filter function to select the elements we care about: map to transform each element and reduce, sometimes known as fold, to combine the results.
Filter-Map-Reduce replaces many, though not all, iterative algorithms used by object-oriented programmers with declarative code.
The main advantage to Filter-Map-Reduce over iteration is code clarity. A well-written Filter-Map-Reduce takes a fraction of the code that the iterative equivalent takes. It can often be read at a glance, like prose, by an experienced practitioner, while the iterative solution requires parsing at least one loop and a conditional.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
16#
 楼主| 发表于 2014-1-29 21:38 | 只看该作者
Express uses the configuration system internally, allowing you to customize how Express behaves, but it’s also available for your own use. For the application you’re building in this chapter, you’ll only use a single setting, photos, whose value is the directory that will be used to store the uploaded images. This value could be changed in production to permit saving and serving photos from a different volume with more disk space:
app.configure(function(){
  ...
  app.set('photos', __dirname + '/public/photos');
... });
app.configure('production', function(){
  ...
  app.set('photos', '/mounted-volume/photos');
... });
Express also provides Boolean variants of app.set() and app.get(). For example, app.enable(setting) is equivalent to app.set(setting, true), and app.enabled (setting) can be used to check if the value was enabled. The methods app.disable (setting) and app.disabled(setting) complement the truthful variants.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
17#
 楼主| 发表于 2014-1-29 21:40 | 只看该作者
In this chapter’s application, we’ll utilize EJS templates, though as previously mentioned almost any template engine in the Node community can be used. If you’re not famil- iar with EJS, don’t worry. It’s similar to tem- plating languages found in other languages (PHP, JSP, ERB). We’ll cover some basics of EJS in this chapter, but we’ll discuss EJS and several other template engines in greater detail in chapter 11.
Whether it’s rendering an entire HTML
page, an HTML fragment, or an RSS feed, rendering views is crucial for nearly every application. The concept is simple: you pass data to a view, and that data is trans- formed, typically to HTML for web applications. You’re likely familiar with the idea of views, because most frameworks provide similar functionality; figure 8.11 illustrates how a view forms a new representation for the data.
Express provides two ways to render views: at the application level with app .render(), and at the request or response level with res.render(), which uses the for- mer internally. In this chapter, you’ll only use res.render(). If you look in ./routes/ index.js, a single function is exported: the index function. This function invokes res.render() in order to render the ./views/index.ejs template, as shown in the fol- lowing code:
        exports.index = function(req, res){
          res.render('index', { title: 'Express' });
};
In this section, you’ll see how to do the following:
 Configure the Express view system  Look up view files
 Expose data when rendering views
Before looking at res.render() more closely, let’s configure the view system.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
18#
 楼主| 发表于 2014-1-29 21:40 | 只看该作者
In this chapter’s application, we’ll utilize EJS templates, though as previously mentioned almost any template engine in the Node community can be used. If you’re not famil- iar with EJS, don’t worry. It’s similar to tem- plating languages found in other languages (PHP, JSP, ERB). We’ll cover some basics of EJS in this chapter, but we’ll discuss EJS and several other template engines in greater detail in chapter 11.
Whether it’s rendering an entire HTML
page, an HTML fragment, or an RSS feed, rendering views is crucial for nearly every application. The concept is simple: you pass data to a view, and that data is trans- formed, typically to HTML for web applications. You’re likely familiar with the idea of views, because most frameworks provide similar functionality; figure 8.11 illustrates how a view forms a new representation for the data.
Express provides two ways to render views: at the application level with app .render(), and at the request or response level with res.render(), which uses the for- mer internally. In this chapter, you’ll only use res.render(). If you look in ./routes/ index.js, a single function is exported: the index function. This function invokes res.render() in order to render the ./views/index.ejs template, as shown in the fol- lowing code:
        exports.index = function(req, res){
          res.render('index', { title: 'Express' });
};
In this section, you’ll see how to do the following:
Configure the Express view system  Look up view files
Expose data when rendering views
Before looking at res.render() more closely, let’s configure the view system.

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
19#
 楼主| 发表于 2014-1-29 21:41 | 只看该作者
Rendering views
In this chapter’s application, we’ll utilize EJS templates, though as previously mentioned almost any template engine in the Node community can be used. If you’re not famil- iar with EJS, don’t worry. It’s similar to tem- plating languages found in other languages (PHP, JSP, ERB). We’ll cover some basics of EJS in this chapter, but we’ll discuss EJS and several other template engines in greater detail in chapter 11.
Whether it’s rendering an entire HTML
page, an HTML fragment, or an RSS feed, rendering views is crucial for nearly every application. The concept is simple: you pass data to a view, and that data is trans- formed, typically to HTML for web applications. You’re likely familiar with the idea of views, because most frameworks provide similar functionality; figure 8.11 illustrates how a view forms a new representation for the data.
Express provides two ways to render views: at the application level with app .render(), and at the request or response level with res.render(), which uses the for- mer internally. In this chapter, you’ll only use res.render(). If you look in ./routes/ index.js, a single function is exported: the index function. This function invokes res.render() in order to render the ./views/index.ejs template, as shown in the fol- lowing code:
        exports.index = function(req, res){
          res.render('index', { title: 'Express' });
};

使用道具 举报

回复
论坛徽章:
350
2006年度最佳版主
日期:2007-01-24 12:56:49NBA大富翁
日期:2008-04-21 22:57:29地主之星
日期:2008-11-17 19:37:352008年度最佳版主
日期:2009-03-26 09:33:53股神
日期:2009-04-01 10:05:56NBA季后赛大富翁
日期:2009-06-16 11:48:01NBA季后赛大富翁
日期:2009-06-16 11:48:01ITPUB年度最佳版主
日期:2011-04-08 18:37:09ITPUB年度最佳版主
日期:2011-12-28 15:24:18ITPUB年度最佳技术原创精华奖
日期:2012-03-13 17:12:05
20#
 楼主| 发表于 2014-1-29 22:14 | 只看该作者
DEFAULT TEMPLATE ENGINE
When express(1) generated the application, the view engine setting was assigned ejs because EJS was the template engine selected by the -e command-line option. This setting enables you to render index rather than index.ejs. Otherwise Express requires the extension in order to determine which template engine is to be used.
You might be wondering why Express even considers extensions. The use of exten- sions allows you to use multiple template engines within a single Express application, while providing a clean API for common use cases, because most applications will use one template engine.
Suppose, for example, you find writing RSS feeds easier with another template engine, or perhaps you’re migrating from one template engine to another. You might use Jade as the default, and EJS for the /feed route, as indicated in the following list- ing by the .ejs extension.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表