2013-01-30

Use internal keywords to build safety reference between assemblies.


You probably already know how to use internal keyword in .NET, but I still wanna share this .NET feature in my thought. If you think this is not right, then I am glad to hearing from you what is possible the correct pattern.

In .NET, once we can reference any assembly if we need, e.g. Lib1.dll, then we can directly use most public classes and methods, or even use reflection access some private methods or properties as well. If consider high safety and security, how to protect those private stuff is really important. In another word, sometimes we really do want to expose some class and method to other sibling projects, and generally we just make it public. Which is okay for implement feature, but it is also bad for security, cause some care stuff we really want nobody touch it. For example, in Lib1 project, we really want keep invisible for those core classes/properties/methods which refer to unmanaged stuff, if we exposed it, then there will be a big chance some smart developer -- he or she you have no idea who they are-- 
tries to use them, and if the unmanaged code base is a massive, then all bad things will happening( I have some bad experience on this scenario). So how to avoid this and keep those classes/properties/methods internal visibility is very important to make whole software under high security protection, we need use internal keyword.

Here is my way:
  • First make the assembly strongly name support in client project(I do not want discuss it is necessary or not, but in my onion every assembly need strongly name), e.g. Share.snk
  • Then make client project compile successfully, and open command line to extract publicKey token. See below image:
  • Go back to your shared project, open Properties->AssemblyInfo.cs, adding internal friendly assembly[assembly: InternalsVisibleTo("AQMetaTypeFix, PublicKey=002400000.....370cad")]
  •  Recompile shared project, and in client project, we can be able to access all of internal classes and methods from share project.

Using this mechanism, I build some projects with much strong security than before, especially for those SDK which my company shipped to customer, I never met any unexpected fatal error any more. And except all these, use internal keyword to hidden stuff is a fun game to play.

2012-06-13

ESET NOD32 Antivirus slow down Visual Studio

Recently install ReSharper and TypeMock into my dev machine, which is extreme slow down Visual Studio 2008 and 2010, when ever I input some thing, it will have seconds latency to see the keyboard cursor. Original I think it maybe comes from ReSharper and TypeMock, but I am wrong. Indeed, the antivirus does the bad thing on the background, which take a 30% CPU and 15Mbps disk queue IO.

Then I go to antivirus( I am useing ESET NOD32 Antivirus TM 4.2.71.2):

First open configuration setup:
Context Menu->Advance Menu
Then In left menu tree panel added a batch of exclusions in:
Antivirus and antispyware->Exclusions

C:\$mon_sys_files\*.*                                                   
C:\$Recyle.Bin\*.*                     //Disable scan trash
C:\Git.Modules\*.*                     //Disable scan my dev working folder
C:\app\*.*                                 //Disable scan Oracle database
C:\Program Files (x86)\Misrosoft SQL Server\*.*          //Disable scan SQL Server database
C:\RefSrc\**                              //Disable scan VS .net framework symbols

Then it extreme fast now. If you met the problem, apply the similar resolution.


2012-05-31

Racine's 1st and 2nd wok from her paintings class

 Racine's first painting class work.
She wants to be 3-years old, even she is already 5.





































Racine's second painting class work. It is amazing, and Racine is the the fastest one in the class. I love the big dragonfly, a lot of her thought are draw down.        


Racine's first PC mouse painting. 

I am so proud of you, Racine!

2012-05-30

CQRS + Event Sourcing Distribute Architecture Design

Preface

Very good I got a chance to attend the DevTeach 2012 at Vancouver. Even though I only get one day to be there, I am still so happy that we will see what other company are going in Architecture level. 

First I must say thanks to those excellent speakers to show my respect:
Rob Daigneau
Dylan Smith
Ted Neward 
Vincent Grondin

This blog is what I learned form Dylan, and the same time added some my thought to make it better in my opinion.

Content

1. What is CQRS 

It stands as Command Query Responsibility Segregation
Strong point:
The core concepts is split write and read behavior to Command Handler and Query Handler, and it is really good to increase scalability for our server.
Imagine, a cluster which have 2 writing server and 20 read server based on 10 database, we can easily make it.

Weak point:
Command/Query handler is easy to implements, but it less extensibility and flexibility for reuse existing under layer modules. For examples: we want detect a behavior if user did command A, and then B, then D, we should do some specific thing for the case.

2. Event Sourcing

We all know the best benefit for Event-driven is we have flexibility and extensibility to hook up new features into the existing software. Based on CQRS's major weakness, we know if we mix this 2 architecture style together, we will have a round result.

3. CQRS + Event Sourcing

Based on Dylan's model, I added some specific part(red circle) in bellow overall figure:
Architecture illustrate for CQRS+ES

