android軟件
posted @ 2008-11-20 16:24 LukeW 閱讀(37) | 評論 (0) | 編輯 收藏
Look into it ~present
隨筆 - 32, 文章 - 0, 評論 - 3, 引用 - 0
|
linux設備模型
Linux
2.6內核的一個重要特色是提供了統(tǒng)一的內核設備模型。隨著技術的不斷進步,系統(tǒng)的拓撲結構越來越復雜,對智能電源管理、熱插拔以及plug and
play的支持要求也越來越高,2.4內核已經(jīng)難以滿足這些需求。為適應這種形勢的需要,2.6內核開發(fā)了全新的設備模型。
1. Sysfs文件系統(tǒng) Sysfs文件系統(tǒng)是一個類似于proc文件系統(tǒng)的特殊文件系統(tǒng),用于將系統(tǒng)中的設備組織成層次結構,并向用戶模式程序提供詳細的內核數(shù)據(jù)結構信息。其頂層目錄主要有: Block目錄:包含所有的塊設備 Devices目錄:包含系統(tǒng)所有的設備,并根據(jù)設備掛接的總線類型組織成層次結構 Bus目錄:包含系統(tǒng)中所有的總線類型 Drivers目錄:包括內核中所有已注冊的設備驅動程序 Class目錄:系統(tǒng)中的設備類型(如網(wǎng)卡設備,聲卡設備等) 2. 內核對象機制關鍵數(shù)據(jù)結構 2.1 kobject內核對象 Kobject 是Linux 2.6引入的新的設備管理機制,在內核中由struct kobject表示。通過這個數(shù)據(jù)結構使所有設備在底層都具有統(tǒng)一的接口,kobject提供基本的對象管理,是構成Linux 2.6設備模型的核心結構,它與sysfs文件系統(tǒng)緊密關聯(lián),每個在內核中注冊的kobject對象都對應于sysfs文件系統(tǒng)中的一個目錄。 Kobject結構定義為: struct kobject {
char * k_name; // 指向設備名稱的指針 char name[KOBJ_NAME_LEN]; // 設備名稱 struct kref kref; // 對象引用計數(shù) struct list_head entry; // 掛接到所在kset中去的單元 struct kobject * parent; // 指向父對象的指針 struct kset * kset; // 所屬kset的指針 struct kobj_type * ktype; // 指向其對象類型描述符的指針 struct dentry * dentry; // sysfs文件系統(tǒng)中與該對象對應的文件節(jié)點路徑指針 }; 其中的kref域表示該對象引用的計數(shù),內核通過kref實現(xiàn)對象引用計數(shù)管理,內核提供兩個函數(shù)kobject_get()、kobject_put()分別用于增加和減少引用計數(shù),當引用計數(shù)為0時,所有該對象使用的資源將被釋放。 Ktype 域是一個指向kobj_type結構的指針,表示該對象的類型。Kobj_type數(shù)據(jù)結構包含三個域:一個release方法用于釋放kobject占 用的資源;一個sysfs_ops指針指向sysfs操作表和一個sysfs文件系統(tǒng)缺省屬性列表。Sysfs操作表包括兩個函數(shù)store()和 show()。當用戶態(tài)讀取屬性時,show()函數(shù)被調用,該函數(shù)編碼指定屬性值存入buffer中返回給用戶態(tài);而store()函數(shù)用于存儲用戶態(tài) 傳入的屬性值。 2.2 kset內核對象集合 Kobject通常通過kset組織成層次化的結構,kset是具有相同類型的kobject的集合,在內核中用kset數(shù)據(jù)結構表示,定義為: struct kset {
struct subsystem * subsys; // 所在的subsystem的指針 struct kobj_type * ktype; // 指向該kset對象類型描述符的指針 struct list_head list; // 用于連接該kset中所有kobject的鏈表頭 struct kobject kobj; // 嵌入的kobject struct kset_hotplug_ops * hotplug_ops; // 指向熱插拔操作表的指針 }; 包 含在kset中的所有kobject被組織成一個雙向循環(huán)鏈表,list域正是該鏈表的頭。Ktype域指向一個kobj_type結構,被該 kset中的所有kobject共享,表示這些對象的類型。Kset數(shù)據(jù)結構還內嵌了一個kobject對象(由kobj域表示),所有屬于這個kset 的kobject對象的parent域均指向這個內嵌的對象。此外,kset還依賴于kobj維護引用計數(shù):kset的引用計數(shù)實際上就是內嵌的 kobject對象的引用計數(shù)。 2.3 subsystem內核對象子系統(tǒng) Subsystem是一系列kset的集合,描述系統(tǒng)中某一 類設備子系統(tǒng),如block_subsys表示所有的塊設備,對應于sysfs文件系統(tǒng)中的block目錄。類似的,devices_subsys對應于 sysfs中的devices目錄,描述系統(tǒng)中所有的設備。Subsystem由struct subsystem數(shù)據(jù)結構描述,定義為: struct subsystem {
struct kset kset; // 內嵌的kset對象 struct rw_semaphore rwsem; // 互斥訪問信號量 }; 每 個kset必須屬于某個subsystem,通過設置kset結構中的subsys域指向指定的subsystem可以將一個kset加入到該 subsystem。所有掛接到同一subsystem的kset共享同一個rwsem信號量,用于同步訪問kset中的鏈表。 3. 內核對象機制主要相關函數(shù) 針對內核對象不同層次的數(shù)據(jù)結構,linux 2.6內核定義了一系列操作函數(shù),定義于lib/kobject.c文件中。 3.1 kobject相關函數(shù) void kobject_init(struct kobject * kobj);// kobject初始化函數(shù)。設置kobject引用計數(shù)為1,entry域指向自身,其所屬kset引用計數(shù)加1
int kobject_set_name(struct kobject *kobj, const char *format, ![]() void kobject_cleanup(struct kobject * kobj); void kobject_release(struct kref *kref);// kobject清除函數(shù)。當其引用計數(shù)為0時,釋放對象占用的資源。 struct kobject *kobject_get(struct kobject *kobj);// 將kobj 對象的引用計數(shù)加1,同時返回該對象的指針。 void kobject_put(struct kobject * kobj);// 將kobj對象的引用計數(shù)減1,如果引用計數(shù)降為0,則調用kobject_release()釋放該kobject對象。 int kobject_add(struct kobject * kobj);// 將kobj對象加入Linux設備層次。掛接該kobject對象到kset的list鏈中,增加父目錄各級kobject的引用計數(shù),在其parent指向的目錄下創(chuàng)建文件節(jié)點,并啟動該類型內核對象的hotplug函數(shù)。 int kobject_register(struct kobject * kobj);// kobject注冊函數(shù)。通過調用kobject_init()初始化kobj,再調用kobject_add()完成該內核對象的注冊。 void kobject_del(struct kobject * kobj);// 從Linux設備層次(hierarchy)中刪除kobj對象。 void kobject_unregister(struct kobject * kobj);// kobject注銷函數(shù)。與kobject_register()相反,它首先調用kobject_del從設備層次中刪除該對象,再調用kobject_put()減少該對象的引用計數(shù),如果引用計數(shù)降為0,則釋放該kobject對象。 3.2 kset相關函數(shù) 與kobject 相似,kset_init()完成指定kset的初始化,kset_get()和kset_put()分別增加和減少kset對象的引用計數(shù)。 Kset_add()和kset_del()函數(shù)分別實現(xiàn)將指定keset對象加入設備層次和從其中刪除;kset_register()函數(shù)完成 kset的注冊而kset_unregister()函數(shù)則完成kset的注銷。 3.3 subsystem相關函數(shù) subsystem有一組完成類似的函數(shù),分別是: void subsystem_init(struct subsystem *subsys);
int subsystem_register(struct subsystem *subsys); void subsystem_unregister(struct subsystem *subsys); struct subsystem *subsys_get(struct subsystem *subsys) void subsys_put(struct subsystem *subsys); 4. 設備模型組件 在上述內核對象機制的基礎上,Linux的設備模型建立在幾個關鍵組件的基礎上,下面我們詳細闡述這些組件。 4.1 devices 系統(tǒng)中的任一設備在設備模型中都由一個device對象描述,其對應的數(shù)據(jù)結構struct device定義為: struct device {
struct list_head g_list; struct list_head node; struct list_head bus_list; struct list_head driver_list; struct list_head children; struct device *parent; struct kobject kobj; char bus_id[BUS_ID_SIZE]; struct bus_type *bus; struct device_driver *driver; void *driver_data; /* Several fields omitted */ }; g_list 將該device對象掛接到全局設備鏈表中,所有的device對象都包含在devices_subsys中,并組織成層次結構。Node域將該對象掛接 到其兄弟對象的鏈表中,而bus_list則用于將連接到相同總線上的設備組織成鏈表,driver_list則將同一驅動程序管理的所有設備組織為鏈 表。此外,children域指向該device對象子對象鏈表頭,parent域則指向父對象。Device對象還內嵌一個kobject對象,用于引 用計數(shù)管理并通過它實現(xiàn)設備層次結構。Driver域指向管理該設備的驅動程序對象,而driver_data則是提供給驅動程序的數(shù)據(jù)。Bus域描述設 備所連接的總線類型。 內核提供了相應的函數(shù)用于操作device對象。其中Device_register()函數(shù)將一個新的device對象插 入設備模型,并自動在/sys/devices下創(chuàng)建一個對應的目錄。Device_unregister()完成相反的操作,注銷設備對象。 Get_device()和put_device()分別增加與減少設備對象的引用計數(shù)。通常device結構不單獨使用,而是包含在更大的結構中作為一 個子結構使用,比如描述PCI設備的struct pci_dev,其中的dev域就是一個device對象。 4.2 drivers 系統(tǒng)中的每個驅動程序由一個device_driver對象描述,對應的數(shù)據(jù)結構定義為: struct device_driver {
char *name; // 設備驅動程序的名稱 struct bus_type *bus; // 該驅動所管理的設備掛接的總線類型 struct kobject kobj; // 內嵌kobject對象 struct list_head devices; // 該驅動所管理的設備鏈表頭 int (*probe)(struct device *dev); // 指向設備探測函數(shù),用于探測設備是否可以被該驅動程序管理 int (*remove)(struct device *dev); // 用于刪除設備的函數(shù) /* some fields omitted*/ }; 與device 結構類似,device_driver對象依靠內嵌的kobject對象實現(xiàn)引用計數(shù)管理和層次結構組織。內核提供類似的函數(shù)用于操作 device_driver對象,如get_driver()增加引用計數(shù),driver_register()用于向設備模型插入新的driver對 象,同時在sysfs文件系統(tǒng)中創(chuàng)建對應的目錄。Device_driver()結構還包括幾個函數(shù),用于處理熱拔插、即插即用和電源管理事件。 4.3 buses 系統(tǒng)中總線由struct bus_type描述,定義為: struct bus_type {
char * name; // 總線類型的名稱 struct subsystem subsys; // 與該總線相關的subsystem struct kset drivers; // 所有與該總線相關的驅動程序集合 struct kset devices; // 所有掛接在該總線上的設備集合 struct bus_attribute * bus_attrs; // 總線屬性 struct device_attribute * dev_attrs; // 設備屬性 struct driver_attribute * drv_attrs; // 驅動程序屬性 int (*match)(struct device * dev, struct device_driver * drv); int (*hotplug) (struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size); int (*suspend)(struct device * dev, u32 state); int (*resume)(struct device * dev); }; 每 個bus_type對象都內嵌一個subsystem對象,bus_subsys對象管理系統(tǒng)中所有總線類型的subsystem對象。每個 bus_type對象都對應/sys/bus目錄下的一個子目錄,如PCI總線類型對應于/sys/bus/pci。在每個這樣的目錄下都存在兩個子目 錄:devices和drivers(分別對應于bus_type結構中的devices和drivers域)。其中devices子目錄描述連接在該總 線上的所有設備,而drivers目錄則描述與該總線關聯(lián)的所有驅動程序。與device_driver對象類似,bus_type結構還包含幾個函數(shù) (match()、hotplug()等)處理相應的熱插拔、即插即拔和電源管理事件。 4.4 classes 系統(tǒng)中的設備類由 struct class描述,表示某一類設備。所有的class對象都屬于class_subsys子系統(tǒng),對應于sysfs文件系統(tǒng)中的/sys/class目錄。 每個class對象包括一個class_device鏈表,每個class_device對象表示一個邏輯設備,并通過struct class_device中的dev域(一個指向struct device的指針)關聯(lián)一個物理設備。這樣,一個邏輯設備總是對應于一個物理設備,但是一個物理設備卻可能對應于多個邏輯設備。此外,class結構中 還包括用于處理熱插拔、即插即拔和電源管理事件的函數(shù),這與device對象和driver對象相似。 posted @ 2008-11-12 23:14 LukeW 閱讀(190) | 評論 (0) | 編輯 收藏 位運算
C中的位運算
能夠運用到任何整形的數(shù)據(jù)類型上(包括char, int), 無論有沒有short, long, unsigned這樣的限定詞. 位運算的應用 // 交換指針變量x,y所指向的存儲位置處存放的值
// 優(yōu)勢是不需要第三個位置來臨時存儲另一個值 // 但是這個方法并沒有明顯的性能優(yōu)勢,只是一個智力上的消遣 void inplace_swap(int *x, int *y) { *x = *x ^ *y; *x = *x ^ *y; *x = *x ^ *y; } 位運算常見用法: 實現(xiàn)掩碼運算 ----------------------------------- Java中的位運算 posted @ 2008-11-12 13:53 LukeW 閱讀(135) | 評論 (0) | 編輯 收藏 大端小端 -- 各系統(tǒng)及機器的信息表示
因為現(xiàn)行的計算機都是以八位一個字節(jié)為存儲單位,那么一個16位的整數(shù),也就是C語言中的short,在內存中可能有兩種存儲順序big-
endian和litte-endian.考慮一個short整數(shù)0x3132(0x32是低位,0x31是高位),把它賦值給一個short變量,那么它在內存中的存儲可 能有如下兩種情況: 大端字節(jié)(Big-endian): short變量地址 0x1000 0x1001 ___________________________________ | | | 0x31 | 0x32 |________________ | ________________ 高位字節(jié)在低位字節(jié)的前面,也就是高位在內存地址低的一端.可以這樣記住(大端->高位->在前->正常的邏輯順序) 小端字節(jié)(little-endian): short變量地址 0x1000 0x1001 _____________________________________ | | | 0x32 | 0x31 |________________ | __________________ 低位字節(jié)在高位字節(jié)的前面,也就是低位在內存地址低的一端.可以這樣記住(小端->低位->在前->與正常邏輯順序相反) 可以做個實驗 在windows上下如下程序 #include <stdio.h>
#include <assert.h> void main( void ) { short test; FILE* fp; test = 0x3132; //(31ASIIC碼的’1’,32ASIIC碼的’2’) if ((fp = fopen ("c:""test.txt", "wb")) == NULL) assert(0); fwrite(&test, sizeof(short), 1, fp); fclose(fp); } 然后在C盤下打開test.txt文件,可以看見內容是21,而test等于0x3132,可以明顯的看出來x86的字節(jié)順序是低位在前.如果我們 把這段同樣的代碼放到(big-endian)的機器上執(zhí)行,那么打出來的文件就是12.這在本機中使用是沒有問題的.但當你把這個文件從一 個big- endian機器復制到一個little-endian機器上時就出現(xiàn)問題了. 如上述例子,我們在big-endian的機器上創(chuàng)建了這個test文件,把其復制到little-endian的機器上再用fread讀到一個 short里 面,我們得到的就不再是0x3132而是0x3231了,這樣讀到的數(shù)據(jù)就是錯誤的,所以在兩個字節(jié)順序不一樣的機器上傳輸數(shù)據(jù)時需要特別 小心字節(jié)順序,理解了字節(jié)順序在可以幫助我們寫出移植行更高的代碼. 正因為有字節(jié)順序的差別,所以在網(wǎng)絡傳輸?shù)臅r候定義了所有字節(jié)順序相關的數(shù)據(jù)都使用big-endian,BSD的代碼中定義了四個宏來處 理: #define ntohs(n) //網(wǎng)絡字節(jié)順序到主機字節(jié)順序 n代表net, h代表host, s代表short
#define htons(n) //主機字節(jié)順序到網(wǎng)絡字節(jié)順序 n代表net, h代表host, s代表short #define ntohl(n) //網(wǎng)絡字節(jié)順序到主機字節(jié)順序 n代表net, h代表host, s代表 long #define htonl(n) //主機字節(jié)順序到網(wǎng)絡字節(jié)順序 n代表net, h代表host, s代表 long 舉例說明下這其中一個宏的實現(xiàn): #define sw16(x) "
((short)( " (((short)(x) & (short)0x00ffU) << 8) | " (((short)(x) & (short)0xff00U) >> 8) )) 這里實現(xiàn)的是一個交換兩個字節(jié)順序.其他幾個宏類似. 我們改寫一下上面的程序 #include <stdio.h>
#include <assert.h> #define sw16(x) " ((short)( " (((short)(x) & (short)0x00ffU) << 8) | " (((short)(x) & (short)0xff00U) >> 8) )) // 因為x86下面是低位在前,需要交換一下變成網(wǎng)絡字節(jié)順序 #define htons(x) sw16(x) void main( void ) { short test; FILE* fp; test = htons(0x3132); //(31ASIIC碼的’1’,32ASIIC碼的’2’) if ((fp = fopen ("c:""test.txt", "wb")) == NULL) assert(0); fwrite(&test, sizeof(short), 1, fp); fclose(fp); } 如果在高字節(jié)在前的機器上,由于與網(wǎng)絡字節(jié)順序一致,所以我們什么都不干就可以了,只需要把#define htons(x) sw16(x)宏替 換為 #define htons(x) (x). 一開始我在理解這個問題時,總在想為什么其他數(shù)據(jù)不用交換字節(jié)順序?比如說我們write一塊buffer到文件,最后終于想明白了, 因為都是unsigned char類型一個字節(jié)一個字節(jié)的寫進去,這個順序是固定的,不存在字節(jié)順序的問題. 【用函數(shù)判斷系統(tǒng)是Big Endian還是Little Endian】 bool IsBig_Endian()
//如果字節(jié)序為big-endian,返回true; //反之為 little-endian,返回false { unsigned short test = 0x1122; if(*( (unsigned char*) &test ) == 0x11) return TRUE; else return FALSE; }//IsBig_Endian() 【打印程序對象的字節(jié)表示】 // 可在不同平臺與硬件架構的機器中測試運行這段代碼,理解大端表示和小端表示的不同.
// 這段代碼使用強制類型轉換規(guī)避類型系統(tǒng) #incluede <stdio.h> // 假設每個字節(jié)都是非負整數(shù) typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len) { for(int i = 0; i < len; i++) printf(" %.2x", start[i]); printf("\n"); } void show_int(int x) { show_bytes((byte_pointer) &x, sizeof(int)); } void show_float(float x) { show_bytes((byte_pointer) &x, sizeof(float)); } // 在使用相同編碼(如ASCII編碼)的系統(tǒng)中,字符串字節(jié)表示得到的結果一般是相同的.所以文本數(shù)據(jù)比二進制數(shù)據(jù)具有更強的平臺無關性 void show_string(char *x) { show_bytes((byte_pointer) x, strlen(x)); } void show_pointer(void *x) { show_bytes((byte_pointer) &x, sizeof(void *)); } void test_show_bytes(int val) { int ival = val; float fval = (float)ival; int *pval = &ival; show_int(ival); // 各個機器因為大端表示和小端表示的不同,從而只是字節(jié)順序不同 show_float(fval); // 各個機器因為大端表示和小端表示的不同,從而只是字節(jié)順序不同 show_pointer(pval); // 指針值是與機器相關的(linux,sun使用4字節(jié)地址, 而alpha使用八字節(jié)地址) } --------------------------------------------- 對于如數(shù)值12345在int型和float型時的編碼表示 posted @ 2008-11-12 11:58 LukeW 閱讀(659) | 評論 (0) | 編輯 收藏 j2me 聯(lián)網(wǎng)技術分析總結
基本點:
Generic Connections
In the CLDC Generic Connection framework, all connections are created using
the ![]() Figure 1: Connection interface hierarchy
The Connector.open(String);
Here are a few examples: HTTP Connection Connector.open("http://java.sun.com/developer");
Datagram Connection Connector.open("datagram://address:port#");
Communicate with a Port Connector.open("comm:0;baudrate=9600');
Open Files Connector.open("file:/myFile.txt");
The HttpConnection Interface: The HTTP protocol is a request-response application protocol in which the parameters of the request must be set before the request is sent. The connection could be in one of the three following states:
In the setup state the following methods can be invoked:
For example, suppose you have this connection: HttpConnection c = (HttpConnection)
Connector.open("http://java.sun.com/developer");
Then, you can set the request method to be of type c.setRequestMethod(HttpConnection.POST);
And likewise, you can set some of the HTTP properties. For example, you
can set the c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
If there is a method that requires data to be sent or received from the server, there is a state transition from Setup to Connected. Examples of methods that cause the transition include: openInputStream
openOutputStream openDataInputStream openDataOutputStream getLength getType getDate getExpiration And while the connection is open, some of these methods that may be invoked: getURL
getProtocol getHost getPort ![]() ------------------------------------------------------------ 要注意的問題: 開發(fā)中遇到個很頭疼的問題, 與服務器通信write()數(shù)據(jù)時報java.io.IOException: Couldn't write to socket. 但是服務器抓不到任何包. 一開始懷疑是連建立連接出的問題, 實際上服務器抓不到包也有可能是流在沒有close的時候就已經(jīng)報錯了. 如: conn.open("url"); out = conn.openDataOutputStream();//此時將進行與服務器的三次握手; //但是如果在out.close()之前出現(xiàn)異常服務器是抓不到任何包的 out.write(byte[] bb); 關于這個的解釋應該是流的緩沖機制. 所以正確的寫法應該是捕捉到異常之后在catch塊中把流close掉. 服務器端開發(fā)人員一般會說收不到包所以連接有問題,會把責任推給客戶端,抓住這個證據(jù)在跟服務器端的同事扯皮時將處于有利的位置,嘎嘎. 還有就是要多做小實驗, 注意代碼要規(guī)范嚴格. 發(fā)現(xiàn)的幾個問題: 1. java.io.IOException: Couldn't write to socket 2. java.io.IOException: Couldn't read from socket CMNET聯(lián)網(wǎng)方案: CMWAP聯(lián)網(wǎng)方案: 移動資費頁的處理: 一個通用的HTTP連接封裝: posted @ 2008-11-04 16:22 LukeW 閱讀(380) | 評論 (0) | 編輯 收藏 VIM設置
vim簡介
Vim(Vi Improved) 是一個類似于vi 的文本編輯器,在Vi的基礎上增加了很多新的特性和功能。Vim以其強大的功能和可定制能力 ,成為Linux/Unix環(huán)境下開源的最重要的編輯器之一(另一個是 Emacs),被眾多開發(fā)者所喜愛。筆者此時所用的是最新的7.1版本 。 與大部分其它編輯器不同,進入 Vim 后,缺省狀態(tài)下鍵入的字符并不會插入到所編輯的文件之中。Vim 的模式(mode,可以簡單地 理解為“狀態(tài)”)概念非常重要。需要知道,Vim 有以下幾個模式: 1) 正常(normal)模式,缺省的編輯模式;下面如果不加特殊說明,提到的命令都直接在正常模式下輸入;任何其它模式中都 可以通過鍵盤上的 Esc 鍵回到正常模式。 2) 命令(command)模式,用于執(zhí)行較長、較復雜的命令;在正常模式下輸入“:”(一般命令)、“/”(正向搜索)或“?” (反向搜索)即可進入該模式;命令模式下的命令要輸入回車鍵(Enter)才算完成。 3) 插入(insert)模式,輸入文本時使用;在正常模式下鍵入“i”(insert)或“a”(append)即可進入插入模式(也有另 外一些命令,如“c”,也可以進入插入模式,但這些命令有其它的作用)。 4) 可視(visual)模式,用于選定文本塊;可以在正常模式下輸入“v”(小寫)來按字符選定,輸入“V”(大寫)來按行選 定,或輸入“Ctrl-V”來按方塊選定。 5) 選擇(select)模式,與普通的 Windows 編輯器較為接近的選擇文本塊的方式;在以可視模式和選擇模式之一選定文本塊之 后,可以使用“Ctrl-G”切換到另一模式——該模式很少在 Linux 上使用,本文中就不再介紹了。 ------------------------------------------ 首先 vim ~/.vimrc 打開編輯文件 [轉] 1、VI或VIM的配置文件的路徑 發(fā)現(xiàn)/usr/share/vim/vimrc和/etc/vim/vimrc指向是同一個文件,即vimrc,為vi和vim的配置文件,修改這個文件即可。這個路徑在不同的LINUX版本中可能會不同。 2、配置顏色 配軒VI和VIM的顏色顯示,使它能夠高亮度顯示一些特別的單詞,這對編寫程序很有用。后來打開文件發(fā)現(xiàn)里面其實已經(jīng)有一行了,只是用引號注釋掉了,只需 將syntax on 所在行前面的引號去掉即可?;蛘吡硗猹毩⑻砑右恍校簊yntax on 也行,另外編輯/etc/profile 增加一行alias vi="vim"就行了。 3、設置鼠標 使用VI編輯文本時,如果想修文件中改離光標較遠的位置,這時候想用鼠標定位,可默認情況下,鼠標是不可用的。如果你想使用鼠標,只需另起一行,寫上:set mouse=a 即可 4、設置自動縮進 默認情況下,VI和VIM都沒有縮進的,每換一行,光標均定位在頂格,如果你想自動對齊,請將 set autoindent所在行前面的引號去掉,或者另外添加一行:set autoindent也可。這與配置顏色類似。這樣的設置的結果是按回車后新行與上一行自動對齊。 5、設置tab的縮進量 如果用python編寫程序,那么行縮進量是一個極其重要的概念,同一個塊的縮進量必須相同。你可能喜歡在行前加空格來表示縮進,但每次必須敲多次空格 鍵,如果你喜歡用按TAB鍵來表示縮進,你可能覺得寫的文本或程序不太好看,因為默認情況下,VI和VIM的TAB縮進量比較大(至少六,七個字符)。設 置TAB鍵縮進量的方法:set shiftwidth=3 你也可以選一 個你自己喜歡的縮進量,比如2,或4. ---------------------------------------------- 首先從視覺方面: 第一個要做的是縮進,修改你的VIM配置文件_vimrc,在最后加入set cindent,這樣就設置了c風格的縮進,在這里縮進的大小是shiftwidth的值。 第二個要做的是語法高亮,這個是必須的,在中_vimrc加入syntax enable。 第三個要做的字體的設置,設置一個舒服的字體可以讓你編程的時候舒服好多,用editplus的時候我就用的Consolas,在vim中我還是用的這種字體,在_vimrc中加入 if has(”gui_running”) 表示運行界面vim的時候就用這種字體。 第四,設置配色方案,可以到點擊這兒下載,然后放到$VIM"vimfiles"colors這個目錄下,然后在中加入如下配置 if has(”gui_running”) colorscheme_name為你需要設置的配色方案的名稱。 接下來是在運行程序中用到的: 第一,使用ctag VIM中已經(jīng)帶了Ctags這個程序。盡管ctags也可以支持其它編輯器,但是它正式支持的只有VIM。Ctags可以幫助程序員很容易地瀏覽源代碼。用下面的命令可以在源代碼的根目錄下創(chuàng)建“tags”文件: [/home/brimmer/src]$ ctags -R “-R”表示遞歸創(chuàng)建,也就包括源代碼根目錄下的所有子目錄下的源程序。“tags”文件中包括這些對象的列表: l 用#define定義的宏 l 枚舉型變量的值 l 函數(shù)的定義、原型和聲明 l 名字空間(namespace) l 類型定義(typedefs) l 變量(包括定義和聲明) l 類(class)、結構(struct)、枚舉類型(enum)和聯(lián)合(union) l 類、結構和聯(lián)合中成員變量或函數(shù) VIM用這個“tags”文件來定位上面這些做了標記的對象,下面介紹一下定位這些對象的方法: 1) 用命令行。在運行vim的時候加上“-t”參數(shù),例如: [/home/brimmer/src]$ vim -t foo_bar 這個命令將打開定義“foo_bar”(變量或函數(shù)或其它)的文件,并把光標定位到這一行。 2) 在vim編輯器內用“:ta”命令,例如: :ta foo_bar 3) 最方便的方法是把光標移到變量名或函數(shù)名上,然后按下“Ctrl-]”。用“Ctrl-o”退回原來的地方。 注意:運行vim的時候,必須在“tags”文件所在的目錄下運行。否則,運行vim的時候還要用“:set tags=”命令設定“tags”文件的路徑,這樣vim才能找到“tags”文件。 你還可以選擇使用taglist這個插件,這個插件可以在右側顯示函數(shù),變量等的列表 第二,改正程序中的錯誤 在VIM編輯器的環(huán)境下用“:make”(make工具的使用已經(jīng)在我昨天的文章中 提到的云風的幾篇文章中詳細介紹到)就可以編譯程序,當然其前提是在當前目錄下有Makefile文件。運行完“:make”之后,如果程序中有錯誤,就 會顯示出來。這時候,光標會自動指向第一個出現(xiàn)錯誤的地方,而且你還可以看到錯誤的提示。然后,你就可以改正錯誤,而不用手工找到出錯的那一行。記住下面 幾個有用的命令: l “:cl”列出錯誤 l “:cn”讓光標指向下一個錯誤 l “:cp”讓光標指向上一個錯誤 l “:cnew”從頭開始 你甚至可以讓VIM識別出其它編譯器而不是gcc的錯誤提示。這對一些開發(fā)嵌入式系統(tǒng)的程序員這很有用,因為他們很可能用的不是gcc而是其它編譯器。通過設置“errorformat”的值,可以讓VIM識別出編譯器的出錯提示。因為不同的編譯器的出錯提示是不同的,所以如果用的不是gcc就要重新設置。 “errorformat”的值是一個字符串,它的格式和C語言的scanf的字符串格式相識。 gcc的“errorformat”的值為:%f:%l:"%m。其中“%f”表示文件名,“%l”表示行號,“%m”表示出錯信息。 用“:h errorformat”查看詳細的幫助信息。 用“:h quickfix”、“:h make”、“:h makeprg”、“:h errorfile”查看其它的信息。 第三,使用快捷鍵 下面的這些快捷鍵對程序員很有幫助: [[ 轉到上一個位于第一列的“{” ]] 轉到下一個位于第一列的“{” { 轉到上一個空行 } 轉到下一個空行 gd 轉到當前光標所指的局部變量的定義 * 轉到當前光標所指的單詞下一次出現(xiàn)的地方 # 轉到當前光標所指的單詞上一次出現(xiàn)的地方 % 用來進行小括號、中括號和大括號的匹配。這要看當前光標指向的是什么符號了。 ---------------------------------------------- 一個ubuntu 下的vimrc配置例子: """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 一般設定 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 設定默認解碼 set fenc=utf-8 set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936 "設定搜索是的高亮顯示 set hlsearch " 不要使用vi的鍵盤模式,而是vim自己的 set nocompatible " history文件中需要記錄的行數(shù) set history=100 " 在處理未保存或只讀文件的時候,彈出確認 set confirm " 與windows共享剪貼板 set clipboard+=unnamed " 偵測文件類型 filetype on " 載入文件類型插件 filetype plugin on " 為特定文件類型載入相關縮進文件 filetype indent on " 保存全局變量 set viminfo+=! " 帶有如下符號的單詞不要被換行分割 set iskeyword+=_,$,@,%,#,- " 語法高亮 syntax on " 高亮字符,讓其不受100列限制 :highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white ":match OverLength '"%101v.*' " 狀態(tài)行顏色 highlight StatusLine guifg=SlateBlue guibg=Yellow highlight StatusLineNC guifg=Gray guibg=White "高亮當前行 set cursorline """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 文件設置 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 不要備份文件(根據(jù)自己需要取舍) set nobackup " 不要生成swap文件,當buffer被丟棄的時候隱藏它 setlocal noswapfile set bufhidden=hide " 字符間插入的像素行數(shù)目 set linespace=0 " 增強模式中的命令行自動完成操作 set wildmenu " 在狀態(tài)行上顯示光標所在位置的行號和列號 set ruler set rulerformat=%20(%2*%<%f%=" %m%r" %3l" %c" %p%%%) " 命令行(在狀態(tài)行下)的高度,默認為1,這里是2 set cmdheight=2 " 使回格鍵(backspace)正常處理indent, eol, start等 set backspace=2 " 允許backspace和光標鍵跨越行邊界 set whichwrap+=<,>,h,l " 可以在buffer的任何地方使用鼠標(類似office中在工作區(qū)雙擊鼠標定位) set mouse=a set selection=exclusive set selectmode=mouse,key " 啟動的時候不顯示那個援助索馬里兒童的提示 set shortmess=atI " 通過使用: commands命令,告訴我們文件的哪一行被改變過 set report=0 " 不讓vim發(fā)出討厭的滴滴聲 set noerrorbells " 在被分割的窗口間顯示空白,便于閱讀 set fillchars=vert:" ,stl:" ,stlnc:" """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 搜索和匹配 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 高亮顯示匹配的括號 set showmatch " 匹配括號高亮的時間(單位是十分之一秒) set matchtime=5 " 在搜索的時候不忽略大小寫 set noignorecase " 不要高亮被搜索的句子(phrases) "set nohlsearch " 在搜索時,輸入的詞句的逐字符高亮(類似firefox的搜索) set incsearch " 輸入:set list命令是應該顯示些啥? set listchars=tab:"|" ,trail:.,extends:>,precedes:<,eol:$ " 光標移動到buffer的頂部和底部時保持3行距離 set scrolloff=3 " 不要閃爍 set novisualbell " 我的狀態(tài)行顯示的內容(包括文件類型和解碼) set statusline=%F%m%r%h%w" [FORMAT=%{&ff}]" [TYPE=%Y]" [POS=%l,%v][%p%%]" %{strftime(""%d/%m/%y" -" %H:%M"")} " 總是顯示狀態(tài)行 set laststatus=2 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 文本格式和排版 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 自動格式化 set formatoptions=tcrqn " 繼承前一行的縮進方式,特別適用于多行注釋 set autoindent " 為C程序提供自動縮進 set smartindent " 使用C樣式的縮進 set cindent " 制表符為4 set tabstop=4 " 統(tǒng)一縮進為4 set softtabstop=4 set shiftwidth=4 " 不要用空格代替制表符 set noexpandtab " 不要換行 "set nowrap "設置每行80個字符自動換行 set textwidth=80 " 在行和段開始處使用制表符 set smarttab """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " CTags的設定 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 按照名稱排序 let Tlist_Sort_Type = "name" " 在右側顯示窗口 let Tlist_Use_Right_Window = 1 " 壓縮方式 let Tlist_Compart_Format = 1 " 如果只有一個buffer,kill窗口也kill掉buffer let Tlist_Exist_OnlyWindow = 1 " 不要關閉其他文件的tags let Tlist_File_Fold_Auto_Close = 0 " 不要顯示折疊樹 let Tlist_Enable_Fold_Column = 1 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " Autocommands """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 只在下列文件類型被偵測到的時候顯示行號,普通文本文件不顯示 if has("autocmd") autocmd FileType xml,html,c,cs,java,perl,shell,bash,cpp,python,vim,php,ruby set number autocmd FileType xml,html vmap <C-o> <ESC>'<i<!--<ESC>o<ESC>'>o--> autocmd FileType java,c,cpp,cs vmap <C-o> <ESC>'<o/*<ESC>'>o*/ autocmd FileType html,text,php,vim,c,java,xml,bash,shell,perl,python setlocal textwidth=80 autocmd Filetype html,xml,xsl source $VIMRUNTIME/plugin/closetag.vim autocmd BufReadPost * " if line("'""") > 0 && line("'""") <= line("$") | " exe "normal g`""" | " endif endif " has("autocmd") " C/C++的編譯和運行 map <F5> :call CompileRunGcc()<CR> func! CompileRunGcc() exec "w" exec "!make" exec "! ./%<" endfunc " shell script運行 map <F6> :call CompileRunSH()<CR> func! CompileRunSH() exec "w" exec "!chmod a+x %" exec "!./%" endfunc " python運行 map <F7> :call CompileRunPyhton()<CR> func! CompileRunPyhton() exec "w" exec "!chmod a+x %" exec "!./%" endfunc " 能夠漂亮地顯示.NFO文件 set encoding=utf-8 function! SetFileEncodings(encodings) let b:myfileencodingsbak=&fileencodings let &fileencodings=a:encodings endfunction function! RestoreFileEncodings() let &fileencodings=b:myfileencodingsbak unlet b:myfileencodingsbak endfunction au BufReadPre *.nfo call SetFileEncodings('cp437')|set ambiwidth=single au BufReadPost *.nfo call RestoreFileEncodings() " 高亮顯示普通txt文件(需要txt.vim腳本) au BufRead,BufNewFile * setfiletype txt " 用空格鍵來開關折疊 set foldenable set foldlevel=0 set foldmethod=indent nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR> " minibufexpl插件的一般設置 let g:miniBufExplMapWindowNavVim = 1 let g:miniBufExplMapWindowNavArrows = 1 let g:miniBufExplMapCTabSwitchBufs = 1 let g:miniBufExplModSelTarget = 1 ---------------------- 一個相關帖子 http://forum.ubuntu.org.cn/viewtopic.php?f=68&t=138212&st=0&sk=t&sd=a
posted @ 2008-11-03 13:02 LukeW 閱讀(1636) | 評論 (0) | 編輯 收藏 servlet過濾器servlet過濾器
1. Servlet過濾器基礎
Servlet過濾器是Servlet的一種特殊用法,主要用來完成一些通用的操作。比如編碼的過濾,判斷用戶的登陸狀態(tài)等等。Servlet過濾器的適用場合: A.認證過濾 B.登錄和審核過濾 C.圖像轉換過濾 D.數(shù)據(jù)壓縮過濾 E.加密過濾 F.令牌過濾 G.資源訪問觸發(fā)事件過濾 Servlet過濾器接口的構成: 所有的Servlet過濾器類都必須實現(xiàn)javax.servlet.Filter接口。這個接口含有3個過濾器類必須實現(xiàn)的方法: 方法 說明 init(FilterConfig cfg) 這是Servlet過濾器的初始化方法,性質等同與servlet的init方法。 doFilter(ServletRequest,ServletResponse,FilterChain) 完成實際的過濾操作,當請求訪問過濾器關聯(lián)的URL時,Servlet容器將先調用過濾器的doFilter方法。FilterChain參數(shù)用于訪問后續(xù)過濾器 destroy() Servlet容器在銷毀過濾器實例前調用該方法,這個方法中可以釋放Servlet過濾器占用的資源。性質等同與servlet的destory()方法。 Servlet過濾器的創(chuàng)建步驟: A.實現(xiàn)javax.servlet.Filter接口的servlet類 B.實現(xiàn)init方法,讀取過濾器的初始化函數(shù) C.實現(xiàn)doFilter方法,完成對請求或過濾的響應 D.調用FilterChain接口對象的doFilter方法,向后續(xù)的過濾器傳遞請求或響應 F.在web.xml中配置Filter 2.使用過濾器處理中文問題 當用用戶登陸頁面輸入帳號時,如果輸入是中文,后臺servlet再次輸出這個內容時,可能就會是亂碼,這是因為serlvet中默認是以ISO-8859-1格式編碼的,如果后臺有多個Servlet,多個參數(shù),這樣就不合適,這個問題,我們可以通過一個過濾器統(tǒng)一解決,使后臺的輸出輸出都支持中文!將ISO-8859-1轉碼為GBK的那段代碼! 3.使用過濾器認證用戶: 每個過濾器也可以配置初始化參數(shù),可以將不需要過濾的地址配置到這個Filter的配置參數(shù)中,過濾時,如果請求地址在配置參數(shù)中,則放行,這樣 就避免了在程序中硬編碼。每個Filter中初始化時,都可以得到配置對象,在Filter中配置二個不需要過濾的地址,一個是登陸頁面,一個是執(zhí)行登陸 認證的servlet; 4.Servlet監(jiān)聽器 類似與Swing界面應用開發(fā),Servlet也可以創(chuàng)建監(jiān)聽器,以對Servlet容器,或Servlet中以象的事件做出反應。Servlet監(jiān)聽器主要有以下幾種: ServletRequestListener ,ServletRequestAttributeListener, HttpSessionActivationListener ,HttpSessionBindingListener , HttpSessionAttributeListener,HttpSessionListener, ServletContextListener等等。 這些監(jiān)聽器主要用來監(jiān)聽session,request,application這三個對象里存取數(shù)據(jù)的變化。 ---------------------------------------------------------------------------------------------------------------- servlet API中最重要的一個功能就是能夠為servlet和JSP頁面定義過濾器。過濾器提供了某些早期服務器所支持的非標準“servlet鏈接”的一種功能強大且標準的替代品。 過濾器提供了幾個重要好處 :
<xml version="1.0" encoding="ISO-8859-1"?> xml version="1.0" encoding="ISO-8859-1"?>
<filter>
<filter>
xml version="1.0" encoding="ISO-8859-1"?> 解決亂碼 web.xml加配置 <!-- 過濾器 --> EncodingFilter.java package com.hibernate.util; import java.io.IOException; import javax.servlet.Filter; public class EncodingFilter implements Filter { protected String Encoding = null; public void init(FilterConfig config) throws ServletException { this.config = config; } public void doFilter(ServletRequest request, ServletResponse response, if (request.getCharacterEncoding() == null) { OK!!! ----------------------------------------------------------------------------------------------------很簡單的過濾器,就是為了記錄一個url的請求時間 filter:
就會發(fā)現(xiàn)filter并沒有返回,也就是說
chain.doFilter(request, response); 執(zhí)行到這里時會從這里調用剩下的filter和servlet,所以這個調用將會是一個很長的過程。 在這個調用里,將會完全通過request和resonse去操作連接,取得/發(fā)送數(shù)據(jù),如果連接出現(xiàn)異常,將直接彈出Exception 你的代碼里沒有捕獲異常,所以如果出現(xiàn)異常,chain.doFilter后面的就不會執(zhí)行。 可以把chain.doFilter放到try finally結構中,保證后續(xù)會被執(zhí)行 posted @ 2008-10-28 14:58 LukeW 閱讀(348) | 評論 (0) | 編輯 收藏 Android應用程序剖析Anatomy of an Android Application
There are four building blocks to an Android application:
Not every application needs to have all four, but your application will be written with some combination of these. Once you have decided what components you need for your application, you should list them in a file called AndroidManifest.xml. This is an XML file where you declare the components of your application and what their capabilities and requirements are. See the Android manifest file documentation for complete details. Activity Activities are the most common of the four Android building blocks. An activity is usually a single screen in your application. Each activity is implemented as a single class that extends the Activity base class. Your class will display a user interface composed of Views and respond to events. Most applications consist of multiple screens. For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity. Moving to another screen is accomplished by a starting a new activity. In some cases an activity may return a value to the previous activity -- for example an activity that lets the user pick a photo would return the chosen photo to the caller. When a new screen opens, the previous screen is paused and put onto a history stack. The user can navigate backward through previously opened screens in the history. Screens can also choose to be removed from the history stack when it would be inappropriate for them to remain. Android retains history stacks for each application launched from the home screen. Intent and Intent Filters Android uses a special class called an Intent to move from screen to screen. An intent describes what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the activity), VIEW, PICK, EDIT, etc. The data is expressed as a URI. For example, to view contact information for a person, you would create an intent with the VIEW action and the data set to a URI representing that person. There is a related class called an IntentFilter. While an intent is effectively a request to do something, an intent filter is a description of what intents an activity (or intent receiver, see below) is capable of handling. An activity that is able to display contact information for a person would publish an IntentFilter that said that it knows how to handle the action VIEW when applied to data representing a person. Activities publish their IntentFilters in the AndroidManifest.xml file. Navigating from screen to screen is accomplished by resolving intents. To navigate forward, an activity calls startActivity(myIntent). The system then looks at the intent filters for all installed applications and picks the activity whose intent filters best matches myIntent. The new activity is informed of the intent, which causes it to be launched. The process of resolving intents happens at run time when startActivity is called, which offers two key benefits: * Activities can reuse functionality from other components simply by making a request in the form of an Intent * Activities can be replaced at any time by a new Activity with an equivalent IntentFilter Intent Receiver You can use an IntentReceiver when you want code in your application to execute in reaction to an external event, for example, when the phone rings, or when the data network is available, or when it's midnight. Intent receivers do not display a UI, although they may use the NotificationManager to alert the user if something interesting has happened. Intent receivers are registered in AndroidManifest.xml, but you can also register them from code using Context.registerReceiver(). Your application does not have to be running for its intent receivers to be called; the system will start your application, if necessary, when an intent receiver is triggered. Applications can also send their own intent broadcasts to others with Context.broadcastIntent(). Service A Service is code that is long-lived and runs without a UI. A good example of this is a media player playing songs from a play list. In a media player application, there would probably be one or more activities that allow the user to choose songs and start playing them. However, the music playback itself should not be handled by an activity because the user will expect the music to keep playing even after navigating to a new screen. In this case, the media player activity could start a service using Context.startService() to to run in the background to keep the music going. The system will then keep the music playback service running until it has finished. (You can learn more about the priority given to services in the system by reading Lifecycle of an Android Application.) Note that you can connect to a service (and start it if it's not already running) with the Context.bindService() method. When connected to a service, you can communicate with it through an interface exposed by the service. For the music service, this might allow you to pause, rewind, etc. Content Provider Applications can store their data in files, an SQLite database, or any other mechanism that makes sense. A content provider, however, is useful if you want your application's data to be shared with other applications. A content provider is a class that implements a standard set of methods to let other applications store and retrieve the type of data that is handled by that content provider. To get more details on content providers, see Accessing Content Providers. posted @ 2008-10-07 16:22 LukeW 閱讀(275) | 評論 (0) | 編輯 收藏 |
|