Javaな日々

NO JAVA, NO LIFE.

UIGestureRecognizer を強制的に終了させる方法

UIImageView のサブクラスに UIGestureRecognizer を付けて,とあるジェスチャーを検出した際に他のクラスにデリゲートメソッドを用いて処理をさせようとすると,UIGestureRecognizer が操作を受け付けない状況になってしまうことがある.
こういう場合に強制的にジェスチャーを終了させる (ExampleGestureRecognizer.state = UIGestureRecognizerStateEnded) する方法.

#import <UIKit/UIGestureRecognizerSubclass.h>

UIKit/UIGestureRecognizerSubclass.h をインポートするだけで ExampleGestureRecognizer.state が書き換え可能になってしまいます!

割と強引な手法なのでバグの原因になるかもしれませんが,メモしておきます.

LaTeX でどうしても Cannot determine size of graphic in ./example.1.png (no BoundingBo x). エラーが出たら

$ ebb ./example.1.png

として,LaTeX 内で

\includegraphics{./example.1.png}

と記述し,コンパイルしたのに,

Cannot determine size of graphic in ./example.1.png (no BoundingBo x).

とエラーが出てしまうのは,ファイル名が問題だということがあります.

勘がいい人はすぐ気づくと思いますが,この場合はファイル名のドットが原因です.
一般的なファイル名にして画像を扱った方がよさそうです.

Raspberry Pi をセットアップ (OS のインストールと設定)

f:id:yksris:20130124233455j:plain

巷で話題の Raspberry Pi,入手しました.
昔,Java チップが欲しくて,諦めて BeagleBoard を買おうか悩んでいた時期がありましたが,いいタイミングで RPi が発売されたので買ってみました.RS のオンラインショップ で¥2,950 というお手頃価格.買うときはケースも合わせて買うといいと思います.

実際に使ってみると,まあこんなもんかという程度.700MHz の ARM に 512MB のメモリじゃ, Javaアプリケーションサーバーは到底常時稼動させておけないw そして BeagleBoard みたいにマイコンとしては開発されていないので,GPIO にちょっと物足りなさを感じる.家庭内のサーバーとかおもちゃ程度に使うなら魅力的な一品ですね.

早速 Raspbian という RPi 向けに作られた Debian をインストールしてみます.

環境

必要なもの

  • HDMI ケーブル
  • USB - Micro USB ケーブル
  • USB キーボード
  • (USB マウス)
  • SD カード (2GB 以上)
  • 電源 (cheero Power Plus 2 ←USB ケーブルも付属していて RPi の電源としても使えるのでオススメ)
  • LAN ケーブル

