第一章
1. 头文件依然是.h
2. .cpp变成了.m
3. #import代替了#include,#import保证只会引用一次,相当于.h里的#ifndef #define #endif模式
4. 用@interface … @end来声明类,取代class ClassName{}
5. 用@implementation … @end来实现类
6. Data Members放在@interface ClassName : Parent{ … }中,默认权限为@protected,在ObjC里称为Instance Variables
7. Member Functions放在@interface ClassName: Parent{} … @end中,在ObjC里称为Instance Methods
8. Instance Methods的声明方式为:scope (returnType) methodName: (parameter1Type) parameter1Name;
9. scope分instance和class两种,分别用-和+表示
10. 调用method的方式是[object method],相当于object->method()
11. 带参数调用method的方式是[object method: parameter]
12. 没有Object,只有Pointer to Object
13. 通常的构造方式: Object * obj = [[Object alloc] init]
14. 析构的方式:[obj release]15.多参数method的声明方式: scope (returnType) methodName: (parameter1Type) parameter1Name label1Name: (parameter2Type) parameter2Name … ;
16. labelName不是必须的
17. 这种特别的语法源自SmallTalk
18. private: [list of vars] protected: [list of vars] public: [list of vars] 改成了@private, @protected, @public
19. Class Variable用static的方法实现⋯⋯
20. +(void) initialize 会在构造的时候被调用
21. ObjC通常用@符号表示语言的衍生部分
22. ObjC用id来表示范型对象的指针
23. 支持动态类型识别
24. Categories机制可用于不继承已有class的前提下加入新功能
25. Posing机制允许Child取代Parent
26. Protocol相当于pure virtual class
27. ObjC由两种内存管理的方法,1) retain and release,2) retain and release/autorelease
28. Foundation相当于STL,NSArray对应vector,NSDictionary对应map
29. ObjC不支持Namespace
30. 不支持重载
1. Hidden iframe
将iframe的src指向一个url,server收到请求后,Keep-Alive。
数据直接以script的方式下发到Browser,Browser收到数据后直接执行。
只要不超时,链接会一直保留。
优:感觉这是真正的长连接,对stream也有完整的支持。
劣:Browser状态栏会一直处于“连接中”,ESC会导致链接断开,会有跨域问题
2. Script Tag
用JS创建一个script对象,将该对象的src指向一个url,Keep-Alive。
在一定时间内(超时前),如果sever有数据下发,则用script的方式发送到Browser。
Browser收到数据后直接执行,此时需要重建script对象,建立另一个链接。
优:没有Hidden iframe的缺点,也比较轻量
劣:不是真正的长链接,每收到一个新的下发数据,都需要重新建立链接
3 AJAX
用JS创建一个XHR对象,将该对象的src指向一个url,Keep-Alive。
在一定时间内(超时前),如果sever有数据下发,则通过已建立的链接发送到Browser。
Browser收到数据后直接执行,此时需要重建XHR对象,建立另一个链接。
优:没有Hidden iframe的缺点
劣:存在跨域问题,不是真正的长链接,每收到一个新的下发数据,都需要重新建立链接
2 和 3也可以叫做Long Polling
除了这三种方法,还可以用Flash,由Flash和Server通信,页面用过JS和Flash通信。
这可以实现真正的下发,甚至不需要维护长链接。
但也可能存在被防火墙屏蔽的问题。
三种方法都用php模拟了一下:
Hidden Iframe
<html> <head> <script> function callback(data) { document.getElementById("t").value = data + "\n" + document.getElementById("t").value; } callback("abc"); </script> </head> <body> hello <textarea id="t"></textarea> <iframe src="/hiddeniframe.php" width="0" height="0"></iframe> </body> </html>
<?php ob_end_flush(); echo "<script>"; echo "domain=serverpush"; echo "</script>"; for( ; ; ) { $t = time(); echo "<script>"; echo "parent.callback($t);"; echo "</script>"; flush(); sleep(1); } ?>
Script Tag
<html> <head> <script> function callback(data) { document.getElementById("t").value = data + "\n" + document.getElementById("t").value; } function connect() { var _script = document.createElement("script"); _script.setAttribute("type", "text/javascript"); _script.setAttribute("src", "script.php"); document.getElementsByTagName("head")[0].appendChild(_script); } connect(); </script> </head> <body> <textarea id="t"></textarea> </body> </html>
<?php sleep(10); $r = time(); echo "callback($r);"; echo "setTimeout(connect, 1000)"; ?>
AJAX
<html> <head> <script> function callback(data) { document.getElementById("t").value = data + "\n" + document.getElementById("t").value; } function connect() { var ajax = new XMLHttpRequest; ajax.open("POST", "ajax.php"); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { if (ajax.status == 200) { callback(ajax.responseText); setTimeout(connect, 1000); } } } ajax.send(); } connect(); </script> </head> <body> <textarea id="t"></textarea> </body> </html>
<?php sleep(10); echo time(); ?>
昨晚看设计模式,顺便复习UML。
这里是一个比较好的图例说明,其中有些错误,于是用visio重画了一下。
美中不足的是,似乎visio不支持带箭头的Association,也可能是我不会用吧。
Google到CSDN上的结果:
在Visio图形元素上,点击右键,选择“形状显示选项”,将“实现链接”选中,这个时候,类图形元素上会出现一个黄点,拖动这个黄点,连接到表示接口的图形元素上。实现类和接口之间的关系就自动出来了。
花了半天时间,看了一次http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
下面是相关的笔记和理解,
感觉很多Status Code在实际的BS通信中都不常见,
更多像是为了定义完备的协议语义,
而这些语义现在已经部分由更上层的协议或者应用实现。
1xx Informational
100 Continue
如果server可能拒绝request,则client只发送header部分,试探server的反应
如果server接受,则返回100,于是client再发送剩余的body部分
101 Switch Protocol
2xx Successful
200 OK
201 Created
有资源因Request而产生
202 Accepted
Request要求的动作已经被记录,但真正的动作要等待若干时间才执行,而且不保证一定执行
203 Non-Authoritative Information
204 No Content
google.cn的web server会返回
205 Reset Content
206 Partial Content
用于断点续传,起始位置由Range指定。(可以参考迅雷的输出)
3xx Redirection
User Agent需要处理循环的问题
300 Multiple Choices
目标已变,有若干个替代品可供选择
301 Moved Permanently
目标URI在Location里定义,除了HEAD和GET请求外,不能自动重定向
对SEO不会有负面作用
302 Found
临时重定向,是否用Moved Temporarily这个名字比较合适?
目标URI在Location里定义,除了HEAD和GET请求外,不能自动重定向
对SEO有负面作用
303 See Other
和302类似,但期待User Agent用GET来请求Location里指定的URI
304 Not Modified
通常Request会带If-Modified-Since
不能带message-body
305 Use Proxy
期待User Agent会使用Location中指定的Proxy地址,来访问数据
只能由origin server产生
306 (not used)
之前的版本用过,已经废弃,作为保留字
307 Temporary Redirect
没看出和302有什么不同
只有HTTP/1.1支持
4xx Client Error
在message-body中,可以自定义给User的提示
400 Bad Request
server不能理解Request的格式
用telnet可以很容易模拟
401 Unauthorized
不能通过HTTP Authentication
response header必须包含WWW-Authoricate,request header可能包含Authorization
402 Payment Required
保留字
403 Forbidden
如果不希望暴露过多的信息,可以用404代替
404 Not Found
最常见的4字头Status Code,通常会自定义
405 Method Not Allowed
response header需要包含Allow,列出支持的method
406 Not Acceptable
response不被request header中的Accept做支持
除了HEAD,response会给出包含更多信息的entity
entity format由request header中的Content-Type决定
407 Proxy Authentication Required
与401类似
Proxy的response header必须包含Proxy-Authenticate
408 Request Timeout
409 Conflict
需要User干预解决
在PUT的时候可能遇到
410 Gone
URI已经不存在,且不提供3xx来重定向
如果server没有足够的信息来判断URI的不存在是temporary还是permanent,可以用404代替
411 Length Required
request header中缺少Content-Length
412 Precondition Failed
413 Request Entity Too Large
如果只是temporary,可以包含Retry-After,指定重试的时间间隔
414 Request-URI Too Long
有几种可能导致这个返回码的情况
将POST当成了GET
循环重定向,每次都append上当前URI
攻击
415 Unsupported Media Type
416 Requested Range Not Satisfiable
参考206
417 Expectation Failed
不能满足request header中的Expect,参考100
5xx Server Error
500 Internal Server Error
在server没有输出正确的response header的时候会出现
501 Not Implemented
502 Bad Gateway
由gateway或者proxy返回,表明上游server不能返回请求的URI
503 Service Unavailable
server可能过载,或者正在维护
可以返回Retry-After,如果没有,则视作500
504 Gateway Timeout
上游server不能及时返回
505 HTTP Version Not Supported
实在很简陋,许多控制功能都没有实现:)
不过也能表现大致的框架吧。
/* * A simple process pool */ #include <iostream> #include <queue> #include <vector> #include <algorithm> #include <sys/wait.h> #include <assert.h> #include <stdlib.h> #include <unistd.h> #include <signal.h> #include <sys/mman.h> #include <sys/select.h> using namespace std; int iProcessCount = 0; int *pDispatchCount; void InterruptHanler(int iSigno) { //ToDo: kill all children cerr << "Dispatch Count: " << endl; for( unsigned int i = 0; i < iProcessCount; i++ ) { cerr << i << "\t" << pDispatchCount[i] << endl; } exit(0); } //ToDo: replace two pipes with socketpair int main(int argc, char **argv) { if( argc == 2 ) { iProcessCount = atoi(argv[1]); } if( iProcessCount <= 0 ) iProcessCount = 10; int (*iPipeFD)[2] = new(int[iProcessCount][2]); int (*iNotifyPipeFD)[2] = new(int[iProcessCount][2]);; pDispatchCount = (int *)mmap(0, sizeof(int) * iProcessCount, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); queue<int> queIdle; vector<int> vecBusy; for( unsigned int i = 0; i < iProcessCount; i++ ) { if( pipe(iPipeFD[i]) == -1 ) { cerr << "pipe error" << endl; return 0; } if( pipe(iNotifyPipeFD[i]) == -1 ) { cerr << "pipe error" << endl; return 0; } } for( unsigned int i = 0; i < iProcessCount; i++ ) { pid_t pid = fork(); if( pid < 0 ) { cerr << "fork error" << endl; return 0; } if( pid == 0 ) //child { int iCount = 0; while(1) { char sBuf[16] = {0}; read( iPipeFD[i][0], sBuf, 15 ); cout << i << ": " << getpid() << ", " << ++iCount << "\t" << sBuf << endl; pDispatchCount[i]++; //Do your work here int iMax = atoi(sBuf); usleep(iMax % 10000); write( iNotifyPipeFD[i][1], "1", 1); } } queIdle.push(i); } signal( SIGINT, InterruptHanler ); int iCounter = 0; while( 1 ) { if( queIdle.size() ) { //Dispatch work char sBuf[16] = {0}; int iRand = rand(); snprintf(sBuf, 16, "%d", iRand); int iIndex = queIdle.front(); queIdle.pop(); write( iPipeFD[iIndex][1], sBuf, strlen(sBuf) ); cout << "dispatch " << sBuf << " to " << iIndex << endl; vecBusy.push_back(iIndex); } cout << ++iCounter << "\t: queIdle.size = " << queIdle.size() << " vecBusy.size = " << vecBusy.size() << endl; //if( vecBusy.size() ) { fd_set set; FD_ZERO(&set); int iMaxFD = 0; for( unsigned int i = 0; i < vecBusy.size(); i++ ) { int iIndex = vecBusy[i]; FD_SET( iNotifyPipeFD[iIndex][0], &set); iMaxFD = max( iMaxFD, iNotifyPipeFD[iIndex][0]); } struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; select(iMaxFD + 1, &set, NULL, NULL, &timeout); for( vector<int>::iterator it = vecBusy.begin(); it != vecBusy.end(); ) { int iIndex = *it; int iReadFD = iNotifyPipeFD[iIndex][0]; if( FD_ISSET( iReadFD, &set ) ) { queIdle.push(iIndex); it = vecBusy.erase(it); } else { it++; } } } } return 0; }
到了总部培训,见到很多老同事,中午老大请吃饭:)
培训的题目是《边重构边生活》,讲师Bison,讲得非常不错。
本身对行业和实践有深入的认识,关键是可以将想法有条理的表达出来。
如果可以讲得更加生动,将是一个非常棒的课程。
对“火箭发射”的例子印象深刻。
做了详细的笔记。
其实生活中很多例子都是相通的。
相比较而言,另一门《set模型》讲得就逊色很多。



最近评论