Archive

Archive for the ‘Technology’ Category

今天准备Lift codekata的时候又发现新东西了

August 7th, 2010 honnix No comments

那本著名的书上也没写,但是应该是比较常用的功能:如何绑定一个attribute。
有一段html,内容如下:

<div>
  <dl id="xxx">
    <dt>xxx</dt>
    <dd>yyy</dd>
  </dl>
</div>

要实现的是把“dl”的“id”动态绑定到一个值,这样可以直接使用数据库中的id,并且也方便jQuery操作。lift里面可以这样实现:
首先把上面的html改造一下:

<div>
  <dl entry:id="xxx">
    <dt>xxx</dt>
    <dd>yyy</dd>
  </dl>
</div>

然后Scala代码这样写:

      bind("entry", xhtml,
           AttrBindParam("id", Text(article.id.is.toString), "id")
      )
Categories: Lift, Scala, Technology Tags: , , ,

在问题没出现之前,能不能不把事情整得太复杂?

July 21st, 2010 honnix No comments

是不是所有做软件的人都有这种天生的冲动,把系统设计得所有人都看不懂?

究竟是不是需要一开始就把一切都考虑清楚,然后做出复杂无比的方案,去应对你根本无法确定会不会出现的问题或异常?

难道所谓的商用级别的软件都一定要极度复杂、无法维护、一般水平的人根本读不懂,才能称得上是好的软件?

我很怀疑。

Categories: Technology Tags:

如何对付那些发了疯的蜘蛛们

June 6th, 2010 honnix No comments

最近两天,二奶机经常过热关机,毕竟是老机器了,经不起过高的温度。什么原因呢?top一下发现apache占用CPU高达30%。这就诡异了,我的网站啥时候也没有这么高的访问量啊。查了下access_log,居然有无数的IP来访。whois加上Google,原来是一堆的蜘蛛,大部分来自腾讯和搜狗。简直是疯狗,同时放出几十个蜘蛛来爬,这哪受得了!

我的路由器没有防火墙功能,所以只好在二奶机上弄了。还好上次编内核的时候把iptables编进去了,否则我估计这种天气,内核都编不下去就要关机了。折腾了一番,终于搞定。Gentoo对iptables的支持还不错,emerge了之后/etc/init.d里面就有了启动脚本,简单地看了一下也就会用了。现在我的iptables里面是这种状况,列出来给需要的人参考一下(基本上我把IP在中国的都给踢出去了,谁知道它们在找什么):

# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
all  –  anywhere             anywhere
DROP       all  –  124.115.4.0/24       anywhere
DROP       all  –  124.115.0.0/24       anywhere
DROP       all  –  122.194.170.0/24     anywhere
DROP       all  –  114.80.93.56         anywhere
DROP       all  –  122.81.25.8          anywhere
DROP       all  –  123.125.66.0/24      anywhere
DROP       all  –  218.5.3.149          anywhere
DROP       all  –  220.181.94.222       anywhere
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
最后简单说一下iptables的用法,网上到处都是,不多啰嗦了。
iptables -A INPUT -s <ip> -j DROP

很久没更新了,说说翻墙的事儿吧

June 1st, 2010 honnix 6 comments

买了个ssh,淘宝上到处都是,就不说具体哪家了。价格还可以,一个月3块,先来了一个月尝尝。
速度还行,就是有一点比较讨厌,没法保持连接,一段时间idle之后就被断开了,尝试设了一些参数,还是不行,还是写个脚本来干。

?Download wall.expect
#!/usr/bin/expect -f
 
set timeout 30
spawn ssh wall
match_max 100000
expect -re ".*password:.*"
send -- "xxxxxx\r"
send -- "\r\r\r\r"
expect timeout
 
for {} 1 {} {
    send "ls\r"
    expect timeout
}
 
exit 0

其实很简单,就是登录之后每隔30秒就ls一下。估计这样就行了。

Categories: Linux Tags: , , ,

如何在Lift里用javascript的confirm

May 13th, 2010 honnix No comments

使用Lift的SHtml.a()设计ajax调用的时候,老版本的Lift不支持给onclick加入用户自己定义的javascript方法,新版本的有了。

/**
   * Create an anchor tag around a body which will do an AJAX call and invoke the function
   *
   * @param jsFunc -- the user function that will be executed. This function will receive as last parameter
   *                  the function that will actually do the ajax call. Hence the user function can decide when
   * 				  to make the ajax request.
   * @param func - the function to invoke when the link is clicked
   * @param body - the NodeSeq to wrap in the anchor tag
   * @param attrs - the anchor node attributes
   */
  def a(jsFunc: Call, func: () =>; JsCmd, body: NodeSeq, attrs: (String, String)*): Elem = {
    attrs.foldLeft(fmapFunc(contextFuncBuilder(func))(name =>;
            <a onclick="{deferCall(Str(name" href="javascript://">{body}</a>))(_ % _)
  }

