Trade Off

supercalifragilisticexpialidocious

Rarp

Packet Format

RARP packet is identical to ARP packet, differences: RARP ‘type’ field is 0x8035 for request and reply and ‘op’ field is 3 for RARP request and 4 for RARP reply.

RARP’s reply is unicast.

The implementation of RARP server is complex…

Timestamp

今天在做批量导入图片功能的时候,发现一个问题,原先save image过程用timestamp当文件名,这样在一般使用的时候倒是没什么问题了,但让程序去做的时候,timestamp就显得不好用了,程序跑的很快,可能三四张图都在1s内处理完上传,这样就很难保证文件名不重复。

传一张停1秒呢?问题不大,但看着还是不保险,而且变慢了,因为要等。。。

纯粹uuid倒是问题不大,但日后管理起来确实就不爽了——看uuid根本不知道谁先谁后,所以就把tiemstamp和uuid4连起来当新文件名,这样程序批量传也不会有问题(其实概率已经低到这个时代可接受的程度吧),又在trade off了。

Conceptual-models

Conceptual Models

Designing software so that its structure reflects that of the problem.

Analysis is not only listing requirments, but also looking behind the surface requirments to come up with a mental model of what is going in the problem.

Conceptual model is a human artifact.

Developers use conceptual models to represent a model of the real world in order to understanding the read part of problems easily.

There is no right or wrong model, merely one that is more useful for the job at hand.

The example, snooker simulation, if your choose a Einsteinian model would be flexible enought to low speed or high speed situation, but the complexity is totally different you know, so the flexibility and many others aspects need a trade-off.

When building a model in a programming language, maybe Python, you may get lost in this language or building this model using some features belong to Python, so this model is a Python-model, and it is hard to be implemented in other languages such as Java, PHP, etc. To avoid these problems, you need a third-party analysis techniques.

Only domain experts could build right model, because of their domain knowledge.

Conceptual model is closely to software interfaces rather than software implementations. One of the important things about OO is that it separates interface from implementation.

Arp

32 bits Internet address
|                   /|
|ARP                 |RARP
|/                   |
48 bits Ethernet address

Gratuitous ARP

  1. send its own IP to verify whether someone has used this.
  2. update old MAC address in ARP cache

Manage-rasp-pi-in-internal-network

今天早晨去公司后一开邮箱发现好多树莓派发来的警告信,如果是一封信可能是误报,但这么多,难道是真的有人闯入?不过看了大部分的图片都是空空的房间,但仍然有新的邮件过来,看来是红外探测器出问题了。

想了半天以为是昨天加的DHT11在读取数据的时候是不是改变了GPIO的布局,导致探测器的数据口得到数据,激活了它?后来证明不是。猜测啊:探测器放到了无线网卡下方,无线网卡有个工作指示灯一直闪烁,会不会触发了探测器,因为我换了个地方后探测器就正常工作了。

在没有远程管理的前提下,解决的方法是去七牛把bucket先删掉,这样就不能再上传图片了,然后去sendgrid修改密码,这样就不能发送邮件了。其实我期待能引发一个异常,这样程序就退出了,但很可惜,七牛的lib似乎做了些处理,删掉bucket或者修改API KEY这样的方法也就是给个提示出来,他们处理了异常,不过好在sendgrid的lib没有处理异常,密码错误后引发的异常终止了程序!GREAT!我还担心了一天摄像头不断拍照会不会坏掉。

上面都是背景吧,主题是一个远程管理的重要性,特别是出现这种突发情况而你又不在边上。

受限于当地的网络环境,树莓派在一个很深的网络中——有家庭的路由器,再往上是小区的路由器,也许再往上就是公网IP了,和在原来的家不同,直接能拿到公网IP,这样基本可以让树莓派暴露在公网上,管理起来当然方便了。穿透这么多层网络,感觉好麻烦,虽然毕业论文是NAT穿透的应用。。。

简单的做法就是利用第三方,我有一个VPS,当然在海外,除了速度上不如国内快之外,别的没啥了。

于是在树莓派上建立一个反向SSH:ssh -p VPS_PORT -fNR 2222:127.0.0.1:22 USER@VPS_HOST,其中VPS_PORT如果你没修改默认的22的话,可以不写-p VPS_PORT,其他的,比如fN,你可以先不写,测试成功后再写上,只做端口转发而不执行命令。后面的R表示反向连接:就是127.0.0.1的22端口在后面远程主机中映射到2222端口。这样,登陆了远程主机的人,访问2222端口就可以访问到树莓派的22端口了。