Core classes:
CommandHandler
Queryhandler 
DomainObject
Repository
Subscriber

EventHandler
DTO


I do not have time to write a small prototype for this, but I believe it is is good to deal with a large scale system.

4 Benifits

  • Simple
    • E=mc^2. Everything  should be made as simple as possible, not simpler. In this architecture design, a new feature we can easily implement a bunch of classes, e.g. Command, Query, Repository...
  • Single Responsibility Principle
  • Scalability
    1. Split write and read
    2. Support sync and notification evens
  • Flexibility & Extensibility
    • Event Bus allow us plug in new module to handler new events
  • Can be noSQL System 
    • Writing to a file server, QUEUE
  • Testing 
    • Each class are single responsibility, we can easily write unit test with mock


.........................................
Any comments are welcome.

2009-02-24

Memcahced setup


1. libevent-1.3e.tar.gz       libevent-1.3e.tar.gz [GPG Sig] - ChangeLog - Release 2007-09-24



One of the dependencies of memcache is libevent, so firstly download the source files for Libevent http://www.monkey.org/~provos/libevent/.
tar -xvf libevent-1.3b.tar.gz
cd libevent-1.3b
./configure;make;make install;

2. memcached-1.2.2.tar.gz  

Download the latest Memcached source code from danga.com  http://www.danga.com/memcached/
gunzip memcached-1.2.1.tar.gz
tar -xvf memcached-1.2.1.tar
cd memcached-1.2.1
./configure;make;make install;

Often libevent.so cannot be found when executing memcache. A useful command LD_DEBUG, is very helpful to determine where libraries are being loaded from.
 LD_DEBUG=help memcached -v
 LD_DEBUG=libs memcached -v 2>&1 > /dev/null | less
 18990: find library=libevent-1.3b.so.1 [0]; searching
  ...
 18990: trying file=/usr/lib/libevent-1.3b.so.1
 18990:
 memcached: error while loading shared libraries: libevent-1.3b.so.1: cannot open shared object file: No such file or directory
 Simply place the library where memcached will find it and execute memcached.
 ln -s /usr/local/lib/libevent-1.3b.so.1 /lib/libevent-1.3b.so.1
 memcached -d -u nobody -m 512 127.0.0.1 -p 11211
  The options for memcached are:
      -l <ip_addr>
      Listen on <ip_addr>; default to INDRR_ANY. This is an important option to consider as there is no other way to secure the installation. Binding to an internal or firewalled network interface is suggested.
      -d
      Run memcached as a daemon.
      -u <username>
      Assume the identity of <username> (only when run as root).
      -m <num>
      Use <num> MB memory max to use for object storage; the default is 64 megabytes.
      -M
      Instead of throwing items from the cache when max memory is reached, throw an error
      -c <num>
      Use <num> max simultaneous connections; the default is 1024.
      -k
      Lock down all paged memory. This is a somewhat dangerous option with large caches, so consult the README and memcached homepage for configuration suggestions.
      -p <num>
      Listen on port <num>, the default is port 11211.
      -r
      Maximize core file limit
      -M
      Disable automatic removal of items from the cache when out of memory. Additions will not be possible until adequate space is freed up.
      -r
      Raise the core file size limit to the maximum allowable.
      -h
      Show the version of memcached and a summary of options.
      -v
      Be verbose during the event loop; print out errors and warnings.
      -vv
      Be even more verbose; same as -v but also print client commands and responses.
      -i
      Print memcached and libevent licenses.
      -P <filename>
      Print pidfile to <filename>, only used under -d option.

3. memcache-2.2.0.tgz

To install the pecl package for PHP   http://pecl.php.net/get/memcache
wget http://pecl.php.net/get/memcache-2.1.2.tgz
gzip -df memcache-2.1.2.tgz
tar -xvf memcache-2.1.2.tar
cd memcache-2.1.2
phpize
./configure;make;make install;
Add memcache.so to the php.ini file
extension=memcache.so
Then run
php -i | grep -i 'memcache'
memcache should be listed and then restart the web server.

4. SHELL script