对于jsFunc可以这样理解:
用户定义了一个方法:

?View Code JAVASCRIPT
function delete(toDelete) {
  if (confirm("Delete?"))
    toDelete()
}

那么jsFunc可以这样定义:Call(“delete”),Lift会把它生成的ajax方法作为最后一个参数传递给delete方法。

APE解码器MAC在Snow Leopard上的问题

April 23rd, 2010 honnix 1 comment

其实也不是多大的问题,只是方向不对,搞了很久才解决。

移植MAC的这哥们儿提供了一个configure,当然,是根据Leopard生成的,或者更早Tiger,然后对于SL就不工作了。问题在于port里面的yasm不再接受-fno-common这个选项,只能用-fmacho32或者-fmacho64。

我满心欢喜地统统配成64位,结果一跑就core;不过还好开源,直接gdb上,调了半天才发现很多指针直接cast成int,而SL里int仍然是32位的,所以用截了一半儿的地址肯定会core。统统改成32位再来,就好了。

顺便改了一下configure.in,然后重新autoconf一下,貌似这样比较完美。

SL里面看core文件生成在什么地方用这个命令:sysctl kern.corefile;生成core文件么,同样也是:ulimit -c unlimited。

Categories: Mac, Technology Tags: , ,

USB邮件提醒器

April 16th, 2010 honnix 2 comments

买了个山寨版的这个玩意儿,软件不爽,随即hack之。libusb+pyusb,搞定。不过Mac下面还不行,kernel的module直接claim设备,不知道怎么unclaim。

然后封装了一个简单的库:

?Download lamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python
 
from Queue import Queue
from threading import Thread
 
import usb.core
import usb.util
 
class Singleton(type):
    def __init__(cls, name, bases, dict):
        super(Singleton, cls).__init__(name, bases, dict)
        cls.instance = None
 
    def __call__(cls, *args, **kw):
        if cls.instance is None:
            cls.instance = super(Singleton, cls).__call__(*args, **kw)
 
        return cls.instance
 
class Lamp(object):
    GREEN = 0x01
    RED = 0x02
    BLUE = 0x03
    PURPLE = 0x04
    X = 0x05
    Y = 0x06
    WHITE = 0x07
    __QUIT = -1
 
    __metaclass__ = Singleton
 
    def __init__(self):
        self.t = None
        self.q = None
 
        self.dev = usb.core.find(idVendor = 0x1294, idProduct = 0x1320)
 
        import os
 
        if os.uname()[0] == 'Linux':
            self.dev.detach_kernel_driver(0)
 
        self.dev.set_configuration()
 
    def __worker(self):
        item = self.q.get()
        while item != Lamp.__QUIT:
            if item &gt;= Lamp.GREEN and item &lt;= Lamp.WHITE:
                print item
                data = (item, 0x04, 0x04, 0x04, 0x04)
                self.dev.write(2, data, 0)
 
            self.q.task_done()
            item = self.q.get()
 
    def plug(self):
        if self.q is None:
            self.q = Queue()
            self.t = Thread(target=self.__worker)
            self.t.start()
 
    def unplug(self):
        if self.q is not None:
            self.q.put(Lamp.__QUIT)
            self.t.join()
            self.q = None
            self.t = None
 
    def on(self, color):
        self.q.put(color)
 
    def off(self):
        self.q.put(0x00)
 
if __name__ == '__main__':
    lamp1 = Lamp()
    lamp2 = Lamp()
    lamp3 = Lamp()
 
    lamp1.plug()
    lamp2.plug()
    lamp3.plug()
 
    lamp1.on(Lamp.GREEN)
    lamp1.on(Lamp.RED)
    lamp1.on(Lamp.WHITE)
    lamp1.on(100)
 
    lamp1.unplug()
    lamp2.unplug()
    lamp3.unplug()

忘记还有其它什么颜色了,暂时用X、Y代替吧。

Java HotSpot JVM Memory Leak

April 1st, 2010 honnix 4 comments

频繁地调用AttachCurrentThread和DetachCurrentThread,在并发的情况下会有比较严重的Memory Leak,在一个CPU的机器上几率很小,但是多个或者多核的机器上很容易跑出来。

问题出在ParkEvent链表上(其实有问题的还有Park链表),对它的读写并没有全局加锁。具体可以参考源码:thread.cpp。

解决方法也很简单,调用者自己加把锁就行了。

VirtualBox里面的NumLock

February 18th, 2010 honnix No comments

一直困惑我的问题,现在终于必须要解决了。

host是Mac,guest是Win,那么在guest里面shift+clear就是NumLock。简单,但是一直不知道。

Categories: Mac, Technology Tags: , ,

Finder里如何给一个folder加上icon

February 12th, 2010 honnix No comments

直接看这里吧,懒得写了。还是很简单的,隐藏的功能,不搜还真不知道。

Categories: Mac, Technology Tags: , ,