手順

  1. RPi の公式サイト から Raspbian をダウンロード.
    将来的に JavaVM を動かしたいなら,JavaVM が RPi のような Hard-float に対応していないので,Soft-float の Raspbian をダウンロードしてきましょう.
  2. SD カードを PC に接続して OS を焼く.
    df コマンドで利用可能なファイルシステム一覧が表示されるので SD カードの項目を見つける (自分の環境では disk1s1 でした).
    diskutill コマンドで SD カード (自分の環境では disk1s1) を一旦アンマウント.
    dd コマンドで Raspbean の img ファイルと書き込み先を指定して焼く (書き込み先は先のコマンドで確認した SD カードの項目が disk1s1 だった場合,頭に r を付けて最後の s1 をカットした "rdisk1" となる).
    diskutill コマンドで SD カードをアンマウント,取り外して RPi へ接続.

    $ df -h
    Filesystem      Size   Used  Avail Capacity  iused    ifree %iused  Mounted on
    /dev/disk0s2   465Gi   98Gi  366Gi    22% 25857737 96029005   21%   /
    devfs          189Ki  189Ki    0Bi   100%      652        0  100%   /dev
    map -hosts       0Bi    0Bi    0Bi   100%        0        0  100%   /net
    map auto_home    0Bi    0Bi    0Bi   100%        0        0  100%   /home
    /dev/disk1s1    56Mi   17Mi   39Mi    30%      512        0  100%   /Volumes/Untitled
    
    $ diskutil unmount /dev/disk1s1
    Volume (null) on disk1s1 unmounted
    
    $ sudo dd if=~/2012-12-16-wheezy-raspbian.img of=/dev/rdisk1 bs=1m
    1850+0 records in
    1850+0 records out
    1939865600 bytes transferred in 156.012604 secs (12434031 bytes/sec)
    
    $ diskutil unmount /dev/disk1s1
    Volume (null) on disk1s1 unmounted

  3. HDMI ケーブル,USB キーボード,LANケーブルを接続して,最後に電源ケーブル (MicroUSB 端子) を接続すると OS がブートされる.
  4. [expand_rootfs] で SD カードの領域が使えるように.
    [Configure keyboard] は普通の JIS キーボードであれば [Generic 105-key(Intl) PC] → [Other] → [Japanese] → [Japanese] → [The default for the keyboard layout] → [No compose key] と設定.
    [change_locale] は [en_GB.UTF-8 UTF-8] と [ja_JP.UTF-8 UTF-8] をチェック,デフォルトは [en_GB.UTF-8] にしておく (日本語フォントが入ってなく,文字化けるため).
    [change_timezone] は [Asia] → [Tokyo].
    [ssh] は [Enable] にしておく.

    [Finish] で再起動する.

  5. SD カードの領域の割り当てに時間がかかる.10 分くらいするとログイン画面になるのでユーザー名は "pi",パスワードは "raspberry" でログイン.
    startx コマンドを実行すると LXDE が立ち上がる.

    f:id:yksris:20130125002833p:plain

    Web ブラウザや Scratch (さすが教育向けに開発されたボードだけある) がインストールされているが,画像の右下が示しているように,GUI の常用は難しいほど重い.
    以上.

Java 脳な人間が C/C++ を扱う時の注意

ICPC という大学対象のプログラミングコンテストを知って,競技プログラミング用に C++ でもはじめるかーと勉強していたらハマった.

この仕様になかなか慣れないので,ICPC に出る時は主のパッケージをまとめて Java で出ようかなぁと思う.
(Java API Doc は試合の中で読んでもいいらしい.)

void func(int x[]){
    ;
}

int main(){
    int array[5] = {0};
    func(array);

    return 0;
}

引数で配列を渡そうと,C/C++ でこのようなプログラムを書いた時,実は func 関数の中で x はポインタ変数となる.従って,func 関数の中で配列を操作すれば main 関数の配列の値が直接操作されてしまう.
(Java であれば func 関数の中で x は,渡された配列のコピーである.)

引数の挙動として "func(int n)" などと書いたときは,引数で渡された変数のコピーが n となる.配列の場合はこのような挙動になる.

C/C++ において,以下の書き方はすべて同義らしい.

int func(int x[5]);
int func(int x[]);
int func(int *x);

従って,C/C++ で配列を操作する関数の引数を書くときは,明示的にポインタを示した方が分かりやすくて良いのかもしれない.

void func(int *x){
    ;
}

int main(){
    int array[5] = {0};
    func(array);

    return 0;
}

C++ を使うなら,配列ではなく Vector を使うのが一般的なようです.

// そういえば C 言語の講義で文字列を char の配列で実装したときに,「配列名はアドレスを返すから scanf で文字列を取得する時は引数に & 演算子がいらない」と習っていた….