#!/bin/bash
#script memcacheDaemon.sh
LANG="zh_CN.utf8"
DAEMON_TIME=10
stopProcess()
{
        DATESTR=`date '+%Y-%m-%d %H:%M:%S'`
        echo "$DATESTR (INFO)  : STOP memcache begin.."
        PID=`ps -ef --cols=200 |grep $1 | awk '{print $2 " " $8}'| grep $1 | sed -e '/grep/d' -e 's/^  *//' -e 's/ .*//'`
        if [ -n "$PID" ]
        then
                echo "$DATESTR (ERROR) : $1 ID $PID, FORCE KILL NORMAL RUNNING PROCESS!"
                kill -9 $PID
        else
                echo "$DATESTR (INFO)  : $1 IS NORMAL DOWN!"
        fi
        echo "$DATESTR (INFO)  : STOP apache end."
}
runDaemon()
{
        DATESTR=`date '+%Y-%m-%d %H:%M:%S'`
        PID=`ps -ef --cols=200 |grep memcached | awk '{print $2 " " $8}'| grep memcached | sed -e '/grep/d' -e 's/^  *//' -e 's/ .*//'`
        if [ -n "$PID" ]
        then
                echo "$DATESTR (INFO)  : memcached NORMAL running. "
        else
                echo "$DATESTR (INFO)  : RELOAD memcached START. "
                if [ "`id -un`" = "root" ] ; then
                        echo "killall memcached"
                        killall memcached
                        echo "su - kangq -c '/usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211'"
                        su - kangq -c '/usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211'
                else
                        echo "killall memcached"
                        killall memcached
                        echo "/usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211"
                        /usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211
                fi
                echo "$DATESTR (INFO)  : RELOAD END."
        fi
}
if [ "`id -un`" = "root" ] ; then
case "$1" in
start)
        DATESTR=`date '+%Y-%m-%d %H:%M:%S'`
        echo "$DATESTR (INFO)  : INTIAL memcached START. "
        if [ "`id -un`" = "root" ] ; then
                echo "killall memcached"
                killall memcached
                echo "su - kangq -c '/usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211'"
                su - kangq -c '/usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211'
        else
                echo "killall memcached"
                killall memcached
                echo "/usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211"
                /usr/local/bin/memcached -d -m 2048 127.0.0.1 -p 11211
        fi
        echo "$DATESTR (INFO)  : INTIAL END."
        while [ "  " != "" ]
        do
                runDaemon
                ##echo begin sleep ...
                sleep $DAEMON_TIME
                ##echo sleep over ...
        done
        ;;
stop)
        stopProcess memcached
        stopProcess memcacheDaemon.sh
        killall         memcacheDaemon.sh
        ;;
*)
        echo "usage: $0 (start|stop|help)"
        ;;
esac
else
        echo "ERROR: using root run this shell!"
fi
#!/bin/bash
#script memcacheStart.sh
SHELL_DIR=$(cd "$(dirname "$0")"; pwd)
$SHELL_DIR/memcacheDaemon.sh stop
nohup $SHELL_DIR/memcacheDaemon.sh start >>$SHELL_DIR/memcacheDaemon.log &
#!/bin/bash
#script memcacheStop.sh
SHELL_DIR=$(cd "$(dirname "$0")"; pwd)
nohup $SHELL_DIR/memcacheDaemon.sh stop >>/data/share/bin/memcacheDaemon.log &

2008-10-15

JME开发环境的搭建

大家好:
这几天一直在弄JME的开发环境,JAVA的东西就是这样,版本出奇的多,如果搜索的话,在网上能够找到的关于不同版本的东东能够呈现十几页之多。
记得以前JME叫J2ME的时候,所有的东西都是通过指令行,配合Ultraedit就可以搞定了,现在时代不同了,能够用IDE。
那是相当的多呀,综合来看,主要是Codewarrior,eclipse,netbeans,JBuilder.
按照我一贯的思路,Free的东西就是好的,至不济俺继续用指令行进行开发,不调试了,用IDE不就图个调试吗!
ECLIPSE
网上一看,没人说什么版本号,我一次下载了eclipse3.1.2,eclipse3.2.2,eclipse3.3.2,eclipse3.4.1,然后下载了eclipseme1.7.6,eclipseme1.7.8,eclipseme1.7.9,下了sunWTK2.2,WTK2.5.2,nokia S60 3rd FP1, nokia S40 5th fp1.
在eclipse3.2.2+eclipseme1.7.8+WTK2.2运行WTK2.2代码,good!
高版本呢?
于是尝试了eclipse3.4.1+eclipseme1.7.9+nokia S60 3rd FP1,不成,永远不行,倒是sun WTK一直表现很好。
最好上nokia JME论坛看了看,上eclipseme官方看了看,发现eclipseme已经成为eclipse的一个子项目,现在叫MTJ,于是我用了mtj里面给定的ECLIPSE sdk 3.3.2 + eclipseme 1.7.9 ,然后下载了NOKIA官方的Carbide.j 1.5,并且了解到NOKIA S40的速度比S60优越不止100倍,确定了我最终的开发策略。
暂定使用eclipse3.3.2 + eclipseme 1.7.9 + Carbide.j 1.5 + WTK2.5.2 ,默认项目用WTK2.5.2开发,完成后在S40,S60下做兼容性修改。
路漫漫其修远兮,今天就写到这里,以后如果开发不畅,我再来补充。

