2011年8月24日水曜日

maptailを動かしてみる

環境が揃ってきたいので、手始めにmaptailを動かしてみます

$npm install maptail

でインストールは終わりです。

使い方は、access_logのpathとexpressのbindの設定を引数で与えるだけです

apacheのaccresslogをchmodとかchownするのもあれなので、一旦rootでテストしてみます

$sudo su - 
#export PATH=/home/shin/local/node/bin:$PATH
#export  NODE_PATH=/home/shin/local/node:/home/shin/local/node/lib/node_modules
#cd /home/shin/dev/node_modules/maptail/
#node ./maptail.js /var/log/apache2/access.log luckyxrea.bf1.jp 5124


 これで、
http://luckyxrea.bf1.jp:5124/map
にchromeでアクセスすると見たかった画面を見ることができます。


まずは、サンプルが動いたので次にコード検証をしていきます。


26-34行目で、express...って書いてある。何となくこいつがweb frameworkっぽい初期化コードになっている。
// configuration
var app = express.createServer()
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.logger());
  app.use(express.methodOverride());
  app.use(express.bodyParser());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

viewの定義が気になったので
 #cat views/map.jade

link(rel='stylesheet', href='/css/wargames.css')
script(src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
script(src='/js/config.js')
script(src='http://' + wshost + ':' + wsport + '/socket.io/socket.io.js')
script(src='/js/raphael.js')
script(src='/js/map.js')
script(src='/js/client.js')
|
|
| had activity 0.00 sec ago
|
| messages in the last 40min
|
|
|
    |
    となっている。なるほど。
    ふんわりexpressについての意義は分かったので、maptailを掘り下げてみる

     74 // start Server
     75 app.listen(port, host);
    引数のportとhostでbindしている


     77 // socket.io 
     79 var wsserver = http.createServer() 
     80 wsserver.listen(wsport, wshost);
    Websocketのサーバもbindしておくのね
     18   var wsport = process.env.WSPORT || + port + 111
     19   var wshost = process.env.WSHOST || host
     この辺のportも使ってるのか。
    #netstal -l 
    tcp        0      0 localhost:5235          *:*                     LISTEN     
    tcp        0      0 localhost:ipp           *:*                     LISTEN     
    tcp        0      0 localhost:postgresql    *:*                     LISTEN     
    tcp        0      0 localhost:smtp          *:*                     LISTEN     
    tcp        0      0 localhost:5124          *:*                     LISTEN     

    たしかに、port5124と+111の5235が使われていますね
    コードの続きを読むと
     81 var socket = io.listen(wsserver)
     83 socket.on('connection', function(client) {
     84   var id = client.sessionId
     85     , ip = client && client.request && client.request.socket && client.request.socket.remoteAddress || '000'
     86 
     87   connected[id] = client
     88 
     89   world.sendStartupData(client)
     90 
     91   client.on('disconnect', function() {
     92     delete connected[id]
     93   })
     94 })
    なるほど。worldの中身はgeoip判定がメインな感じなので大枠この辺までが重要な感じです。


    2011年8月20日土曜日

    Node Package Manager(npm)のインストール

    前回に続きnpmをインストールします

    shin@debian:~/dev$ curl http://npmjs.org/install.sh | sh
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  3927  100  3927    0     0   4464      0 --:--:-- --:--:-- --:--:--  9350
    fetching: http://registry.npmjs.org/npm/-/npm-1.0.26.tgz
    0.4.11
    1.0.26
    cleanup prefix=/home/shin/local/node

    This script will find and eliminate any shims, symbolic
    links, and other cruft that was installed by npm 0.x.

    Is this OK? enter 'yes' or 'no' 
    yes

    All clean!
    ! [ -d .git ] || git submodule update --init --recursive
    node cli.js rm npm -g -f
    node cli.js cache clean
    node cli.js install -g -f
    /home/shin/local/node/bin/npm -> /home/shin/local/node/lib/node_modules/npm/bin/npm.js
    /home/shin/local/node/bin/npm_g -> /home/shin/local/node/lib/node_modules/npm/bin/npm.js
    /home/shin/local/node/bin/npm-g -> /home/shin/local/node/lib/node_modules/npm/bin/npm.js
    npm@1.0.26 /home/shin/local/node/lib/node_modules/npm 

    いい感じにインストールできました
    早速試してみます

    shin@debian:~/dev$ npm install express
    express@2.4.5 ./node_modules/express 
    ├── mime@1.2.2
    ├── connect@1.6.2
    └── qs@0.3.1

    うまくインストール出来たようです

    node.jsインストール

    もろもろバージョンが上がって来たので、今日はnodeのインストールを再構築してみます。
    v4.11で進めます。

    環境はW5に入れている、debian squeezeです。
    Linux debian 2.6.32-5-686 #1 SMP Mon Jun 13 04:13:06 UTC 2011 i686 GNU/Linux
    日本語解説サイトはもはや役に立たないので公式情報を参考に進めていきます
    特にsidしかパッケージがないこともあり自前でbuildする必要があります。/usr以下を汚したくないため$HOME/localにインストール方法なので後で後くされなく消せるのでおすすめな方法です。


    まずは、gitから落としてきます
    shin@debian:~/dev$ git clone --depth 1 git://github.com/joyent/node.git
    Cloning into node...
    remote: Counting objects: 21010, done.
    remote: Compressing objects: 100% (8487/8487), done.
    remote: Total 21010 (delta 16287), reused 16130 (delta 12136)
    Receiving objects: 100% (21010/21010), 32.83 MiB | 263 KiB/s, done.
    Resolving deltas: 100% (16287/16287), done.

    つぎにv4.11 をcheck outします


    shin@debian:~/dev$ cd node/
    shin@debian:~/dev/node$ git checkout v0.4.11
    Note: checking out 'v0.4.11'.

    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by performing another checkout.

    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -b with the checkout command again. Example:

      git checkout -b new_branch_name

    HEAD is now at a745d19... Bump version to v0.4.11

    $HOME/local/nodeへのインストールを指示しconfigureします。


    shin@debian:~/dev/node$ export JOBS=2
    shin@debian:~/dev/node$ mkdir ~/local
    shin@debian:~/dev/node$ ./configure --prefix=$HOME/local/node
    Checking for program g++ or c++          : /usr/bin/g++ 
    Checking for program cpp                 : /usr/bin/cpp 
    Checking for program ar                  : /usr/bin/ar 
    Checking for program ranlib              : /usr/bin/ranlib 
    Checking for g++                         : ok  
    Checking for program gcc or cc           : /usr/bin/gcc 
    Checking for program ar                  : /usr/bin/ar 
    Checking for program ranlib              : /usr/bin/ranlib 
    Checking for gcc                         : ok  
    Checking for library dl                  : yes 
    Checking for openssl                     : yes 
    Checking for library util                : yes 
    Checking for library rt                  : yes 
    --- libeio ---
    Checking for library pthread             : yes 
    Checking for function pthread_create     : yes 
    Checking for function pthread_atfork     : yes 
    Checking for futimes(2)                  : yes 
    Checking for readahead(2)                : yes 
    Checking for fdatasync(2)                : yes 
    Checking for pread(2) and pwrite(2)      : yes 
    Checking for sendfile(2)                 : yes 
    Checking for sync_file_range(2)          : yes 
    --- libev ---
    Checking for header sys/inotify.h        : yes 
    Checking for function inotify_init       : yes 
    Checking for header sys/epoll.h          : yes 
    Checking for function epoll_ctl          : yes 
    Checking for header port.h               : not found 
    Checking for header poll.h               : yes 
    Checking for function poll               : yes 
    Checking for header ['sys/types.h', 'sys/event.h'] : not found 
    Checking for header sys/queue.h                    : yes 
    Checking for function kqueue                       : not found 
    Checking for header sys/select.h                   : yes 
    Checking for function select                       : yes 
    Checking for header sys/eventfd.h                  : yes 
    Checking for function eventfd                      : yes 
    Checking for SYS_clock_gettime                     : yes 
    Checking for library rt                            : yes 
    Checking for function clock_gettime                : yes 
    Checking for function nanosleep                    : yes 
    Checking for function ceil                         : yes 
    Checking for fdatasync(2) with c++                 : yes 
    'configure' finished successfully (4.283s)

     あとは、まけまけインストール

    shin@debian:~/dev/node$ make
    shin@debian:~/dev/node$ make install

    最後に.profileにPATHを設定します

    shin@debian:~$ echo 'export PATH=$HOME/local/node/bin:$PATH' >> ~/.profile
    shin@debian:~$ echo 'export NODE_PATH=$HOME/local/node:$HOME/local/node/lib/node_modules' >> ~/.profile
    shin@debian:~$ source ~/.profile

    インストールはこれで終わりです