通用联接框架(GCF)连接类型使用总结
2007-12-23 12:40:20 来源:WEB开发网通用连接框架(Generic Connection Framework,GCF)是在J2ME平台中广泛使用的用于联网和IO处理的类。本文主要讲述GCF的关键特性,然后把各种连接的关键信息总结为列表的形式,供开发者在使用GCF的时候参考。
使用GCF建立连接的方式类似下面程序片断:
...
import java.lang.String;
import javax.microedition.io.*;
static Connection Connector.open( String name );
...
我们关注的主要是Connection和name参数,下面解释什么是name?什么是Connection。Name实际上是与Connection关联的URI。形式如:
scheme : [address] [params]
其中scheme是协议的名称,比如http;address是指定协议的连接端点;params是一些可选的参数,形式类似于language=Chinese。我们给出一个例子:http://www.j2medev.com/servlet?hello=world。
在介绍Connection之前,我们有必要说明一下MIDP规范中说明的一个问题,那就是设备支持什么协议。在MIDP规范中,协议支持的分类共三种:必须,推荐和可选。http和https是MIDP规范要求设备必须支持的,其他的协议都列入了推荐和可选类别中。在其他的可选包中通常也会用到GCF的扩展,比如WMA使用了MessageConnection,JSR75使用了FileConnection。我们可以通过查询系统属性的方式来检查设备是否支持相关的协议连接。比如,我们可能使用下面的方法查询设备对jsr75的支持情况:
...
import java.lang.System;
import java.lang.String;
String name = "microedition.io.file.FileConnection.version";
String value = System.getPRoperty( name );
Boolean hasFile;
if ( ( value != null ) && value.equals( "1.0" ) )
hasFile = true;
else
hasFile = false;
...
在使用GCF的时候,我们首先应该根据项目需求选择适当的Connection。关于Connection的层次结构可以从下图清晰的了解到,
下面的部分总结了GCF支持的连接类型,这些信息按照接口、BNF语法和例子来分组,供开发者参考:
MIDP2.0(JSR 118)
串口连接
支持: 可选
接口: javax.microedition.io.CommConnection,扩展了StreamConnection
BNF:
url ::== "comm:" port_id *(option_list)
port_id ::== 1*(characters)
option_list ::== *(baudrate bitsperchar stopbits parity blocking
autocts autorts)
baudrate ::== ";baudrate=" digits
bitsperchar ::== ";bitsperchar=" bit_value
bit_value ::== "7" "8"
stopbits ::== ";stopbits=" stop_value
stop_value ::== "1" "2"
parity ::== ";parity=" parity_value
parity_value ::== "even" "odd" "none"
blocking ::== ";blocking=" on_off
autocts ::== ";autocts=" on_off
autorts ::== ";autorts=" on_off
on_off ::== "on" "off"
例子:
"comm:0;bitsperchar=8;stopbits=1;parity=none"
"IR1"
可以使用下面的方法列出port_id的值:
import java.lang.System;
import java.lang.String;
String ports = System.getProperty( "microedition.commports" );
--------------------------------------------------------------------------------
HTTP连接
支持: 必须
接口: javax.microedition.io.HttpConnection ,扩展了ContentConnection
定义: IETF RFC 1738, Section 3.3 HTTP
例子:
"http://java.sun.com"
"http://www.sun.com:80"
--------------------------------------------------------------------------------
HTTPS Connection
支持: 必须
接口: javax.microedition.io.HttpsConnection,扩展了HttpConnection
例子:
"https://java.sun.com"
"https://www.sun.com:443"
--------------------------------------------------------------------------------
安全连接
支持: 推荐
接口: javax.microedition.io.SecureConnection ,扩展了SocketConnection
BNF:
url ::== "ssl://" hostport
hostport ::== host ":" port
host ::== host name or ip address
port ::== numeric port number
例子:
"ssl://www.sun.com"
"ssl://java.sun.com:443"
注意: 只支持客户端模式,设备上不支持服务器模式
--------------------------------------------------------------------------------
Server Socket Connection
支持: 推荐
接口: javax.microedition.io.ServerSocketConnection ,扩展了StreamConnectionNotifier
BNF:
url ::== "socket://" "socket://" hostport
hostport ::== host ":" port
host ::== omitted for inbound connections
port ::== numeric port number (omitted for system assigned port)
例子:
"socket://:25"
"socket://:17"
"socket://"
--------------------------------------------------------------------------------
Socket Connection
支持: 推荐
接口: javax.microedition.io.SocketConnection ,扩展了StreamConnection
BNF:
url ::== "socket://" hostport
hostport ::== host ":" port
host ::== host name or IP address (omitted for inbound connections)
port ::== numeric port number
例子:
"socket://developer.sun.com/techtopics/mobility:80"
"socket://www.sun.com:53"
--------------------------------------------------------------------------------
Datagram Connection
支持: 推荐
接口: javax.microedition.io.UDPDatagram.Connection ,扩展了DatagramConnection
BNF:
url ::== "datagram://" "datagram://" hostport
hostport ::== host ":" port
host ::== host name or IP address (omitted for inbound connections)
port ::== numeric port number (omitted for system assigned port)
例子:
"datagram://" - server with system assigned port
"datagram://:7" - server with specified port
"datagram://java.sun.com:80" - client
JSR 75 PDA Optional Packages
查询方式: System.getProperty( "microedition.io.file.FileConnection.version" ).equals( "1.0" );
接口: javax.microedition.io.file.FileConnection ,扩展了StreamConnection
定义: IETF RFC 1738, Section 3.10 Files
例子:
"file:///CFCard/music/thewayup/opening.mp3"
"file:///RWM/photos/emma.jpg"
"file://localhost/RWM/photos/thekids.jpg"
JSR 82 Java APIs for Bluetooth Wireless Technology, version
是否支持蓝牙只能通过尝试的方式得到了。
接口: javax.microedition.io.StreamConnection
接口: javax.microedition.io.StreamConnectionNotifier
BNF:
url ::== srvString cliString
srvString ::== "BTspp" ":" "//" srvHost 0*5(srvParams)
cliString ::== "btspp" ":" "//" cliHost 0*3(cliParams)
cliHost ::== btAddress ":" channel
srvHost ::== "localhost" ":" uuid
channel ::== %d1-30
uuid ::== 1*32(HEXDIG)
bool ::== "true" "false"
btAddress ::== 12*12(HEXDIG)
text ::== 1*( ALPHA DIGIT " " "-" "_" )
HEXDIG ::== "A".."F" "a".."f" "0".."9"
ALPHA ::== "A".."Z" "a".."z"
DIGIT ::== "0".."9"
name ::== ";name=" text
master ::== ";master=" bool
encrypt ::== ";encrypt=" bool
authorize ::== ";authorize=" bool
authenticate ::== ";authenticate=" bool
cliParams ::== master encrypt authenticate
srvParams ::== name master encrypt authorize authenticate
例子:
"btspp://
"btspp://localhost:8CDF20D
--------------------------------------------------------------------------------
Logical Link Control and Adaptation Profile (L2CAP)
接口: javax.bluetooth.L2CAPConnection ,扩展了Connection
接口: javax.bluetooth.L2CAPConnectionNotifier ,扩展了Connection
BNF:
url ::== srvString cliString
srvString ::== "btl2cap" ":" "//" srvHost 0*7(srvParams)
cliString ::== "btl2cap" ":" "//" cliHost 0*5(cliParams)
cliHost ::== address ":' psm
srvHost ::== "localhost" ":" uuid
psm ::== 4*4(HEXDIG)
receiveMTU ::== ";receiveMTU=" 1*(DIGIT)
transmitMTU ::== ";transmitMTU=" 1*(DIGIT)
cliParams ::== master encrypt authenticate receiveMTU transmitMTU
srvParams ::== name master encrypt authorize authenticate
receiveMTU transmitMTU
例子:
"btl2cap://
"btl2cap://localhost:8CDF20D
--------------------------------------------------------------------------------
Object Exchange Protocol (OBEX)
接口: javax.obex.sessionNotifier ,扩展了Connection
接口: javax.obex.ClientSession ,扩展了Connection
接口: javax.obex.Operation ,扩展了ContentConnection
BNF:
url ::== tcpObex irdaObex btObex
btObex ::== btSrvString btCliString
tcpObex ::== tcpSrvString tcpCliString
irdaObex ::== irdaSrvString irdaCliString
tcpCliString ::== "tcpobex" ":" "//" tcpHost
tcpSrvString ::== "tcpobex" ":" "//" 0*1(ipPort)
ipPort ::== 1*(DIGIT)
ipAddress ::== 3*3(%d0-255 ".") (%d0-255)
ipName ::== 1*( hostLabel "." ) topLabel
topLabel ::== ALPHA ALPHA *(alphaNum "-") alphaNum
hostLabel ::== alphaNum alphaNum *(alphaNum "-") alphaNum
tcpHost ::== ipName 0*1(":" ipPort) ipAddress 0*1(colon ipPort)
btSrvString ::== "btgoep" ":" "//" btSrvHost 0*5(btSrvParams)
btCliString ::== "btgoep" ":" "//" btCliHost 0*3(btCliParams)
btCliParams ::== master encrypt authenticate
btSrvParams ::== name master encrypt authorize authenticate
btCliHost ::== btAddress ":" channel
btSrvHost ::== "localhost" ":" uuid
irdaSrvString ::== "irdaobex" ":" "//" irdaSrvHost 0*1(irdaParams)
irdaCliString ::== "irdaobex" ":" "//" irdaCliHost 0*1(irdaParams)
irdaSrvHost ::== "localhost" 0*1("." 1*(DIGIT))
irdaCliHost ::== "discover" 0*1("." 1*(DIGIT))
"addr." 2*8(HEXDIG)
"conn"
"name." 1*(characters)
irdaParams ::== ";ias=" 1*(characters) 0*("," 1*(characters))
characters ::== %d0-255
alphaNum ::== ALPHA DIGIT
例子:
"tcpobex://litespeed:6512"
"tcpobex://:6512"
"btgoep://
"btgoep://localhost:8CDF20D
"irdaobex://discover.08;ias=fax"
"irdaobex://localhost.08;ias=fax,OBEX,OBEX:IrXfer"
JSR 120 Wireless Messaging API (WMA), version 1.1
查询方式: System.getProperty( "wireless.messaging.sms.smsc" ) != null
接口: javax.wireless.messaging.MessageConnection ,扩展了Connection
BNF:
url ::== "sms://" address_part
address_part ::== foreign_host_address local_host_address
local_host_address ::== port_number_part
port_number_part ::== ":" digits
foreign_host_address ::== msisdn msisdn port_number_part
msisdn ::== "+" digits digits
digits ::== DIGIT DIGIT digits
例子:
"sms://+19055551212"
"sms://+14165551212:5009"
"sms://:5009"
--------------------------------------------------------------------------------
GSM Cell Broadcast Adapter
接口: javax.wireless.messaging.MessageConnection ,扩展了Connection
BNF:
url ::== "cbs://" address_part
address_part ::== message_identifier_part
message_identifier_part ::== ":" digits
digits ::== DIGIT DIGIT digits
例子:
"cbs://:5009"
"cbs://:1089"
JSR 205 Wireless Messaging API (WMA), version 2.0
查询方式: System.getProperty( "wireless.messaging.mms.mmsc" ) != null
接口: javax.wireless.messaging.MessageConnection ,扩展了Connection
BNF:
url ::== "mms://" address_part
address_part ::== (e-mail-address device-address shortcode-address
application-id)
device-address ::== general-phone-address [application-id]
general-phone-address ::== (global-telephone-type "/TYPE=PLMN")
(ipv4 "/TYPE=IPv4")
(ipv6 "/TYPE=IPv6")
(escaped value "/TYPE=" address-type)
global-telephone-type ::== "+" *(DIGIT)
ipv6 ::== IETF RFC 1884
ipv4 ::== 1 *DIGIT "." 1*DIGIT "." 1*DIGIT "." 1*DIGIT
application-id ::== ":" [("com" "org" "edu" "gov")] [organization] [*(package-name)] class-name
organization ::== "." *applicationID-symbol
package-name ::== "." *applicationID-symbol
class-name ::== "." *applicationID-symbol
address-type ::== *address-char
address-char ::== *(ALPHA DIGIT "_")
e-mail-address ::== mailbox group
group ::== phrase "." [#mailbox] ";"
phrase ::== *(space Word space)
mailbox ::== addr-spec [phrase] route-addr
route-addr ::== "<" [route] addr-spec ">"
route ::== 1#("@" domain) ":"
addr-spec ::== local-part "@" domain
local-part ::== word *("." word)
domain ::== sub-domain *("." sub-domain)
sub-domain ::== domain-ref domain-literal
domain-ref ::== atom
domain-literal ::== "[" *(dtext quoted-pair) "]"
atom ::== ALPHA DIGIT "!" "#" "$" "%" "'" "*"
"+" "-" "=" "?" "{" "}" "" "~"
"^" "_"
word ::== atom quoted-string
quoted-string ::== '"' (qtext qpair) *endq"
qtext ::== (^'"')["\"]
endq ::== [^"\\"]
space ::== *(" ")
qpair ::== "\\."
shortcode-address ::== shortcode-string
shortcode-string ::== *(ALPHA DIGIT "!" '"' "$" "%" "&"
"/" "(" ")" "+" "*" "." "-"
"/" "=" "< ">" "[" "]" "_"
"^" "?" "{" "}" "'" "~" ";"
applicationID-symbol ::== ALPHA DIGIT "." "_"
例子:
"mms://+14165552112"
"mms://richard@sun.com"
"mms://+16475551212:com.sun.wireless.photo"
"mms://:com.sun.wireless.voicenote"
原文请参考这里
(出处:http://www.cncms.com)
赞助商链接