さくら VPS (CentOS 6.3) に LAMP を構築する - PHP インストール

  1. PHP をインストール

    [admin@www****** ~]$ sudo yum -y install php php-mbstring php-mysql

  2. php.ini の設定
    PHP の設定ファイル (php.ini) を以下のように書き換える.

    [admin@www****** ~]$ sudo vi /etc/php.ini

    • short_open_tag = Off
      ↓
      short_open_tag = On 

    • expose_php = On
      ↓
      expose_php = Off

    • max_execution_time = 30
      ↓
      max_execution_time = 300

    • error_reporting  =  E_ALL
      ↓
      error_reporting  =  E_ALL & ~E_NOTICE

    • magic_quotes_gpc = On
      ↓
      magic_quotes_gpc = Off

    • ;default_charset = "iso-8859-1"
      ↓
      default_charset = "UTF-8"

    • upload_max_filesize = 2M
      ↓
      upload_max_filesize = 15M

    • ;date.timezone =
      ↓
      date.timezone = Asia/Tokyo

    • ;mbstring.language = Japanese
      ↓
      mbstring.language = Japanese

    • ;mbstring.internal_encoding = EUC-JP
      ↓
      mbstring.internal_encoding = UTF-8

    • ;mbstring.http_input = auto
      ↓
      mbstring.http_input = auto

    • ;mbstring.http_output = SJIS
      ↓
      mbstring.http_output = pass

    • ;mbstring.encoding_translation = Off
      ↓
      mbstring.encoding_translation = On

    • ;mbstring.detect_order = auto
      ↓
      mbstring.detect_order = auto

    • ;mbstring.substitute_character = none;
      ↓
      mbstring.substitute_character = none; 

  3. PHPApache で利用できるように設定.

    [admin@www****** ~]$ sudo vi /etc/httpd/conf/httpd.conf

    • PHP を使えるようにする.

      # If the AddEncoding directives above are commented-out, then you
      # probably should define those extensions to indicate media types:
      #
      AddType application/x-compress .Z
      AddType application/x-gzip .gz .tgz
      AddType application/x-httpd-php .php # 追記

    • index の設定 (PHP を許可する)

      DirectoryIndex index.html index.html.var
      ↓
      DirectoryIndex index.html index.php index.cgi index.html.var

  4. Apache を再起動して設定を適用.

    [admin@www****** ~]$ sudo /etc/rc.d/init.d/httpd restart

さくら VPS (CentOS 6.3) に LAMP を構築する - MySQL インストール

  1. MySQL をインストール
    yum より MySQL をインストール,設定ファイルを編集.

    [admin@www****** ~]$ sudo yum -y install mysql-server
    [admin@www****** ~]$ sudo vi /etc/my.cnf

    デフォルトの文字コードUTF-8 に設定.

    character-set-server = utf8

    MySQL を起動,スタートアップに設定.

    [admin@www****** ~]$ sudo /etc/rc.d/init.d/mysqld start
    MySQL データベースを初期化中:  Installing MySQL system tables...
    OK
    Filling help tables...
    OK
    
    To start mysqld at boot time you have to copy
    support-files/mysql.server to the right place for your system
    
    PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
    To do so, start the server, then issue the following commands:
    
    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h www******.sakura.ne.jp password 'new-password'
    
    Alternatively you can run:
    /usr/bin/mysql_secure_installation
    
    which will also give you the option of removing the test
    databases and anonymous user created by default.  This is
    strongly recommended for production servers.
    
    See the manual for more instructions.
    
    You can start the MySQL daemon with:
    cd /usr ; /usr/bin/mysqld_safe &
    
    You can test the MySQL daemon with mysql-test-run.pl
    cd /usr/mysql-test ; perl mysql-test-run.pl
    
    Please report any problems with the /usr/bin/mysqlbug script!
    
                                                               [  OK  ]
    mysqld を起動中:                                           [  OK  ]
    [admin@www****** ~]$ sudo chkconfig mysqld on

    MySQL の初期設定を行う.

    [admin@www****** ~]$ mysql_secure_installation
    
    
    
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    
    In order to log into MySQL to secure it, we'll need the current
    password for the root user.  If you've just installed MySQL, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.
    
    Enter current password for root (enter for none): 空Enter
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MySQL
    root user without the proper authorisation.
    
    Set root password? [Y/n] 空Enter
    New password: rootのパスワード
    Re-enter new password: 再入力
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    
    By default, a MySQL installation has an anonymous user, allowing anyone
    to log into MySQL without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] 空Enter
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] 空Enter
     ... Success!
    
    By default, MySQL comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] 空Enter
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] 空Enter
     ... Success!
    
    Cleaning up...
    
    
    
    All done!  If you've completed all of the above steps, your MySQL
    installation should now be secure.
    
    Thanks for using MySQL!

  2. MySQL 接続用のポートを開放.

    [admin@www****** ~]$ sudo vi /etc/sysconfig/iptables

    以下を追記します.

    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

