<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Computer, Electron and Technology &#187; java</title>
	<atom:link href="http://www.donevii.com/post/tag/java/feed" rel="self" type="application/rss+xml" />
	<link>http://www.donevii.com</link>
	<description>关注技术、移动互联网以及一切 GEEK &#38; NERD 的事情</description>
	<lastBuildDate>Wed, 21 Dec 2011 10:49:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>二十一世纪让人悲伤的七个原因</title>
		<link>http://www.donevii.com/post/640.html</link>
		<comments>http://www.donevii.com/post/640.html#comments</comments>
		<pubDate>Mon, 01 Dec 2008 11:54:52 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[生活]]></category>
		<category><![CDATA[类]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/640.html</guid>
		<description><![CDATA[裸照测试, 它是这样的一个测试: 假如说有一张照片, 照的是你正裸体的干一件事,这个事会让你和你的家人甚至以后的几代人都会感到羞耻. &#160;打个比方, 人兽交. &#160;问问你自己, 身边能有几... ]]></description>
			<content:encoded><![CDATA[<p><b>裸照测试</b>, 它是这样的一个测试: 假如说有一张照片, 照的是你正裸体的干一件事,<br />这个事会让你和你的家人甚至以后的几代人都会感到羞耻. &nbsp;打个比方, 人兽交. &nbsp;问问你自己, 身边能有几个人可以信任到能够共同面对这张照片.<br />&nbsp;如果你像下面我们说的那样, 你可能至多会有两个. <br /> 
<p> 事实更令人沮丧,研究表明大约四分之一的人没有一个可以信赖的朋友.                                               </p>
<p align="center"> <a target="ned" href="http://www.nedroid.com/"><img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/sadbear1.jpg" border="0" /></a> </p>
<p>                                                <font size="1">The Sad Bear 1, by <a class="b" target="ned" href="http://www.nedroid.com/">Nedroid</a></font> </p>
<p> 
<p> &nbsp;我们发现人们拥有的亲密朋友的数量正在快速的戏剧性的减少,特别是最近的20年里.  为什么会这样?</p>
<p></p>
<p>#1.   &nbsp;我们的生活中缺少让人讨厌的人.
<div id="container1">
<div id="Title_box">                                              </div>
<p> 
<p> &nbsp;这不是讽刺.  &nbsp; 讨厌的人和事能够锻炼你忍耐的性格, 就像面对乙醇或其它难闻的气味.  &nbsp;如果我们尽量的去避免烦恼,那么,我们处理烦恼的能力也就越差. </p>
<p> 
<p>&nbsp;问<br />题就出在，我们使用科技手段给自己建立里一个令人恐惧的，四处蔓延的web网络，而这个网络就是要让我们躲避那些让人烦恼的朋友。<br />&nbsp;不是吗？所有的圣诞采购都在网上进行，这样就避免了和那些胖大嫂为了同一个商品而发生购物车之间的磕磕碰碰。<br />&nbsp;花5000美元买一个家庭影院系统，这样就可以坐在家里看电影，像在电影院里有个淘气的孩子老踢你的后座的事情再也不会发生了。<br />&nbsp;天，连租DVD也只需要到Netflix网站上，根本不需要到音像店里和那些笨头笨脑的登记影碟的孩子打交道。 </p>
<p> 
<p>&nbsp; 老老实实的在候诊室里等着大夫？ &nbsp;遗憾，与邻座的散发着难闻气味的老人聊天是不可能的。<br />我们会用iPod听音乐,发短信给朋友或者玩任天堂的DS 把烦恼踢出我们的世界&nbsp; </p>
<p> 
<p align="center"> <a href="http://www.outofbalance.org/days/2005/day051227.php"><img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/waitingroom.jpg" border="0" /></a> </p>
<p>                                                <font size="1">&nbsp;来自outofbalance.org</font> </p>
<p>&nbsp;如果现在的生活真的没有烦恼,那真的太好了. 但是事实并不是这样&nbsp; 而且永远也不会这样.&nbsp; 只要你有需求,你就要不时的和你不能忍受的人打交道&nbsp;<br />我们已经慢慢不懂得怎样和陌生人打交道, 不懂得忍受他们刺耳的声音,还有白痴的幽默,不能忍受他们的体味还有吱嘎作响的鞋.<br />那么,什么才能令你走到外面这个世界,一个你不能控制,一个令你想尖叫的世界.&nbsp;
<p align="center"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/leafhead.jpg" /> </p>
<p>                                                <font size="1">Oh, yeah. Right in the crotch, buddy.</font> </p>
<p> 
</p>
<p>                                            </div>
<p> 
<div id="container1"> 
<div id="Title_box"> 
<div class="Title2">#2.  我们也没有足够的令人讨厌的朋友.</div>
<p>                                              </div>
<p> 
<p>我们大部分人生活的地方都充满了我们不能容忍的人.&nbsp; &nbsp;当你是小孩的时候,也许你就会发现,在你小学教室里,装满了很多和你兴趣爱好都不同的同学,而你又不能选择的同学. 也许你会经常感到失望.&nbsp; </p>
<p> 
<p>但<br />是,你已经长大了.&nbsp; 还有,如果你是一个忠实的DragonForce(英国某乐队)迷,你可以到他们的论坛上找到一大堆和你一样喜欢这个乐队的人.<br />或者更可以自己开一个私密的房间,只和你的好友在一齐,其他人都不能进来.&nbsp; 可以告别和兴趣真的不同的人打交道的烦人,无聊,痛苦的过程 &nbsp;<br />都是旧社会的问题,就像以前你要到河边洗衣服,或者要打猎穿兽皮. </p>
<p> 
<p>问题是友好的解决人与人之间的矛盾,在社会上是很重要的生存之道.&nbsp; 事实上,如果你认真的想一下,你能友好的解决和你有矛盾的人的关系.,这就是所谓的社会. &nbsp; 只要和那些有不同思想,不同性格的人一起生活,合作,通常会存在摩擦. </p>
<p> 
<p> 50年前,你不得不坐在一个拥挤的房间里看一部电影&nbsp; 你根本不能选择,除非你不想看这部电影.&nbsp;  当你买了新车,街上所有的人都会站在你院子里看来看去.&nbsp;  &nbsp;你能肯定有些人里面有不少傻B. </p>
<p> 
<p align="center"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/mayberry.jpg" /> </p>
<p>                                                <font size="1">Your parents, circa 1982</font> </p>
<p>但是总的来说,那时的人还是对他们的工作感到高兴,对生活感到满足的.&nbsp; 是因为:他们比现在有更多的朋友.&nbsp;
<p> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/emo2.jpg" align="right" width="200" height="200" /> &nbsp;没错. 虽然他们几乎都是不能根据共同兴趣来选择朋友,(靠,那时候只要是你邻居都是你朋友了啦.),但是他们仍然能有很多很要好的朋友,起码比现代人来说,那时候的人更值得相信. </p>
<p> 
<p>那<br />就是说,很明显,<br />在你和别人发生第一次争吵,当你为争面子说:&#8221;他们不听我喜欢的歌,是他们不懂得欣赏.&#8221;之后,在某程度上,除了共同兴趣外,你们会感到互相熟悉了,这所<br />谓不打不相识. 那就是说,毕竟人都是需要社交的.&nbsp;&nbsp; 而那容忍别人和事的能力,就是你能正常生活在这充满和你不同的人的世界里唯一的保证.<br />否则,你会变另类.&nbsp; 这是有科学根据D&#8230;&nbsp; </p>
<p> 
</p>
<p>                                            </div>
<p> 
<div id="container1"> 
<div id="Title_box"> 
<div class="Title2">#3.    文字交流是一个很白痴的交流方式.&nbsp;</div>
<p>                                              </div>
<p> 
<p> 我有个朋友会讽刺的说,&#8221;不用了,谢谢.&#8221;&nbsp; 他的意思是,&#8221;我宁愿死也不要&#8221;.他说最后两个字的时候带讽刺的味道,让你知道他的意思.&nbsp; 当你问他&#8221;想不想去看罗伯施奈德最新的电影?&#8221;他会说&#8221;不用了,谢谢.&#8221; </p>
<p> 
<p>  有一天,我们用这样的文字交流:&nbsp; </p>
<p> <br />
<blockquote> <font face="courier"> 我&#8221;喂,要不要试下剩下的红辣椒,我亲手做的啊.&#8221;&nbsp; </p>
<p>                                                他:&#8221;不用了,谢谢. &#8221; </font></p></blockquote>
<p><font face="courier">                                              </font>
<p><font face="courier"> 我看到就很生气.&nbsp; 我很喜欢我做的红辣椒&nbsp; 我都做了4天了.&nbsp; 是我自己磨的胡椒粉, 那牛肉也很贵,是手打牛肉. &nbsp;而现在我的好意就让他这样恶毒地拒绝了. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> 后来我六个月不和他说话.&nbsp; 他之后发了封邮件给我,我看都不看就回了, 还装了个死老鼠在里面. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">最后是我太太去找他弄清楚了他那句&#8221;不用了,谢谢.&#8221;并不是反话,是&#8221;真的不用了,很感谢你&#8221;,原来他的冰箱已经满了. </font></p>
<p><font face="courier">                                              </font>
<p align="center"><font face="courier"> <a href="http://www.nedroid.com/"><img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/sadbear2.jpg" border="0" /></a> </p>
<p>                                                <font size="1">The Sad Bear #2, by <a href="http://www.nedroid.com/">Nedroid</a></font> </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> 所以我们是不是需要一个研究结果来告诉我们,你每写一封邮件,里面可能就有百分之四十的内容会令别人误会.&nbsp;  嗯,真的有人研究了.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">你<br />有多少没见过真人的网友?&nbsp;&nbsp; 当你们用文字交流的时候,有百分之四十内容不是真的表达你自己的意思,你觉得你们真的了解对方吗?&nbsp;<br />有人因为你在留言板,聊天室或者其他什么地方的言论而不喜欢你,是不是你真的那么令人讨厌呢?&nbsp; 又或者,其实是因为这百分之四十的误会.&nbsp;<br />还有,那些喜欢你的人呢?&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp;我们很多人都在纯粹为了虚构好友量,在My Space 上积累了大批的所谓好友. 但是,问题来了.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> </font></p>
<p><font face="courier">                                            </font></div>
<p><font face="courier">                                            </font>
<div id="container1"><font face="courier">                                              </font>
<div id="Title_box"><font face="courier">                                                </font>
<div class="Title2"><font face="courier">#4. 网络交友只会让我们更加寂寞.&nbsp;</font></div>
<p><font face="courier">                                              </font></div>
<p><font face="courier">                                              </font>
<p><font face="courier"> 当人们面对面交流的时候, 说话的内容占了实际表达意思多少比例? 此外还有身体语言和声音语气. &nbsp;猜一下。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"><br />只有百分之七.其余百分之九十三不是语言,是有研究得出的数据.&nbsp; 是的,我也不知道他们怎么算出来的.&nbsp; 他们有个机器吧.&nbsp; 但是我们不需要它&nbsp;<br />我的意思是,醒醒吧.&nbsp; 我们很多幽默都是来自于讽刺,而讽刺就是说话的时候语气和内容不相符.&nbsp; 就像我朋友说的:&#8221;不用啦,谢谢.&#8221;&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> 你不用等一个女孩亲口说她喜欢你&nbsp; 她眼力的火花,她的姿态,她塞你的脸到她胸前的方式,都告诉你,她喜欢你.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/emo3.jpg" align="right" width="175" height="145" />&nbsp;这<br />就是问题的关键. 人类通过这种潜意识去理解别人情绪的能力是很重要的.&nbsp; &nbsp;如果一个小孩天生没有这个能力,我们会认为他是智力有缺陷的<br />&nbsp;如果有人的这个能力异常发达的话,我们称之为&#8221;魅力&#8221;,他极有可能成为电影明星和政治家. 不是因为他们说了什么,而是<br />他们散发出来的能量,令我们感觉良好. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">当我们生活在文字世界里,所有的一切都没了.&nbsp; &nbsp;还有一个奇怪的理论:由于缺少对别人情绪的理解,我们每看到一句话,都会从我们自己的情绪去理解它. 我之所以以为我那个朋友说的话是讽刺,是因为我那时候心情并不是很好. 带着那种情绪,我是希望能有人给我骂的.&nbsp; </font></p>
<p><font face="courier"> 更糟糕的是,如果我继续这样聊下去,我的情绪不会改变.&nbsp; 毕竟,总有人说些我不愿意听的话.&nbsp;<br />&nbsp;当然我会很沮丧. 是我在和这个世界斗争! </font>
<p><font face="courier"> 不是这样的,我只是想有人能搭搭我肩膀,赶走我的不好情绪.&nbsp; 这就是我要说的第五点.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> </font></p>
<p><font face="courier">                                            </font></div>
<p><font face="courier">                                            </font>
<div id="container1"><font face="courier">                                              </font>
<div id="Title_box"><font face="courier">                                                </font>
<div class="Title2"><font face="courier">#5. 我们受到的批评不够.&nbsp;</font></div>
<p><font face="courier">                                              </font></div>
<p><font face="courier">                                              </font>
<p><font face="courier">&nbsp;没有好朋友最糟糕的地方不是没有生日派对可以玩,不是自己凄凉的对着墙打乒乓球. 都不是,最糟糕的是缺少真正的批评&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">我<br />上网的时候,别人叫我&#8221;同性恋&#8221;叫了几乎104.165次.&nbsp; 我就做了个Excel 表.&nbsp; 还有人叫我&#8221;混蛋&#8221;,&#8221;<br />鸡头黄鼠狼&#8221;,&#8221;日骆驼&#8221;,&#8221;阴道饼&#8221;,&#8221;吃屎的&#8221;,&#8221;猪肉刀&#8221;,&#8221;王八蛋&#8221;,&#8221;屎口哨&#8221;,&#8221;雷阴道&#8221;,&#8221;屁王&#8221;,&#8221;屎绒&#8221;,&#8221;瘤精灵&#8221;,&#8221;无聊&#8221;.<br />&nbsp; (译者:-_-||&#8230;.) </font></p>
<p><font face="courier">                                              </font>
<p align="center"><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/insultexcel.jpg" /> </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">全<br />部都没关系,因为没有人真的了解我,能说中我.&nbsp; &nbsp;我给人侮辱很多,但是给人批评非常少. 千万别搞乱这两个词.<br />&nbsp;侮辱是指某个讨厌你的人发出的噪音去表达他们的不满. 一只会吠的狗.&nbsp; 批评是指有人想去帮助你,指出你自己都不知道的缺点.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p align="center"><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/tranny.jpg" /> </p>
<p>                                                <font size="1">Above:  A flamboyant transvestite with about </p>
<p>                                                five times as many friends as the average person</font> </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp;可悲的是,有非常多的人根本没有那样的谈话. &nbsp;就好像&#8221;你知道吗,所有人都生你的气,就因为你昨晚说的话,但是没人敢告诉你,因为大家都害怕你了.&#8221;这样的话,这样真诚的话.  那些可怕的 ,笨拙的,悲痛的,不舒服的时刻,只有一些能看穿你的人会告诉你. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/emo1.jpg" align="right" /><br />电子邮件和文字聊天是阻止真诚对话的有效工具.&nbsp;&nbsp; 用文字,当你喜欢的时候可以回复.&nbsp; 你可以评估你的话语.&nbsp; 你可以选择问题来回答.&nbsp;<br />在网络另一头的人看不到你的脸,看不到你在紧张,察觉不出你在撒谎.&nbsp;<br />&nbsp;你几乎能控制一切,所以,没人能撕下你的面具,永远看不到你的缺点,永远不知道你所做过的蠢事.<br />&nbsp;怪癖,丢脸,缺点这些组成友谊关系的重要元素,都消失了. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">&nbsp;浏览一下人们在My Space的页面,看看他们为自己而虚构的人物性格. &nbsp;如果你通过写博客,已经把自己塑造成一个神秘的夜游大师,的确很难让你说出事实你是怎样去那个舞会,还有在舞池上腹泻. 你永远不能做真正的自己, 那是一种非常寂寞的感觉 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">  总的来说&#8230;&#8230; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> </font></p>
<p><font face="courier">                                            </font></div>
<p><font face="courier">                                            </font>
<div id="container1"><font face="courier">                                              </font>
<div id="Title_box"><font face="courier">                                                </font>
<div class="Title2"><font face="courier">#6. 我们是现代机器的牺牲品.&nbsp;</font></div>
<p><font face="courier">                                              </font></div>
<p><font face="courier">                                              </font>
<p><font face="courier"> 看这文章的一大部分人都会说,&#8221;我就是很沮丧.&#8221;&nbsp; &nbsp;人们都饿坏了! 美国变成了德国纳粹时代.&nbsp; 我父母在看完白痴电视节目后还要继续讨论几个小时.&nbsp; 世界上的人们正在死于一些毫无意义的战争.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">但<br />是,我们是怎样从一个比我们老一辈日子更负面的世界里 发奋? 或者说更老一辈.&nbsp; 回到那个时候,人没有那么长命,婴儿也经常早死.&nbsp;<br />疾病是更普遍&nbsp; 在那时候,如果你的好友搬走了,你们唯一的联络方法就是一支笔,一张纸和一张邮票.&nbsp;<br />我们现在有伊拉克,但是老一辈有越南(比伊拉克死多50倍人) ,再老一辈有第二次世界大战(比伊拉克死多1000倍人)<br />&nbsp;你们的祖父是生长在一个没有空调的时代. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> 在各个方面,我们自然比以前生活的更好,但是TMD网上的新闻都不知道是真的假的.&nbsp; Why? </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> 你问问你自己,如果有个音乐网站发了篇文章叫&#8221; Fall Out Boy是一支不错的乐队&#8221;,同一天还有另外一篇文章叫&#8221;专家说,Fall Out Boy是TMD一百年里最垃圾的乐队.&#8221;你猜哪篇文章会更热门. &nbsp;第二篇肯定火了. 夸张制造口碑.&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">你<br />看了多少写新闻的博客&nbsp; 写这个的人都知道一个事实.&nbsp;<br />&nbsp;每个网站都像狗和狗打架似的在争流量,(就算他们网站没有广告,他们会觉得多人看就满足了.)所以他们都会选一些奇怪的,激动的事情来写.<br />其他的网站开始模仿同样的手法.&nbsp; 你愿意的话,你可以在这充满温暖的不流动的的水的池塘里游一整天,(意思是看一天都是垃圾新闻) </font></p>
<p><font face="courier">                                              </font>
<p align="center"><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/leafhead.jpg" /> </p>
<p>                                                <font size="1"> Actually, if you count the guy holding the camera, this man </p>
<p>                                                statistically has more friends than most of us do. </font> </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">&nbsp;只有在这样的环境下,才会有那白痴的911阴谋论出现(说是布什主脑的,由FDNY炸毁大厦,还有那飞机是全息图.) 他们都说,每个说反对的政治家都是希特勒,每个选举都是怪异的启示录.&nbsp; 都是为了让你去阅读&nbsp; </font></p>
<p><font face="courier">                                              </font>
<p align="center"><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/911conspiracy.jpg" /> </p>
<p>                                                <font size="1">9/11 photos. Circled:  Conspiracy</font> </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">当<br />然这些在以前是不会发生的.&nbsp; &nbsp;我们中的一些人会记得那时候电视只有3个台. 没错.&nbsp; 三个.&nbsp; 我现在说的是80后.&nbsp;<br />&nbsp;所以有些东西都统一了,我们都看同样的新闻,都是同样的观点. 就算这些观点是弱智还有错的,<br />就算有些新闻是因为违法而不能报道的,我们都能分享它. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"><br />完了.&nbsp;<br />&nbsp;肯定是不会再有&#8221;混合媒体&#8221;的了,以前我们有异议是因为看了相同的新闻但是有不同的理解,现在我们有异议是因为我们看的是完全不同的奇怪的新闻.<br />&nbsp;当我们连基本的真相都不能认同的时候,小小的不同就成了很大的矛盾. &nbsp;那持续的点点优越感带来了不断的紧张和不安. </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> 过去我们人类有很多天生的方法来释放这种焦虑。 &nbsp;但如今&#8230; </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> </font></p>
<p><font face="courier">                                            </font></div>
<p><font face="courier">                                            </font><br /><font face="courier">                                              </font>
<div id="Title_box"><font face="courier">                                                </font>
<div class="Title2"><font face="courier">#7.  &nbsp;我们觉得生活没有价值，因为我们确实没有价值。</font></div>
<p><font face="courier">                                              </font></div>
<p><font face="courier">                                              </font>
<p><font face="courier"> 去找更多的在线朋友，这有个好处，而且是没人会谈到： </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp;他们对你几乎没有要求。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"><br />&nbsp;的确，当他们有失恋时，你可以从感情上帮助他们，安慰他们，甚至能将他们从自杀的冲动中解救出来。 &nbsp;但现实世界的人们会强加你一大堆的讨厌的要求。<br />&nbsp;浪费你整个下午去给他们修电脑。 &nbsp;跟他们一起去参加葬礼。 &nbsp;用你的车带着他们四处奔走，直到他们从银行哪里拿回自己的东西。<br />&nbsp;正当你收拾好了要看Discovery频道的 Dirty Jobs<br />马拉松时，他们不请自来，而且告诉你他们如此的饥饿，直到把你的三明治分给他们一半。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp;然而在聊天工具里，或论坛里，或魔兽争霸里，你却能有更多的支配权。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"><br />&nbsp;问题就在这，由于人类进化的原因，你和社会已经绑在一起了，你需要为他人做些服务。 &nbsp;五千年来，每个都明白这些，然而就在近十年，人们却忘了。<br />&nbsp;十几岁的孩子就有自杀心理，于是我们争先恐后的去教他们自爱，自尊。<br />&nbsp;然而，不幸的是，这自尊和自爱只有当做了能让自己喜欢的事情后才会产生。这可不是胡说八道。<br />&nbsp;如果那个Todd整体待在屋里，喝着百事，一只手玩着视频游泳，另一只手在手淫，我会认为他在做毫无价值的事情，但如果是我在做这些事情，我又会怎么认<br />为自己呢？ </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> </font></p>
<p><font face="courier">                                              </font>
<p align="center"><font face="courier"> <img src="http://suntips.cn/archives/2008-06/7-Reasons-the-21st-Century-is-Making-You-Miserable_files/sadbear3.jpg" /> </p>
<p>                                                <font size="1">The Sad Bear #3, by <a class="b" target="ned" href="http://www.nedroid.com/">Nedroid</a></font> </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp;你能够把那些导致自卑的黑药丸丢掉吗？ &nbsp;收拾一下盖住眼睛的头发，从电脑面前走开，给你讨厌的人买一个礼物。 &nbsp;给你最大的死对头送张卡片。 &nbsp;给你的父母做顿晚餐。 &nbsp;或者简单做一些东西，一些真实存在的东西。 &nbsp;清扫屋檐下的落叶。 &nbsp;收拾一下快要死的花草。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier">&nbsp;这<br />些都是简单易做的事情；人是一个社会性的动物，你出生时身体里就带有一点快乐荷尔蒙激素。当你看到自己的活动产生了实际存在的价值时，它就会释放到你的血<br />液里。 &nbsp;想想那些十几岁的孩子，在黑屋子里，抱着电脑，把他们的所有生活问题全都幻化到可笑的游戏、戏剧情节里。<br />&nbsp;为什么他们会在自己的臂膀上能出那些小伤口？ &nbsp;因为这样可以能出疼痛—之后又能愈合—这是他们唯一能刺激体内的多肽激素的方法。<br />&nbsp;这是疼痛，但至少是真实的。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"><br />&nbsp;这种通过一些适度的困难任务来调节压力的办法过去曾是日常生活的一部分，例如猎捕羚羊，摘草莓，攀岩，猎捕熊。 &nbsp;不为别的。<br />&nbsp;这就是整天坐在办公室工作里会让如此多的人感到难受；我没有获得任何实在的、有形的产出。<br />&nbsp;在火热的太阳底下花两个月时间盖个建筑，余生里每当你开车经过这个建筑时你都可以说，“看看，我盖的。”这也许就是办公室里的枪击事件比工地里要常见的<br />多的原因。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp;这种体力上的，手工劳动获得的快感只有在你关掉计算机，走出家门，重新融入真实社会后才能获得。 &nbsp;这种感觉，这种“我建的”或“我喂养的”或“我种的”或“我制作的这些裤子”的感觉是任何网络给你的东西都不能相比的。 </font></p>
<p><font face="courier">                                              </font>
<p><font face="courier"> &nbsp; 当然，除了这篇文章能给你的 </font></p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/640.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>slideshare 服务中断</title>
		<link>http://www.donevii.com/post/635.html</link>
		<comments>http://www.donevii.com/post/635.html#comments</comments>
		<pubDate>Wed, 26 Nov 2008 10:05:54 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ror]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/635.html</guid>
		<description><![CDATA[在北京，此网站无法访问。 Something went wrong! This is probably a temporary error. - Try refreshing the page or - Go back and try again 504 Erro... ]]></description>
			<content:encoded><![CDATA[<p>在北京，此网站无法访问。</p>
<div id="container">
<h1>Something went wrong!</h1>
<p>This is probably a temporary error.
  </p>
<ul>
<li>- Try <a href="javascript:location.reload(true)">refreshing</a> the page or</li>
<li>- <a href="javascript:history.go(-1);">Go back</a> and try again</li>
</ul>
</div>
<p><span class="errorCode">504 Error</p>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/635.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>google背后的分布式架构</title>
		<link>http://www.donevii.com/post/504.html</link>
		<comments>http://www.donevii.com/post/504.html#comments</comments>
		<pubDate>Tue, 23 Sep 2008 08:48:29 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[unix]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[优化]]></category>
		<category><![CDATA[平台]]></category>
		<category><![CDATA[开发]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[类]]></category>
		<category><![CDATA[缓存]]></category>
		<category><![CDATA[芯片]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/504.html</guid>
		<description><![CDATA[　　Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用 (比如那个大堂里的地球模型)，更在于其有别常规的IT策略…… 　　加利福尼亚州山景城(Mountain View)Google公司(Google... ]]></description>
			<content:encoded><![CDATA[<p>　　Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用  (比如那个大堂里的地球模型)，更在于其有别常规的IT策略……</p>
<p>　　加利福尼亚州山景城(Mountain  View)Google公司(Google，下称Google)总部有一个43号大楼，该建筑的中央大屏幕上显示着一个与Google地球(Google  Earth)相仿的世界地图，一个转动的地球上不停地闪动着五颜六色的光点，恍如罗马宫廷的千万烛灯，每一次闪动标志着地球的这个角落一名Google用  户发起了一次新的搜索。</p>
<p>　　这同时意味着Google又一次满足了人们对未知信息的好奇与渴望。</p>
<p>　　Google是与众不同的。它的独特不仅仅表现于革新的思维和充满创意的应用  (比如那个大堂里的地球模型)，更在于其有别常规的IT策略。从人们的常理来看，简单的硬件商品和免费软件是无法构建出一个帝国的，但是Google做到  了。在性能调整后，Google把它们变成一个无可比拟的分布式计算平台，该平台能够支持大规模的搜索和不断涌现的新兴应用。我们原本认为这些应用都是个  人消费级别的，但是Google改变了这一切。现在商业世界也在使用它们，这就令这家搜索公司显得那么与众不同。</p>
<p>　　GoogleWeb  服务背后的IT架构对无数使用搜索引擎的用户来说也许并不是非常重要，但它是Google几百位致力于把全球信息组织起来，实现“随处可达，随时可用”目  标的工程师们的最核心工作。这就需要一个在覆盖范围和野心上都与Google的商业愿景完全相符的IT蓝图作为支撑。</p>
<p>　　Google  的经理们一直对公司的IT策略话题保持沉默，他们厌恶谈及特定的厂商或者产品，当被问到他们的服务器和数据中心时，他们总是闭口不谈。但与几位  Google的IT领导一起呆了一天后，我们最终得以揭示该公司的IT是如何运作的，那可不仅仅是一个运行在无数服务器集群上的、表面看来非常简单的搜索  引擎。在其简单的外表下，蕴涵着许多内部研发软件、定制硬件、人工智能，以及对性能的执着追求和打破常规的人力管理模式。</p>
<p>　　IT理念方面，Google对同行有一条建议：尽量避免那些人人都在使用的系统和软件，以自己的方式做事会更有独特的竞争优势。</p>
<p>　　“企业文化决定了你的做事方式。”道格拉斯&#8221;美林(Douglas  Merrill)，这位Google工程副总裁和事实上的首席信息官(CIO)  指出，“到了我们这样的发展阶段，企业观念和文化非常与众不同，这也反过来鞭策我们必须要采用与众不同的方式来运行那些他人看来很常规的系统。”<br />
　　Google  最大的IT优势在于它能建造出既富于性价比(并非廉价)又能承受极高负载的高性能系统。因此IT顾问史蒂芬&#8221;阿诺德(Stephen  Arnold)指出，Google与竞争对手，如亚马逊网站(Amazon)、电子港湾公司(eBay)、微软公司(Microsoft，下称微软)和雅 虎公司  (Yahoo，下称雅虎)等公司相比，具有更大的成本优势。Google程序员的效率比其他Web公司同行们高出50%～100%，原因是Google已  经开发出了一整套专用于支持大规模并行系统编程的定制软件库。据他估算，其他竞争公司可能要花上四倍的时间才能获得同等的效果。</p>
<p>　　打造服务器</p>
<p>　　Google  究竟是怎样做到这点的呢?其中一个手段，美林认为，“是因为我们自己动手打造硬件。”Google并不制造计算机系统，但它根据自己的参数定制硬件，然后  像MTV的节目“靓车打造”(Pimp My Ride)那样自己安装和调整硬件系统。开源程序经理克里斯&#8221;迪博纳(Chris  DiBona)评论道：“我们很善于购买商业服务器，并且改造他们为我们所用，最后把性能压榨和发挥到极致，以致有时候他们热得像要融化了似的。”</p>
<p>　　这种亲手打造的方式，来源于Google从车库诞生时与生俱来的节俭风格，更与Google那超大型的系统规模息息相关，良好的习惯一直延续至  今。据说 Google在65个数据中心拥有20万～45万台服务器—这个数目会有偏差(取决于你如何定义服务器和由谁来做这项统计)。但是，不变的是持续上升的趋势。</p>
<p>　　Google不会去讨论这些资产，因为它认为保密也是一种竞争优势。事实上，Google之所以喜欢开源软件也是因为它的私密性。“如果我们购  买了软件许可或代码许可，人们只要对号入座，就可以猜出Google的IT基础架构。”迪博纳分析说，  “使用开源软件，就使我们多了一条把握自己命运的途径。”</p>
<p>　　Google喜欢规模化的服务器运行方式。当有成百上千台机器时，定制服务器的优势也会成倍增加，效果也会更趋明显。Google正在俄勒冈州  哥伦比亚河边的达勒斯市建造一个占地30亩的数据中心，在那儿它可以获得运算和降温需要的低价水力电力能源(参见边栏《Google数据中心自有一套》)。</p>
<p>　　Google以“单元”(Cell)的形式组织这些运行  Linux操作系统的服务器，迪博纳把这种形式比喻成互联网服务的“磁盘驱动器”(但别和一直谣传的Google存储服务Gdrive混淆了，“并没有  Gdrive这回事。”一位Google女发言人明确表示。)，公司的软件程序都驻扎在这些并不昂贵的电脑机箱里，由程序员决定它们的冗余工作量。这种由  很多单元组成的文件系统代替了商业存储设备;迪博纳表示Google这些单元设备更易于建造和维护，他还暗示他们能处理更大规模的数据。</p>
<p>　　Google  不会漏过对任何技术细节的关注。多年来，公司的工程师就在研究微处理器的内部工作机制，随着Google规模的持续壮大，必然会用到特别定制和调节过的芯  片。知名工程师路易斯&#8221;巴罗索(Luiz  Barroso)去年在一篇发表在工业杂志上的论文中证实，近年来Google的主要负荷都由单核设计的系统承担着。但许多服务器端的应用，如  Google搜索索引服务，所需的并行计算在单核芯片的指令级别上执行得并不好。</p>
<p>　　曾在数据设备公司(Digital  Equipment)和康柏公司(Compaq)当过芯片设计师的巴罗索认为，随着AMD公司、英特尔公司(Intel)、太阳计算机系统公司(Sun)开始制造多核芯片，必将会出现越来越多芯片级别的并行计算。</p>
<p>　　Google  也曾考虑过自己制造计算机芯片，但从业界潮流来看，这个冒险的举动似乎不是很必要。“微处理器的设计非常复杂而且成本昂贵，”运营高级副总裁乌尔斯&#8221;霍尔 茨勒(Urs  Holzle)表示。Google宁愿与芯片制造商合作，让他们去理解自己的应用并设计适合的芯片。这是一种客户建议式的设计，其关注点在于总体吞吐量、  效能，以及耗电比，而不是看单线程的峰值性能。霍尔茨勒表示，“这也是最近多核CPU的设计潮流与未来方向。”<br />
裁缝般地定制软件</p>
<p>　　为了能尽量压榨硬件性能，Google开发了相当数量的定制软件。创新产品主要包括用于简化处理和创建大规模数据集的编程模型  MapReduce;用于存储和管理大规模数据的系统BigTable;分析分布式运算环境中大规模数据集的解释编程语言Sawzall;用于数据密集型  应用的分布式文件系统的 “Google文件系统”(Google File  System);还有为处理分布式系统队列分组和任务调度的“Google工作队列”(Google  Workqueue)。</p>
<p>　　正是从Sawzall这些工具里体现出Google对计算效率的执著关注。并不是每家公司都能从底层去解决效率问题，但是对Google来说，  为常规关系型数据库无法容纳的大规模数据集专门设计一种编程语言是完全合理的。即使其他编程工具可以解决问题，Google的工程师们仍然会为了追求效率  而另外开发一套定制方案。Google工程师认为，Sawzall能与C++中的MapReduce相媲美，而且它更容易编写一些。</p>
<p>　　Google  对效率的关注使它不可能对标准Linux内核感到满意;Google会根据自己的需要运行修改过的内核版本。通过调整Linux的底层性能，Google  工程师们在提高了整体系统可靠性的基础上，还一并解决了数据损坏和数据瓶颈等一系列棘手问题。对内核的修改也使Google的计算机集群系统因为通信效率  的提高而运行得更快。</p>
<p>　　当然，Google偶尔也会出现系统故障，情况一旦发生，无数的用户就会受到影响了。三年前一次持续30分钟的系统故障使20%的搜索流量受到影响。</p>
<p>　　Google  开发了自己的网站服务器却没有使用开源的Apache服务器，尽管它在网站服务器的市场占有率超过60%。迪博纳认为，Google的网站服务器可以运行  在更多数量的主机上，对Google站点上内容庞大又彼此互相依赖的应用程序来说，这种服务器的负载均衡能力远比Apache的能力更高。同时，在用标准  公共网关接口(CGI)访问数据库动态网页方面，Google服务器的编程难度要比  Apache更高，但是最终运行速度却更快。“如果我们能够压榨出10%～20%的性能，我们就可以节省出更多系统资源、电量和人力了。”迪博纳在总结中指出。</p>
<p>　　Google还设计了自己的客户关系管理(CRM)系统用于支持自己基于竞价和点击的互联网广告收费业务。但对是否需要设计自己的工具，Google的态度也不是一成不变的。比如在财会软件上，它就使用了甲骨文公司(Oracle)的Financials软件。</p>
<p>　　美林拿着一只叉子举例说明现成的产品也可以带来价值。但在有些场合现成的软件产品就不一定适用了。“我们的文化在各个层面对我们的运作都有深远影响，”他表示，“所以我们不想让购买所得的工具改变我们的工作方式和文化层面。”<br />
Google&#8217;s  BigTable 原理 （翻译）</p>
<p>    题记：google 的成功除了一个个出色的创意外，还因为有 Jeff Dean  这样的软件架构天才。<br />
                                                  &#8212;&#8212; 编者</p>
<p>官方的  Google Reader <a href="http://www.donevii.com/post/tag/blog" class="st_tag internal_tag" rel="tag" title="Posts tagged with blog">blog</a> 中有对BigTable 的解释。这是Google 内部开发的一个用来处理大数据量的系统。这种系统适合处理半结构化的数据比如  RSS 数据源。 以下发言  是 Andrew Hitchcock  在 2005 年10月18号 基于： Google 的工程师 Jeff Dean  在华盛顿大学的一次谈话 (Creative Commons License).</p>
<p>首先，BigTable 从 2004  年初就开始研发了，到现在为止已经用了将近8个月。（2005年2月）目前大概有100个左右的服务使用BigTable，比如： Print,Search  History,Maps和 Orkut。根据Google的一贯做法，内部开发的BigTable是为跑在廉价的PC机上设计的。BigTable  让Google在提供新服务时的运行成本降低，最大限度地利用了计算能力。</p>
<p>BigTable 是建立在 GFS ，Scheduler ，Lock Service 和  MapReduce 之上的。</p>
<p>每个Table都是一个多维的稀疏图 sparse map。Table 由行和列组成，并且每个存储单元 cell  都有一个时间戳。在不同的时间对同一个存储单元cell有多份拷贝，这样就可以记录数据的变动情况。在他的例子中，行是URLs  ，列可以定义一个名字，比如：contents。Contents  字段就可以存储文件的数据。或者列名是：”language”，可以存储一个“EN”的语言代码字符串。</p>
<p>为了管理巨大的Table，把Table根据行分割，这些分割后的数据统称为：Tablets。每  个Tablets大概有 100-200 MB，每个机器存储100个左右的  Tablets。底层的架构是：GFS。由于GFS是一种分布式的文件系统，采用Tablets的机制后，可以获得很好的负载均衡。比如：可以把经常响应  的表移动到其他空闲机器上，然后快速重建。</p>
<p>Tablets在系统中的存储方式是不可修改的 immutable  的SSTables，一台机器一个日志文件。当系统的内存满后，系统会压缩一些Tablets。由于Jeff在论述这点的时候说的很快，所以我没有时间把听到的都记录下来，因此下面是一个大概的说明：</p>
<p>压缩分为：主要和次要的两部分。次要的压缩仅仅包括几个Tablets，而主要的压缩时关于整个系统的压缩。主压缩有回收硬盘空间的功能。Tablets的位置实际上是存储在几个特殊的BigTable的存储单元cell中。看起来这是一个三层的系统。<br />
客户端有一个指向METAO的Tablets的指针。如果METAO的Tablets被频繁使用，那个这台机器就会放弃其他的tablets专门支持  METAO这个Tablets。METAO tablets  保持着所有的META1的tablets的记录。这些tablets中包含着查找tablets的实际位置。（老实说翻译到这里，我也不太明白。）在这个系统中不存在大的瓶颈，因为被频繁调用的数据已经被提前获得并进行了缓存。</p>
<p>     现在我们返回到对列的说明：列是类似下面的形式：  family:optional_qualifier。在他的例子中，行：www.search-analysis.com   也许有列：”contents:其中包含html页面的代码。 “ anchor:cnn.com/news” 中包含着  相对应的url，”anchor:www.search-analysis.com/” 包含着链接的文字部分。列中包含着类型信息。<br />
     (翻译到这里我要插一句，以前我看过一个关于万能数据库的文章，当时很激动，就联系了作者，现在回想起来，或许google的 bigtable  才是更好的方案，切不说分布式的特性，就是这种建华的表结构就很有用处。)</p>
<p>     注意这里说的是列信息，而不是列类型。列的信息是如下信息，一般是：属性/规则。 比如：保存n份数据的拷贝或者保存数据n天长等等。当 tablets  重新建立的时候，就运用上面的规则，剔出不符合条件的记录。由于设计上的原因，列本身的创建是很容易的，但是跟列相关的功能确实非常复杂的，比如上文提到 的  类型和规则信息等。为了优化读取速度，列的功能被分割然后以组的方式存储在所建索引的机器上。这些被分割后的组作用于 列 ,然后被分割成不同的  SSTables。这种方式可以提高系统的性能，因为小的，频繁读取的列可以被单独存储，和那些大的不经常访问的列隔离开来。</p>
<p>在一台机器上的所有的  tablets  共享一个log，在一个包含1亿的tablets的集群中，这将会导致非常多的文件被打开和写操作。新的log块经常被创建，一般是64M大小，这个GFS的块大小相等。当一个机器down掉后，控制机器就会重新发布他的log块到其他机器上继续进行处理。这台机器重建tablets然后询问控制机器处理结构的存储位置，然后直接对重建后的数据进行处理。这个系统中有很多冗余数据，因此在系统中大量使用了压缩技术。</p>
<p>     Dean 对压缩的部分说的很快，我没有完全记下来，所以我还是说个大概吧：压缩前先寻找相似的 \行，列，和时间数据。</p>
<p>    他们使用不同版本的：  BMDiff 和 Zippy <a href="http://www.donevii.com/post/tag/%e6%8a%80%e6%9c%af" class="st_tag internal_tag" rel="tag" title="Posts tagged with 技术">技术</a>。</p>
<p>   BMDiff 提供给他们非常快的写速度： 100MB/s – 1000MB/s 。Zippy 是和 LZW  类似的。Zippy 并不像 LZW 或者 gzip 那样压缩比高，但是他处理速度非常快。</p>
<p>    Dean 还给了一个关于压缩 <a href="http://www.donevii.com/post/tag/web" class="st_tag internal_tag" rel="tag" title="Posts tagged with web">web</a>  蜘蛛数据的例子。这个例子的蜘蛛 包含 2.1B 的页面，行按照以下的方式命名：“com.cnn.www/index.html:http”.在未压缩前的web  page 页面大小是：45.1 TB ，压缩后的大小是：4.2 TB ， 只是原来的 9.2%。Links 数据压缩到原来的 13.9% ,  链接文本数据压缩到原来的 12.7%。<br />
<span id="more-504"></span><br />
Google 还有很多没有添加但是已经考虑的功能。<br />
    1.   数据操作表达式，这样可以把脚本发送到客户端来提供修改数据的功能。<br />
    2. 多行数据的事物支持。<br />
    3.   提高大数据存储单元的效率。<br />
    4. BigTable 作为服务运行。<br />
    好像：每个服务比如： maps 和 search history  历史搜索记录都有他们自己的集群运行 BigTable。<br />
    他们还考虑运行一个全局的 BigTable  系统，但这需要比较公平的分割资源和计算时间。</p>
<p>大表(Bigtable):结构化数据的分布存储系统</p>
<p>http://labs.google.com/papers/bigtable-osdi06.pdf</p>
<p>｛中是译者评论,程序除外｝<br />
{本文的翻译可能有不准确的地方,详细资料请参考原文.}</p>
<p>摘要<br />
bigtable是设计来分布存储大规模结构化数据的，从设计上它可以扩展到上２^50字节，分布存储在几千个普通服务器上．google的很多项目使用  bt来存储数据，包括网页查询，google  earth和google金融．这些应用程序对bt的要求各不相同：数据大小（从URL到网页到卫星图象）不同，反应速度不同（从后端的大批处理到实时数  据服务）．对于不同的要求，bt都成功的提供了灵活高效的服务．在本文中，我们将描述bt的数据模型．这个数据模型让用户动态的控制数据的分布和结构．我  们还将描述BT的设计和实现．<br />
１．介绍<br />
在过去两年半里，我们设计，实现并部署了BT．BT是用来分布存储和管理结构化数据的．BT的设计使它能够管理2^50  bytes(petabytes)数据，并可以部署到上千台机器上．BT完成了以下目标：应用广泛，可扩展，高性能和高可用性（high availability）.  包括google analytics, google finance, orkut, personalized search, writely和google  earth在内的60多个项目都使用BT.这些应用对BT的要求各不相同，有的需要高吞吐量的批处理，有的需要快速反应给用户数据．它们使用的BT集群也各不相同，有的只有几台机器，有的有上千台，能够存储2^40字节(terabytes)数据．<br />
BT在很多地方和数据库很类似：它使用了很多数据库的实现策略．并行数据库[14]和内存数据库[13]有可扩展性和高性能，但是BT的界面不同．BT不支持完全的关系数据模型；而是为客户提供了简单的数据模型，让客户来动态控制数据的分布和格式{就是只存储字串，格式由客户来解释}，并允许客户推断底层存储数据的局部性｛以提高访问速度｝．数据下标是行和列的名字，数据本身可以是任何字串．BT的数据是字串，没有解释｛类型等｝．客户会在把各种结构或者半结构化的数据串行化｛比如说日期串｝到数据中．通过仔细选择数据表示，客户可以控制数据的局部化．最后，可以使用BT模式来控制数据是放在内存里还是在硬盘上．｛就是说用模式，你可以把数据放在离应用最近的地方．毕竟程序在一个时间只用到一块数据．在体系结构里，就是：locality,  locality,  locality｝<br />
第二节描述数据模型细节．第三节关于客户API概述．第四节简介BT依赖的google框架．第五节描述BT的实现关键部分．第6节叙述提高BT性  能的一些调整．第7节提供BT性能的数据．在第8节，我们提供BT的几个使用例子，第9节是经验教训．在第10节，我们列出相关研究．最后是我们的结论．<br />
２．数据模型<br />
BT是一个稀疏的，长期存储的｛存在硬盘上｝，多维度的，排序的映射表．这张表的索引是行关键字，列关键字和时间戳．每个值是一个不解释的字符数组．｛数据都是字符串，没类型，客户要解释就自力更生吧｝．<br />
(row:string,  column:string,time:int64)-&gt;string  {能编程序的都能读懂，不翻译了}<br />
我们仔细查看过好些类似bigtable的系统之后定下了这个数据模型。举一个具体例子（它促使我们做出某些设计决定），  比如我们想要存储大量网页及相关信息，以用于很多不同的项目；我们姑且叫它Webtable。在Webtable里，我们将用URL作为行关键字，用网页  的某些属性作为列名，把网页内容存在contents:列中并用获取该网页的时间戳作为标识，如图一所示。<br />
图一：一个存储Web网页的范例列表片断。行名是一个反向URL｛即com.cnn.www｝。contents列族｛原文用  family，译为族，详见列族｝ 存放网页内容，anchor列族存放引用该网页的锚链接文本。CNN的主页被Sports  Illustrater｛即所谓SI，CNN的王牌体育节目｝和MY-look的主页引用，因此该行包含了名叫“anchor:cnnsi.com”和  “anchhor:my.look.ca”的列。每个锚链接只有一个版本｛由时间戳标识，如t9，t8｝；而contents列则有三个版本，分别由时间  戳t3，t5，和t6标识。<br />
行<br />
表中的行关键字可以是任意字符串（目前支持最多64KB，多数情况下10－100字节足够了）。在一个行关键字下的每一个读写操作都是原子操作（不管读写这一行里多少个不同列），这是一个设计决定，这样在对同一行进行并发操作时，用户对于系统行为更容易理解和掌控。<br />
Bigtable通过行关键字的字典序来维护数据。一张表可以动态划分成多个连续行。连续行在这里叫做“子表”｛tablet｝，是数据分布和负载  均衡的单位。这样一来，读较少的连续行就比较有效率，通常只需要较少机器之间的通信即可。用户可以利用这个属性来选择行关键字，从而达到较好数据访问地域  性｛locality｝。举例来说，在Webtable里，通过反转URL中主机名的方式，可以把同一个域名下的网页组织成连续行。具体来说，可以把  maps.google.com/index.html中的数据存放在关键字com.google.maps/index.html下。按照相同或属性相  近的域名来存放网页可以让基于主机和基于域名的分析更加有效。<br />
列族<br />
一组列关键字组成了“列族”，这是访问控制的基本单位。同一列族下存放的所有数据通常都是同一类型（同一列族下的数据可压缩在一起）。列族必须先创  建，然后在能在其中的列关键字下存放数据；列族创建后，族中任何一个列关键字均可使用。我们希望，一张表中的不同列族不能太多（最多几百个），并且列族在  运作中绝少改变。作为对比，一张表可以有无限列。<br />
列关键字用如下语法命名：列族：限定词。  列族名必须是看得懂｛printable｝的字串，而限定词可以是任意字符串。比如，Webtable可以有个列族叫language，存放撰写网页的语  言。我们在language列族中只用一个列关键字，用来存放每个网页的语言标识符。该表的另一个有用的列族是anchor；给列族的每一个列关键字代表  一个锚链接，如图一所示。而这里的限定词则是引用该网页的站点名；表中一个表项存放的是链接文本。<br />
访问控制，磁盘使用统计，内存使用统计，均可在列族这个层面进行。在Webtable举例中，我们可以用这些控制来管理不同应用：有的应用添加新的基本数据，有的读取基本数据并创建引申的列族，有的则只能浏览数据（甚至可能因为隐私权原因不能浏览所有数据）。<br />
时间戳<br />
Bigtable表中每一个表项都可以包含同一数据的多个版本，由时间戳来索引。Bigtable的时间戳是64位整型。可以由Bigtable来  赋值，表示准确到毫秒的“实时”；或者由用户应用程序来赋值。需要避免冲突的应用程序必须自己产生具有唯一性的时间戳。不同版本的表项内容按时间戳倒序排  列，即最新的排在前面。<br />
为了简化对于不同数据版本的数据的管理，我们对每一个列族支持两个设定，以便于Bigtable对表项的版本自动进行垃圾清除。用户可以指明只保留表项的最后n个版本，或者只保留足够新的版本（比如，只保留最近7天的内容）。<br />
在Webtable举例中，我们在contents:列中存放确切爬行一个网页的时间戳。如上所述的垃圾清除机制可以让我们只保留每个网页的最近三个版本。<br />
3.API<br />
BT的API提供了建立和删除表和列族的函数．还提供了函数来修改集群，表和列族的元数据，比如说访问权限．<br />
//  Open the table<br />
Table *T = OpenOrDie(”/bigtable/web/webtable”);<br />
// Write a  new anchor and delete an old anchor<br />
RowMutation r1(T,  “com.cnn.www”);<br />
r1.Set(”anchor:www.c-span.org”,  “CNN”);<br />
r1.Delete(”anchor:www.abc.com”);<br />
Operation op;<br />
Apply(&amp;op,  &amp;r1);<br />
图 2:  写入Bigtable.<br />
在BT中，客户应用可以写或者删除值，从每个行中找值，或者遍历一个表中的数据子集．图2的c++代码是使用RowMutation抽象表示来进行一系列的更新（为保证代码精简，没有包括无关的细节）．调用Apply函数，就对Ｗebtable进行了一个原子修改：它为http://www.cnn.com/增加了一个锚点，并删除了另外一个锚点．<br />
Scanner  scanner(T);<br />
ScanStream *stream;<br />
stream =  scanner.FetchColumnFamily(”anchor”);<br />
stream-&gt;SetReturnAllVersions();<br />
scanner.Lookup(”com.cnn.www”);<br />
for  (; !stream-&gt;Done(); stream-&gt;Next()) {<br />
printf(”%s %s %lld  %s\n”,<br />
scanner.RowName(),<br />
stream-&gt;ColumnName(),<br />
stream-&gt;MicroTimestamp(),<br />
stream-&gt;Value());<br />
}<br />
图3:  从Bigtable读数据.<br />
图3的C++代码是使用Scanner抽象来遍历一个行内的所有锚点．客户可以遍历多个列族．有很多方法可以限制一次扫描中产生的行，列和时间戳．  例如，我们可以限制上面的扫描，让它只找到那些匹配正则表达式*.cnn.com的锚点，或者那些时间戳在当前时间前10天的锚点．<br />
BT还支持其他一些更复杂的处理数据的功能．首先，BT支持单行处理．这个功能可以用来对存储在一个行关键字下的数据进行原子的读-修改-写操作．  BT目前不支持跨行关键字的处理，但是它有一个界面，可以用来让客户进行批量的跨行关键字处理操作．其次，BT允许把每个表项用做整数记数器．最后，BT  支持在服务器的地址空间内执行客户端提供的脚本程序．脚本程序的语言是google开发的Sawzall[28]数据处理语言．目前，我们基于的  Sawzall的API还不允许客户脚本程序向BT内写数据，但是它允许多种形式的数据变换，基于任何表达式的过滤和通过多种操作符的摘要．<br />
BT可以和MapReduce[12]一起使用．MapReduce是google开发的大规模并行计算框架．我们为编写了一套外层程序，使BT可以作为MapReduce处理的数据源头和输出结果．<br />
4.建立BT的基本单元<br />
BT是建立在其他数个google框架单元上的．BT使用google分布式文件系统(GFS)[17]来存储日志和数据文件{yeah,  right, what else can it use,  FAT32?}．一个BT集群通常在一个共享的机器池中工作，池中的机器还运行其他的分布式应用{虽然机器便宜的跟白菜似的，可是一样要运行多个程序，命苦的象小白菜}，BT和其他程序共享机器｛BT的瓶颈是ＩＯ/内存，可以和CPU要求高的程序并存｝．BT依赖集群管理系统来安排工作，在共享的机器上管理资源，处理失效机器并监视机器状态｛典型的server  farm结构，BT是上面的应用之一｝．<br />
BT内部存储数据的格式是google  SSTable格式．一个SSTable提供一个从关键字到值的映射，关键字和值都可以是任意字符串．映射是排序的，存储的｛不会因为掉电而丢失｝，不可改写的．可以进行以下操作：查询和一个关键字相关的值；或者根据给出的关键字范围遍历所有的关键字和值．在内部，每个SSTable包含一列数据块（通常每个块的大小是64KB,但是大小是可以配置的｛索引大小是16  bits，应该是比较好的一个数｝）．块索引（存储在SSTable的最后）用来定位数据块；当打开SSTable的时候，索引被读入内存｛性能｝．每次查找都可以用一个硬盘搜索完成｛根据索引算出数据在哪个道上，一个块应该不会跨两个道，没必要省那么点空间｝：首先在内存中的索引里进行二分查找找到数据块的位置，然后再从硬盘读去数据块．最佳情况是：整个SSTable可以被放在内存里，这样一来就不必访问硬盘了．｛想的美，前面是谁口口声声说要跟别人共享机器来着？你把内存占满了别人上哪睡去？｝<br />
BT还依赖一个高度可用的，存储的分布式数据锁服务Chubby[8]｛看你怎么把这个high  performance给说圆喽｝．一个Chubby服务由5个活的备份｛机器｝构成，其中一个被这些备份选成主备份，并且处理请求．这个服务只有在大多数备份都活着并且互相通信的时候才是活的｛绕口令？去看原文吧，是在有出错的前提下的冗余算法｝．当有机器失效的时候，Chubby使用Paxos算法[9,23]来保证备份的一致性｛这个问题还是比较复杂的，建议去看引文了解一下问题本身｝．Chubby提供了一个名字空间，里面包括了目录和小文件｛万变不离其宗｝．每个目录或者文件可以当成一个锁来用，读写文件操作都是原子化的．Chubby客户端的程序库提供了对Chubby文件的一致性缓存｛究竟是提高性能还是降低性能？如果访问是分布的，就是提高性能｝．每个Chubby客户维护一个和Chubby服务的会话．如果一个客户不能在一定时间内更新它的会话，这个会话就过期失效了｛还是针对大server  farm里机器失效的频率设计的｝．当一个会话失效时，其拥有的锁和打开的文件句柄都失效｛根本设计原则：失效时回到安全状态｝．Chubby客户可以在文件和目录上登记回调函数，以获得改变或者会话过期的通知．｛翻到这里，有没有人闻到java的味道了？｝<br />
BT使用Chubby来做以下几个任务：保证任何时间最多只有一个活跃的主备份；来存储BT数据的启动位置（参考5.1节）；发现小表  （tablet）服务器，并完成tablet服务器消亡的善后（5.2节）；存储BT数据的模式信息（每张表的列信息）；以及存储访问权限列表．如果有相当长的时间Chubby不能访问，BT就也不能访问了｛任何系统都有其弱点｝．最近我们在使用11个Chubby服务实例的14个BT集群中度量了这个效果，由于Chubby不能访问而导致BT中部分数据不能访问的平均百分比是0.0047%,这里Chubby不能访问的原因是Chubby本身失效或者网络问题．单个集群里，受影响最大的百分比是0.0326%｛基于文件系统的Chubby还是很稳定的｝.<br />
GFS是一个可扩展的分布式文件系统，用于大型的、分布式的、对大量数据进行访问的应用。它运行于廉价的普通硬件上，但可以提供容错功能。它可以给大量的用户提供总体性能较高的服务。<br />
出处：http://labs.google.com/papers/gfs.html<br />
1、设计概览<br />
（1）设计想定<br />
GFS与过去的分布式文件系统有很多相同的目标，但GFS的设计受到了当前及预期的应用方面的工作量及技术环境的驱动，这反映了它与早期的文件系统明显不同的设想。这就需要对传统的选择进行重新检验并进行完全不同的设计观点的探索。<br />
GFS与以往的文件系统的不同的观点如下：<br />
1、部件错误不再被当作异常，而是将其作为常见的情况加以处理。因为文件系统由成百上千个用于存储的机器构成，而这  些机器是由廉价的普通部件组成并被大量的客户机访问。部件的数量和质量使得一些机器随时都有可能无法工作并且有一部分还可能无法恢复。所以实时地监控、错  误检测、容错、自动恢复对系统来说必不可少。<br />
2、按照传统的标准，文件都非常大。长度达几个GB的文件是很平常的。每个文件通常包含很多应用对象。当经常要处理  快速增长的、包含数以万计的对象、长度达TB的数据集时，我们很难管理成千上万的KB规模的文件块，即使底层文件系统提供支持。因此，设计中操作的参数、  块的大小必须要重新考虑。对大型的文件的管理一定要能做到高效，对小型的文件也必须支持，但不必优化。<br />
3、大部分文件的更新是通过添加  新数据完成的，而不是改变已存在的数据。在一个文件中随机的操作在实践中几乎不存在。一旦写完，文件就只可读，很多数据都有这些特性。一些数据可能组成一  个大仓库以供数据分析程序扫描。有些是运行中的程序连续产生的数据流。有些是档案性质的数据，有些是在某个机器上产生、在另外一个机器上处理的中间数据。  由于这些对大型文件的访问方式，添加操作成为性能优化和原子性保证的焦点。而在客户机中缓存数据块则失去了吸引力。<br />
4、工作量主要由两种读操作构成：对大量数据的流方式的读操作和对少量数据的随机方式的读操作。在前一种读操作中，  可能要读几百KB，通常达  1MB和更多。来自同一个客户的连续操作通常会读文件的一个连续的区域。随机的读操作通常在一个随机的偏移处读几个KB。性能敏感的应用程序通常将对少量  数据的读操作进行分类并进行批处理以使得读操作稳定地向前推进，而不要让它来来回回的读。<br />
5、工作量还包含许多对大量数据进行的、连续的、向文件添加数据的写操作。所写的数据的规模和读相似。一旦写完，文件很少改动。在随机位置对少量数据的写操作也支持，但不必非常高效。<br />
6、系统必须高效地实现定义完好的大量客户同时向同一个文件的添加操作的语义。<br />
（2）系统接口<br />
GFS提供了一个相似地文件系统界面，虽然它没有向POSIX那样实现标准的API。文件在目录中按层次组织起来并由路径名标识。<br />
（3）体系结构：<br />
一个GFS集群由一个master和大量的chunkserver构成，并被许多客户（Client）访问。如图1  所示。Master和  chunkserver通常是运行用户层服务进程的Linux机器。只要资源和可靠性允许，chunkserver和client可以运行在同一个机器  上。<br />
文件被分成固定大小的块。每个块由一个不变的、全局唯一的64位的chunk－handle标识，chunk－ handle是在块创建时由  master分配的。ChunkServer将块当作Linux文件存储在本地磁盘并可以读和写由chunk－handle和位区间指定的数据。出于可靠  性考虑，每一个块被复制到多个chunkserver上。默认情况下，保存3个副本，但这可以由用户指定。<br />
Master维护文件系统所以的元数据（metadata），包括名字空间、访问控制信息、从文件到块的映射以及块  的当前位置。它也控制系统范围的活动，如块租约（lease）管理，孤儿块的垃圾收集，chunkserver间的块迁移。Master定期通过  HeartBeat消息与每一个  chunkserver通信，给chunkserver传递指令并收集它的状态。<br />
与每个应用相联的GFS客户代码实现了文件系统的API并与master和chunkserver通信以代表应用程序读和写数据。客户与master的交换只限于对元数据（metadata）的操作，所有数据方面的通信都直接和chunkserver联系。<br />
客户和chunkserver都不缓存文件数据。因为用户缓存的益处微乎其微，这是由于数据太多或工作集太大而无法  缓存。不缓存数据简化了客户程序和整个系统，因为不必考虑缓存的一致性问题。但用户缓存元数据（metadata）。Chunkserver也不必缓存文  件，因为块时作为本地文件存储的。<br />
（4）单master。<br />
只有一个master也极大的简化了设计并使得master可以根据全局情况作出先进的块放置和复制决定。但是我们  必须要将master对读和写的参与减至最少，这样它才不会成为系统的瓶颈。Client从来不会从master读和写文件数据。Client只是询问  master它应该和哪个  chunkserver联系。Client在一段限定的时间内将这些信息缓存，在后续的操作中Client直接和chunkserver交互。<br />
以图1解释一下一个简单的读操作的交互。<br />
1、client使用固定的块大小将应用程序指定的文件名和字节偏移转换成文件的一个块索引（chunk  index）。<br />
2、给master发送一个包含文件名和块索引的请求。<br />
3、master回应对应的chunk  handle和副本的位置（多个副本）。<br />
4、client以文件名和块索引为键缓存这些信息。（handle和副本的位置）。<br />
5、Client  向其中一个副本发送一个请求，很可能是最近的一个副本。请求指定了chunk handle（chunkserver以chunk  handle标识chunk）和块内的一个字节区间。<br />
6、除非缓存的信息不再有效（cache for a limited  time）或文件被重新打开，否则以后对同一个块的读操作不再需要client和master间的交互。<br />
通常Client可以在一个请求中询问多个chunk的地址，而master也可以很快回应这些请求。<br />
（5）块规模：<br />
块规模是设计中的一个关键参数。我们选择的是64MB，这比一般的文件系统的块规模要大的多。每个块的副本作为一个普通的Linux文件存储，在需要的时候可以扩展。<br />
块规模较大的好处有：<br />
1、减少client和master之间的交互。因为读写同一个块只是要在开始时向master请求块位置信息。对于读写大型文件这种减少尤为重要。即使对于访问少量数据的随机读操作也可以很方便的为一个规模达几个TB的工作集缓缓存块位置信息。<br />
2、Client在一个给定的块上很可能执行多个操作，和一个chunkserver保持较长时间的TCP连接可以减少网络负载。<br />
3、这减少了master上保存的元数据（metadata）的规模，从而使得可以将metadata放在内存中。这又会带来一些别的好处。<br />
不利的一面：<br />
一个小文件可能只包含一个块，如果很多Client访问改文件的话，存储这些块的chunkserver将成为访问的热点。但在实际应用中，应用程序通常顺序地读包含多个块的文件，所以这不是一个主要问题。<br />
（6）元数据（metadata）：<br />
master存储了三中类型的metadata：文件的名字空间和块的名字空间，从文件到块的映射，块的副本的位  置。所有的metadata都放在内存中。前两种类型的metadata通过向操作日志登记修改而保持不变，操作日志存储在master的本地磁盘并在几  个远程机器上留有副本。使用日志使得我们可以很简单地、可靠地更新master的状态，即使在master崩溃的情况下也不会有不一致的问题。相反，  mater在每次启动以及当有  chuankserver加入的时候询问每个chunkserver的所拥有的块的情况。<br />
A、内存数据结构：<br />
因为metadata存储在内存中，所以master的操作很快。进一步，master可以轻易而且高效地定期在后台扫描它的整个状态。这种定期地扫描被用于实现块垃圾收集、chunkserver出现故障时的副本复制、为平衡负载和磁盘空间而进行的块迁移。<br />
这种方法的一个潜在的问题就是块的数量也即整个系统的容量是否受限与master的内存。实际上，这并不是一个严重  的问题。Master为每个  64MB的块维护的metadata不足64个字节。除了最后一块，文件所有的块都是满的。类似的，每个文件的名字空间数据也不足64个字节，因为文件名  是以一种事先确定的压缩方式存储的.如果要支持更大的文件系统，那么增加一些内存的方法对于我们将元数据（metadata）保存在内存种所获得的简单  性、可靠性、高性能和灵活性来说，这只是一个很小的代价。<br />
B、块位置：<br />
master并不为chunkserver所拥有的块的副本的保存一个不变的记录。它在启动时通过简单的查询来获得这些信息。Master可以保持这些信息的更新，因为它控制所有块的放置并通过HeartBeat消息来监控chunkserver的状态。<br />
这样做的好处：因为chunkserver可能加入或离开集群、改变路径名、崩溃、重启等，一个集群重有成百个server，这些事件经常发生，这种方法就排除了master与chunkserver之间的同步问题。<br />
另一个原因是：只有chunkserver才能确定它自己到底有哪些块，由于错误，chunkserver中的一些块可能会很自然的消失，这样在master中就没有必要为此保存一个不变的记录。<br />
C、操作日志：<br />
操作日志包含了对metadata所作的修改的历史记录。它作为逻辑时间线定义了并发操作的执行顺序。文件、块以及它们的版本号都由它们被创建时的逻辑时间而唯一地、永久地被标识。<br />
操作日志是如此的重要，我们必须要将它可靠地保存起来，并且只有在metadata的改变固定下来之后才将变化呈现给用户。所以我们将操作日志复制到数个远程的机器上，并且只有在将相应的日志记录写到本地和远程的磁盘上之后才回答用户的请求。<br />
Master可以用操作日志来恢复它的文件系统的状态。为了将启动时间减至最小，日志就必须要比较小。每当日志的长度增长到超过一定的规模后，master就要检查它的状态，它可以从本地磁盘装入最近的检查点来恢复状态。<br />
创建一个检查点比较费时，master的内部状态是以一种在创建一个检查点时并不耽误即将到来的修改操作的方式来组  织的。Master切换到一个新的日子文件并在一个单独的线程中创建检查点。这个新的检查点记录了切换前所有的修改。在一个有数十万文件的集群中用一分钟  左右就能完成。创建完后，将它写入本地和远程的磁盘。<br />
（7）数据完整性<br />
名字空间的修改必须是原子性的，它们只能有master处理：名字空间锁保证了操作的原子性和正确性，而master的操作日志在全局范围内定义了这些操作的顺序。<br />
文  件区间的状态在修改之后依赖于修改的类型，不论操作成功还是失败，也不论是不是并发操作。如果不论从哪个副本上读，所有的客户都看到同样的数据，那么文件  的这个区域就是一致的。如果文件的区域是一致的并且用户可以看到修改操作所写的数据，那么它就是已定义的。如果修改是在没有并发写操作的影响下完成的，那  么受影响的区域是已定义的，所有的client都能看到写的内容。成功的并发写操作是未定义但却是一致的。失败的修改将使区间处于不一致的状态。<br />
Write操作在应用程序指定的偏移处写入数据，而record  append操作使得数据（记录）即使在有并发修改操作的情况下也至少原子性的被加到GFS指定的偏移处，偏移地址被返回给用户。<br />
在一系列成功的修改操作后，最后的修改操作保证文件区域是已定义的。GFS通过对所有的副本执行同样顺序的修改操作并且使用块版本号检测过时的副本（由于chunkserver退出而导致丢失修改）来做到这一点。<br />
因为用户缓存了会位置信息，所以在更新缓存之前有可能从一个过时的副本中读取数据。但这有缓存的截止时间和文件的重新打开而受到限制。<br />
在修改操作成功后，部件故障仍可以是数据受到破坏。GFS通过master和chunkserver间定期的handshake，借助校验和来检测对数据的破坏。一旦检测到，就从一个有效的副本尽快重新存储。只有在GFS检测前，所有的副本都失效，这个块才会丢失。<br />
2、系统交互<br />
（1）租约（lease）和修改顺序<br />
（2）数据流<br />
我们的目标是充分利用每个机器的网络带宽，避免网络瓶颈和延迟<br />
为了有效的利用网络，我们将数据流和控制流分离。数据是以流水线的方式在选定的chunkerserver链上线性的传递的。每个机器的整个对外带宽都被用作传递数据。为避免瓶颈，每个机器在收到数据后，将它收到数据尽快传递给离它最近的机器。<br />
（3）原子性的record  Append：<br />
GFS提供了一个原子性的添加操作：record  append。在传统的写操作中，client指定被写数据的偏移位置，向同一个区间的并发的写操作是不连续的：区间有可能包含来自多个client的数  据碎片。在record append中，  client只是指定数据。GFS在其选定的偏移出将数据至少原子性的加入文件一次，并将偏移返回给client。<br />
在分布式的应用中，不同机器上的许多client可能会同时向一个文件执行添加操作，添加操作被频繁使用。如果用传  统的write操作，可能需要额外的、复杂的、开销较大的同步，例如通过分布式锁管理。在我们的工作量中，这些文件通常以多个生产者单个消费者队列的方式  或包含从多个不同 client的综合结果。<br />
Record  append和前面讲的write操作的控制流差不多，只是在primary上多了一些逻辑判断。首先，client将数据发送到文件最后一块的所有副本  上。然后向primary发送请求。Primary检查添加操作是否会导致该块超过最大的规模（64M）。如果这样，它将该块扩充到最大规模，并告诉其它  副本做同样的事，同时通知client该操作需要在下一个块上重新尝试。如果记录满足最大规模的要求，primary就会将数据添加到它的副本上，并告诉  其它的副本在在同样的偏移处写数据，最后primary向client报告写操作成功。如果在任何一个副本上record  append操作失败，client将重新尝试该操作。这时候，同一个块的副本可能包含不同的数据，因为有的可能复制了全部的数据，有的可能只复制了部  分。GFS不能保证所有的副本每个字节都是一样的。它只保证每个数据作为一个原子单元被写过至少一次。这个是这样得出的：操作要是成功，数据必须在所有的  副本上的同样的偏移处被写过。进一步，从这以后，所有的副本至少和记录一样长，所以后续的记录将被指定到更高的偏移处或者一个不同的块上，即使另一个副本  成了primary。根据一致性保证，成功的record  append操作的区间是已定义的。而受到干扰的区间是不一致的。<br />
（4）快照（snapshot）<br />
快照操作几乎在瞬间构造一个文件和目录树的副本，同时将正在进行的其他修改操作对它的影响减至最小。<br />
我们使用copy-on-write技术来实现snapshot。当master受到一个snapshot请求时，  它首先将要snapshot的文件上块上的lease。这使得任何一个向这些块写数据的操作都必须和master交互以找到拥有lease的副本。这就给  master一个创建这个块的副本的机会。<br />
副本被撤销或终止后，master在磁盘上登记执行的操作，然后复制源文件或目录树的metadata以对它的内存状态实施登记的操作。这个新创建的snapshot文件和源文件（其metadata）指向相同的块（chunk）。<br />
Snapshot之后，客户第一次向chunk  c写的时候，它发一个请求给master以找到拥有lease的副本。Master注意到chunk c的引用记数比1大，它延迟对用户的响应，选择一个chunk  handle C’,然后要求每一有chunk c的副本的chunkserver创建一个块C’。每个chunkserver在本地创建chunk  C’避免了网络开销。从这以后和对别的块的操作没有什么区别。<br />
3、MASTER操作<br />
MASTER执行所有名字空间的操作，除此之外，他还在系统范围管理数据块的复制：决定数据块的放置方案，产生新数据块并将其备份，和其他系统范围的操作协同来确保数据备份的完整性，在所有的数据块服务器之间平衡负载并收回没有使用的存储空间。<br />
3.1  名字空间管理和加锁<br />
与传统文件系统不同的是，GFS没有与每个目录相关的能列出其所有文件的数据结构，它也不支持别名（unix中的硬连接或符号连接），不管是对文件或是目录。GFS的名字空间逻辑上是从文件元数据到路径名映射的一个查用表。<br />
MASTER在执行某个操作前都要获得一系列锁，例如，它要对/d1/d2…/dn/leaf执行操作，则它必须获  得/d1，/d1/d2，…，  /d1/d2/…/dn的读锁，/d1/d2…/dn/leaf的读锁或写锁（其中leaf可以使文件也可以是目录）。MASTER操作的并行性和数据的  一致性就是通过这些锁来实现的。<br />
3.2  备份存储放置策略<br />
一个GFS集群文件系统可能是多层分布的。一般情况下是成千上万个文件块服务器分布于不同的机架上，而这些文件块服  务器又被分布于不同机架上的客户来访问。因此，不同机架上的两台机器之间的通信可能通过一个或多个交换机。数据块冗余配置策略要达到连个目的：最大的数据  可靠性和可用性，最大的网络带宽利用率。因此，如果仅仅把数据的拷贝置于不同的机器上很难满足这两个要求，必须在不同的机架上进行数据备份。这样即使整个  机架被毁或是掉线，也能确保数据的正常使用。这也使数据传输，尤其是读数据，可以充分利用带宽，访问到多个机架，而写操作，则不得不涉及到更多的机架。<br />
3.3  产生、重复制、重平衡数据块<br />
当MASTER产生新的数据块时，如何放置新数据块，要考虑如下几个因素：（1）尽量放置在磁盘利用率低的数据块服  务器上，这样，慢慢地各服务器的磁盘利用率就会达到平衡。（2）尽量控制在一个服务器上的“新创建”的次数。（3）由于上一小节讨论的原因，我们需要把数  据块放置于不同的机架上。<br />
MASTER在可用的数据块备份低于用户设定的数目时需要进行重复制。这种情况源于多种原因：服务器不可用，数据被  破坏，磁盘被破坏，或者备份数目被修改。每个被需要重复制的数据块的优先级根据以下几项确定：第一是现在的数目距目标的距离，对于能阻塞用户程序的数据  块，我们也提高它的优先级。最后，  MASTER按照产生数据块的原则复制数据块，并把它们放到不同的机架内的服务器上。<br />
MASTER周期性的平衡各服务器上的负载：它检查chunk分布和负载平衡，通过这种方式来填充一个新的服务器而  不是把其他的内容统统放置到它上面带来大量的写数据。数据块放置的原则与上面讨论的相同，此外，MASTER还决定那些数据块要被移除，原则上他会清除那  些空闲空间低于平均值的那些服务器。<br />
3.4  垃圾收集<br />
在一个文件被删除之后，GFS并不立即收回磁盘空间，而是等到垃圾收集程序在文件和数据块级的的检查中收回。<br />
当一个文件被应用程序删除之后，MASTER会立即记录下这些变化，但文件所占用的资源却不会被立即收回，而是重新  给文件命了一个隐藏的名字，并附上了删除的时间戳。在MASTER定期检查名字空间时，它删除超过三天（可以设定）的隐藏的文件。在此之前，可以以一个新  的名字来读文件，还可以以前的名字恢复。当隐藏的文件在名字空间中被删除以后，它在内存中的元数据即被擦除，这就有效地切断了他和所有数据块的联系。<br />
在一个相似的定期的名字空间检查中，MASTER确认孤儿数据块（不属于任何文件）并擦除他的元数据，在和MASTER的心跳信息交换中，每个服务器报告他所拥有的数据块，MASTER返回元数据不在内存的数据块，服务器即可以删除这些数据块。<br />
3.5  过时数据的探测<br />
在数据更新时如果服务器停机了，那么他所保存的数据备份就会过时。对每个数据块，MASTER设置了一个版本号来区别更新过的数据块和过时的数据块。<br />
当MASTER授权一个新的lease时，他会增加数据块的版本号并会通知更新数据备份。MASTER和备份都会记  录下当前的版本号，如果一个备份当时不可用，那么他的版本号不可能提高，当ChunkServer重新启动并向MASTER报告他的数据块集时，  MASTER就会发现过时的数据。<br />
MASTER在定期的垃圾收集程序中清除过时的备份，在此以前，处于效率考虑，在各客户及英大使，他会认为根本不存  在过时的数据。作为另一个安全措施，  MASTER在给客户及关于数据块的应答或是另外一个读取数据的服务器数据是都会带上版本信息，在操作前客户机和服务器会验证版本信息以确保得到的是最新  的数据。<br />
4、容错和诊断<br />
4.1 高可靠性<br />
4.1.1  快速恢复<br />
不管如何终止服务，MASTER和数据块服务器都会在几秒钟内恢复状态和运行。实际上，我们不对正常终止和不正常终止进行区分，服务器进程都会被切断而终止。客户机和其他的服务器会经历一个小小的中断，然后它们的特定请求超时，重新连接重启的服务器，重新请求。<br />
4.1.2  数据块备份<br />
如上文所讨论的，每个数据块都会被备份到放到不同机架上的不同服务器上。对不同的名字空间，用户可以设置不同的备份级别。在数据块服务器掉线或是数据被破坏时，MASTER会按照需要来复制数据块。<br />
4.1.3  MASTER备份<br />
为确保可靠性，MASTER的状态、操作记录和检查点都在多台机器上进行了备份。一个操作只有在数据块服务器硬盘上  刷新并被记录在MASTER和其备份的上之后才算是成功的。如果MASTER或是硬盘失败，系统监视器会发现并通过改变域名启动它的一个备份机，而客户机  则仅仅是使用规范的名称来访问，并不会发现MASTER的改变。<br />
4.2  数据完整性<br />
每个数据块服务器都利用校验和来检验存储数据的完整性。原因：每个服务器随时都有发生崩溃的可能性，并且在两个服务器间比较数据块也是不现实的，同时，在两台服务器间拷贝数据并不能保证数据的一致性。<br />
每个Chunk按64kB的大小分成块，每个块有32位的校验和，校验和和日志存储在一起，和用户数据分开。<br />
在读数据时，服务器首先检查与被读内容相关部分的校验和，因此，服务器不会传播错误的数据。如果所检查的内容和校验  和不符，服务器就会给数据请求者返回一个错误的信息，并把这个情况报告给MASTER。客户机就会读其他的服务器来获取数据，而MASTER则会从其他的  拷贝来复制数据，等到一个新的拷贝完成时，MASTER就会通知报告错误的服务器删除出错的数据块。<br />
附加写数据时的校验和计算优化了，因为这是主要的写操作。我们只是更新增加部分的校验和，即使末尾部分的校验和数据已被损坏而我们没有检查出来，新的校验和与数据会不相符，这种冲突在下次使用时将会被检查出来。<br />
相反，如果是覆盖现有数据的写，在写以前，我们必须检查第一和最后一个数据块，然后才能执行写操作，最后计算和记录校验和。如果我们在覆盖以前不先检查首位数据块，计算出的校验和则会因为没被覆盖的数据而产生错误。<br />
在空闲时间，服务器会检查不活跃的数据块的校验和，这样可以检查出不经常读的数据的错误。一旦错误被检查出来，服务器会拷贝一个正确的数据块来代替错误的。<br />
4.3  诊断工具<br />
广泛而细致的诊断日志以微小的代价换取了在问题隔离、诊断、性能分析方面起到了重大的作用。GFS服务器用日志来记  录显著的事件（例如服务器停机和启动）和远程的应答。远程日志记录机器之间的请求和应答，通过收集不同机器上的日志记录，并对它们进行分析恢复，我们可以  完整地重现活动的场景，并用此来进行错误分析。<br />
5 测量<br />
5.1  测试环境<br />
一台主控机，两台主控机备份，16台数据块服务器，16台客户机。<br />
每台机器：2块PIII1.4G处理器，2G内存，2块80G5400rpm的硬盘，1块100Mbps全双工网卡<br />
19台服务器连接到一个HP2524交换机上，16台客户机俩接到领外一台交换机上，两台交换机通过1G的链路相连。</p>
<p>原文转自：http://bigweb.group.javaeye.com/group/topic/5749</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/504.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[转]嵌入式系统 Boot Loader 技术内幕</title>
		<link>http://www.donevii.com/post/387.html</link>
		<comments>http://www.donevii.com/post/387.html#comments</comments>
		<pubDate>Mon, 14 Jan 2008 05:24:32 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[mtk]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[boot]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[loader]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[Spreadtrum]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[串口]]></category>
		<category><![CDATA[内幕]]></category>
		<category><![CDATA[开发]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[类]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/387.html</guid>
		<description><![CDATA[级别： 初级 詹荣开 (zhanrk@sohu.com), Linux爱好者 2003 年 12 月 01 日 本文详细地介绍了基于嵌入式系统中的 OS 启动加载程序 ―― Boot Loader 的概念、软件设计的主要任务以及结构框架等内容。 1. 引... ]]></description>
			<content:encoded><![CDATA[<p>级别： 初级</p>
<p><a href="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/index.html#author"><font color="#996699">詹荣开</font></a> (<a href="mailto:zhanrk@sohu.com?subject=嵌入式系统 Boot Loader 技术内幕"><font color="#5c81a7">zhanrk@sohu.com</font></a>), Linux爱好者</p>
<p>2003 年 12 月 01 日</p>
<blockquote><p>本文详细地介绍了基于嵌入式系统中的 OS 启动加载程序 ―― <a href="http://www.donevii.com/post/tag/boot" class="st_tag internal_tag" rel="tag" title="Posts tagged with boot">Boot</a> <a href="http://www.donevii.com/post/tag/loader" class="st_tag internal_tag" rel="tag" title="Posts tagged with loader">Loader</a> 的概念、软件设计的主要任务以及结构框架等内容。</p></blockquote>
<p><!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--END RESERVED FOR FUTURE USE INCLUDE FILES--><a name="1" title="1"></a><span class="atitle">1. 引言</span></p>
<p>在专用的嵌入式板子运行 GNU/<a href="http://www.donevii.com/post/tag/linux" class="st_tag internal_tag" rel="tag" title="Posts tagged with linux">Linux</a> 系统已经变得越来越流行。一个嵌入式 <a href="http://www.donevii.com/post/tag/linux" class="st_tag internal_tag" rel="tag" title="Posts tagged with linux">Linux</a> 系统从软件的角度看通常可以分为四个层次：</p>
<p>1. <strong>引导加载程序。</strong>包括固化在固件(firmware)中的 boot 代码(可选)，和 Boot Loader 两大部分。</p>
<p>2. <strong>Linux 内核。</strong>特定于嵌入式板子的定制内核以及内核的启动参数。</p>
<p>3. <strong>文件系统。</strong>包括根文件系统和建立于 Flash 内存设备之上文件系统。通常用 ram disk 来作为 root fs。</p>
<p>4. <strong>用户应用程序。</strong>特定于用户的应用程序。有时在用户应用程序和内核层之间可能还会包括一个嵌入式图形用户界面。常用的嵌入式 GUI 有：MicroWindows 和 MiniGUI 懂。</p>
<p>引导加载程序是系统加电后运行的第一段软件代码。回忆一下 PC 的体系结构我们可以知道，PC 机中的引导加载程序由 BIOS(其本质就是一段固件程序)和位于硬盘 MBR 中的 OS Boot Loader（比如，LILO 和 GRUB 等）一起组成。BIOS 在完成硬件检测和资源分配后，将硬盘 MBR 中的 Boot Loader 读到系统的 RAM 中，然后将控制权交给 OS Boot Loader。Boot Loader 的主要运行任务就是将内核映象从硬盘上读到 RAM 中，然后跳转到内核的入口点去运行，也即开始启动操作系统。</p>
<p>而在嵌入式系统中，通常并没有像 BIOS 那样的固件程序（注，有的嵌入式 CPU 也会内嵌一段短小的启动程序），因此整个系统的加载启动任务就完全由 Boot Loader 来完成。比如在一个基于 ARM7TDMI core 的嵌入式系统中，系统在上电或复位时通常都从地址 0&#215;00000000 处开始执行，而在这个地址处安排的通常就是系统的 Boot Loader 程序。</p>
<p>本文将从 Boot Loader 的概念、Boot Loader 的主要任务、Boot Loader 的框架结构以及 Boot Loader 的安装等四个方面来讨论嵌入式系统的 Boot Loader。</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" /><br />
<img border="0" width="8" src="http://www.ibm.com/i/c.gif" height="6" /></td>
</tr>
</table>
<table align="right" cellPadding="0" cellSpacing="0" class="no-print">
<tr align="right">
<td><img width="100%" src="http://www.ibm.com/i/c.gif" height="4" /></p>
<table border="0" cellPadding="0" cellSpacing="0">
<tr>
<td vAlign="middle"><img border="0" width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" /></td>
<td align="right" vAlign="top"><a href="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/index.html#main" class="fbox"><strong><font color="#996699">回页首</font></strong></a></td>
</tr>
</table>
</td>
</tr>
</table>
<p><a name="2" title="2"></a><span class="atitle">2. Boot Loader 的概念</span></p>
<p>简单地说，Boot Loader 就是在操作系统内核运行之前运行的一段小程序。通过这段小程序，我们可以初始化硬件设备、建立内存空间的映射图，从而将系统的软硬件环境带到一个合适的状态，以便为最终调用操作系统内核准备好正确的环境。</p>
<p>通常，Boot Loader 是严重地依赖于硬件而实现的，特别是在嵌入式世界。因此，在嵌入式世界里建立一个通用的 Boot Loader 几乎是不可能的。尽管如此，我们仍然可以对 Boot Loader 归纳出一些通用的概念来，以指导用户特定的 Boot Loader 设计与实现。</p>
<p><a name="N10072" title="N10072"></a><span class="smalltitle"><strong><font face="Arial">1. Boot Loader 所支持的 CPU 和嵌入式板</font></strong></span></p>
<p>每种不同的 CPU 体系结构都有不同的 Boot Loader。有些 Boot Loader 也支持多种体系结构的 CPU，比如 U-Boot 就同时支持 ARM 体系结构和MIPS 体系结构。除了依赖于 CPU 的体系结构外，Boot Loader 实际上也依赖于具体的嵌入式板级设备的配置。这也就是说，对于两块不同的嵌入式板而言，即使它们是基于同一种 CPU 而构建的，要想让运行在一块板子上的 Boot Loader 程序也能运行在另一块板子上，通常也都需要修改 Boot Loader 的源程序。</p>
<p><a name="N1007B" title="N1007B"></a><span class="smalltitle"><strong><font face="Arial">2. Boot Loader 的安装媒介（Installation Medium）</font></strong></span></p>
<p>系统加电或复位后，所有的 CPU 通常都从某个由 CPU 制造商预先安排的地址上取指令。比如，基于 ARM7TDMI core 的 CPU 在复位时通常都从地址 0&#215;00000000 取它的第一条指令。而基于 CPU 构建的嵌入式系统通常都有某种类型的固态存储设备(比如：ROM、EEPROM 或 FLASH 等)被映射到这个预先安排的地址上。因此在系统加电后，CPU 将首先执行 Boot Loader 程序。</p>
<p>下图1就是一个同时装有 Boot Loader、内核的启动参数、内核映像和根文件系统映像的固态存储设备的典型空间分配结构图。<br />
<a name="N10089" title="N10089"></a><strong>图1 固态存储设备的典型空间分配结构</strong><br />
<img src="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/images/image001.gif" /><br />
<a name="N10094" title="N10094"></a><span class="smalltitle"><strong><font face="Arial">3. 用来控制 Boot Loader 的设备或机制</font></strong></span></p>
<p>主机和目标机之间一般通过串口建立连接，Boot Loader 软件在执行时通常会通过串口来进行 I/O，比如：输出打印信息到串口，从串口读取用户控制字符等。</p>
<p><a name="N1009D" title="N1009D"></a><span class="smalltitle"><strong><font face="Arial">4. Boot Loader 的启动过程是单阶段（Single Stage）还是多阶段（Multi-Stage）</font></strong></span></p>
<p>通常多阶段的 Boot Loader 能提供更为复杂的功能，以及更好的可移植性。从固态存储设备上启动的 Boot Loader 大多都是 2 阶段的启动过程，也即启动过程可以分为 stage 1 和 stage 2 两部分。而至于在 stage 1 和 stage 2 具体完成哪些任务将在下面讨论。</p>
<p><a name="N100A6" title="N100A6"></a><span class="smalltitle"><strong><font face="Arial">5. Boot Loader 的操作模式 (Operation Mode)</font></strong></span></p>
<p>大多数 Boot Loader 都包含两种不同的操作模式：&#8221;启动加载&#8221;模式和&#8221;下载&#8221;模式，这种区别仅对于开发人员才有意义。但从最终用户的角度看，Boot Loader 的作用就是用来加载操作系统，而并不存在所谓的启动加载模式与下载工作模式的区别。</p>
<p><strong>启动加载（Boot loading）模式：</strong>这种模式也称为&#8221;自主&#8221;（Autonomous）模式。也即 Boot Loader 从目标机上的某个固态存储设备上将操作系统加载到 RAM 中运行，整个过程并没有用户的介入。这种模式是 Boot Loader 的正常工作模式，因此在嵌入式产品发布的时侯，Boot Loader 显然必须工作在这种模式下。</p>
<p><strong>下载（Downloading）模式：</strong>在这种模式下，目标机上的 Boot Loader 将通过串口连接或网络连接等通信手段从主机（Host）下载文件，比如：下载内核映像和根文件系统映像等。从主机下载的文件通常首先被 Boot Loader 保存到目标机的 RAM 中，然后再被 Boot Loader 写到目标机上的FLASH 类固态存储设备中。Boot Loader 的这种模式通常在第一次安装内核与根文件系统时被使用；此外，以后的系统更新也会使用 Boot Loader 的这种工作模式。工作于这种模式下的 Boot Loader 通常都会向它的终端用户提供一个简单的命令行接口。</p>
<p>像 Blob 或 U-Boot 等这样功能强大的 Boot Loader 通常同时支持这两种工作模式，而且允许用户在这两种工作模式之间进行切换。比如，Blob 在启动时处于正常的启动加载模式，但是它会延时 10 秒等待终端用户按下任意键而将 blob 切换到下载模式。如果在 10 秒内没有用户按键，则 blob 继续启动 Linux 内核。</p>
<p><a name="N100BE" title="N100BE"></a><span class="smalltitle"><strong><font face="Arial">6. BootLoader 与主机之间进行文件传输所用的通信设备及协议</font></strong></span></p>
<p>最常见的情况就是，目标机上的 Boot Loader 通过串口与主机之间进行文件传输，传输协议通常是 xmodem／ymodem／zmodem 协议中的一种。但是，串口传输的速度是有限的，因此通过以太网连接并借助 TFTP 协议来下载文件是个更好的选择。</p>
<p>此外，在论及这个话题时，主机方所用的软件也要考虑。比如，在通过以太网连接和 TFTP 协议来下载文件时，主机方必须有一个软件用来的提供 TFTP 服务。</p>
<p>在讨论了 BootLoader 的上述概念后，下面我们来具体看看 BootLoader 的应该完成哪些任务。</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" /><br />
<img border="0" width="8" src="http://www.ibm.com/i/c.gif" height="6" /></td>
</tr>
</table>
<table align="right" cellPadding="0" cellSpacing="0" class="no-print">
<tr align="right">
<td><img width="100%" src="http://www.ibm.com/i/c.gif" height="4" /></p>
<table border="0" cellPadding="0" cellSpacing="0">
<tr>
<td vAlign="middle"><img border="0" width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" /></td>
<td align="right" vAlign="top"><a href="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/index.html#main" class="fbox"><strong><font color="#996699">回页首</font></strong></a></td>
</tr>
</table>
</td>
</tr>
</table>
<p><a name="3" title="3"></a><span class="atitle">3. Boot Loader 的主要任务与典型结构框架</span></p>
<p>在继续本节的讨论之前，首先我们做一个假定，那就是：假定内核映像与根文件系统映像都被加载到 RAM 中运行。之所以提出这样一个假设前提是因为，在嵌入式系统中内核映像与根文件系统映像也可以直接在 ROM 或 Flash 这样的固态存储设备中直接运行。但这种做法无疑是以运行速度的牺牲为代价的。</p>
<p>从操作系统的角度看，Boot Loader 的总目标就是正确地调用内核来执行。</p>
<p>另外，由于 Boot Loader 的实现依赖于 CPU 的体系结构，因此大多数 Boot Loader 都分为 stage1 和 stage2 两大部分。依赖于 CPU 体系结构的代码，比如设备初始化代码等，通常都放在 stage1 中，而且通常都用汇编语言来实现，以达到短小精悍的目的。而 stage2 则通常用C语言来实现，这样可以实现给复杂的功能，而且代码会具有更好的可读性和可移植性。</p>
<p>Boot Loader 的 stage1 通常包括以下步骤(以执行的先后顺序)：</p>
<ul>
<li>硬件设备初始化。</li>
<li>为加载 Boot Loader 的 stage2 准备 RAM 空间。</li>
<li>拷贝 Boot Loader 的 stage2 到 RAM 空间中。</li>
<li>设置好堆栈。</li>
<li>跳转到 stage2 的 C 入口点。</li>
</ul>
<p>Boot Loader 的 stage2 通常包括以下步骤(以执行的先后顺序)：</p>
<ul>
<li>初始化本阶段要使用到的硬件设备。</li>
<li>检测系统内存映射(memory map)。</li>
<li>将 kernel 映像和根文件系统映像从 flash 上读到 RAM 空间中。</li>
<li>为内核设置启动参数。</li>
<li>调用内核。</li>
</ul>
<p><a name="N1012A" title="N1012A"></a><span class="smalltitle"><strong><font face="Arial">3.1 Boot Loader 的 stage1</font></strong></span></p>
<p><strong>3.1.1 基本的硬件初始化</strong></p>
<p>这是 Boot Loader 一开始就执行的操作，其目的是为 stage2 的执行以及随后的 kernel 的执行准备好一些基本的硬件环境。它通常包括以下步骤（以执行的先后顺序）：</p>
<p>1． <strong>屏蔽所有的中断。</strong>为中断提供服务通常是 OS 设备驱动程序的责任，因此在 Boot Loader 的执行全过程中可以不必响应任何中断。中断屏蔽可以通过写 CPU 的中断屏蔽寄存器或状态寄存器（比如 ARM 的 CPSR 寄存器）来完成。</p>
<p>2． <strong>设置 CPU 的速度和时钟频率。</strong></p>
<p>3． <strong>RAM 初始化。</strong>包括正确地设置系统的内存控制器的功能寄存器以及各内存库控制寄存器等。</p>
<p>4． <strong>初始化 LED。</strong>典型地，通过 GPIO 来驱动 LED，其目的是表明系统的状态是 OK 还是 Error。如果板子上没有 LED，那么也可以通过初始化 UART 向串口打印 Boot Loader 的 Logo 字符信息来完成这一点。</p>
<p>5． <strong>关闭 CPU 内部指令／数据 cache。</strong></p>
<p><strong>3.1.2 为加载 stage2 准备 RAM 空间</strong></p>
<p>为了获得更快的执行速度，通常把 stage2 加载到 RAM 空间中来执行，因此必须为加载 Boot Loader 的 stage2 准备好一段可用的 RAM 空间范围。</p>
<p>由于 stage2 通常是 C 语言执行代码，因此在考虑空间大小时，除了 stage2 可执行映象的大小外，还必须把堆栈空间也考虑进来。此外，空间大小最好是 memory page 大小(通常是 4KB)的倍数。一般而言，1M 的 RAM 空间已经足够了。具体的地址范围可以任意安排，比如 blob 就将它的 stage2 可执行映像安排到从系统 RAM 起始地址 0xc0200000 开始的 1M 空间内执行。但是，将 stage2 安排到整个 RAM 空间的最顶 1MB(也即(RamEnd-1MB) &#8211; RamEnd)是一种值得推荐的方法。</p>
<p>为了后面的叙述方便，这里把所安排的 RAM 空间范围的大小记为：stage2_size(字节)，把起始地址和终止地址分别记为：stage2_start 和 stage2_end(这两个地址均以 4 字节边界对齐)。因此：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">stage2_end＝stage2_start＋stage2_size</pre>
</td>
</tr>
</table>
<p>另外，还必须确保所安排的地址范围的的确确是可读写的 RAM 空间，因此，必须对你所安排的地址范围进行测试。具体的测试方法可以采用类似于 blob 的方法，也即：以 memory page 为被测试单位，测试每个 memory page 开始的两个字是否是可读写的。为了后面叙述的方便，我们记这个检测算法为：test_mempage，其具体步骤如下：</p>
<p>1． 先保存 memory page 一开始两个字的内容。</p>
<p>2． 向这两个字中写入任意的数字。比如：向第一个字写入 0&#215;55，第 2 个字写入 0xaa。</p>
<p>3． 然后，立即将这两个字的内容读回。显然，我们读到的内容应该分别是 0&#215;55 和 0xaa。如果不是，则说明这个 memory page 所占据的地址范围不是一段有效的 RAM 空间。</p>
<p>4． 再向这两个字中写入任意的数字。比如：向第一个字写入 0xaa，第 2 个字中写入 0&#215;55。</p>
<p>5． 然后，立即将这两个字的内容立即读回。显然，我们读到的内容应该分别是 0xaa 和 0&#215;55。如果不是，则说明这个 memory page 所占据的地址范围不是一段有效的 RAM 空间。</p>
<p>6． 恢复这两个字的原始内容。测试完毕。</p>
<p>为了得到一段干净的 RAM 空间范围，我们也可以将所安排的 RAM 空间范围进行清零操作。</p>
<p><strong>3.1.3 拷贝 stage2 到 RAM 中</strong></p>
<p>拷贝时要确定两点：(1) stage2 的可执行映象在固态存储设备的存放起始地址和终止地址；(2) RAM 空间的起始地址。</p>
<p><strong>3.1.4 设置堆栈指针 sp</strong></p>
<p>堆栈指针的设置是为了执行 C 语言代码作好准备。通常我们可以把 sp 的值设置为(stage2_end-4)，也即在 3.1.2 节所安排的那个 1MB 的 RAM 空间的最顶端(堆栈向下生长)。</p>
<p>此外，在设置堆栈指针 sp 之前，也可以关闭 led 灯，以提示用户我们准备跳转到 stage2。</p>
<p>经过上述这些执行步骤后，系统的物理内存布局应该如下图2所示。</p>
<p><strong>3.1.5 跳转到 stage2 的 C 入口点</strong></p>
<p>在上述一切都就绪后，就可以跳转到 Boot Loader 的 stage2 去执行了。比如，在 ARM 系统中，这可以通过修改 PC 寄存器为合适的地址来实现。<br />
<a name="N101A5" title="N101A5"></a><strong>图2 bootloader 的 stage2 可执行映象刚被拷贝到 RAM 空间时的系统内存布局</strong><br />
<img src="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/images/image002.gif" /><br />
<a name="N101B0" title="N101B0"></a><span class="smalltitle"><strong><font face="Arial">3.2 Boot Loader 的 stage2 </font></strong></span></p>
<p>正如前面所说，stage2 的代码通常用 C 语言来实现，以便于实现更复杂的功能和取得更好的代码可读性和可移植性。但是与普通 C 语言应用程序不同的是，在编译和链接 boot loader 这样的程序时，我们不能使用 glibc 库中的任何支持函数。其原因是显而易见的。这就给我们带来一个问题，那就是从那里跳转进 main() 函数呢？直接把 main() 函数的起始地址作为整个 stage2 执行映像的入口点或许是最直接的想法。但是这样做有两个缺点：1)无法通过main() 函数传递函数参数；2)无法处理 main() 函数返回的情况。一种更为巧妙的方法是利用 trampoline(弹簧床)的概念。也即，用汇编语言写一段trampoline 小程序，并将这段 trampoline 小程序来作为 stage2 可执行映象的执行入口点。然后我们可以在 trampoline 汇编小程序中用 CPU 跳转指令跳入 main() 函数中去执行；而当 main() 函数返回时，CPU 执行路径显然再次回到我们的 trampoline 程序。简而言之，这种方法的思想就是：用这段 trampoline 小程序来作为 main() 函数的外部包裹(external wrapper)。</p>
<p>下面给出一个简单的 trampoline 程序示例(来自blob)：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">.text .globl _trampoline _trampoline: 	bl	main 	/* if main ever returns we just call it again */ 	b	_trampoline</pre>
</td>
</tr>
</table>
<p>可以看出，当 main() 函数返回后，我们又用一条跳转指令重新执行 trampoline 程序――当然也就重新执行 main() 函数，这也就是 trampoline(弹簧床)一词的意思所在。</p>
<p><strong>3.2.1初始化本阶段要使用到的硬件设备</strong></p>
<p>这通常包括：（1）初始化至少一个串口，以便和终端用户进行 I/O 输出信息；（2）初始化计时器等。</p>
<p>在初始化这些设备之前，也可以重新把 LED 灯点亮，以表明我们已经进入 main() 函数执行。</p>
<p>设备初始化完成后，可以输出一些打印信息，程序名字字符串、版本号等。</p>
<p><strong>3.2.2 检测系统的内存映射（memory map）</strong></p>
<p>所谓内存映射就是指在整个 4GB 物理地址空间中有哪些地址范围被分配用来寻址系统的 RAM 单元。比如，在 SA-1100 CPU 中，从 0xC000,0000 开始的 512M 地址空间被用作系统的 RAM 地址空间，而在 Samsung S3C44B0X CPU 中，从 0x0c00,0000 到 0&#215;1000,0000 之间的 64M 地址空间被用作系统的 RAM 地址空间。虽然 CPU 通常预留出一大段足够的地址空间给系统 RAM，但是在搭建具体的嵌入式系统时却不一定会实现 CPU 预留的全部 RAM 地址空间。也就是说，具体的嵌入式系统往往只把 CPU 预留的全部 RAM 地址空间中的一部分映射到 RAM 单元上，而让剩下的那部分预留 RAM 地址空间处于未使用状态。 <strong>由于上述这个事实，因此 Boot Loader 的 stage2 必须在它想干点什么 (比如，将存储在 flash 上的内核映像读到 RAM 空间中) 之前检测整个系统的内存映射情况，也即它必须知道 CPU 预留的全部 RAM 地址空间中的哪些被真正映射到 RAM 地址单元，哪些是处于 &#8220;unused&#8221; 状态的。</strong></p>
<p><strong>(1) 内存映射的描述</strong></p>
<p>可以用如下数据结构来描述 RAM 地址空间中的一段连续(continuous)的地址范围：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">typedef struct memory_area_struct { 	u32 start; /* the base address of the memory region */ 	u32 size; /* the byte number of the memory region */ 	int used; } memory_area_t;</pre>
</td>
</tr>
</table>
<p>这段 RAM 地址空间中的连续地址范围可以处于两种状态之一：(1)used=1，则说明这段连续的地址范围已被实现，也即真正地被映射到 RAM 单元上。(2)used=0，则说明这段连续的地址范围并未被系统所实现，而是处于未使用状态。</p>
<p>基于上述 memory_area_t 数据结构，整个 CPU 预留的 RAM 地址空间可以用一个 memory_area_t 类型的数组来表示，如下所示：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">memory_area_t memory_map[NUM_MEM_AREAS] = { 	[0 ... (NUM_MEM_AREAS - 1)] = { 		.start = 0, 		.size = 0, 		.used = 0 	}, };</pre>
</td>
</tr>
</table>
<p>(2) 内存映射的检测</p>
<p>下面我们给出一个可用来检测整个 RAM 地址空间内存映射情况的简单而有效的算法：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">/* 数组初始化 */ for(i = 0; i &lt; NUM_MEM_AREAS; i++) 	memory_map[i].used = 0; /* first write a 0 to all memory locations */ for(addr = MEM_START; addr &lt; MEM_END; addr += PAGE_SIZE) 	* (u32 *)addr = 0; for(i = 0, addr = MEM_START; addr &lt; MEM_END; addr += PAGE_SIZE) {      /*       * 检测从基地址 MEM_START+i*PAGE_SIZE 开始,大小为 * PAGE_SIZE 的地址空间是否是有效的RAM地址空间。       */      调用3.1.2节中的算法test_mempage()；      if ( current memory page isnot a valid ram page) { 		/* no RAM here */ 		if(memory_map[i].used ) 			i++; 		continue; 	} 	 	/* 	 * 当前页已经是一个被映射到 RAM 的有效地址范围 	 * 但是还要看看当前页是否只是 4GB 地址空间中某个地址页的别名？ 	 */ 	if(* (u32 *)addr != 0) { /* alias? */ 		/* 这个内存页是 4GB 地址空间中某个地址页的别名 */ 		if ( memory_map[i].used ) 			i++; 		continue; 	} 	 	/* 	 * 当前页已经是一个被映射到 RAM 的有效地址范围 	 * 而且它也不是 4GB 地址空间中某个地址页的别名。 	 */ 	if (memory_map[i].used == 0) { 		memory_map[i].start = addr; 		memory_map[i].size = PAGE_SIZE; 		memory_map[i].used = 1; 	} else { 		memory_map[i].size += PAGE_SIZE; 	} } /* end of for (…) */</pre>
</td>
</tr>
</table>
<p>在用上述算法检测完系统的内存映射情况后，Boot Loader 也可以将内存映射的详细信息打印到串口。</p>
<p><strong>3.2.3 加载内核映像和根文件系统映像</strong></p>
<p><strong>(1) 规划内存占用的布局</strong></p>
<p>这里包括两个方面：(1)内核映像所占用的内存范围；（2）根文件系统所占用的内存范围。在规划内存占用的布局时，主要考虑基地址和映像的大小两个方面。</p>
<p>对于内核映像，一般将其拷贝到从(MEM_START＋0&#215;8000) 这个基地址开始的大约1MB大小的内存范围内(嵌入式 Linux 的内核一般都不操过 1MB)。为什么要把从 MEM_START 到 MEM_START＋0&#215;8000 这段 32KB 大小的内存空出来呢？这是因为 Linux 内核要在这段内存中放置一些全局数据结构，如：启动参数和内核页表等信息。</p>
<p>而对于根文件系统映像，则一般将其拷贝到 MEM_START+0&#215;0010,0000 开始的地方。如果用 Ramdisk 作为根文件系统映像，则其解压后的大小一般是1MB。</p>
<p><strong>（2）从 Flash 上拷贝</strong></p>
<p>由于像 ARM 这样的嵌入式 CPU 通常都是在统一的内存地址空间中寻址 Flash 等固态存储设备的，因此从 Flash 上读取数据与从 RAM 单元中读取数据并没有什么不同。用一个简单的循环就可以完成从 Flash 设备上拷贝映像的工作：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">  while(count) { 	*dest++ = *src++; /* they are all aligned with word boundary */ 	count -= 4; /* byte number */ };</pre>
</td>
</tr>
</table>
<p><strong>3.2.4 设置内核的启动参数</strong></p>
<p>应该说，在将内核映像和根文件系统映像拷贝到 RAM 空间中后，就可以准备启动 Linux 内核了。但是在调用内核之前，应该作一步准备工作，即：设置 Linux 内核的启动参数。</p>
<p>Linux 2.4.x 以后的内核都期望以标记列表(tagged list)的形式来传递启动参数。启动参数标记列表以标记 ATAG_CORE 开始，以标记 ATAG_NONE 结束。每个标记由标识被传递参数的 tag_header 结构以及随后的参数值数据结构来组成。数据结构 tag 和 tag_header 定义在 Linux 内核源码的include/asm/setup.h 头文件中：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">/* The list ends with an ATAG_NONE node. */ #define ATAG_NONE	0x00000000 struct tag_header { 	u32 size; /* 注意，这里size是字数为单位的 */ 	u32 tag; }; …… struct tag { 	struct tag_header hdr; 	union { 		struct tag_core		core; 		struct tag_mem32	mem; 		struct tag_videotext	videotext; 		struct tag_ramdisk	ramdisk; 		struct tag_initrd	initrd; 		struct tag_serialnr	serialnr; 		struct tag_revision	revision; 		struct tag_videolfb	videolfb; 		struct tag_cmdline	cmdline; 		/* 		 * Acorn specific 		 */ 		struct tag_acorn	acorn; 		/* 		 * DC21285 specific 		 */ 		struct tag_memclk	memclk; 	} u; };</pre>
</td>
</tr>
</table>
<p>在嵌入式 Linux 系统中，通常需要由 Boot Loader 设置的常见启动参数有：ATAG_CORE、ATAG_MEM、ATAG_CMDLINE、ATAG_RAMDISK、ATAG_INITRD等。</p>
<p>比如，设置 ATAG_CORE 的代码如下：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">params = (struct tag *)BOOT_PARAMS; 	params-&gt;hdr.tag = ATAG_CORE; 	params-&gt;hdr.size = tag_size(tag_core); 	params-&gt;u.core.flags = 0; 	params-&gt;u.core.pagesize = 0; 	params-&gt;u.core.rootdev = 0; 	params = tag_next(params);</pre>
</td>
</tr>
</table>
<p>其中，BOOT_PARAMS 表示内核启动参数在内存中的起始基地址，指针 params 是一个 struct tag 类型的指针。宏 tag_next() 将以指向当前标记的指针为参数，计算紧临当前标记的下一个标记的起始地址。注意，内核的根文件系统所在的设备ID就是在这里设置的。</p>
<p>下面是设置内存映射情况的示例代码：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">for(i = 0; i &lt; NUM_MEM_AREAS; i++) { 		if(memory_map[i].used) { 			params-&gt;hdr.tag = ATAG_MEM; 			params-&gt;hdr.size = tag_size(tag_mem32); 			params-&gt;u.mem.start = memory_map[i].start; 			params-&gt;u.mem.size = memory_map[i].size; 			 			params = tag_next(params); 		} }</pre>
</td>
</tr>
</table>
<p>可以看出，在 memory_map［］数组中，每一个有效的内存段都对应一个 ATAG_MEM 参数标记。</p>
<p>Linux 内核在启动时可以以命令行参数的形式来接收信息，利用这一点我们可以向内核提供那些内核不能自己检测的硬件参数信息，或者重载(override)内核自己检测到的信息。比如，我们用这样一个命令行参数字符串&#8221;console=ttyS0,115200n8&#8243;来通知内核以 ttyS0 作为控制台，且串口采用 &#8220;115200bps、无奇偶校验、8位数据位&#8221;这样的设置。下面是一段设置调用内核命令行参数字符串的示例代码：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">char *p; 	/* eat leading white space */ 	for(p = commandline; *p == ' '; p++) 		; 	/* skip non-existent command lines so the kernel will still     * use its default command line. 	 */ 	if(*p == '\0') 		return; 	params-&gt;hdr.tag = ATAG_CMDLINE; 	params-&gt;hdr.size = (sizeof(struct tag_header) + strlen(p) + 1 + 4) &gt;&gt; 2; 	strcpy(params-&gt;u.cmdline.cmdline, p); 	params = tag_next(params);</pre>
</td>
</tr>
</table>
<p>请注意在上述代码中，设置 tag_header 的大小时，必须包括字符串的终止符&#8217;\0&#8242;，此外还要将字节数向上圆整4个字节，因为 tag_header 结构中的size 成员表示的是字数。</p>
<p>下面是设置 ATAG_INITRD 的示例代码，它告诉内核在 RAM 中的什么地方可以找到 initrd 映象(压缩格式)以及它的大小：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">	params-&gt;hdr.tag = ATAG_INITRD2; 	params-&gt;hdr.size = tag_size(tag_initrd); 	 	params-&gt;u.initrd.start = RAMDISK_RAM_BASE; 	params-&gt;u.initrd.size = INITRD_LEN; 	 	params = tag_next(params);</pre>
</td>
</tr>
</table>
<p>下面是设置 ATAG_RAMDISK 的示例代码，它告诉内核解压后的 Ramdisk 有多大（单位是KB）：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">params-&gt;hdr.tag = ATAG_RAMDISK; params-&gt;hdr.size = tag_size(tag_ramdisk); 	 params-&gt;u.ramdisk.start = 0; params-&gt;u.ramdisk.size = RAMDISK_SIZE; /* 请注意，单位是KB */ params-&gt;u.ramdisk.flags = 1; /* automatically load ramdisk */ 	 params = tag_next(params);</pre>
</td>
</tr>
</table>
<p>最后，设置 ATAG_NONE 标记，结束整个启动参数列表：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">static void setup_end_tag(void) { 	params-&gt;hdr.tag = ATAG_NONE; 	params-&gt;hdr.size = 0; }</pre>
</td>
</tr>
</table>
<p><strong>3.2.5 调用内核</strong></p>
<p>Boot Loader 调用 Linux 内核的方法是直接跳转到内核的第一条指令处，也即直接跳转到 MEM_START＋0&#215;8000 地址处。在跳转时，下列条件要满足：</p>
<p>1． CPU 寄存器的设置：</p>
<ul>
<li>R0＝0；</li>
<li>R1＝机器类型 ID；关于 Machine Type Number，可以参见 <strong>linux/arch/arm/tools/mach-types。</strong></li>
<li>R2＝启动参数标记列表在 RAM 中起始基地址；</li>
</ul>
<p>2． CPU 模式：</p>
<ul>
<li>必须禁止中断（IRQs和FIQs）；</li>
<li>CPU 必须 SVC 模式；</li>
</ul>
<p>3． Cache 和 MMU 的设置：</p>
<ul>
<li>MMU 必须关闭；</li>
<li>指令 Cache 可以打开也可以关闭；</li>
<li>数据 Cache 必须关闭；</li>
</ul>
<p>如果用 C 语言，可以像下列示例代码这样来调用内核：</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td class="code-outline">
<pre class="displaycode">void (*theKernel)(int zero, int arch, u32 params_addr) = (void (*)(int, int, u32))KERNEL_RAM_BASE; …… theKernel(0, ARCH_NUMBER, (u32) kernel_params_start);</pre>
</td>
</tr>
</table>
<p>注意，theKernel()函数调用应该永远不返回的。如果这个调用返回，则说明出错。</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" /><br />
<img border="0" width="8" src="http://www.ibm.com/i/c.gif" height="6" /></td>
</tr>
</table>
<table align="right" cellPadding="0" cellSpacing="0" class="no-print">
<tr align="right">
<td><img width="100%" src="http://www.ibm.com/i/c.gif" height="4" /></p>
<table border="0" cellPadding="0" cellSpacing="0">
<tr>
<td vAlign="middle"><img border="0" width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" /></td>
<td align="right" vAlign="top"><a href="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/index.html#main" class="fbox"><strong><font color="#996699">回页首</font></strong></a></td>
</tr>
</table>
</td>
</tr>
</table>
<p><a name="4" title="4"></a><span class="atitle">4. 关于串口终端</span></p>
<p>在 boot loader 程序的设计与实现中，没有什么能够比从串口终端正确地收到打印信息能更令人激动了。此外，向串口终端打印信息也是一个非常重要而又有效的调试手段。但是，我们经常会碰到串口终端显示乱码或根本没有显示的问题。造成这个问题主要有两种原因：(1) boot loader 对串口的初始化设置不正确。(2) 运行在 host 端的终端仿真程序对串口的设置不正确，这包括：波特率、奇偶校验、数据位和停止位等方面的设置。</p>
<p>此外，有时也会碰到这样的问题，那就是：在 boot loader 的运行过程中我们可以正确地向串口终端输出信息，但当 boot loader 启动内核后却无法看到内核的启动输出信息。对这一问题的原因可以从以下几个方面来考虑：</p>
<p>(1) 首先请确认你的内核在编译时配置了对串口终端的支持，并配置了正确的串口驱动程序。</p>
<p>(2) 你的 boot loader 对串口的初始化设置可能会和内核对串口的初始化设置不一致。此外，对于诸如 s3c44b0x 这样的 CPU，CPU 时钟频率的设置也会影响串口，因此如果 boot loader 和内核对其 CPU 时钟频率的设置不一致，也会使串口终端无法正确显示信息。</p>
<p>(3) 最后，还要确认 boot loader 所用的内核基地址必须和内核映像在编译时所用的运行基地址一致，尤其是对于 uClinux 而言。假设你的内核映像在编译时用的基地址是 0xc0008000，但你的 boot loader 却将它加载到 0xc0010000 处去执行，那么内核映像当然不能正确地执行了。</p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td><img width="100%" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" height="1" /><br />
<img border="0" width="8" src="http://www.ibm.com/i/c.gif" height="6" /></td>
</tr>
</table>
<table align="right" cellPadding="0" cellSpacing="0" class="no-print">
<tr align="right">
<td><img width="100%" src="http://www.ibm.com/i/c.gif" height="4" /></p>
<table border="0" cellPadding="0" cellSpacing="0">
<tr>
<td vAlign="middle"><img border="0" width="16" src="http://www.ibm.com/i/v14/icons/u_bold.gif" height="16" /></td>
<td align="right" vAlign="top"><a href="http://www-128.ibm.com/developerworks/cn/linux/l-btloader/index.html#main" class="fbox"><strong><font color="#996699">回页首</font></strong></a></td>
</tr>
</table>
</td>
</tr>
</table>
<p><a name="5" title="5"></a><span class="atitle">5. 结束语</span></p>
<p>Boot Loader 的设计与实现是一个非常复杂的过程。如果不能从串口收到那激动人心的&#8221;uncompressing linux&#8230;&#8230;&#8230;&#8230;&#8230;&#8230; done, booting the kernel……&#8221;内核启动信息，恐怕谁也不能说：&#8221;嗨，我的 boot loader 已经成功地转起来了！&#8221;。</p>
<p><a name="author" title="author"></a><span class="atitle">关于作者</span></p>
<table border="0" width="100%" cellPadding="0" cellSpacing="0">
<tr>
<td colSpan="3"><img width="100%" src="http://www.ibm.com/i/c.gif" height="5" /></td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/387.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP招聘信息－PHP/MySQL DEVELOPER－TORONTO</title>
		<link>http://www.donevii.com/post/364.html</link>
		<comments>http://www.donevii.com/post/364.html#comments</comments>
		<pubDate>Thu, 13 Dec 2007 07:09:39 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[hire]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[resume]]></category>
		<category><![CDATA[toronto]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[工作]]></category>
		<category><![CDATA[招聘]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/364.html</guid>
		<description><![CDATA[Divaris Alexander Corporation is looking for an experienced PHP developer to contribute as a contractor in the current development of our web based property tax platform. Duties/ Responsibilities: Database Modeling. Preparation of application architectur... ]]></description>
			<content:encoded><![CDATA[<p>Divaris Alexander Corporation is looking for an experienced <a href="http://www.donevii.com/post/tag/php" class="st_tag internal_tag" rel="tag" title="Posts tagged with php">PHP</a> <a href="http://www.donevii.com/post/tag/developer" class="st_tag internal_tag" rel="tag" title="Posts tagged with developer">developer</a> to contribute as a contractor in the current development of our <a href="http://www.donevii.com/post/tag/web" class="st_tag internal_tag" rel="tag" title="Posts tagged with web">web</a> based property tax platform.<br />
Duties/ Responsibilities:<br />
Database Modeling.<br />
Preparation of application architecture.<br />
Design and implement application.<br />
We are looking for a well-organized, conscientious person with skills in PHP programming and a working knowledge of <a href="http://www.donevii.com/post/tag/mysql" class="st_tag internal_tag" rel="tag" title="Posts tagged with mysql">MySQL</a>. You will be required to interact with our team and deploy the application features quickly, maintaining quality and reliability.<br />
Experience required:<br />
Organizations Skills<br />
Communication Skills (oral and written in English)<br />
Understanding of <a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">HTML</a> standards<br />
Working knowledge of XML, CSS and <a href="http://www.donevii.com/post/tag/javascript" class="st_tag internal_tag" rel="tag" title="Posts tagged with javascript">JavaScript</a><br />
PHP 4 and 5 (4 years +)<br />
MySQL 4.11 and 5.1 (4 years +)<br />
Additional Skills:<br />
Working knowledge in a <a href="http://www.donevii.com/post/tag/linux" class="st_tag internal_tag" rel="tag" title="Posts tagged with linux">Linux</a> Environment<br />
Knowledgeable in database modeling and tools(programs)<br />
Flex 2 or 3 and <a href="http://www.donevii.com/post/tag/actionscript" class="st_tag internal_tag" rel="tag" title="Posts tagged with actionscript">Actionscript</a><br />
Coldfusion<br />
Based in <a href="http://www.donevii.com/post/tag/toronto" class="st_tag internal_tag" rel="tag" title="Posts tagged with toronto">Toronto</a>, <a href="http://www.dival.com">Divaris Alexander Corporation</a>, incorporated in 1987, provides property assessment and municipal tax consulting services for industrial, commercial, investment and special use property owners and occupants.<br />
We require that you be based in Toronto and able to work onsite.<br />
Apply via email to <a href="mailto:info@dival.com">info[at]dival.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/364.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>2007软件开发2.0大会－29日</title>
		<link>http://www.donevii.com/post/355.html</link>
		<comments>http://www.donevii.com/post/355.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 18:37:22 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[life]]></category>
		<category><![CDATA[csdn]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sd2c]]></category>
		<category><![CDATA[sd2china]]></category>
		<category><![CDATA[平台]]></category>
		<category><![CDATA[开发]]></category>
		<category><![CDATA[手机]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[类]]></category>
		<category><![CDATA[软件开发]]></category>

		<guid isPermaLink="false">http://www.donevii.com/post/355.html</guid>
		<description><![CDATA[很兴奋能参加这个会议，可惜公司这面早上只有我一个人去，大清早收拾好就在楼下的 984 车站等车，结果等了好久才来车，车上好几个人都是同去开会的，有两位先生还不错，下车后给我领了... ]]></description>
			<content:encoded><![CDATA[<p>很兴奋能参加这个会议，可惜公司这面早上只有我一个人去，大清早收拾好就在楼下的 984 车站等车，结果等了好久才来车，车上好几个人都是同去开会的，有两位先生还不错，下车后给我领了路，在开会过程中还见过几回，好像是 TechExcel 公司的(希望没有记错)，先表示感谢一下吧。</p>
<p>还好没有迟到很多，蒋总(CSDN总裁 蒋涛)刚上台，我就进入主会场了。蒋总的开幕辞解释了为什么要办这次会议，以及这次会议的主题《揭示下一代软件开发趋势》。</p>
<p>查看 <a href="http://www.donevii.com/post/tag/csdn" class="st_tag internal_tag" rel="tag" title="Posts tagged with csdn">CSDN</a> 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111178.html">【SD2.0大会】CSDN总裁蒋涛致大会开幕辞</a></p>
<p>第一场孟岩(CSDN &amp; 《程序员》总编)和Jon Erickson(DDJ杂志主编)讨论了一下什么是软件开发2.0，很可惜，进去时太匆忙，忘了向工作MM要同声传译的耳机了，本来我的听力就巨烂无比，再加上做在我旁边和后台的几位仁兄肆无忌惮的聊天，让我感觉像在菜市场里看哑剧一样。我只注意到了一个词组，“Tipping Point”，关键点？顶点？突破点？</p>
<p>查看 CSDN 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111179.html">【SD2.0大会】DDJ总编与程序员总编孟岩对话SD2.0</a></p>
<p>第二场Ivar Jacobson(现代软件开发之父)的《软件开发大趋势》也不错，主要阐述了三个观点。</p>
<blockquote><p>1、如何开发更优秀、高效和低成本的软件？<br />
高效由多构件和积极主动的开发者来完成，低成本由大规模的构件重用实现，优秀的软件必然是实用性强，可扩展性高和稳定的产品。</p>
<p>2、<a href="http://www.donevii.com/post/tag/%e8%bd%af%e4%bb%b6%e5%bc%80%e5%8f%91" class="st_tag internal_tag" rel="tag" title="Posts tagged with 软件开发">软件开发</a>，实践为王<br />
程序员需要更多的高级技术实践。重要的构件重用技术包括工程流，SOA，企业架构技术。实践是过程的基础，过程只是组成实践的一个元素。优秀的实践之所以成本低是由大规模的构件重用来实现。</p>
<p>3、如何获得实践<br />
传统的方法是通过书籍和网络，这样获得的数据相关性不够。先进的实践来源是没有限制的，包多基本和高级的方式，但只要是实用的就可以自由选择并合并这些实践组成一个新的过程。这种方法为开发人员提供了一种比较新的模式。</p></blockquote>
<p>查看 CSDN 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111184.html">【SD2.0大会】Ivar：为什么我做的架构比你的年龄还长</a></p>
<p><img src="http://images.csdn.net/20071129/ivar.gif" /> </p>
<p>第三场林斌(谷歌工程研究院副院长)介绍了 Google 的新手机平台 Android。之前我仅因为 Android 开源免费，并且出于对 Google 产品先量一贯的信任也装了一个 SDK 和 emulator 来玩一下，今天开会才了解到，Android 相对于其它平台的四个优势：及时响应；不会死机；通话切换；保存状态。不过同时公布的几项数据很有趣。“发布前两周，共250，000下载；下载量中国第一，超过美国1.5倍；”不知道 Google 用于奖励 Android 开发的一千万美元会有多少被中国人拿到？</p>
<p>查看 CSDN 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111182.html" class="broken_link">【SD2.0大会】数据现Andriod惊人影响力，Google林斌公布架构图</a></p>
<p><img width="500" src="http://images.csdn.net/20071129/20071129104857.gif" height="375" /></p>
<p>茶歇时外面的展台也不出，可惜送的东西太少，广告性质的东西太多。中途问过几次是否可以使用 wi-fi ，结果被告知因为太多的媒体在使用 wi-fi 造成网络拥挤。我在主会场内开机试了几回，也始终无法连上，本来手机欠费，准备有什么事再从网上通知其它人，或者先在淘宝上充下话费，唉。总能在不经意的回头间看到李梁一闪而过的身影，每当我再凝神寻找时又不见踪影，还好茶歇时在外面看到了，当我在展台转了一圈，他又不见了，不知道是我眼神不好，还是他“打一枪换一个地方”、“四处流窜”。几次去会场服务区询问，恰恰每次都是总到同一个MM，结果当我最后一次询问完后离开时，那个MM注视我好长一段时间，旁边有人询问她问题时，那个MM也没有反应，让我小小的陶醉了一下，俺也终于能吸引别人的眼球了。（早上起得太早，顶着鸡窝头去的，希望不是这个原因吸引她的目光）</p>
<p>第四场Jim Douglas(CodeGear公司CEO)……i forget……明天再去找别人的笔记回忆一下吧。</p>
<p>第五场陈榕(北京科泰世纪科技有限公司首席科学家、技术总监)对手机的独到见解让我眼前为之一亮。陈总认为计算机内部只有两样东西，数据和处理数据的程序，那么，既然数据可以通过短信、红外和蓝牙等方式传递，那么程序也可以。在陈总的话语中多次听到 1-click，即用户通过一次点击来使用某个服务，而这个服务处理数据的程序是在后台执行，让程序对用户是不可见的，也就没办法要求用户来为程序付费，仅会对服务消费。并且通过一定的方式，比如 metadata 的方式，可以使 handset 自动的去执行一些操作。比如 A 给 B 发送了一个视频文件，但是 B 的手机上没有这个解码器，如果 A 的视频文件的 metadata 中有一个类似于 URL 或者 URI 的东西能指定到解码器的 dll ，那么 B 机器会在后台自动下载安装。正如某位通过<a target="_blank" href="http://jiwai.de/sd2c" class="broken_link">叽歪</a>与现场互动的那位朋友说的，如果手机可以 DIY ，那么按照陈总的想法，手机可以和新组装的电脑一样，是裸机，通过我们安装服务来使用，这样大大的扩展了手机的功能。</p>
<p>查看 CSDN 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111187.html">【软件开发2.0大会】陈榕：让软件消失在用户面前</a></p>
<p>第五场Jim Reinders(Intel软件产品部首席宣传官居兼市场总监)讲的内容好像是关于并行计算这方面的，不过因为我在考虑其它事情，所以内容记得不是很清，也得明天找别人去“补课”了。</p>
<p>第六场Andrie Alexandrescu(C++天才作家、D语言领袖人物，华尔街咨询师)给我的感觉就是风趣……并且他那一长串长场介绍时的 title 也吸引了我，“某国伞兵”。</p>
<p>中午的午餐不过，就是同时就餐的人太多，要排好长的队，流量大，并发高，这也是我们这次会议要解决的问题。伙食不错，不过因为时间太紧，我也没有多吃，可能是因为菜有点咸，口味有点重(某些人看到这里不要误会，p囧q )。</p>
<p>下午第一场听的范路(Product Evangelist, China Mainland CodeGear)的《2.0时代软件开发新方法、新工具》，对 RAD STUDIO 2007 有了一些初步的了解。不过范路对软件体系结构、开发过程、语言以及开发工具的演变做了一些很有意思的分析和讲解。</p>
<p>查看 CSDN 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111193.html">【SD2.0大会】2.0开发时代的四大特性改变</a></p>
<p>在听下午第一场的中间，去洗手间的时候，顺路去博文视点的展台看了一下，看到一堆书上放了“请赐名片”和一位老兄把一张名片给了博文的工作人员，然后拿起一本书就走了，我以为是“以名片免费换书”，因为之前博文的传单上有写“免费赠书”。我兴致勃勃的回去找杨恺和老高要名牌，准备回来领书，结果回来一问，原来书是卖的，名片给不给都行……我被鄙视了……我看到的那个人之前给付钱了，不过我没看到。T_T</p>
<p>第二场幺宝刚讲解 Google Gear，之前有了解过 Gear 所以没有用太多的注意力来听，一直在紧紧的盯着“老钱”(钱宏武)，期待着下一场他的演讲。</p>
<p>第三场，终于等到钱宏武演讲了，上回在悠视(悠视网 <a href="http://www.uusee.com">www.uusee.com</a>)听他讲的时候，就感觉比较有内容，不像其它人讲的都是空泛的概念之类的，而他讲的是经验、问题和方法。其实，我这回自费来参加 SD2.0 大会，就是想来听他的演讲。钱宏武针对以前在 SOHU 做互动产品的经验介绍了如何开发和维护。其中他对于团队的构成和任务分配的特殊见解真是让我眼前豁然开朗，“给开发人员定一个稍高一点的目标”，“培养水平低的员工”等等……</p>
<p>查看 CSDN 报道 &gt;&gt; <a target="_blank" href="http://news.csdn.net/n/20071129/111208.html">【SD2.0大会】钱宏武谈做互联网产品就像娶老婆 贤惠又要好看</a></p>
<p><img src="http://info-database.csdn.net/Upload/2007-11-29/IMG_1790.jpg" /></p>
<p>第四场听了前一半的董大伟的《ASP.NET AJAX 与 Silverlight 的完美结合》和冯彦文的《利用 Ajax/<a href="http://www.donevii.com/post/tag/java" class="st_tag internal_tag" rel="tag" title="Posts tagged with java">Java</a> 技术建立高流量的即时双向沟通网站》。</p>
<p>终于等到我发挥特长的时候了，吃……晚宴时间。</p>
<p>晚上的沙龙算是这些大会给我的一个惊喜，原本订课时的沙龙是管理方面的，但是上午突然看到传单上有临时安排 Ben Wang(王文斌？当时离得太远，没看清，Taobao 首席架构师)、老钱(钱宏武)、雷鸣(前 Baidu 架构师)、汤道生(腾讯开发副总裁)四人合讲的《大型网站架构》收益良多啊……</p>
<p>总的来看 CSDN 这次举办的活动还是不错，真希望能坚持下去。</p>
<blockquote><p>有几点让我感觉不太爽的地方：<br />
１、每个人太早的发包，以至于，包比人多（有人来的时候自己本身就带包了，比如我），所以好多座位上都是放的包，让一些人没有地方座，吃饭、听课时都有这种情况。<br />
２、经常在开会过程中听到手机响，真是搞不懂，开会前明明主持人已经让大家把手机都调成振动了。并且有人接到电话后还在原位置大声的煲电话粥，如果是工作就算了，竟然长时间打电话，还在闲聊！真是损人不利已。<br />
３、原本晚上的沙龙是 VIP 才有的？但是看到好像没有什么限制，大家随便听。然后领东西时 VIP 也没有什么多的，就是一本增刊还是外面可以买到的，让我郁闷不已，VIP 和普通的没有什么区别嘛。<br />
４、开会时，一直有人在下边聊天、拉关系，无比烦人！</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/355.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XCache &amp; XDebug on road</title>
		<link>http://www.donevii.com/post/349.html</link>
		<comments>http://www.donevii.com/post/349.html#comments</comments>
		<pubDate>Fri, 28 Sep 2007 08:03:37 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lighttpd]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=349</guid>
		<description><![CDATA[终于配置上 XCache 和 XDebug 了，可惜的是 php-java-bridge 一直没搞好，只有双击运行 JavaBridge 后才行，唉，要是能内置到 PHP 里就好了。 继续研究 FastCGI &#38; Lighttpd 如果说之前在 UUSee 是向上研究，... ]]></description>
			<content:encoded><![CDATA[<p>终于配置上 XCache 和 XDebug 了，可惜的是 <a href="http://www.donevii.com/post/tag/php" class="st_tag internal_tag" rel="tag" title="Posts tagged with php">php</a>-<a href="http://www.donevii.com/post/tag/java" class="st_tag internal_tag" rel="tag" title="Posts tagged with java">java</a>-bridge 一直没搞好，只有双击运行 JavaBridge 后才行，唉，要是能内置到 PHP 里就好了。</p>
<p>继续研究 <a href="http://www.donevii.com/post/tag/fastcgi" class="st_tag internal_tag" rel="tag" title="Posts tagged with fastcgi">FastCGI</a> &amp; <a href="http://www.donevii.com/post/tag/lighttpd" class="st_tag internal_tag" rel="tag" title="Posts tagged with lighttpd">Lighttpd</a></p>
<p>如果说之前在 UUSee 是向上研究，既“抽象”、“架构”的话，那么来 IMobile 之后研究方向则是向下，研究底层，研究以前没注意到的更细节的地方了。</p>
<p>:)</p>
<p>Good days, good luck.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/349.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UML</title>
		<link>http://www.donevii.com/post/340.html</link>
		<comments>http://www.donevii.com/post/340.html#comments</comments>
		<pubDate>Thu, 05 Jul 2007 10:06:17 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[uml]]></category>
		<category><![CDATA[开发]]></category>
		<category><![CDATA[技术]]></category>
		<category><![CDATA[类]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=340</guid>
		<description><![CDATA[1.建模 1.1 为什么要建模 建立大厦和建立狗窝的区别是建设狗窝不需要设计。要生产合格的软件就要有一套关于体系结构、过程和工具的规范。 建模的定义：建模是对现实的简化。 建模的目标... ]]></description>
			<content:encoded><![CDATA[<p>1.建模 <br />1.1 为什么要建模 <br />建立大厦和建立狗窝的区别是建设狗窝不需要设计。要生产合格的软件就要有一套关于体系结构、过程和工具的规范。 <br />建模的定义：建模是对现实的简化。 <br />建模的目标： <br />1）模型帮助我们按照实际情况或按照我们所需要的样式对系统进行可视化。 <br />2）模型允许我们详细说明系统的结构和行为。 <br />3）模型给出一个知道我们构造系统的模板。 <br />4）模型对我们的决策进行文档化。 <br />建模就是把复杂的系统变成小的系统，采用&ldquo;各个击破&rdquo;的原则逐一解决。 <br />1.2 建模原理 <br />1）选择创建什么模型很重要，模型要反映你难于处理的开发问题。 <br />2）模型要在不同的精度级别上来表示。你可以根据观察的角色和观察的原因来选择精度。 <br />3）建造模型要和现实相连。 <br />4）重要的系统需要用一组独立的模型去处理。在面向对象的软件体系中，为了理解系统的体系结构，你需要几个互补和连锁的视图：用例图、设计视图、进程视图、实现视图和实施视图。 <br />1.3 面向对象的建模 <br />面向算法的建模在需求发生变化或者系统增长后就变得难以维护。 <br />面向对象的建模把对象和类作为其主要构造块。例如，在三层结构中，我们可以在用户接口层、中间层和数据库层中找到你想要的对象。 <br />2 UML介绍 <br />2.1 概述 <br />UML可以对软件密集型系统的制品进行可视化、详述、构造和文档化。最好把它用于以用况（用例）为驱动、以体系结构为中心、跌代及增量的过程中。 <br />UML是一种语言，它是一种可视化的语言，它是一组图形符号。它可用于详细描述。它又是一种构造语言，可以直接生成代码。用Rational XDE就可以实现从UML到C#，或者从C#到UML的双向工程。 <br />2.2 UML的概念模型 <br />学习建模的三个要素：UML的基本构造块、这些构造块放在一起的规则、一些运用于整个UML的公共机制。 <br />UML中由一些四种事物 <br />1）结构事物 －－－ <a href="http://www.donevii.com/post/tag/%e7%b1%bb" class="st_tag internal_tag" rel="tag" title="Posts tagged with 类">类</a>、接口、协作（它是一个交互，它是由一组共同工作以提供某协作行为的角色和其它元素构成的一个群体。）、用例、主动类（至少拥有一个进程或者线程，其元素的行为可以和其它元素的行为并发）、构件（如COM+和Java Bean）、节点。 <br />2）行为事物 －－－ 交互、状态机（描述了一个对象或者一个交互在生命期内响应事件所经历的状态序列）。 <br />3）分组事物 －－－ 包 <br />4）注释事物 －－－ 注解 <br />UML中的四种关系 <br />1）依赖 －－－ 两个事物间的语义关系 <br />2）关联 －－－ 是一种结构关系，如聚合 <br />3）泛化 －－－ 一般/特殊关系 <br />4）实现 －－－ 用在两种地方：接口和实现他们之间的类和构件之间；用例和实现他们的协作之间。 <br />UML中的图 <br />1）类图 －－－ 系统的静态状态图，包含主动类的类图给除系统的静态进程视图。 <br />2）对象图 <br />3）用例图 <br />4）顺序图 <br />5）协作图 <br />6）状态图 <br />7）活动图 －－－ 强调对象间的控制流程 <br />8）构件图 <br />9）实施图 <br />2.3 体系结构 <br />我们用5个互联的视图来描述软件密集型系统的体系结构： <br />1）系统的用例图 <br />2）系统的设计视图 －－－ 静态方面由类图和对象图描述，动态方面由交互图、状态图和活动图描述。 <br />3）系统的进程视图 －－－ 包含了形成系统并发和同步机制的线程和进程。 <br />4）系统的实现视图 －－－ 主要针对系统发布的配置管理。 <br />5）系统的实施视图 <br />2.4 软件开发生命周期 <br />用况驱动 <br />以体系结构为中心 <br />跌代过程 －－－ 涉及到一连串可执行发布的管理。 <br />软件开发生命周期的四个阶段： <br />初始 <br />细化 <br />构造 <br />移交</p>
<p>3 Hello World <br />4 类 <br />4.3.1 对系统的词汇建模 <br />需要做如下工作： <br />1）识别用户用于描述问题或解决问题的事物。 <br />2）对于每个抽象，识别一个职责集。 <br />3）提供为实现每个类的职责所需的属性和操作。 <br />4.3.2 对系统中职责的分布建模 <br />抽象出来的类要适中，不要过大过小。 <br />需要如下工作： <br />1） 识别一组为了完成某些行为而紧密协同工作的类。 <br />2） 对上面的每一个类识别出一组职责。 <br />3） 从整体上观察这些类，过大的分解，过小的合并。 <br />4） 考虑相映的协作方式，重新分配职责。 <br />5 关系 <br />面向对象建模中三种最重要的关系： <br />依赖 －－－ 使用关系 <br />泛化 －－－ 一般/特殊关系 <br />关联 －－－ 结构关系 <br />当你开始建模是，特别是解决这些对象的动态协作时，你还会遇到两种其它的关系：链（可能发送消息的对象间的实例）和转换（状态机中不同状态的连接） <br />对简单依赖建模 －－－(如一个类作为另一个类的参数) 依赖从操作的类指向当作参数的类。 <br />对单继承建模 <br />对结构关系建模 <br />6 公共机制 <br />四个公共机制可以使UML简化:详述、修饰、公共划分和扩展 <br />构造型、标记值和约束使UML提供的用以增加新的构造块、创建新的特性以及描述新的语义的机制。 <br />7 图 <br />有5种最重要的互补视图：用例视图、设计视图、进程视图、实现视图和实施视图 <br />每一种视图包含结构建模（静态）和行为建模（动态） <br />建图的两种方法：正向工程和逆向工程 <br />一些术语： <br />系统:由子系统组成； 子系统:由元素组成； 模型:对现实的简化； 视图:对系统模型的组织和结构的投影，注重于系统的一个方面。 <br />结构图：类图、对象图、构件图、实施图。 <br />行为图: 用况图、顺序图、协作图、状态图、活动图。 <br />顺序图和协作图统称交互图。顺序图强调消息的时间次序，协作图强调收发消息的对象的结构组织。 <br />活动图强调对象之间的控制流。 <br />如何选择视图来建模？ <br />1）视图能够表达系统的体系结构，能够暴露项目的技术风险。 <br />2）决定用哪些制品来捕获视图的基本细节。 <br />3）作为你的过程策略的一部分，决定把那种视图至于某种形式或者半形式的控制之下。 <br />4）保留废弃的视图。 <br />两种系统建模的方法： <br />1）针对同一模型，用不同层次上的细节描述图。 <br />2）在不同的抽象层次上，从一个模型跟踪到另一个模型的方法建模。 <br />8 类图 <br />类图包括如下内容： <br />类 <br />接口 <br />协作 <br />依赖、泛化和关联关系。 <br />对系统的静态设计视图建模时使用类图的3种方式： <br />1）对系统的词汇建模 <br />2）对简单协作建模 －－－ 协作提供的一些合作行为强于其所有元素行为之和。 <br />3）对逻辑数据库模式建模 <br />9 高级类 <br />在UML中更一般的构造块是类元，类元包括：类、接口、数据类型、信号、构件、节点、用况和子系统。他是描述结构特性和行为特性的机制。</p>
<p>转自：http://<a href="http://www.donevii.com/post/tag/blog" class="st_tag internal_tag" rel="tag" title="Posts tagged with blog">blog</a>.<a href="http://www.donevii.com/post/tag/csdn" class="st_tag internal_tag" rel="tag" title="Posts tagged with csdn">csdn</a>.net/binjuny/archive/2007/07/05/1679193.aspx</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/340.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Internet Explorer Toolbar (Deskband) Tutorial</title>
		<link>http://www.donevii.com/post/316.html</link>
		<comments>http://www.donevii.com/post/316.html#comments</comments>
		<pubDate>Sat, 24 Mar 2007 13:50:29 +0000</pubDate>
		<dc:creator>dengwei</dc:creator>
				<category><![CDATA[doc]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=316</guid>
		<description><![CDATA[转至http://www.codeproject.com Download Toolbar Binary &#8211; 20Kb Download Source &#8211; 21 Kb Introduction Having recieved a number of requests for a tutorial of sorts on developing Internet Explorer Toolbars with the RBDeskband and CWindowImpl wiz... ]]></description>
			<content:encoded><![CDATA[<p>转至<a href="http://www.codeproject.com">http://www.codeproject.com</a></p>
<ul class="download">
<li><a href="http://www.codeproject.com/atl/IEToolbarTutorial/MotleyFool_dll.zip" class="broken_link">Download Toolbar Binary &#8211; 20Kb</a> </li>
<li><a href="http://www.codeproject.com/atl/IEToolbarTutorial/MotleyFool_src.zip" class="broken_link"><font color="#800080">Download Source &#8211; 21 Kb</font></a> </li>
</ul>
<p><img height="190" alt="The Motley Fool Quote IE Toolbar" src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure15.jpg" width="607" /> </p>
<h2>Introduction </h2>
<p nd="1">Having recieved a number of requests for a tutorial of sorts on developing Internet Explorer Toolbars with the RBDeskband and CWindowImpl wizards that I created, I have come up with a simple sample toolbar which can be used as a reference when developing your own toolbars or explorer bars. The tutorial will walk you through the stages of developing a toolbar for IE that is very similar to the Address bar that is already present in IE. I wanted to do a tutorial that would provide a realistic sample and would produce an end result that could be used by others after the tutorial was finish. So, the tutorial is going to show you how to develop an IE toolbar to get stock quote information from The Motley Fool website. So with that, let us get started. </p>
<h2>Prequisites </h2>
<p nd="2">This tutorial assumes that you already know how to program in C++ and know some information about ATL and COM. To work through this tutorial, you will need the following installed on your development machine: </p>
<ul>
<li nd="3">Visual C++6 installed </li>
<li nd="4">RBDeskBand ATL Object Wizard (Version 2.0) [<a href="http://www.codeproject.com/atl/rbdeskband.asp"><font color="#800080">get it here</font></a>] </li>
<li nd="5">CWindowImpl ATL Object Wizard [<a href="http://www.codeproject.com/atl/rbcwindowimpl.asp"><font color="#800080">get it here</font></a>] </li>
</ul>
<h2>The Framework </h2>
<p nd="6">The IE toolbar consists of a COM component supporting IDeskband and a few other necessary interfaces for which IE looks for when loading registered toolbars, explorer bars and deskbands. The RBDeskband ATL Object Wizard provides most of the framework for this article. What we will need to do is create our project, a new COM object to house our toolbar, and a few CWindowImpl classes using the CWindowImpl ATL Object Wizard. Then connecting these parts together we will produce the IE toolbar in the picture at the top of the article. Visually the toolbar consists of an editbox and a toolbar with one button on it. In actuality the toolbar consists of the fore mentioned and a non visible window that is used to reflect messages to the Toolbar window, which will process or forward messges to itself and the edit box. </p>
<h2>Creating The <a href="http://www.donevii.com/post/tag/shell" class="st_tag internal_tag" rel="tag" title="Posts tagged with shell">Shell</a> </h2>
<p nd="7">We will not work through the steps in creating the shell for our toolbar. </p>
<h3>Creating The Project </h3>
<ul>
<li nd="8">If you have not done so already, start Visual C++6. </li>
<li nd="9">Then, from the File menu select New menu item; the New Dialog pops up. </li>
<li nd="10">In the New Dialog, select the Projects tab, if not already selected. </li>
<li nd="11">Select ATL COM AppWizard from the list view, if not already selected. </li>
<li nd="12">In the Project name, type &quot;MotleyFool&quot;. See Figure 1. </li>
<li nd="13">Click the OK Button. </li>
</ul>
<p><center nd="14"><img height="400" alt="Figure 1. New Dialog." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure1.jpg" width="561" name="Figure1" /> <br />Figure 1. New Dialog. </center>
<ul>
<li nd="15">The ATL COM AppWizard will kick in. </li>
<li nd="16">Clicking the Finish Button, accepting the default AppWizard attributes. See Figure 2. </li>
<li nd="17">The New Project Information Dialog will present itself requesting confirmation of your project settings. </li>
<li nd="18">Click the OK Button. </li>
</ul>
<p><center nd="19"><img height="384" alt="Figure 2. ATL COM AppWizard" src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure2.jpg" width="477" name="Figure2" /> <br />Figure 2. ATL COM AppWizard. </center><br />
<h3>Creating The DeskBand Object </h3>
<p>Now that we have our project container we need to add our IDeskBand derived component so that the DLL actually exposes something. </p>
<ul>
<li nd="21">From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked. </li>
<li nd="22">In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed. </li>
<li nd="23">Next select the DeskBand item from the Objects list. </li>
<li nd="24">Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3. </li>
<li nd="25">On the Names property page, type &quot;StockBar&quot; into the Short Name field. See Figure 4. </li>
<li nd="26">Select the DeskBand ATL Object Wizard property page </li>
<li nd="27">Check the Internet Explorer Toolbar checkbox. See Figure 5. </li>
<li nd="28">Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our DeskBand&#8217;s base implementation. </li>
</ul>
<p><center nd="29"><img height="257" alt="Figure 3. ATL Object Wizard." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure3.jpg" width="413" name="Figure3" /> <br />Figure 3. ATL Object Wizard. </center><center nd="30"><img height="279" alt="Figure 4. ATL Object Wizard Properties - Names." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure4.jpg" width="433" name="Figure4" /> <br />Figure 4. ATL Object Wizard Properties &#8211; Names. </center><center nd="31"><img height="279" alt="Figure 5. ATL Object Wizard Properties - DeskBand ATL Object Wizard." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure5.jpg" width="433" name="Figure5" /> <br />Figure 5. ATL Object Wizard Properties &#8211; DeskBand ATL Object Wizard </center>
<p>Now our project has the DeskBand implementation that we will modify to produce the toolbar pictured at the top of the article. First we will create the window classes we will need and then come back to the Desbkand object and update it to use our window classes. </p>
<h2>Creating The Window Classes </h2>
<p nd="32">So back in the Framework section we said that we would need three window classes. One for the Edit Box, one for the toolbar, and one for message reflection back to the toolbar. Let us now create these window classes. </p>
<h3>The Edit Window </h3>
<p nd="33">We need to create a derived <a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> from the standard EDIT button window <a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> because we are going to be adding methods to our <a href="http://www.donevii.com/post/tag/class" class="st_tag internal_tag" rel="tag" title="Posts tagged with class">class</a> to help support functionality of the toolbar. This is one reason why we cannot use a CContainedWindow object directly. </p>
<ul>
<li nd="34">From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked. </li>
<li nd="35">In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed. </li>
<li nd="36">Next select the CWindowImpl item from the Objects list. </li>
<li nd="37">Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3. </li>
<li nd="38">On the Names property page, type &quot;EditQuote&quot; into the Short Name field. </li>
<li nd="39">Select the CWindowImpl property page. See Figure 6. </li>
<li nd="40">Select the SUPERCLASS radio button from the DECLAR_WND_* group. </li>
<li nd="41">In the Window Class Name field, type &quot;EDITQUOTE&quot;. </li>
<li nd="42">In the Original Class Name list, select the EDIT listbox item. See Figure 7. </li>
<li nd="43">Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our CWindowImpl derived class implementation. </li>
</ul>
<p><center nd="44"><img height="279" alt="Figure 6. ATL Object Wizard Properties - Names." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure6.jpg" width="433" name="Figure6" /> <br />Figure 6. ATL Object Wizard Properties &#8211; Names. </center><center nd="45"><img height="279" alt="Figure 7. ATL Object Wizard Properties - Names." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure7.jpg" width="433" name="Figure7" /> <br />Figure 7. ATL Object Wizard Properties &#8211; CWindowImpl. </center><br />
<h3>The Toolbar Window </h3>
<p nd="46">We need to create a derived class from the standard TOOLBARCLASSNAME window class because we are going to be adding methods to our class to help support functionality of the toolbar. It will also be the parent for the edit box and the window which the IE host will request from our DeskBand. </p>
<ul>
<li nd="47">From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked. </li>
<li nd="48">In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed. </li>
<li nd="49">Next select the CWindowImpl item from the Objects list. </li>
<li nd="50">Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3. </li>
<li nd="51">On the Names property page, type &quot;MFToolbar&quot; into the Short Name field. </li>
<li nd="52">Select the CWindowImpl property page. See Figure 8. </li>
<li nd="53">Select the SUPERCLASS radio button from the DECLAR_WND_* group. </li>
<li nd="54">In the Window Class Name field, type &quot;MOTLEYFOOLTOOLBAR&quot;. </li>
<li nd="55">In the Original Class Name list, select the TOOLBARCLASSNAME listbox item. See Figure 9. </li>
<li nd="56">Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our CWindowImpl derived class implementation. </li>
</ul>
<p><center nd="57"><img height="279" alt="Figure 8. ATL Object Wizard Properties - Names." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure8.jpg" width="433" name="Figure8" /> <br />Figure 8. ATL Object Wizard Properties &#8211; Names. </center><center nd="58"><img height="279" alt="Figure 9. ATL Object Wizard Properties - Names." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure9.jpg" width="433" name="Figure9" /> <br />Figure 9. ATL Object Wizard Properties &#8211; CWindowImpl. </center><br />
<h3>The Reflection Window </h3>
<p nd="59">We need to create a reflection window. It&#8217;s just a CWindowImpl window implmented class. We are going to be adding a small bit of functionality just to create the toolbar object and be able to access the toolbar member from our deskband class. </p>
<ul>
<li nd="60">From the Insert menu, select New ATL Object menu item; the ATL Object Wizard dialog is invoked. </li>
<li nd="61">In the ATL Object Wizard dialog, select the RadBytes Category. If this category is missing then make sure that the RBDeskband and CWindowImpl ATL Object Wizards are installed. </li>
<li nd="62">Next select the CWindowImpl item from the Objects list. </li>
<li nd="63">Click the Next button to invoke the ATL Object Wizard Properties dialog for the Deskband object. See Figure 3. </li>
<li nd="64">On the Names property page, type &quot;ReflectionWnd&quot; into the Short Name field. See Figure 10. </li>
<li nd="65">We will not change any of the CWindowImpl property page values this time. </li>
<li nd="66">Click the OK button on the ATL Object Wizard Properties Dialog. The ATL Object Wizard will create the files necessary for our CWindowImpl derived class implementation. </li>
</ul>
<p><center nd="67"><img height="279" alt="Figure 10. ATL Object Wizard Properties - Names." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure10.jpg" width="433" name="Figure10" /> <br />Figure 10. ATL Object Wizard Properties &#8211; Names. </center><br />
<h2>Adding The Details </h2>
<p nd="68">Now that we have our window classes available we can add our functionality for our toolbar to the appropriate window classes. Let us start with the deepest window class and work our way back out. </p>
<h3>The EditQuote Details </h3>
<p nd="69">For the EditQuote implementation, we need to be able to process keystrokes from the user and let the host that created our deskband object know our edit box has focus. To accomplish the first part, we need to look ahead and see that our DeskBand object will be implementing the IInputObject interface. So the host will query for that interface and know that we want to recieve messages and be given the chance to recieve focus. When the host sends our band messages to process they come through the IInputObject::TranslateAccelerator method. Our DeskBand will implement this method and it is best if our edit box, which will process the message, copy the TranslateAcceleratorIO method definition so our deskband can forward the message easily through a logical method call. </p>
<p><center nd="70"><img height="503" alt="Figure 11. FileView Pane." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure11.jpg" width="408" name="Figure11" /> <br />Figure 11. FileView Pane. </center>
<p>In the FileView pane (See Figure 11), double click the EditQuote.h item under Header Files. This will open the header file in the editing area. We now need to define the method definition for TranslateAcceleratorIO. To do this, add below the virtual CEditQuote destructor the following line of code: </p>
<pre lang="c++" nd="71">STDMETHOD(TranslateAcceleratorIO)(LPMSG lpMsg);</pre>
<p>Now Open the EditQuote.cpp source file and add the implementation of TranslateAcceleratorIO to the file </p>
<pre nd="72" langu="c++">STDMETHODIMP CEditQuote::TranslateAcceleratorIO(LPMSG lpMsg){   TranslateMessage(lpMsg);   DispatchMessage(lpMsg);   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>Now our DeskBand implementation can call this message and the edit box will process the key strokes properly. But wait, our edit box should notify the toolbar to load the quote details entered if the key stroke is the enter key. For this, we will need to define a message id and send that message to the parent window to process. In the EditQuote.h header file below the include statement, add the definition of our message id as shown below in bold. </p>
<pre lang="c++"><span class="cpp-preprocessor" nd="73">#include &lt;commctrl.h&gt;</span><strong><span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span> WM_GETQUOTE = WM_USER + <span class="cpp-literal">1024</span>;</strong>			</pre>
<p>In our EditQuote.cpp file we will add code to our TranslateAcceleratorIO method to process the enter key. Add the code below in bold to the EditQuote.cpp file. </p>
<pre lang="c++" nd="74">STDMETHODIMP CEditQuote::TranslateAcceleratorIO(LPMSG lpMsg){<strong>   <span class="cpp-keyword">int</span> nVirtKey = (<span class="cpp-keyword">int</span>)(lpMsg-&gt;wParam);   <span class="cpp-keyword">if</span> (VK_RETURN == nVirtKey)   {      <span class="cpp-comment">// remove system beep on enter key by setting key code to 0</span>      lpMsg-&gt;wParam = <span class="cpp-literal">0</span>;      ::PostMessage(GetParent(), WM_GETQUOTE, <span class="cpp-literal">0</span>, <span class="cpp-literal">0</span>);      <span class="cpp-keyword">return</span> S_OK;   }</strong>   TranslateMessage(lpMsg);   DispatchMessage(lpMsg);   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>Now our edit box will notify the parent when the user presses the enter key so that the parent can retrieve the requested ticker symbol details, this part will be implemented when we get to the toolbar details. </p>
<p nd="75">The first part of the Edit boxes implementation is finished. Now we need to be able for the edit box to have the deskband notify the host that we have focus or that we don&#8217;t have focus any longer. To do this we will need to add a method for the deskband to pass us it&#8217;s address so that we can call a method of the deskband class. These next steps will involve adding code to the CEditQuote class and to our Deskband class implementation. </p>
<p nd="76">Open the EditQuote.h file and add a forward reference to the CStockBar class so that we can defined our methods and members in our class header without knowing the implementation details of our deskband class, add the line in bold. </p>
<pre lang="c++" nd="78"><span class="cpp-preprocessor" nd="77">#include &lt;commctrl.h&gt;</span><span class="cpp-keyword">const</span> <span class="cpp-keyword">int</span> WM_GETQUOTE = WM_USER + <span class="cpp-literal">1024</span>;<strong><span class="cpp-keyword">class</span> CStockBar;</strong></pre>
<p>For our class to notify the host that our deskband has focus, we need to add a message handler for EN_SETFOCUS. Add the command code handler code below in bold to your EditQuote.h file. </p>
<pre lang="c++" nd="79">   BEGIN_MSG_MAP(CEditQuote)<strong>      COMMAND_CODE_HANDLER(EN_SETFOCUS, OnSetFocus)</strong>   END_MSG_MAP()</pre>
<p>Then add the method definition for OnSetFocus to the header file below the commented out handler prototypes as follows below in bold. </p>
<pre lang="c++"><span class="cpp-comment">// Handler prototypes:</span><span class="cpp-comment">// LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</span><span class="cpp-comment">// LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled);</span><span class="cpp-comment">// LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL&amp; bHandled);</span><strong>   LRESULT OnSetFocus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled);</strong></pre>
<p>Before we implement the OnSetFocus method, we need to define a method for our deskband to tells of it&#8217;s address and to retain that address for later use. Add the following lines of code to your EditQuote.h file below the TranslateAcceleratorIO definition. </p>
<pre lang="c++" nd="80">   <span class="cpp-keyword">void</span> SetBand(CStockBar* pBand);<span class="cpp-keyword">private</span>:   CStockBar* m_pBand;</pre>
<p>Now we can move to your EditQuote.cpp source file and implement the message handler, the SetBand method, and update the TranslateAcceleratorIO method for focus change. At the top of the EditQuote.cpp file add the following includes to the include list as shown below in bold. </p>
<pre lang="c++"><span class="cpp-preprocessor" nd="81">#include &quot;stdafx.h&quot;</span><span class="cpp-preprocessor" nd="82">#include &quot;EditQuote.h&quot;</span><strong>#include &quot;MotleyFool.h&quot;<span class="cpp-preprocessor">#include &quot;StockBar.h&quot;</span></strong></pre>
<p>Now when we can use the methods of the CStockBar class in our code. Add to the end of the constructor, the initalization of m_pBand. Don&#8217;t forget the colon operator. </p>
<pre lang="c++" nd="83">CEditQuote::CEditQuote()<strong>: m_pBand(NULL)</strong>{}</pre>
<p>Next we will add the SetBand implementation to our CEditQuote class. Notice that since it is not a com object we don&#8217;t call AddRef or Release on it. It&#8217;s just a pointer to the class and when it&#8217;s destroyed our CEditQuote instance will also be destroyed. We could have also done this inline in our header file. </p>
<pre lang="c++" nd="84"><span class="cpp-keyword">void</span> CEditQuote::SetBand(CStockBar* pBand){   m_pBand = pBand;}</pre>
<p>Next we need to add our message handler for our EN_SETFOCUS message. Add the code below to the end of the EditQuote.cpp source file. </p>
<pre lang="c++" nd="85">LRESULT CEditQuote::OnSetFocus(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled){   <span class="cpp-comment">//Notify host that our band has the focus so TranslateAcceleratorIO </span>   <span class="cpp-comment">//messages are directed towards our band.</span>   <span class="cpp-keyword">if</span> (m_pBand) m_pBand-&gt;FocusChange(TRUE);   <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}</pre>
<p>We have one more section of code to add to our CEditQuote implementation then we can move to our CStockBar class to define and implement the FocusChange method. Add the following code to the CEditQuote TranslateAcceleratorIO method as shown in bold. We add this code so the host knows that we are no longer needing messages. </p>
<pre lang="c++" nd="86">STDMETHODIMP CEditQuote::TranslateAcceleratorIO(LPMSG lpMsg){   <span class="cpp-keyword">int</span> nVirtKey = (<span class="cpp-keyword">int</span>)(lpMsg-&gt;wParam);   <span class="cpp-keyword">if</span> (VK_RETURN == nVirtKey)   {      <span class="cpp-comment">// remove system beep on enter key by setting key code to 0</span>      lpMsg-&gt;wParam = <span class="cpp-literal">0</span>;      ::PostMessage(GetParent(), WM_GETQUOTE, <span class="cpp-literal">0</span>, <span class="cpp-literal">0</span>);      <span class="cpp-keyword">return</span> S_OK;   }<strong>   <span class="cpp-keyword">else</span> <span class="cpp-keyword">if</span> (WM_KEYDOWN == lpMsg-&gt;message &amp;&amp; nVirtKey == VK_TAB)   {      <span class="cpp-comment">// we no longer need messages forwarded to our band</span>      <span class="cpp-keyword">if</span> (m_pBand) m_pBand-&gt;FocusChange(FALSE);      <span class="cpp-keyword">return</span> S_FALSE;   }</strong>   TranslateMessage(lpMsg);   DispatchMessage(lpMsg);   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>Open the StockBar.h header file and add the definition of FocusChange to it as shown below in bold. </p>
<pre lang="c++" nd="87"><span class="cpp-comment">// IStockBar</span><span class="cpp-keyword">public</span>:<strong>   <span class="cpp-keyword">void</span> FocusChange(BOOL bHaveFocus);</strong></pre>
<p>Now open the StockBar.cpp source file and add the implementation of FocusChange to it at the bottom. </p>
<pre lang="c++" nd="88"><span class="cpp-keyword">void</span> CStockBar::FocusChange(BOOL bHaveFocus){   <span class="cpp-keyword">if</span> (m_pSite)   {      IUnknown* pUnk = NULL;      <span class="cpp-keyword">if</span> (SUCCEEDED(QueryInterface(IID_IUnknown, (LPVOID*)&amp;pUnk)) &amp;&amp; pUnk != NULL)      {         m_pSite-&gt;OnFocusChangeIS(pUnk, bHaveFocus);         pUnk-&gt;Release();         pUnk = NULL;      }   }}</pre>
<p>We have finished off the work needed for the edit box to work properly in our toolbar. Now we need to build our toolbar up so that it has a button and contains our edit box. Then we will add the nessecities to our reflection window and update our IDeskBand to provide the correct information to our host. We are almost there. If you were to compile the project and run it, it would except that the band would look like the following in figure X. </p>
<h3>The MFToolbar Details </h3>
<p nd="89">For the implementation of the MFToolbar window, we need to be able to have it do the following things. It must be able to process the WM_GETQUOTE message from the EditQuote window, communicate with the <a href="http://www.donevii.com/post/tag/web" class="st_tag internal_tag" rel="tag" title="Posts tagged with web">web</a> browser in which the toolbar is located, create the buttons and place the child <a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">windows</a> on itself, forward messages to the EditQuote child window and size itself appropriately to the users actions. </p>
<p nd="90">So, the first thing we should do since our toolbar is going to contain an instance of CEditQuote is include the header file for the CEditQuote class. We will do this by opening the MFToolbar.h file and inserting the include statement for the CEditQuote class as shown in bold below. </p>
<pre lang="c++"><span class="cpp-preprocessor" nd="91">#include &lt;commctrl.h&gt;</span><strong>#include &quot;EditQuote.h&quot;</strong>		</pre>
<p>Next we need to add a member to our toolbar class for the CEditQuote class. We will do this by adding a private section to the end of our class and defining a member variable as shown below in bold. </p>
<pre lang="c++" nd="92">   CMFToolbar();   <span class="cpp-keyword">virtual</span> ~CMFToolbar();<strong><span class="cpp-keyword">private</span>:   CEditQuote m_EditWnd;</strong></pre>
<p>Now that we have our member defined for our EditQuote window, we need to forward window messages to it so that keyboard inputs are processed appropriately. We do this by updating the toolbar message map to chain messages to our member as shown below in bold. </p>
<pre lang="c++" nd="93">   BEGIN_MSG_MAP(CMFToolbar)<strong>      CHAIN_MSG_MAP_MEMBER(m_EditWnd)</strong>   END_MSG_MAP()</pre>
<p>Looking forward, our deskband will need to get the EditQuote member to deterimine if it has focus and also to make it function. We could just expose the EditQuote member directly by having made it a public member instead of private, but by making it private we can expose a method that will expose our member giving us flexibility later to modify the class if the need should arise. So to expose the EditQuote member, we will add a fuction to our toolbar class to return the reference to the EditQuote member. In the toolbar header file, add the method definition and implementation below in bold to it. </p>
<pre lang="c++" nd="94">   CMFToolbar();   <span class="cpp-keyword">virtual</span> ~CMFToolbar();<strong>   <span class="cpp-keyword">inline</span> CEditQuote&amp; GetEditBox() {<span class="cpp-keyword">return</span> m_EditWnd;};</strong></pre>
<p>Now we will create our toolbar window. Our toolbar consists of the EditQuote box and a button with an icon and text on it. To house the icon, our toolbar will need an image list handle to send to the toolbar window. So we need to add a few things to our toolbar header file before we go and implement the toolbar&#8217;s creation. The first thing we will add is the member variable for our image list. Add the line in bold below to your toolbar header file. </p>
<pre lang="c++" nd="95"><span class="cpp-keyword">private</span>:   CEditQuote m_EditWnd;<strong>   HIMAGELIST m_hImageList;</strong></pre>
<p>Then we will add a message handler to our toolbar&#8217;s message map and define the message handlers function definition to our header file and the follow lines of code in bold to your header file. </p>
<pre lang="c++" nd="96">   BEGIN_MSG_MAP(CMFToolbar)      CHAIN_MSG_MAP_MEMBER(m_EditWnd)<strong>      MESSAGE_HANDLER(WM_CREATE, OnCreate)</strong>   END_MSG_MAP()<span class="cpp-comment">// Handler prototypes:</span><span class="cpp-comment">//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL&amp; bHandled);</span><strong>   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</strong></pre>
<p>Before we can implement our toolbar&#8217;s creation, we need to create a icon resource that our toolbar button will use next to its text. So go to the resource view and add a new icon to the project resources. You can do this by right clicking on &quot;MotleyFool resources&quot; and selecting &quot;Insert&#8230;&quot; from the context menu. In the Insert Resource dialog box, select Icon from the Resource type list and click the New button. This will insert a blank icon resource into your project. Rename the icon&#8217;s resource ID by right clicking on the icon resource in the resource view and selecting the properties menu item from the context menu. Change the id to IDI_MOTLEY. Then draw or graciously borrow an icon from The Motley Fool to use on the toolbar. I graciously borrowed the icon from their website and adapted it into the icon. </p>
<p>Now we can implement it our toolbars creation. Open the MFToolbar source file and implement the details of the toolbar creation as described below. </p>
<p>First we need to include the project resource file so we can use the icon ID in our code. Add the line in bold below to our toolbar&#8217;s source file as shown. </p>
<pre lang="c++"><span class="cpp-preprocessor" nd="97">#include &quot;stdafx.h&quot;</span><strong>#include &quot;resource.h&quot;</strong><span class="cpp-preprocessor" nd="98">#include &quot;MFToolbar.h&quot;</span></pre>
<p>Next we need to update our constructor implementation. We need to initialize our handle to the image list by setting it to NULL. Don&#8217;t forget the colon. </p>
<pre lang="c++" nd="99">CMFToolbar::CMFToolbar()<strong>: m_hImageList(NULL)</strong>{}</pre>
<p>Next we need to update our destructor, it should destroy the image list and destroy the window if it has not yet been destroyed. </p>
<pre lang="c++" nd="100">CMFToolbar::~CMFToolbar(){<strong>   ImageList_Destroy(m_hImageList);   <span class="cpp-keyword">if</span> (IsWindow()) DestroyWindow();</strong>}</pre>
<p>Before we can implement our toolbar&#8217;s creation, we need to add a resource symbol to our project for the toolbar button&#8217;s ID. We could just use a <code><span class="cpp-preprocessor" nd="101">#define</span></code> statement at the top of the source file, but for cleanliness and since we are already including the resource.h file, we will add it to our resource file. Go to the &quot;View&quot; menu and select &quot;Resource Symbols&quot; menu item. Click the &quot;New&quot; button on the Resource Symbols dialog. Then enter a name of &quot;IDM_GETQUOTE&quot; and click OK. Then close the Resource Symbols dialog. </p>
<p>Now we can create our toolbar, we defined the OnCreate method in our header file and need to now implement it. Add the following function and its implementation to the end of the toolbar source file. </p>
<div class="precollapse" id="premain24" style="WIDTH: 100%"><img id="preimg24" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="24" alt="" /><span id="precollapse24" style="MARGIN-BOTTOM: 0px; CURSOR: hand" nd="102" preid="24"> Collapse</span></div>
<pre lang="c++" id="pre24" style="MARGIN-TOP: 0px" nd="103">LRESULT CMFToolbar::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled){   <span class="cpp-comment">// buttons with images and text</span>   SendMessage(m_hWnd, TB_SETEXTENDEDSTYLE, <span class="cpp-literal">0</span>, (LPARAM)TBSTYLE_EX_MIXEDBUTTONS);   <span class="cpp-comment">// Sets the size of the TBBUTTON structure.</span>   SendMessage(m_hWnd, TB_BUTTONSTRUCTSIZE, <span class="cpp-keyword">sizeof</span>(TBBUTTON), <span class="cpp-literal">0</span>);   <span class="cpp-comment">// Set the maximum number of text rows and bitmap size.</span>   SendMessage(m_hWnd, TB_SETMAXTEXTROWS, <span class="cpp-literal">1</span>, <span class="cpp-literal">0L</span>);   <span class="cpp-comment">// add our button's caption to the toolbar window</span>   TCHAR* pCaption = _T(&quot;Get Quote&quot;);   <span class="cpp-keyword">int</span> iIndex = ::SendMessage(m_hWnd, TB_ADDSTRING, <span class="cpp-literal">0</span>,(LPARAM)pCaption);   <span class="cpp-comment">// load our button's icon and create the image list to house it.</span>   HICON hMotley = LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_MOTLEY));   m_hImageList = ImageList_Create(<span class="cpp-literal">16</span>,<span class="cpp-literal">16</span>, ILC_COLOR16, <span class="cpp-literal">1</span>, <span class="cpp-literal">0</span>);   <span class="cpp-keyword">int</span> iImageIndex = ImageList_AddIcon(m_hImageList, hMotley);   DestroyIcon(hMotley);   <span class="cpp-comment">// Set the toolbar's image</span>   ::SendMessage(m_hWnd, TB_SETIMAGELIST, <span class="cpp-literal">0</span>, (LPARAM)m_hImageList);   <span class="cpp-comment">// add the button for the toolbar to the window</span>   TBBUTTON Button;   ZeroMemory((<span class="cpp-keyword">void</span>*)&amp;Button, <span class="cpp-keyword">sizeof</span>(TBBUTTON));   Button.idCommand = IDM_GETQUOTE;   Button.fsState = TBSTATE_ENABLED;   Button.fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE | BTNS_SHOWTEXT;   Button.dwData = <span class="cpp-literal">0</span>;   Button.iString = iIndex;   Button.iBitmap = <span class="cpp-literal">0</span>;   ::SendMessage(m_hWnd, TB_INSERTBUTTON, <span class="cpp-literal">0</span>, (LPARAM)&amp;Button);   <span class="cpp-comment">// create our EditQuote window and set the font.</span>   RECT rect = {<span class="cpp-literal">0</span>,<span class="cpp-literal">0</span>,<span class="cpp-literal">0</span>,<span class="cpp-literal">0</span>};   m_EditWnd.Create(m_hWnd, rect, NULL, WS_CHILD|WS_VISIBLE, WS_EX_CLIENTEDGE);   m_EditWnd.SetFont(<span class="cpp-keyword">static_cast</span>&lt;HFONT&gt;(GetStockObject(DEFAULT_GUI_FONT)));   <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}</pre>
<p>If you try to compile at this point, you will see that there are unresolved externals for the image list method calls. We need to add a library to the project. To do this select the &quot;Project|Settings&quot; menu item. On the Project Settings dialog, Select All Configurations from the &quot;Settings For&quot; combo box. Then select the &quot;Link&quot; tab and append to the &quot;Object/Library modules&quot; edit box &quot;comctl32.lib&quot;. Then click OK. If you compile the project now, it will compile successfully and the image list unresolved externals will disappear. </p>
<p>We still have a few things we need to do to the Toolbar window. It needs to process Command messages, resopnd to WM_GETQUOTE messages, and resize itself. Let&#8217;s conquer the latter first. </p>
<p>To orgainze the tooblar correctly, we should have the toolbar responsd to WM_SIZE messages. To do this, we will add to our tooblar header file a message handler for the WM_SIZE message and add a function definition for OnSize which WM_SIZE messages will be sent to. Open our toolbar header file and add the lines in bold below to it as shown. </p>
<pre lang="c++" nd="104">   BEGIN_MSG_MAP(CMFToolbar)      CHAIN_MSG_MAP_MEMBER(m_EditWnd)      MESSAGE_HANDLER(WM_CREATE, OnCreate)<strong>      MESSAGE_HANDLER(WM_SIZE, OnSize)</strong>   END_MSG_MAP()<span class="cpp-comment">// Handler prototypes:</span><span class="cpp-comment">//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL&amp; bHandled);</span>   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);<strong>   LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</strong></pre>
<p>Now we need to implement our OnSize function. Open the toolbar source file and add the function implementation below to the end of the file. </p>
<div class="precollapse" id="premain26" style="WIDTH: 100%"><img id="preimg26" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="26" alt="" /><span id="precollapse26" style="MARGIN-BOTTOM: 0px; CURSOR: hand" nd="105" preid="26"> Collapse</span></div>
<pre lang="c++" id="pre26" style="MARGIN-TOP: 0px" nd="106">LRESULT CMFToolbar::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled){   <span class="cpp-comment">// based on the size of the window area minus the size of the toolbar button, </span>   <span class="cpp-comment">// indent the toolbar so that we can place the edit box before the toolbar </span>   <span class="cpp-comment">// button. This will right justify the toolbar button in the toolbar and the </span>   <span class="cpp-comment">// edit box will use the vaction space to the left of the button but after the </span>   <span class="cpp-comment">// toolbar text as it's usable space.</span>   RECT wndRect, btnRect;   GetClientRect(&amp;wndRect);   ::SendMessage(m_hWnd, TB_GETITEMRECT, <span class="cpp-literal">0</span>, (LPARAM)&amp;btnRect);   wndRect.right -= (btnRect.right - btnRect.left);   SendMessage(TB_SETINDENT, wndRect.right - wndRect.left);   <span class="cpp-comment">// put a small spacing gap between the edit box's right edge and the toolbar button's left edge</span>   wndRect.right -= <span class="cpp-literal">3</span>;   m_EditWnd.MoveWindow(&amp;wndRect, FALSE);   <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}</pre>
<p>We still need to respond to user input for the toolbar button and from the edit box when the user presses the enter key. What we want the toolbar to do is tell the web browser host to navigate to the motely fool website and retrieve the stock quotes requested. First we need for the Deskband object to tell our toolbar window what the web browser instance is so that the toolbar window can communicate with it. To do this, we will add a private member variable and a public method in which the deskband can set the web browser instance. Our window will then use the member variable set to tell the web browser where to navigate and what to retrieve. </p>
<p>To do this, open the toolbar header file and add the lines in bold to the file. </p>
<pre lang="c++" nd="107">   CMFToolbar();   <span class="cpp-keyword">virtual</span> ~CMFToolbar();   <span class="cpp-keyword">inline</span> CEditQuote&amp; GetEditBox() {<span class="cpp-keyword">return</span> m_EditWnd;};<strong>   <span class="cpp-keyword">void</span> SetBrowser(IWebBrowser2* pBrowser);</strong><span class="cpp-keyword">private</span>:   CEditQuote m_EditWnd;   HIMAGELIST m_hImageList;<strong>   IWebBrowser2* m_pBrowser;</strong></pre>
<p>Now, open the toolbar source file. We will update the constructor and initialize our member variable to null. Then we will update the toolbar destructor and release the member variable if it hasn&#8217;t been. Then we will implement the SetBrowser method. </p>
<p>Initialize the web browser member variable. </p>
<pre lang="c++" nd="108">CMFToolbar::CMFToolbar(): m_hImageList(NULL)<strong>, m_pBrowser(NULL)</strong>{}</pre>
<p>Release the web browser object if held. </p>
<pre lang="c++" nd="109">CMFToolbar::~CMFToolbar(){   ImageList_Destroy(m_hImageList);   SetBrowser(NULL);   <span class="cpp-keyword">if</span> (IsWindow()) DestroyWindow();}</pre>
<p>Implement SetBrowser() </p>
<pre lang="c++" nd="110"><span class="cpp-keyword">void</span> CMFToolbar::SetBrowser(IWebBrowser2* pBrowser){   <span class="cpp-keyword">if</span> (m_pBrowser) m_pBrowser-&gt;Release();   m_pBrowser = pBrowser;   <span class="cpp-keyword">if</span> (m_pBrowser) m_pBrowser-&gt;AddRef();}</pre>
<p>If you try and compile the project, you will notice that IWebBrowser2 is undefine in our header file. This is because we need to update our stdafx.h header file to include system files that define IWebBrowser2. To do this, open stdafx.h and add the following lines in bold to the file, then recompile. </p>
<pre lang="c++" nd="111"><span class="cpp-keyword">extern</span> CComModule _Module;<span class="cpp-preprocessor" nd="112">#include &lt;atlcom.h&gt;</span><span class="cpp-preprocessor" nd="113">#include &lt;atlwin.h&gt;</span><strong><span class="cpp-comment">//</span><span class="cpp-comment">// These are needed for IDeskBand</span><span class="cpp-comment">//</span><span class="cpp-preprocessor">#include &lt;shlguid.h&gt;</span><span class="cpp-preprocessor">#include &lt;shlobj.h&gt;</span></strong></pre>
<p>Now we can add message handlers for WM_COMMAND and WM_GETQUOTE to our toolbar class to handle the toolbar button being pressed and the enter key being pressed in the edit box by the user. To do this, we will need to add to our toolbar header file message handlers and function definitions for WM_COMMAND and WM_GETQUOTE. We will also need to add a private method which both will call if they need to to preform the same functionality (better than repeating code that does the same thing). So let&#8217;s add the message handlers to the header file. </p>
<div class="precollapse" id="premain32" style="WIDTH: 100%"><img id="preimg32" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="32" alt="" /><span id="precollapse32" style="MARGIN-BOTTOM: 0px; CURSOR: hand" nd="114" preid="32"> Collapse</span></div>
<pre lang="c++" id="pre32" style="MARGIN-TOP: 0px" nd="115">   BEGIN_MSG_MAP(CMFToolbar)      CHAIN_MSG_MAP_MEMBER(m_EditWnd)      MESSAGE_HANDLER(WM_CREATE, OnCreate)      MESSAGE_HANDLER(WM_SIZE, OnSize)<strong>      MESSAGE_HANDLER(WM_COMMAND, OnCommand)      MESSAGE_HANDLER(WM_GETQUOTE, OnGetQuote)</strong>   END_MSG_MAP()<span class="cpp-comment">// Handler prototypes:</span><span class="cpp-comment">//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL&amp; bHandled);</span>   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);   LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);<strong>   LRESULT OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);   LRESULT OnGetQuote(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</strong></pre>
<p>Now we can add the function delcaration for our GetQuote privaet method. </p>
<pre lang="c++" nd="116"><span class="cpp-keyword">private</span>:   CEditQuote m_EditWnd;   HIMAGELIST m_hImageList;   IWebBrowser2* m_pBrowser;<strong>   <span class="cpp-keyword">void</span> GetQuote();</strong></pre>
<p>Now let&#8217;s switch to our source file and implement our message handler functions and the GetQuote method. Add the code below to the end of the toolbar source file. </p>
<div class="precollapse" id="premain34" style="WIDTH: 100%"><img id="preimg34" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="34" alt="" /><span id="precollapse34" style="MARGIN-BOTTOM: 0px; CURSOR: hand" nd="117" preid="34"> Collapse</span></div>
<pre lang="c++" id="pre34" style="MARGIN-TOP: 0px" nd="118">LRESULT CMFToolbar::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled){   <span class="cpp-keyword">if</span> (!HIWORD(wParam))   {      <span class="cpp-keyword">long</span> lSite = LOWORD(wParam);      <span class="cpp-keyword">if</span> ( lSite == IDM_GETQUOTE)      {         GetQuote();         <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;      }   }   <span class="cpp-keyword">return</span> -<span class="cpp-literal">1</span>;}LRESULT CMFToolbar::OnGetQuote(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled){   GetQuote();   <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}<span class="cpp-keyword">void</span> CMFToolbar::GetQuote(){   <span class="cpp-comment">// if we have a web browser pointer then try to navigate to The Motley Fool site to retrieve stock quotes.</span>   <span class="cpp-keyword">if</span> (m_pBrowser)   {      VARIANT vEmpty;      VariantInit(&amp;vEmpty);      m_pBrowser-&gt;Stop();      _bstr_t bsSite;      <span class="cpp-comment">// if the user has entered stock quotes then append them to the url</span>      <span class="cpp-keyword">if</span> (m_EditWnd.GetWindowTextLength())      {         BSTR bstrTickers = NULL;         m_EditWnd.GetWindowText(&amp;bstrTickers);         bsSite = &quot;http:<span class="cpp-comment">//quote.fool.com/news/symbolnews.asp?Symbols=&quot;;</span>         bsSite += bstrTickers;         SysFreeString(bstrTickers);      }      <span class="cpp-comment">// if the user has not entered any stock quotes then just take them to The Motley Fool website.</span>      <span class="cpp-keyword">else</span>         bsSite = &quot;http:<span class="cpp-comment">//www.fool.com&quot;;</span>      <span class="cpp-comment">// have the webrowser navigate to the site URL requested depending on user input.</span>      m_pBrowser-&gt;Navigate(bsSite, &amp;vEmpty, &amp;vEmpty, &amp;vEmpty, &amp;vEmpty);   }}</pre>
<p>If you try to compile, you will notice that _bstr_t is undefined. That is because the class is defined in comdef.h. We need to add this to our stdafx.h header file so that we can use it as well as any other class in our project (which we will need to for IInputObject). Open the stdafx.h header file and add the lines in bold to the file as indicated. </p>
<pre lang="c++"><span class="cpp-preprocessor" nd="119">#include &lt;shlobj.h&gt;</span><strong><span class="cpp-comment">// needed for IInputObject and _bstr_t</span><span class="cpp-preprocessor">#include &lt;comdef.h&gt;</span></strong></pre>
<p>Our implementation of the toolbar window is complete. Now we can move on to the Reflection window which creates our toolbars and forwards command messages to it. </p>
<h3>The Reflection Window Details </h3>
<p>For the Reflection Window, it&#8217;s only purpose is to create the toolbar window (which it doesn&#8217;t need to really do, but by doing so eases message forwarding) and forward messages to it. The reflection window is not visible, it&#8217;s just a layer added so that message from the toolbar get to the toolbar. If we didn&#8217;t have this window present, toolbar messages would get sent to the parent window (which we do not control) and we would never get them. This is not good since we need to respond to WM_COMMAND messages from the toolbar. Thus the need for the reflection window. So let&#8217;s create the toolbar window and the message forwarding for it. </p>
<p>Open the ReflectionWnd.h header file. We will need to include the toolbar header file and add a private member variable to our reflection window to create it and to pass it to the message chain. First things first, add the include statement below so we can use the CMFToolbar class. </p>
<pre lang="c++"><span class="cpp-preprocessor">#include &lt;commctrl.h&gt;</span><strong>#include &quot;MFToolbar.h&quot;</strong></pre>
<p>Next add a private member variable to the end of the reflection window class for the toolbar as shown below. </p>
<pre lang="c++">	CReflectionWnd();   <span class="cpp-keyword">virtual</span> ~CReflectionWnd();<strong><span class="cpp-keyword">private</span>:   CMFToolbar m_ToolbarWnd;</strong></pre>
<p>Next update the reflection window message map to forward messages to the toolbar window as shown below. </p>
<pre lang="c++">   BEGIN_MSG_MAP(CReflectionWnd)<strong>      CHAIN_MSG_MAP_MEMBER(m_ToolbarWnd)</strong>   END_MSG_MAP()</pre>
<p>We will also need a public function for our deskband class to get at our toolbar window. We will do the same as we did with the EditQuote window by providing a function to get at the member variable indirectly. Add the line of code in bold below to the header file as indicated. </p>
<pre lang="c++">   CReflectionWnd();   <span class="cpp-keyword">virtual</span> ~CReflectionWnd();<strong>   <span class="cpp-keyword">inline</span> CMFToolbar&amp; GetToolBar() { <span class="cpp-keyword">return</span> m_ToolbarWnd;};</strong></pre>
<p>Lastly, we need to create the toolbar window and will do so in the WM_CREATE message handler for our reflection window. Add the code below in bold to the reflection window header file. Then we will implement the OnCreate method in the source file. </p>
<pre lang="c++">   BEGIN_MSG_MAP(CReflectionWnd)<strong>      MESSAGE_HANDLER(WM_CREATE, OnCreate)</strong>      CHAIN_MSG_MAP_MEMBER(m_ToolbarWnd)   END_MSG_MAP()<span class="cpp-comment">// Handler prototypes:</span><span class="cpp-comment">//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL&amp; bHandled);</span><span class="cpp-comment">//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL&amp; bHandled);</span><strong>   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled);</strong></pre>
<p>Now open the ReflectionWnd.cpp source file and add the implementation of OnCreate to its end. </p>
<pre lang="c++"><span class="cpp-keyword">const</span> DWORD DEFAULT_TOOLBAR_STYLE =       <span class="cpp-comment">/*Window styles:*/</span> WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | WS_TABSTOP |      <span class="cpp-comment">/*Toolbar styles:*/</span> TBSTYLE_TOOLTIPS | TBSTYLE_FLAT | TBSTYLE_TRANSPARENT | TBSTYLE_LIST | TBSTYLE_CUSTOMERASE |                          TBSTYLE_WRAPABLE |      <span class="cpp-comment">/*Common Control styles:*/</span> CCS_TOP | CCS_NODIVIDER | CCS_NOPARENTALIGN | CCS_NORESIZE;LRESULT CReflectionWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&amp; bHandled){   RECT rect;   GetClientRect(&amp;rect);   m_ToolbarWnd.Create(m_hWnd, rect, NULL, DEFAULT_TOOLBAR_STYLE);   <span class="cpp-keyword">return</span> <span class="cpp-literal">0</span>;}</pre>
<p>You will notice that we defined a constant for the toolbar style. This was done to make the code more readable. </p>
<p>The only thing left to do to the reflection window code is update the destructor to destory the window if it&#8217;s still present. </p>
<pre lang="c++">CReflectionWnd::~CReflectionWnd(){<strong>   <span class="cpp-keyword">if</span> (IsWindow()) DestroyWindow();</strong>}</pre>
<p>&nbsp;</p>
<h2>Finishing Off The Deskband Toolbar </h2>
<p>All that&#8217;s left is for our deskband to create the toolbar window, have the host use the toolbar window, remove the unused IPersistStream implementation, implement IInputObject (for focus control) and do some code tweaking. So lets wrap this toolbar up. Open the StockBar.h header file. Remove the following lines of code that are in bold since we moved them to our stdafx.h for our other classes to also use. </p>
<pre lang="c++"><span class="cpp-preprocessor">#include &quot;resource.h&quot;       // main symbols</span><strong><span class="cpp-comment">//</span><span class="cpp-comment">// These are needed for IDeskBand</span><span class="cpp-comment">//</span><span class="cpp-preprocessor">#include &lt;shlguid.h&gt;</span><span class="cpp-preprocessor">#include &lt;shlobj.h&gt;</span></strong></pre>
<p>Next remove the following line from the class delcaration. </p>
<pre lang="c++"><span class="cpp-keyword">public</span> IPersistStream,</pre>
<p>The top of the class declaration should now look like this, </p>
<pre lang="c++"><span class="cpp-keyword">class</span> ATL_NO_VTABLE CStockBar :    <span class="cpp-keyword">public</span> CComObjectRootEx&lt;CComSingleThreadModel&gt;,   <span class="cpp-keyword">public</span> CComCoClass&lt;CStockBar, &amp;CLSID_StockBar&gt;,   <span class="cpp-keyword">public</span> IDeskBand,   <span class="cpp-keyword">public</span> IObjectWithSite,   <span class="cpp-keyword">public</span> IDispatchImpl&lt;IStockBar, &amp;IID_IStockBar, &amp;LIBID_MOTLEYFOOLLib&gt;{</pre>
<p>Next scroll down to the COM Map and remove the following two lines of code, </p>
<pre lang="c++">   COM_INTERFACE_ENTRY(IPersist)   COM_INTERFACE_ENTRY(IPersistStream)</pre>
<p>Your COM Map should now look like this, </p>
<pre lang="c++">BEGIN_COM_MAP(CStockBar)   COM_INTERFACE_ENTRY(IStockBar)   COM_INTERFACE_ENTRY(IOleWindow)   COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow)   COM_INTERFACE_ENTRY(IObjectWithSite)   COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand)   COM_INTERFACE_ENTRY(IDispatch)END_COM_MAP()</pre>
<p>Scroll down the header file further and remove the sections of function declarations for IPersist and IPersistStream, which includes the following lines. </p>
<pre lang="c++"><span class="cpp-comment">// IPersist</span><span class="cpp-keyword">public</span>:   STDMETHOD(GetClassID)(CLSID *pClassID);<span class="cpp-comment">// IPersistStream</span><span class="cpp-keyword">public</span>:   STDMETHOD(IsDirty)(<span class="cpp-keyword">void</span>);   STDMETHOD(Load)(IStream *pStm);   STDMETHOD(Save)(IStream *pStm, BOOL fClearDirty);   STDMETHOD(GetSizeMax)(ULARGE_INTEGER *pcbSize);</pre>
<p>There should now be nothing related to IPersist or IPersistStream between the IDockingWindow and IStockBar function declaration sections. </p>
<p>Now we need to remove the IPersist and IPersistStream function implementations from the StockBar.cpp source file. Find the GetClassID, IsDirty, Load, Save, and GetSizeMax function implementations and remove them from the file. They should be directly above the FocusChange Method we added earlier. </p>
<p>Now we can start adding code to our deskband. Open the stockbar.h header file and add the include for the reflection window class as shown below in bold. </p>
<pre lang="c++"><span class="cpp-preprocessor">#include &quot;resource.h&quot;       // main symbols</span><strong>#include &quot;ReflectionWnd.h&quot;</strong></pre>
<p>Next, find the protected section at the end of the file and replace the line </p>
<pre lang="c++">   HWND m_hWnd;</pre>
<p>with </p>
<pre lang="c++">   CReflectionWnd m_ReflectWnd;</pre>
<p>If you try to compile, you will find that it will be unsuccessful since we have removed m_hWnd and have not removed all occurances of m_hWnd from the class source file. We will replace all occurances with more appropriate code for our Reflection window and toolbar window. Open the StockBar.cpp source file, Remove the following line from the class constructor </p>
<pre lang="c++">   m_hWnd(NULL),</pre>
<p>your class constructor should now look as follows with a SetBand call added, </p>
<pre lang="c++">CStockBar::CStockBar():    m_dwBandID(<span class="cpp-literal">0</span>),    m_dwViewMode(<span class="cpp-literal">0</span>),    m_bShow(FALSE),    m_bEnterHelpMode(FALSE),    m_hWndParent(NULL),    m_pSite(NULL){	m_ReflectWnd.GetToolBar().GetEditBox().SetBand(<span class="cpp-keyword">this</span>);}</pre>
<p>Next, update the RegisterAndCreateWindow function by replacing the temporary m_hWnd construction with the Reflection Window construction. Your RegisterAndCreateWindow implementation should look as follows: </p>
<pre lang="c++">BOOL CStockBar::RegisterAndCreateWindow(){   RECT rect;   ::GetClientRect(m_hWndParent, &amp;rect);   m_ReflectWnd.Create(m_hWndParent, rect, NULL, WS_CHILD);   <span class="cpp-comment">// The toolbar is the window that the host will be using so it is the window that is important.</span>   <span class="cpp-keyword">return</span> m_ReflectWnd.GetToolBar().IsWindow();}</pre>
<p>As we scroll through the code, we should fix some other things. A toolbar has a default height of 22 so we need to update our GetBandInfo method so that all the y measurements are 22 not 20. We also want our Integral sizing to be non sizeable so we need to set the DBIM_INTEGRAL x and y values to 0. While we are at it, we should fix the title of our toolbar so it says &quot;The Motley Fool&quot;. You&#8217;ll find this constant defined near the top of the source file, update it now. </p>
<p>We will now update the GetWindow call so that the toolbar window handle is returned and not the invalid m_hWnd variable. Update your GetWindow method so it looks as follows, </p>
<pre lang="c++">STDMETHODIMP CStockBar::GetWindow(HWND* phwnd){   HRESULT hr = S_OK;   <span class="cpp-keyword">if</span> (NULL == phwnd)   {      hr = E_INVALIDARG;   }   <span class="cpp-keyword">else</span>   {<strong>      *phwnd = m_ReflectWnd.GetToolBar().m_hWnd;</strong>   }   <span class="cpp-keyword">return</span> hr;}</pre>
<p>Now we need to update teh CloseDW method so that it does not destory our window buy hide it. We do this much like the MFC CToolbar class does, the class destructor will destroy the window. Your CloseDW method should look as follows, </p>
<pre lang="c++">STDMETHODIMP CStockBar::CloseDW(<span class="cpp-keyword">unsigned</span> <span class="cpp-keyword">long</span> dwReserved){   ShowDW(FALSE);   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>Working our way through our class implementation, we will update the next method that needs updating. Update the ShowDW class to show or hide the toolbar window which the host is using. Your ShowDW class should look as follows, </p>
<pre lang="c++">STDMETHODIMP CStockBar::ShowDW(BOOL fShow){   m_bShow = fShow;   m_ReflectWnd.GetToolBar().ShowWindow(m_bShow ? SW_SHOW : SW_HIDE);   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>We are almost done modifying the CStockBar class we need to do one last bit of updating. We need to modify the SetSite implementation to release the browser window that the toolbar may be using if we have a IInputObjectSite object and we need to query the OleCommandTarget&#8217;s ServiceProvider for the IWebBrowser2 interface which we will then set to our toolbar. The modified parts of the SetSite method implementation are below in bold. </p>
<div class="precollapse" id="premain58" style="WIDTH: 100%"><img id="preimg58" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="58" alt="" /><span id="precollapse58" style="MARGIN-BOTTOM: 0px; CURSOR: hand" preid="58"> Collapse</span></div>
<pre lang="c++" id="pre58" style="MARGIN-TOP: 0px">STDMETHODIMP CStockBar::SetSite(IUnknown* pUnkSite){<span class="cpp-comment">//If a site is being held, release it.</span>   <span class="cpp-keyword">if</span>(m_pSite)   {<strong>      m_ReflectWnd.GetToolBar().SetBrowser(NULL);</strong>      m_pSite-&gt;Release();      m_pSite = NULL;   }   <span class="cpp-comment">//If punkSite is not NULL, a new site is being set.</span>   <span class="cpp-keyword">if</span>(pUnkSite)   {      <span class="cpp-comment">//Get the parent window.</span>      IOleWindow  *pOleWindow = NULL;      m_hWndParent = NULL;      <span class="cpp-keyword">if</span>(SUCCEEDED(pUnkSite-&gt;QueryInterface(IID_IOleWindow, (LPVOID*)&amp;pOleWindow)))      {         pOleWindow-&gt;GetWindow(&amp;m_hWndParent);         pOleWindow-&gt;Release();      }      <span class="cpp-keyword">if</span>(!::IsWindow(m_hWndParent))         <span class="cpp-keyword">return</span> E_FAIL;      <span class="cpp-keyword">if</span>(!RegisterAndCreateWindow())         <span class="cpp-keyword">return</span> E_FAIL;      <span class="cpp-comment">//Get and keep the IInputObjectSite pointer.</span><strong>      <span class="cpp-keyword">if</span>(FAILED(pUnkSite-&gt;QueryInterface(IID_IInputObjectSite, (LPVOID*)&amp;m_pSite)))      {         <span class="cpp-keyword">return</span> E_FAIL;      }        IWebBrowser2* s_pFrameWB = NULL;      IOleCommandTarget* pCmdTarget = NULL;      HRESULT hr = pUnkSite-&gt;QueryInterface(IID_IOleCommandTarget, (LPVOID*)&amp;pCmdTarget);      <span class="cpp-keyword">if</span> (SUCCEEDED(hr))      {         IServiceProvider* pSP;         hr = pCmdTarget-&gt;QueryInterface(IID_IServiceProvider, (LPVOID*)&amp;pSP);         pCmdTarget-&gt;Release();         <span class="cpp-keyword">if</span> (SUCCEEDED(hr))         {            hr = pSP-&gt;QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID*)&amp;s_pFrameWB);            pSP-&gt;Release();            _ASSERT(s_pFrameWB);            m_ReflectWnd.GetToolBar().SetBrowser(s_pFrameWB);            s_pFrameWB-&gt;Release();         }      }</strong>   }   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>If you try to compile and use the toolbar right now, it will function partially. Tabbing and input control will not work correctly since we have yet to implement IInputObject for our deskband. Let&#8217;s do that now since it&#8217;s the last bit of code we will write for our simple deskband. You may also notice that the Toolbars context menu and View|Toolbars menus still say CStockBar Class. we will fix this problem in the Finishing Touches section below. </p>
<h3>IInputObject Implementation </h3>
<p>To get tabbing and input control to work correctly for any deskband is quite simple. You need but to implement IInputObject. The host will query our deskband to see if this interface is implemented and if it is will call the methods to see if we require input focus and let us also process messages from the user through the host. To do this, open the stockbar.h header file. To the stockbar class declaration add the line below in bold, </p>
<pre lang="c++"><span class="cpp-keyword">class</span> ATL_NO_VTABLE CStockBar :    <span class="cpp-keyword">public</span> CComObjectRootEx&lt;CComSingleThreadModel&gt;,   <span class="cpp-keyword">public</span> CComCoClass&lt;CStockBar, &amp;CLSID_StockBar&gt;,   <span class="cpp-keyword">public</span> IDeskBand,   <span class="cpp-keyword">public</span> IObjectWithSite,<strong>   <span class="cpp-keyword">public</span> IInputObject,</strong>    <span class="cpp-keyword">public</span> IDispatchImpl&lt;IStockBar, &amp;IID_IStockBar, &amp;LIBID_MOTLEYFOOLLib&gt;{</pre>
<p>Next scroll down to the COM Map and add an entry for IInputObject as shown below in bold, </p>
<pre lang="c++">BEGIN_COM_MAP(CStockBar)   COM_INTERFACE_ENTRY(IStockBar)<strong>   COM_INTERFACE_ENTRY(IInputObject)</strong>   COM_INTERFACE_ENTRY(IOleWindow)   COM_INTERFACE_ENTRY_IID(IID_IDockingWindow, IDockingWindow)   COM_INTERFACE_ENTRY(IObjectWithSite)   COM_INTERFACE_ENTRY_IID(IID_IDeskBand, IDeskBand)   COM_INTERFACE_ENTRY(IDispatch)END_COM_MAP()</pre>
<p>Next add the following section of function declarations to your header file, I placed mine before the IStockBar section. </p>
<pre lang="c++"><span class="cpp-comment">// IInputObject</span><span class="cpp-keyword">public</span>:   STDMETHOD(HasFocusIO)(<span class="cpp-keyword">void</span>);   STDMETHOD(TranslateAcceleratorIO)(LPMSG lpMsg);   STDMETHOD(UIActivateIO)(BOOL fActivate, LPMSG lpMsg);</pre>
<p>All that remains is to implement these three functions. Add the function implementations below to the end of the stockbar.cpp source file. </p>
<div class="precollapse" id="premain62" style="WIDTH: 100%"><img id="preimg62" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="62" alt="" /><span id="precollapse62" style="MARGIN-BOTTOM: 0px; CURSOR: hand" preid="62"> Collapse</span></div>
<pre lang="c++" id="pre62" style="MARGIN-TOP: 0px">STDMETHODIMP CStockBar::HasFocusIO(<span class="cpp-keyword">void</span>){   <span class="cpp-comment">// if any of the windows in our toolbar have focus then return S_OK else S_FALSE.</span>   <span class="cpp-keyword">if</span> (m_ReflectWnd.GetToolBar().m_hWnd == ::GetFocus())      <span class="cpp-keyword">return</span> S_OK;   <span class="cpp-keyword">if</span> (m_ReflectWnd.GetToolBar().GetEditBox().m_hWnd == ::GetFocus())     <span class="cpp-keyword">return</span> S_OK;   <span class="cpp-keyword">return</span> S_FALSE;}STDMETHODIMP CStockBar::TranslateAcceleratorIO(LPMSG lpMsg){   <span class="cpp-comment">// the only window that needs to translate messages is our edit box so forward them.</span>   <span class="cpp-keyword">return</span> m_ReflectWnd.GetToolBar().GetEditBox().TranslateAcceleratorIO(lpMsg);}STDMETHODIMP CStockBar::UIActivateIO(BOOL fActivate, LPMSG lpMsg){   <span class="cpp-comment">// if our deskband is being activated then set focus to the edit box.</span>   <span class="cpp-keyword">if</span>(fActivate)   {      m_ReflectWnd.GetToolBar().GetEditBox().SetFocus();   }   <span class="cpp-keyword">return</span> S_OK;}</pre>
<p>Our toolbar is functionaly done, compile, run it and see. It works as described and is fairly simple. Let&#8217;s put some finishing UI touches on it for IE and our users to use. </p>
<h2>Finishing Touches </h2>
<p>The are only 2 finishing touches to make, One is to fix the context menu text. The other is to add button support to the main IE toolbar. Let&#8217;s do them in order. </p>
<p>To fix the context menu text problem, open the StockBar.rgs project file and change all occurances of &quot;StockBar Class&quot; to &quot;The Motley Fool Quotes&quot;. Compile it, run it, and see. While you only need to change one of them, it&#8217;s nicer if they all match. </p>
<p>Now let&#8217;s add button support for our toolbar. Update the stockbar.rgs file contents by appending the text below to it&#8217;s contents. </p>
<div class="precollapse" id="premain63" style="WIDTH: 100%"><img id="preimg63" style="CURSOR: hand" height="9" src="http://www.codeproject.com/images/minus.gif" width="9" preid="63" alt="" /><span id="precollapse63" style="MARGIN-BOTTOM: 0px; CURSOR: hand" preid="63"> Collapse</span></div>
<pre id="pre63" style="MARGIN-TOP: 0px">HKLM{   Software   {      Microsoft      {         <span class="cpp-string">'Internet Explorer'</span>         {            Extensions            {               ForceRemove	{A26ABCF0-1C8F-<span class="cpp-literal">46e7</span>-A67C-0489DC21B9CC} = s <span class="cpp-string">'The Motley Fool Quotes'</span>               {                  val BandClsid = s <span class="cpp-string">'{A6790AA5-C6C7-4BCF-A46D-0FDAC4EA90EB}'</span>                  val ButtonText = s <span class="cpp-string">'The Motley Fool'</span>                  val Clsid = s <span class="cpp-string">'{E0DD6CAB-2D10-11D2-8F1A-0000F87ABD16}'</span>                  val <span class="cpp-string">'Default Visible'</span> = s <span class="cpp-string">'Yes'</span>                  val <span class="cpp-string">'Hot Icon'</span> = s <span class="cpp-string">'%MODULE%,425'</span>                  val Icon = s <span class="cpp-string">'%MODULE%,425'</span>                  val MenuStatusBar = s <span class="cpp-string">'The Motley Fool Stock Quote Toolbar'</span>                  val MenuText = s <span class="cpp-string">'The Motley Fool'</span>               }            }         }      }   }}</pre>
<p>The replace the 425 with the id from resource.h of IDI_MOTLEY. Also replace the BandClsid value with the GUID of our toolbar, the above values represent the source code for the article. Now compile the toolbar again. Then start IE and right click on the Standard Buttons toolbar, Select &quot;Customize&quot; from the context menu. Scroll down the Available toolbar buttons listbox and find &quot;The Motley Fool&quot; item, See Figure 12. Select it and click the Add button in the middle of the dialog. The item will move to the right as in Figure 13. Click the Close Button. You&#8217;ll see the button added as shown in the before figure 14 and after figure 15. </p>
<p><center><img height="282" alt="Figure 12. Customize Toolbar - Available Toolbar Buttons." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure12.jpg" width="542" name="Figure12" /> <br />Figure 12. CustomizeToolbar &#8211; Available Toolbar Buttons. </center><center><img height="282" alt="Figure 13. Customize Toolbar - Current Toolbar Buttons." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure13.jpg" width="542" name="Figure13" /> <br />Figure 13. Customize Toolbar &#8211; Current Toolbar Buttons. </center><center><img height="190" alt="Figure 14. IE Standard Buttons - Before." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure14.jpg" width="607" name="Figure14" /> <br />Figure 14. IE Standard Buttons &#8211; Before. </center><center><img height="190" alt="Figure 15. IE Standard Buttons - After." src="http://www.codeproject.com/atl/IEToolbarTutorial/Figure15.jpg" width="607" name="Figure15" /> <br />Figure 15. IE Standard Buttons &#8211; After. </center><br />
<h2>Conclusion </h2>
<p>While this tutorial is long hopefully the explaination was clear. From writing this tutorial it is easy to see that the RBDeskband ATL Object Wizard has some room for improvement but provided enough of a base for us to develop our simple example. In the end you can see that the toolbar we created is much like the Address bar. The differences lie in how MS implemented theirs versus how I implemented mine. As always feedback is welcome. Enjoy. </p>
<p><!-- Article Ends --><script src="/script/togglePre.js" type="text/javascript"></script><br />
<h2>About Erik Thompson</h2>
<div style="OVERFLOW: hidden">
<table border="0">
<tbody>
<tr valign="top">
<td class="smallText" nowrap="nowrap"><img src="http://www.codeproject.com/script/profile/images/{889A4A05-021A-4EBB-BBA5-CC7AFD49E71D}.jpg" alt="" /></p>
<p>            <img src="http://www.codeproject.com/script/images/sitebuild_icon.gif" alt="" /> Site Builder</td>
<td class="smallText">Erik lives in Redmond, Washington. He works as a Senior Software Engineer specializing in C++, COM, ATL and the middle-tier and now .NET. When he isn&#8217;t coding for work, he can be found trying to extend Internet Explorer with yet another Desk band or simplifying his development process with ATL Object Wizards.</p>
<p>            He spends his free time snowboarding, mountain biking, reading, dining out at those hidden finds.
<p class="smallText">Click <a href="http://www.codeproject.com/script/profile/whos_who.asp?vt=arts&amp;id=78">here</a> to view Erik Thompson&#8217;s online profile.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>
<table cellpadding="4" width="100%" border="0">
<tbody>
<tr valign="top">
<td width="100%">
<h2>Other popular ATL articles:</h2>
<ul>
<li><a href="http://www.codeproject.com/atl/com_atl.asp">Beginner&#8217;s Tutorial: COM/ATL Simple Project</a>
<div class="smallText">The purpose of this tutorial is to give you an idea on how to create a COM <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">Server</a> using ATL, and then being able to call the <a href="http://www.donevii.com/post/tag/server" class="st_tag internal_tag" rel="tag" title="Posts tagged with server">server</a> from both a Visual C++ and Visual Basic program.</div>
</li>
<li><a href="http://www.codeproject.com/atl/atl_underthehood_.asp">ATL Under the Hood &#8211; Part 1</a>
<div class="smallText">In this series of tutorials I am going to discuss some of the inner workings of ATL and the techniques that ATL uses.</div>
</li>
<li><a href="http://www.codeproject.com/atl/MouseGestures.asp">Mouse Gestures for Internet Explorer</a>
<div class="smallText">Adding mouse gesture recognition to Internet Explorer.</div>
</li>
<li><a href="http://www.codeproject.com/atl/udtdemo.asp">Using User Defined Types in COM &amp; ATL</a>
<div class="smallText">A Step by Step tutorial on SAFEARRAYs and UDTs in COM</div>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/316.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>(转)lucene安装</title>
		<link>http://www.donevii.com/post/301.html</link>
		<comments>http://www.donevii.com/post/301.html#comments</comments>
		<pubDate>Wed, 31 Jan 2007 07:46:02 +0000</pubDate>
		<dc:creator>gavinkwoe</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[web]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[安装]]></category>
		<category><![CDATA[测试]]></category>
		<category><![CDATA[车东]]></category>

		<guid isPermaLink="false">http://www.donevii.com/?p=301</guid>
		<description><![CDATA[网上有许多lucene的材料，中文材料大家看的都是车东的那篇(http://www.chedong.com/tech/lucene.html)，而大家在网上讨论最多的是中文的全文检索，而对中文的全文检索最有影响力的文章，还是车东写的... ]]></description>
			<content:encoded><![CDATA[<p>网上有许多lucene的材料，中文材料大家看的都是车东的那篇(http://www.chedong.com/tech/<a href="http://www.donevii.com/post/tag/lucene" class="st_tag internal_tag" rel="tag" title="Posts tagged with lucene">lucene</a>.<a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">html</a>)，而大家在网上讨论最多的是中文的全文检索，而对中文的全文检索最有影响力的文章，还是车东写的那篇weblucene(http://www.chedong.com/tech/weblucene.<a href="http://www.donevii.com/post/tag/html" class="st_tag internal_tag" rel="tag" title="Posts tagged with html">html</a>)，但那些都是lucene1.2版本的事，现在不同了，<a href="http://www.donevii.com/post/tag/lucene" class="st_tag internal_tag" rel="tag" title="Posts tagged with lucene">lucene</a>1.3-final据称完全支持中文的全文检索了。<br />
因为在lucene1.3-final.zip包中的changes.txt中的第五项描述如下：<br />
5. Fix StandardTokenizer’s handling of CJK characters (Chinese,<br />
 Japanese and Korean ideograms). Previously contiguous sequences<br />
 were combined in a single token, which is not very useful. Now<br />
 each ideogram generates a separate token, which is more useful.<br />
这说明lucene1.3-final可以检索中日韩等表意文字了。<br />
<br />
测试一下：<br />
测试环境：<a href="http://www.donevii.com/post/tag/windows" class="st_tag internal_tag" rel="tag" title="Posts tagged with windows">windows</a> 2000 pro,jdk1.3.1或以上版本<br />
1、下载lucene-1.3-final.zip。<br />
<br />
2、解压lucene-1.3-final.zip，并将其中的lucene-1.3-final.jar和lucene-demos-1.3-final.jar加入到系统的classpath中。<br />
<br />
3、建一个目录，并将一些html或txt文件（文件内容要中文的！）拷入到这个目录中，作为全文检索的材料。如：建一个目录d:\lucenetest\index，在其中拷入一些中文内容的文件，其中也可以有多级子目录的。<br />
OK，环境准备好了，可以试验了！<br />
<br />
4、进入dos模式，输入命令：<a href="http://www.donevii.com/post/tag/java" class="st_tag internal_tag" rel="tag" title="Posts tagged with java">java</a> org.apache.lucene.demo.IndexFiles d:\lucenetest\index<br />
如：c:\&gt;java org.apache.lucene.demo.IndexFiles d:\lucenetest\index 回车，这时会索引d:\lucenetest\index目录下的所有文件，包括子目录中的文件，并将索引文件写入：c:\index目录中（自动创建的,根据你的dos符起始路径，将在其下建index目录）。<br />
好，索引建完了，下面试验检索。<br />
<br />
5、输入命令：java org.apache.lucene.demo.SearchFiles<br />
如：c:\&gt;java org.apache.lucene.demo.SearchFiles 回车<br />
Query:在这里输入检索内容，如：“建议最好自己先做一下语法检查”，这么长：）<br />
成功了，结果出来了：<br />
Searching for: “建 议 最 好 自 己 先 做 一 下 语 法 检 查”<br />
1 total matching documents<br />
0. d:\lucenetest\index\学习Lucene的一点心得.txt<br />
可以看出lucene-1.3-final完全支持中文的全文检索了，使用的是单字切分！！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donevii.com/post/301.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

