module CTI::CTIP2
- Version
-
$Id: CTIP2.rb 902 2013-04-23 05:07:04Z miyabe $
通信プロトコルに関連する各操作です。
このモジュールに所属するメソッドは、 IO に対するものです。
Constants
- CTI_BUFFER_SIZE
-
パケットの送信に使うバッファのサイズです。
- FRG_MEM_SIZE
-
メモリ上のフラグメントの最大サイズです。
フラグメントがこの大きさを超えるとディスクに書き込みます。
- ON_MEMORY
-
メモリ上に置かれるデータの最大サイズです。
メモリ上のデータがこのサイズを超えると、FRG_MEM_SIZEとは無関係にディスクに書き込まれます。
- REQ_ABORT
- REQ_CLIENT_RESOURCE
- REQ_CLOSE
- REQ_CONTINUOUS
- REQ_DATA
- REQ_EOF
- REQ_JOIN
- REQ_MISSING_RESOURCE
- REQ_PROPERTY
- REQ_RESET
- REQ_SERVER_INFO
- REQ_SERVER_MAIN
- REQ_START_MAIN
- REQ_START_RESOURCE
- RES_ABORT
- RES_ADD_BLOCK
- RES_BLOCK_DATA
- RES_CLOSE_BLOCK
- RES_DATA
- RES_EOF
- RES_INSERT_BLOCK
- RES_MAIN_LENGTH
- RES_MAIN_READ
- RES_MESSAGE
- RES_NEXT
- RES_RESOURCE_REQUEST
- RES_START_DATA
- SEGMENT_SIZE
-
一時ファイルのセグメントサイズです。
Public Instance Methods
Source
# File src/code/CTI/CTIP2/CTIP2.rb 71 def cti_connect(encoding) 72 self.write("CTIP/2.0 #{encoding}\n") 73 end
セッションを開始します。
- encoding
-
通信に用いるエンコーディング
Source
# File src/code/CTI/CTIP2/Utils.rb 99 def read_byte 100 b = self.read(1) 101 b = b.unpack('C') 102 return b[0] 103 end
8ビット数値を読み込みます。
- 戻り値
-
数値、エラーであればfalse
Source
# File src/code/CTI/CTIP2/Utils.rb 111 def read_bytes 112 b = self.read(2) 113 a = b.unpack('n') 114 len = a[0] 115 b = self.read(len); 116 return b; 117 end
16ビットビッグインディアン数値を読み込み、そのバイト数だけバイト列を読み込みます。
- 戻り値
-
バイト列、エラーであればfalse
Source
# File src/code/CTI/CTIP2/Utils.rb 65 def read_int 66 b = self.read(4) 67 a = b.unpack('N') 68 return a[0] 69 end
32ビットビッグインディアン数値を読み込みます。
- 戻り値
-
数値、エラーであればfalse
Source
# File src/code/CTI/CTIP2/Utils.rb 77 def read_long 78 b = self.read(8) 79 a = b.unpack('NN') 80 h = a[0] 81 l = a[1] 82 if h >> 31 != 0 then 83 h ^= 0xFFFFFFFF 84 l ^= 0xFFFFFFFF 85 b = (h << 32) | l 86 b = -(b + 1) 87 else 88 b = (h << 32) | l 89 end 90 return b; 91 end
64ビットビッグインディアン数値を読み込みます。
- 戻り値
-
数値、エラーであればfalse
Source
# File src/code/CTI/CTIP2/Utils.rb 53 def read_short 54 b = self.read(2) 55 a = b.unpack('n') 56 return a[0] 57 end
16ビットビッグインディアン数値を読み込みます。
- 戻り値
-
数値、エラーであればfalse
Source
# File src/code/CTI/CTIP2/CTIP2.rb 148 def req_abort(mode) 149 payload = 2 150 self.write_int(payload) 151 self.write_byte(CTIP2::REQ_ABORT) 152 self.write_byte(mode) 153 self.flush 154 end
変換処理の中断を要求します。
- mode
-
0=生成済みのデータを出力して中断, 1=即時中断
Source
# File src/code/CTI/CTIP2/CTIP2.rb 95 def req_client_resource(mode) 96 payload = 2 97 self.write_int(payload) 98 self.write_byte(CTIP2::REQ_CLIENT_RESOURCE) 99 self.write_byte(mode) 100 self.flush 101 end
サーバーからクライアントのリソースを要求するモードを切り替えます。
- mode
-
0=off, 1=on
Source
# File src/code/CTI/CTIP2/CTIP2.rb 266 def req_close 267 payload = 1 268 self.write_int(payload) 269 self.write_byte(CTIP2::REQ_CLOSE) 270 self.flush 271 end
通信を終了します。
Source
# File src/code/CTI/CTIP2/CTIP2.rb 109 def req_continuous(mode) 110 payload = 2 111 self.write_int(payload) 112 self.write_byte(CTIP2::REQ_CONTINUOUS) 113 self.write_byte(mode) 114 self.flush 115 end
複数の結果を結合するモードを切り替えます。
- mode
-
0=off, 1=on
Source
# File src/code/CTI/CTIP2/CTIP2.rb 171 def req_eof 172 payload = 1 173 self.write_int(payload) 174 self.write_byte(CTIP2::REQ_EOF) 175 self.flush 176 end
終了を通知します。
Source
# File src/code/CTI/CTIP2/CTIP2.rb 160 def req_join 161 payload = 1 162 self.write_int(payload) 163 self.write_byte(CTIP2::REQ_JOIN) 164 self.flush 165 end
変換結果を結合します。
Source
# File src/code/CTI/CTIP2/CTIP2.rb 123 def req_missing_resource(uri) 124 payload = 1 + 2 + uri.bytesize 125 self.write_int(payload) 126 self.write_byte(CTIP2::REQ_MISSING_RESOURCE) 127 self.write_bytes(uri) 128 self.flush 129 end
リソースの不存在を通知します。
- uri
-
URI
Source
# File src/code/CTI/CTIP2/CTIP2.rb 185 def req_property(name, value) 186 payload = name.bytesize + value.bytesize + 5 187 self.write_int(payload) 188 self.write_byte(CTIP2::REQ_PROPERTY) 189 self.write_bytes(name) 190 self.write_bytes(value) 191 self.flush 192 end
プロパティを送ります。
- name
-
名前
- value
-
値
Source
# File src/code/CTI/CTIP2/CTIP2.rb 135 def req_reset 136 payload = 1 137 self.write_int(payload) 138 self.write_byte(CTIP2::REQ_RESET) 139 self.flush 140 end
状態のリセットを要求します。
Source
# File src/code/CTI/CTIP2/CTIP2.rb 217 def req_resource(uri, mime_type = 'text/css', encoding = '', length = -1) 218 payload = uri.bytesize + mime_type.bytesize + encoding.bytesize + 7 + 8 219 self.write_int(payload) 220 self.write_byte(CTIP2::REQ_START_RESOURCE) 221 self.write_bytes(uri) 222 self.write_bytes(mime_type) 223 self.write_bytes(encoding) 224 self.write_long(length) 225 self.flush 226 end
リソースの開始を通知します。
- uri
-
URI
- mime_type
-
MIME型
- encoding
-
エンコーディング
- length
-
長さ
Source
# File src/code/CTI/CTIP2/CTIP2.rb 81 def req_server_info(uri) 82 payload = 1 + 2 + uri.bytesize 83 self.write_int(payload) 84 self.write_byte(CTIP2::REQ_SERVER_INFO) 85 self.write_bytes(uri) 86 self.flush 87 end
サーバー情報を要求します。
- uri
-
URI
Source
# File src/code/CTI/CTIP2/CTIP2.rb 200 def req_server_main(uri) 201 payload = uri.bytesize + 3 202 self.write_int(payload) 203 self.write_byte(CTIP2::REQ_SERVER_MAIN) 204 self.write_bytes(uri) 205 self.flush 206 end
サーバー側データの変換を要求します。
- uri
-
URI
Source
# File src/code/CTI/CTIP2/CTIP2.rb 237 def req_start_main(uri, mime_type = 'text/html', encoding = '', length = -1) 238 payload = uri.bytesize + mime_type.bytesize + encoding.bytesize + 7 + 8 239 self.write_int(payload) 240 self.write_byte(CTIP2::REQ_START_MAIN) 241 self.write_bytes(uri) 242 self.write_bytes(mime_type) 243 self.write_bytes(encoding) 244 self.write_long(length) 245 self.flush 246 end
本体の開始を通知します。
- uri
-
URI
- mime_type
-
MIME型
- encoding
-
エンコーディング
- length
-
長さ
Source
# File src/code/CTI/CTIP2/CTIP2.rb 254 def req_write(b) 255 payload = b.bytesize + 1 256 self.write_int(payload) 257 self.write_byte(CTIP2::REQ_DATA) 258 self.write(b) 259 self.flush 260 end
データを送ります。
- b
-
データ
Source
# File src/code/CTI/CTIP2/CTIP2.rb 289 def res_next 290 payload = self.read_int 291 type = self.read_byte 292 293 case type 294 when CTIP2::RES_ADD_BLOCK, CTIP2::RES_EOF, CTIP2::RES_NEXT 295 return { 296 'type' => type, 297 } 298 299 when CTIP2::RES_START_DATA 300 uri = self.read_bytes 301 mime_type = self.read_bytes 302 encoding = self.read_bytes 303 length = self.read_long 304 return { 305 'type' => type, 306 'uri' => uri, 307 'mime_type' => mime_type, 308 'encoding' => encoding, 309 'length' => length 310 } 311 312 when CTIP2::RES_MAIN_LENGTH, CTIP2::RES_MAIN_READ 313 length = self.read_long 314 return { 315 'type' => type, 316 'length' => length 317 } 318 319 when CTIP2::RES_INSERT_BLOCK, CTIP2::RES_CLOSE_BLOCK 320 block_id = self.read_int 321 return { 322 'type' => type, 323 'block_id' => block_id 324 } 325 326 when CTIP2::RES_MESSAGE 327 code = self.read_short 328 payload -= 1 + 2 329 message = self.read_bytes 330 payload -= 2 + message.bytesize 331 args = [] 332 while payload > 0 333 arg = self.read_bytes 334 payload -= 2 + arg.bytesize 335 args << arg 336 end 337 return { 338 'type' => type, 339 'code' => code, 340 'message' => message, 341 'args' => args 342 } 343 344 when CTIP2::RES_BLOCK_DATA 345 length = payload - 5 346 block_id = self.read_int 347 bytes = self.read(length) 348 return { 349 'type' => type, 350 'block_id' => block_id, 351 'bytes' => bytes, 352 'length' => length 353 } 354 355 when CTIP2::RES_DATA 356 length = payload - 1 357 bytes = self.read(length) 358 return { 359 'type' => type, 360 'bytes' => bytes, 361 'length' => length 362 } 363 364 when CTIP2::RES_RESOURCE_REQUEST 365 uri = self.read_bytes 366 return { 367 'type' => type, 368 'uri' => uri 369 } 370 371 when CTIP2::RES_ABORT 372 mode = self.read_byte 373 code = self.read_short 374 payload -= 1 + 1 + 2 375 message = self.read_bytes 376 payload -= 2 + message.bytesize 377 args = [] 378 while payload > 0 379 arg = self.read_bytes 380 payload -= 2 + arg.bytesize 381 args << arg 382 end 383 return { 384 'type' => type, 385 'mode' => mode, 386 'code' => code, 387 'message' => message, 388 'args' => args 389 } 390 391 else 392 raise "Bad response type:#{type}" 393 end 394 end
次のレスポンスを取得します。
結果ハッシュには次のデータが含まれます。
-
‘type’ レスポンスタイプ
-
‘anchorId’ 挿入する場所の直後のフラグメントID
-
‘level’ エラーレベル
-
‘error’ エラーメッセージ
-
‘id’ 断片ID
-
‘progress’ 処理済バイト数
-
‘bytes’ データのバイト列
- 戻り値
-
レスポンス
Source
# File src/code/CTI/CTIP2/Utils.rb 31 def write_byte(b) 32 self.write([b].pack('C')) 33 end
8ビット数値を書き出します。
- a
-
数値
- 戻り値
-
書き込んだバイト数
Source
# File src/code/CTI/CTIP2/Utils.rb 42 def write_bytes(b) 43 self.write([b.bytesize].pack('n')) 44 self.write(b) 45 end
バイト数を16ビットビッグインディアンで書き出した後、バイト列を書き出します。
- b
-
バイト列
- 戻り値
-
書き込んだバイト数
Source
# File src/code/CTI/CTIP2/Utils.rb 9 def write_int(a) 10 self.write([a].pack('N')) 11 end
32ビット数値をビッグインディアンで書き出します。
- a
-
数値
- 戻り値
-
書き込んだバイト数
Source
# File src/code/CTI/CTIP2/Utils.rb 20 def write_long(a) 21 self.write([a >> 32, a & 0xFFFFFFFF].pack('NN')) 22 end
64ビット数値をビッグインディアンで書き出します。
- a
-
数値
- 戻り値
-
書き込んだバイト数