再临时生成几个rsa key加到authorized_keys中就可以不输密码登陆了,虽然速度稍慢一点,但至少是可用的!

测试通过后使用autossh建立连接,这样即便是中断也会重连上:)

Basestring-and-str-in-python2x

今天在代码中发现原本过滤str的一个func被写成了过滤basestring,于是查了下basestring和str的区别:(在3.0之前)

1
2
3
4
5
6
7
object
             |
             |
         basestring
            / \
           /   \
         str  unicode

3.0之前python中有plain string和unicode string,前者是ascii中的那些字符,后者就是所有字符了,因为historical legacy。。。python出现的时候还木有unicode,所以很多python的lib是需要慢慢转换的,当然到了3.0就会不复存在了,全是unicode。

这样的话就清楚了,str代表了那些ascii字符,unicode就是全部的字符,他们都继承自basestring。basestring是从python2.3开始引入的。

Opencv-with-pi

想了好久都觉得连续拍照片做监控实在是没什么效率,默认情况应该是什么都不做,如果发现有人来了才拍下来或者录下来,但用标配的摄像头录像实在是会录出非常大的文件,保存文件又是问题。在官网的博客中看到了一个用opencv做的人脸识别,可以这样干嘛。于是紧随那片文章开始干。

由于标配摄像头模块不是usb设备,所以需要重新编译使用的几个程序,比如raspistill和raspivid等等。

另外,如果直接在raspberrypi上编译,需要安装一些软件:http://thinkrpi.wordpress.com/2013/04/05/step-3-install-softwares-for-webcam-and-computer-vision/

具体的操作请参照:http://thinkrpi.wordpress.com/2013/05/22/opencv-and-camera-board-csi/

替换arm-linux.cmake的命令:sed -i 's/if (DEFINED CMAKE_TOOLCHAIN_FILE)/if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)/g' arm-linux.cmake,看准了arm-linux.cmake的路径,我运行命令的时候已经到了和arm-linux.cmake一个目录下了。

Got-raspberry

工资一发就买了块表。。。并且还狠心买了这个玩具——raspberry。

默认用了新手的那个镜像,叫NOOBS,就是里面装好了不少东西,系统也有好几个,默认用了推荐的Raspbian,是Debian的一个变种啦,开发用的Ubuntu都是一个家族的。

配置的话,没啥可说的,就是把显卡占用的内存改成了16Mb,默认似乎分配了128M过去,对于我这种总看命令行的人足够了。如果你和我一样买了官方的摄像头模块,那就可以把camera给enable了。再就是在adcanced options中把SSH打开,否则你还得连上键盘鼠标去操作。。。

不喜欢插网线,但很可惜办公室的Cisco双频无线网卡AE2500装不上,貌似是驱动问题,不想折腾,还好买了个免驱的小网卡叫EOUP。我的无线路由是hidden SSID了,所以在连接的时候还有点问题。在配置文件/etc/wpa_supp/wpa_supp.conf中多加了一行: scan_ssid=1

apt的source也改到了ustc.edu.cn的:http://mirrors.ustc.edu.cn/raspbian/raspbian/,具体就是把/etc/apt/source.list中第一行的URL改成上面那个即可。

camera的官方使用教程:http://www.raspberrypi.org/archives/3890

如果想用camera捕捉视频,然后传到你这里看的话,需要在raspberry上先装netcat,我是用的Mac系统,系统本身有nc了,再用brew多装一个mplayer。

raspberrrypi上捕捉视频用:raspivid -t 0 -vs -w 800 -h 600 -o -| nc 172.19.1.124 5001,其中t参数指定了捕捉多少毫秒的视频,用0表示禁用这个选项,后面的vs是防抖,w和h控制宽高,这样就得到一个800x600的画面。

Mac上用nc把发来的stream转到mplayer上:nc -lp 5001 | mplayer -fps 31 -cache 512 -,很无语的是man nc得到一句解释,l和p参数在一起用是个错误,但只用l是不管用的,给作者发了封信问下,不知道会不会得到回信。

2015-01-18:今天有机会使用树莓派的视频功能,因为要监控看看麻麻什么时候从外面回家,于是找相关命令,想到自己以前用过这个,就找出来了,结尾算是遗留的一个尾巴,今天解决一下。

