2007 年 1 月 25 日 20 時 22 分

RFBContext の改造


このアーカイブは同期化されません。 mixi の日記が更新されても、このアーカイブには反映されません。


昨日の考察により、クライアントメッセージのうち、
SetPixelFormat, FixColourMapEntries, SetEncodings は、
通信状態やその後のメッセージに影響を与える。

こういった変化はステートの一種になるため、
通信コンテキストに格納しておくのが好ましい。

そこで、RFBContext を改造し、
追加情報を格納できるようにしておこう。

========== RFBContext.java ==========

package jp.loafer.rfb;

import jp.loafer.rfb.message.ColorEntry;
import jp.loafer.rfb.message.PixelFormat;

/**
* RFB の状態を保持するクラス。
* @author kes
*/
public class RFBContext {

    /**
     * 空の通信コンテキストを作成。
     */
    public RFBContext() {
       
        // 初期値       
        version = 3003;
        format = new PixelFormat(32, 24, true, true,
                0xff, 0xff, 0xff, 16, 8, 0);
        fixedColors = null;
        encodings = new int[] { Encoding.RAW };
    }

    /**
     * 通信中の RFB バージョンを取得。
     * @return RFB バージョン。
     */
    public int getVersion() {
        return version;
    }
    /**
     * 通信中の RFB バージョンを設定。
     * @param version RFB バージョン。
     */
    public void setVersion(int version) {
        this.version = version;
    }


    /**
     * 使用中の画素形式を取得。
     * @return 画素形式。
     */
    public PixelFormat getPixelFormat() {
        return format;
    }
    /**
     * 使用中の画素形式を設定。
     * 固定されたカラーテーブルはクリアされる。
     * @param format 画素形式。
     */
    public void setPixelFormat(PixelFormat format) {
        if (format == null) throw new NullPointerException();
        this.format = format;
        this.fixedColors = null;
    }

    /**
     * 固定されたカラーテーブルを取得。
     * @return 符号化方式。
     */
    public ColorEntry[] getFixedColors() {
        if (fixedColors == null) return null;
        return fixedColors.clone();
    }
    /**
     * 固定されたカラーテーブルを設定。
     * colors 引数は 1 << format.getDepth() で計算される要素数必要であり、
     * 固定されない要素には null が格納されている。
     * @param colors 固定されたカラーテーブル要素。
     */
    public void setFixedColors(ColorEntry[] colors) {
        if (colors != null) {
            if (colors.length != 1 << format.getDepth())
                    throw new IllegalArgumentException("bad color count.");
        }
        this.fixedColors = colors;
    }

    /**
     * 対応している符号化方式を取得。
     * @return 符号化方式の配列。
     */
    public int[] getEncodingTypes() {
        return encodingTypes.clone();
    }
    /**
     * 対応している符号化方式を設定。
     * @param encodingTypes 符号化方式の配列。
     */
    public void setEncodingTypes(int[] encodingTypes) {
        if (encodingTypes == null) throw new NullPointerException();
        this.encodingTypes = encodingTypes;
    }

    /**
     * 指定した符号化方式に対応しているか調べる。
     * @param encoding 調べたい符号化方式。
     * @return 指定した符号化方式に対応していれば true。
     */
    public boolean isEncodingAvailable(int encodingType) {
        if (encodingType == Encoding.RAW) return true; // 常に有効
        for (int e : encodingTypes) {
            if (e == encodingType) return true;
        }
        return false;
    }
   
    // RFB の状態

    // 通信バージョン
    private int version;
   
    // 画素形式
    private PixelFormat format;

    // 固定されたカラーテーブル
    private ColorEntry[] fixedColors;

    // クライアントの扱える符号化方式
    private int[] encodingTypes;

}

========== end of RFBContext.java ==========

今はまだ解説していないが、サーバが送るメッセージには、
この画素形式や符号化方式によって変化するものがあるため、
RFBContext に情報を持たせておく事が重要になる。

RFBContext は、Message インタフェースの、
read や write メソッドに渡されるので、
メッセージ系クラスの読み書きを行う際に、
これらコンテキストの情報を利用する事ができるようになる。



Copyright (c) 1994-2007 Project Loafer. All rights reserved.