2008-05-12

xhtml_css网页制作中的优先级详细说明

发现很多朋友对 CSS 的优先权不甚了解,规则很简单。需要说明的一点,如果你的样式管理需要深层判断 CSS 的优先权,更应反思自己的 CSS 代码,是否合理?是否优化?

CSS2.1 中规定了关于 CSS 规则 Specificity(特异性)的计算方式,用一个四位的数字串(注:CSS2 中是用三位)来表示,最后以 Specificity 的高低判断 CSS 的优先权。

Specificity 具体的计算规则:

元素的 style 样式属性,加 1,0,0,0。
每个 ID 选择符(#id),加 0,1,0,0。
每个 class 选择符(.class)、每个属性选择符(例 [attr=”"] )、每个伪类(例 :hover),加 0,0,1,0。
每个元素或伪元素(例 :firstchild)等,加 0,0,0,1。
其他选择符(例 全局选择符 *,子选择符 >),加 0,0,0,0。
最后逐位相加数字串,得到最终的 Specificity 值,按照从左到右的顺序逐位比较。

除了 Specificity 还有一些其他规则:

!important 声明的规则高于一切,如果 !important 声明冲突,则比较优先权。
如果优先权一样,则按源码中“后来者居上”的原则。
由继承而得到的样式属性不参与 specificity 的计算,低于一切其他规则(例 全局选择符 * )。
范例分析:

h1 {color: red;}
/* 只有一个普通元素加成,结果是 0,0,0,1 */
body h1 {color: green;}
/* 两个普通元素加成,结果是 0,0,0,2 */
/*0,0,0,1 小于 0,0,0,2 ,后者胜出*/

h2.grape {color: purple;}
/* 一个普通元素、一个class选择符加成,结果是 0,0,1,1*/
h2 {color: silver;}
/*一个普通元素,结果是 0,0,0,1 */
/*0,0,1,1 大于 0,0,0,1 ,前者胜出*/

html > body table tr[id=”totals”] td ul > li {color: maroon;}
/* 7个普通元素、一个属性选择符、两个其他选择符(子选择符 >),结果是0,0,1,7 */
li#answer {color: navy;}
/* 一个ID选择符,一个普通选择符,结果是0,1,0,1 */
/*0,0,1,7 小于 0,1,0,1,后者胜出*/

2007-08-14

初来乍到

初来乍到,先试试速度如何,功能如何?


奇怪,为什么博客地址连不到呢?


尝试了dig


; <<>> DiG 9.4.1 <<>> liurunner.blogspot.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 364 ;;
flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0  ;;
QUESTION SECTION: ;liurunner.blogspot.com.                IN      A  ;;
ANSWER SECTION: liurunner.blogspot.com. 3600    IN      CNAME  
blogspot.l.google.com. blogspot.l.google.com.  187     IN      A   72.14.207.191  ;;
Query time: 389 msec ;; SERVER: 192.168.10.1#53(192.168.10.1) ;;
WHEN: Wed Aug 15 09:48:46 2007 ;; MSG SIZE  rcvd: 88  

尝试了whois 72.14.207.191


OrgName:    Google Inc.
OrgID:      GOGL
Address:    1600 Amphitheatre Parkway
City:       Mountain View
StateProv:  CA
PostalCode: 94043
Country:    US

NetRange:   72.14.192.0 - 72.14.255.255
CIDR:       72.14.192.0/18
NetName:    GOOGLE
NetHandle:  NET-72-14-192-0-1
Parent:     NET-72-0-0-0-0
NetType:    Direct Allocation
NameServer: NS1.GOOGLE.COM
NameServer: NS2.GOOGLE.COM
NameServer: NS3.GOOGLE.COM
NameServer: NS4.GOOGLE.COM
Comment:
RegDate:    2004-11-10
Updated:    2007-04-10

RTechHandle: ZG39-ARIN
RTechName:   Google Inc.
RTechPhone:  +1-650-318-0200
RTechEmail:  arin-contact@google.com

OrgTechHandle: ZG39-ARIN
OrgTechName:   Google Inc.
OrgTechPhone:  +1-650-318-0200
OrgTechEmail:  arin-contact@google.com

# ARIN WHOIS database, last updated 2007-08-14 19:10
# Enter ? for additional hints on searching ARIN's WHOIS database.


But I can not visit http://liurunner.blogspot.com/, Why, google not refresh the virtual host for me?
Or maybe I should visit it later!