我给原作者发了邮件,可惜邮件被退了,又给IPV6版本的实现作者发信,还没回,后来给Ubuntu-dev的组发了信(是不是很大胆?),管理员回了我,告诉我不能在这问这种问题,不过说了一些东西,虽然没什么帮助,但我确实在ubuntu上试了下nc -l IPADDRESS 5000这条命令是否管用,还真能用,确实不应该加-p参数,但为何我这里的就不对呢?实际Mac系统本身有nc,后来我用brew装了个netcat,然后系统里面就有点乱了,卸载掉netcat后,直接用系统的nc,就和man中说的一样了,不用加-p,参数,实际是不应该加那个参数,而是nc -l IPADDRESS 5000,文中提到的那种写法我标记删除了。

Internet Protocol

TCP, UDP, ICMP, IGMP need IP datagrams as data transmitting method.

IP is a unrealiable and connectionless delivery service.

  • unrealiable: no guarantee that an IP datagram successfully get to its destination.
  • connectionless: IP doesn’t maintain any state of successive datagrams, it means there is no order in IP datagrams, latter sent IP datagram may arrived before former sent.

Header

TOS: 3 bits precedence field(ignored today), 4 bits TOS(mini delay, max throughput, max relability, mini monetary cost), 1 bit always 0. TOS maynot supported by most TCP/IP implementations today, but some routing protocol such as OSPF and IS-IS are capable of making routing decisions based on this field.

Furthermore, a host isn’t required to receive a datagram larger than 576 bytes. TCP and UDP will divide user data into small pieces, so one IP datagram will below that limit.

Some small IP datagram will be padded up to the mini length of frame, which maybe 46 or 38 bytes, so total length field is required when you want to know how long the IP datagram’s length on earth.

TTL: set by sender, if this datagram pass a router, it will decrease 1, when it reaches 0, this datagram will thrown away, and sender will received an ICMP message. prevent routing loop.

Checksum: TCP, UDP, ICMP, IGMP use same algorithm to computing checksum. Since a router often cheages only TTL value, a router can incrementally update the checksum when it forwards a datagram, instead of calculating the checksum over the entire IP header again.

IP routing

The fundamental difference is that a host never forward a datagram from one of its interface to another, while a router forwards datagram.

Router may reach a local optimum result, because every search route table, the router will get a closer next-hop router address, but it may not global optimum.

Frame’s destination address(MAC address) may not the really destination address, it may the next-hop router’s MAC address, because when it is routing, it can’t be found in the current network, it must be routed to a next-hop router, so the destination address is the next-hop router’s.

Subnet addressing

Don’t forget subtract 2, because all 0 bits and all 1 bits address for host address is invalid.

The advantage to using s single class B address with 30 subnets, compared to 30 class C addresses, is that subnetting reduces the size of the Internet’s routing tables entry.

A special address: 127.x.x.x means loopback address, not only 127.0.0.1 can be loopback address!

Link-layer

link layer

link layer’s usage

  1. send and receive IP datagram
  2. ARP request and reply
  3. RARP request and reply

many different link layers, depending on the type of networking: ethernet, token ring, FDDI, RS232 serial lines, etc.

IEEE 802.2/3 format and Ethernet format

in 802 format, after 2 bytes length field, there are 3 bytes LLC and 5 bytes SNAP and data field(38~1492) but in Ethernet format, there will be data field(46~1500), the end field is 4 bytes CRC.

frame formats use 48-bits destination and source address

SLIP: Serial Line IP

END – 0xc0

SLIP ESC – 0xdb

IP datagram will be terminated by a special character called END(0xc0), most implementations transmit an END at the beginning of the datagram.

if a byte of IP datagram equals:

0xc0 –> 0xdb, 0xdc

0xdb–>0xdb, 0xdd

PPP

  1. async link with 8bits of data and no parity(no checksum)
  2. bit-oriented sync links

start and end with a flag byte whose value is 0x7e, and if a byte value appears in information field, it should be escaped.

batter than SLIP:(pay for 3 bytes addition)

  1. multiple protocols on a serial line
  2. CRC
  3. IP network control protocol negotiate IP address for each end
  4. header compress like CSLIP
  5. link control protocol negotiate many data-link options

Loopback

127.0.0.1 or localhost is the loopback interface, an IP datagram sent to this must not appear on any network.

datagrams sent to broadcast and multicast addtess are copied to the loopback interface and sent out on the Ethernet.

anything sent to one of the host’s own IP addresses is ent to the loopback interface!

MTU

if the length of data field in IP datagram is larger than MTU(1500 or 1492 in Ethernet and 802 format), it will be fragmented.