さくら VPS (CentOS 6.3) に LAMP を構築する - FTP インストール

  1. FTP サーバーをインストール (vsFTPd)
    サーバーに接続.

    $ ssh admin@XXX.XXX.XXX.XXX -p xxxxx

    yum を使って FTP サーバー (vsFTPd) をインストール.

    [admin@www****** ~]$ sudo yum -y install vsftpd

  2. FTP の設定
    設定ファイルを以下のように編集.

    [admin@www****** ~]$ sudo cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.org
    [admin@www****** ~]$ sudo /etc/vsftpd/vsftpd.conf

    • anonymous によるログインを許可しない

      anonymous_enable=YES
      ↓
      anonymous_enable=NO

    • ログファイルの指定

      #xferlog_file=/var/log/vsftpd.log
      ↓
      xferlog_file=/var/log/vsftpd.log

    • ASCII モードでのダウンロードとアップロードを許可

      #ascii_upload_enable=YES
      #ascii_download_enable=YES
      ↓
      ascii_upload_enable=YES
      ascii_download_enable=YES

    • ホームディレクトリより上の階層に移動できるユーザーをリストで管理

      #chroot_local_user=YES
      #chroot_list_enable=YES
      #chroot_list_file=/etc/vsftpd/chroot_list
      ↓
      chroot_local_user=YES
      chroot_list_enable=YES
      chroot_list_file=/etc/vsftpd/chroot_list

    • ディレクトリごとの削除を許可

      #ls_recurse_enable=YES
      ↓
      ls_recurse_enable=YES

    • TCP Wrapper による通信制限の無効化

      tcp_wrappers=YES
      ↓
      tcp_wrappers=NO

    • ローカルタイムの使用 (最後に追記)

      use_localtime=YES

    • 「/etc/vsftpd.user_list」をアクセス許可リストとして機能させる (最後に追記)

      userlist_deny=NO

    • PASV モードによる接続を許可する設定を追記.

      pasv_enable=YES
      pasv_min_port=10050
      pasv_max_port=10100

    ここまで編集したらファイルを保存してエディタを終了.

    FTPへの接続を許すアカウント名を「/etc/vsftpd.user_list」に追記.

    [admin@www****** ~]$ sudo vi /etc/vsftpd/user_list
    # vsftpd userlist
    # If userlist_deny=NO, only allow users in this file
    # If userlist_deny=YES (default), never allow users in this file, and
    # do not even prompt for a password.
    # Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
    # for users that are denied.
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    news
    uucp
    operator
    games
    nobody
    
    admin

    上位ディレクトリ参照許可ユーザーアカウントを「/etc/vsftpd/chroot_list」に記入.

    [admin@www****** ~]$ sudo vi /etc/vsftpd/chroot_list
    root
    admin

    リストを root ユーザー以外編集不可にする.

    [admin@www****** ~]$ sudo chmod 600 /etc/vsftpd/chroot_list

  3. FTP 接続用のポートを開放.

    [admin@www****** ~]$ sudo vi /etc/sysconfig/iptables

    以下を追記します.

    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 20 -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
    -A FIREWALL -m state --state NEW -m tcp -p tcp --dport 10050:10100 -j ACCEPT

  4. vsFTPd を起動させ,スタートアップ起動に登録.

    [admin@www****** vsftpd]$ sudo /etc/rc.d/init.d/vsftpd start
    vsftpd 用の vsftpd を起動中:                               [  OK  ]
    [admin@www****** vsftpd]$ sudo chkconfig vsftpd on
    [admin@www****** vsftpd]$ chkconfig --list vsftpd
    vsftpd         	0:off	1:off	2:on	3:on	4:on	5:on	6:off

    ローカルから FTP にログインできることを確認.

    $ ftp admin@XXX.XXX.XXX.XXX