看上面的客戶端啟動的腳本圖,可以看到,zookeeper客戶端腳本運行的入口ZookeeperMain.java的main()方法, 關(guān)于這個類可以理解成它是程序啟動的輔助類,由它提供開始的位置,進而加載出zk client的上下文
// todo zookeeper的入口方法
public static void main(String args[]) throws KeeperException, IOException, InterruptedException {
// todo new ZK客戶端
ZooKeeperMain main = new ZooKeeperMain(args);
// todo run方法的實現(xiàn)在下面
main.run();
}
跟蹤
ZooKeeperMain main = new ZooKeeperMain(args);能往下追很長的代碼,提前說main.run()的作用,就是對用戶輸入的命令進行下一步處理
如上是入口函數(shù)的位置,跟進這兩個函數(shù),可以找到我們在client端的命令行中可以輸入命令和zookeeper服務(wù)端進行通信的原因(開起了新的線程),以及zookeeper的客戶端所依賴的其他類
跟進ZooKeeperMain main = new ZooKeeperMain(args);
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}
我們在命令行啟動客戶端時,輸入命令zkCli.sh -server localhost:2181,其中的args數(shù)組, 就是我們在啟動就是我們輸入的參數(shù),
構(gòu)建zookeeperMain對象時,上面主要做了兩件事
解析參數(shù)數(shù)組的邏輯就在下面, 很熟悉,就是我們在命令行啟動zookeeper時輸入的命令可選項
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}
接著看如果連接客戶端, connectToZK(String newHost) 同樣是本類方法,源碼如下:
// todo 來到這里
protected void connectToZK(String newHost) throws InterruptedException, IOException {
if (zk != null && zk.getState().isAlive()) {
zk.close();
}
//todo 命令行中的server 后面跟著 host主機地址
host = newHost;
boolean readOnly = cl.getOption("readonly") != null;
// todo 創(chuàng)建zookeeper的實例
zk = new ZooKeeper(host,
Integer.parseInt(cl.getOption("timeout")),
new MyWatcher(), readOnly);
}
到這里算是個小高潮吧,畢竟看到了zookeeper client的封裝類ZooKeeper, 這個類上的注解大概是這么介紹這個類的
host:port,host:port,host:port這種格式的字符串,逗號左右是不同的服務(wù)端的地址跟進ZooKeeper的構(gòu)造方法
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,boolean canBeReadOnly) throws IOException{
LOG.info("Initiating client connection, connectString=" + connectString
+ " sessionTimeout=" + sessionTimeout + " watcher=" + watcher);
watchManager.defaultWatcher = watcher;
// todo 包裝服務(wù)端的地址
ConnectStringParser connectStringParser = new ConnectStringParser(
connectString);
//todo 將服務(wù)端的地址封裝進 StaticHostProvider -> HostProvider中
HostProvider hostProvider = new StaticHostProvider(connectStringParser.getServerAddresses());
// todo 創(chuàng)建客戶端的上下文, 這個上下文對象的亮點就是它維護了一個客戶端的socket
cnxn = new ClientCnxn(connectStringParser.getChrootPath(),
hostProvider, sessionTimeout, this, watchManager,
// todo 跟進這個方法,getClientCnxnSocket, 獲取出客戶端上下文中的socket
getClientCnxnSocket(), canBeReadOnly);
// todo 啟動客戶端
cnxn.start();
}
主要做了這么幾件事
StaticHostProvider類中, 可以把這個類理解成專門存放服務(wù)端地址的set 集合getClientCnxnSocket()這個函數(shù)可以創(chuàng)建出客戶端的NIO Socket然后調(diào)用cnxn.start() 其實就是啟動了客戶端的另外兩條線程sendThread和eventThread 下面會詳細說
繼續(xù)跟進源碼getClientCnxnSocket()通過反射,zk客戶端使用的socket對象是ClientCnxnSocketNIO
//todo 通過反射創(chuàng)建出客戶端上下文中的 socket , 實際的ClientCnxnSocketNIO 是 ClientCnxnSocket的子類
// todo ---> zookeeper 封裝的 NIO的邏輯都在 實際的ClientCnxnSocketNIO
private static ClientCnxnSocket getClientCnxnSocket() throws IOException {
// todo zookeeper.clientCnxnSocket
String clientCnxnSocketName = System.getProperty(ZOOKEEPER_CLIENT_CNXN_SOCKET);
if (clientCnxnSocketName == null) {
// todo 上面String其實就是這個類的name, 根進去看一下它的屬性
// todo 這個類維護了NioSocket使用到的 selector 選擇器 , 已經(jīng)發(fā)生的感興趣的事件SelectionKey
clientCnxnSocketName = ClientCnxnSocketNIO.class.getName();
}
try {
// todo 可以看到客戶端使用的 NioSocket
return (ClientCnxnSocket) Class.forName(clientCnxnSocketName).getDeclaredConstructor()
.newInstance();
} catch (Exception e) {
IOException ioe = new IOException("Couldn't instantiate "
+ clientCnxnSocketName);
ioe.initCause(e);
throw ioe;
}
}
創(chuàng)建上下文,構(gòu)造函數(shù)中的諸多屬性都是在前面讀取配置文件或是新添加進來的,重點是最后兩行,它創(chuàng)建了兩條線程類,和zk客戶端的IO息息相關(guān)
public ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZooKeeper zooKeeper,
ClientWatchManager watcher, ClientCnxnSocket clientCnxnSocket,
long sessionId, byte[] sessionPasswd, boolean canBeReadOnly) {
this.zooKeeper = zooKeeper;
this.watcher = watcher;
this.sessionId = sessionId; // todo 剛才傳遞過來的值為0
this.sessionPasswd = sessionPasswd;
this.sessionTimeout = sessionTimeout;
this.hostProvider = hostProvider;
this.chrootPath = chrootPath;
connectTimeout = sessionTimeout / hostProvider.size();
// todo 添加read的超時時間
readTimeout = sessionTimeout * 2 / 3;
readOnly = canBeReadOnly;
// todo 創(chuàng)建了一個seadThread 線程
sendThread = new SendThread(clientCnxnSocket);
eventThread = new EventThread();
}
sendThred是一個客戶端的線程類,什么時候開啟? 其實就在上面,當(dāng)創(chuàng)建了ClientCnxn后,調(diào)用的cnxn.start()就是在開啟它的run() , 它有什么作用? 它的run()是一個無限循環(huán),除非運到了close的條件,否則他就會一直循環(huán)下去, 比如向服務(wù)端發(fā)送心跳,或者向服務(wù)端發(fā)送我們在控制臺輸入的數(shù)據(jù)以及接受服務(wù)端發(fā)送過來的響應(yīng)
這是他的構(gòu)造方法,可以看到它還是一個守護線程,并擁有客戶端socket的引用,有了NIO Socket相關(guān)技能
//todo
SendThread(ClientCnxnSocket clientCnxnSocket) {
super(makeThreadName("-SendThread()"));
// todo 設(shè)置狀態(tài) Connecting
state = States.CONNECTING;
// todo 就是在 Zookeeper new ClientCnxn 時, 在倒數(shù)第二個位置使傳遞進去一個函數(shù)實際的
this.clientCnxnSocket = clientCnxnSocket;
// todo 設(shè)置成守護線程
setDaemon(true);
}
它的Run方法, 真的是好長啊, 比我上面寫的部分內(nèi)容還長(大概兩百行了), 大概它的流程 ,每次循環(huán):
clientCnxnSocket.doTransport(to, pendingQueue, outgoingQueue, ClientCnxn.this);向服務(wù)端發(fā)送數(shù)據(jù)在這個負責(zé)和服務(wù)端進行IO操作的線程中,只要不是close或其他重大錯誤,一般可以預(yù)知的異常都有try起來,然后記錄日志,并沒有其他操作,循環(huán)還是會進行
// todo introduce 介紹
clientCnxnSocket.introduce(this,sessionId); // todo this,sessionId == 0
clientCnxnSocket.updateNow();
clientCnxnSocket.updateLastSendAndHeard();
int to;
long lastPingRwServer = Time.currentElapsedTime();
final int MAX_SEND_PING_INTERVAL = 10000; //10 seconds
InetSocketAddress serverAddress = null;
// todo 這個while循環(huán)中存在建立連接的過程, 已經(jīng)連接建立失敗后不斷重試的過程
//todo state.isAlive() 默認是 NOT_CONNECTED
while (state.isAlive()) {
try {
//todo 1111 如果socket還沒有連接 /////////////////////////////////////////////////////////////////////////////////////////////////////////
//todo 如果socket還沒有連接
if (!clientCnxnSocket.isConnected()) {
// todo 判斷是不是第一次連接, 如果不是第一次進入下面try代碼塊, 隨機產(chǎn)生一個小于一秒的時間
if(!isFirstConnect){
try {
Thread.sleep(r.nextInt(1000));
} catch (InterruptedException e) {
LOG.warn("Unexpected exception", e);
}
}
// don't re-establish connection if we are closing
// todo 如果是closing 或者 已經(jīng)關(guān)閉了, 直接退出這個循環(huán)
if (closing || !state.isAlive()) {
break;
}
if (rwServerAddress != null) {
serverAddress = rwServerAddress;
rwServerAddress = null;
} else {
// todo 連接失敗時,來這里重試連接
// todo 從我們傳遞進來的host地址中選擇一個地址
serverAddress = hostProvider.next(1000);
}
// todo client和server進行socket連接
// todo 跟進去 ,實現(xiàn)邏輯在上面
// todo 這個方法開始建立連接,并將 isFasterConnect改成了 false
startConnect(serverAddress);
clientCnxnSocket.updateLastSendAndHeard();
}
//todo 2222 如果socket處于連接狀態(tài) /////////////////////////////////////////////////////////////////////////////////////////////////////////
// todo 下面的連接狀態(tài)
if (state.isConnected()) {
// determine whether we need to send an AuthFailed event.
if (zooKeeperSaslClient != null) {
boolean sendAuthEvent = false;
if (zooKeeperSaslClient.getSaslState() == ZooKeeperSaslClient.SaslState.INITIAL) {
try {
zooKeeperSaslClient.initialize(ClientCnxn.this);
} catch (SaslException e) {
LOG.error("SASL authentication with Zookeeper Quorum member failed: " + e);
state = States.AUTH_FAILED;
sendAuthEvent = true;
}
}
KeeperState authState = zooKeeperSaslClient.getKeeperState();
if (authState != null) {
if (authState == KeeperState.AuthFailed) {
// An authentication error occurred during authentication with the Zookeeper Server.
state = States.AUTH_FAILED;
sendAuthEvent = true;
} else {
if (authState == KeeperState.SaslAuthenticated) {
sendAuthEvent = true;
}
}
}
if (sendAuthEvent == true) {
eventThread.queueEvent(new WatchedEvent(
Watcher.Event.EventType.None,
authState,null));
}
}
// todo 連接成功的話執(zhí)行to 為下面值
// todo to = 讀取的超時時間 - 上一次的讀取時間
// todo 如果預(yù)訂的超時時間 - 上次讀的時間 <= 0 說明超時了
to = readTimeout - clientCnxnSocket.getIdleRecv();
} else {
// todo 如果沒有連接成功, 就會來到這里, 給 to 賦值
to = connectTimeout - clientCnxnSocket.getIdleRecv();
}
//todo 3333 異常處理 /////////////////////////////////////////////////////////////////////////////////////////////////////////
// todo 下面拋出來了異常
if (to <= 0) {
String warnInfo;
warnInfo = "Client session timed out, have not heard from server in "
+ clientCnxnSocket.getIdleRecv()
+ "ms"
+ " for sessionid 0x"
+ Long.toHexString(sessionId);
LOG.warn(warnInfo);
// todo 這里拋出來了異常, 下面的try 就會把它抓住
throw new SessionTimeoutException(warnInfo);
}
//todo 44444 連接成功執(zhí)行的邏輯 /////////////////////////////////////////////////////////////////////////////////////////////////////////
// todo 下面的是連接成功執(zhí)行的邏輯
if (state.isConnected()) {
// todo 為了防止競爭狀態(tài)丟失發(fā)送第二個ping, 同時也避免出現(xiàn)很多的ping
//1000(1 second) is to prevent(阻止) race condition missing to send the second ping
//also make sure not to send too many pings when readTimeout is small
int timeToNextPing = readTimeout / 2 - clientCnxnSocket.getIdleSend() -
((clientCnxnSocket.getIdleSend() > 1000) ? 1000 : 0);
//send a ping request either time is due or no packet sent out within MAX_SEND_PING_INTERVAL
if (timeToNextPing <= 0 || clientCnxnSocket.getIdleSend() > MAX_SEND_PING_INTERVAL) {
// todo 客戶端一直在這里循環(huán), 如果連接成功的話, 每次循環(huán)都來到這個邏輯這里發(fā)送 ping
sendPing();
clientCnxnSocket.updateLastSend();
} else {
if (timeToNextPing < to) {
to = timeToNextPing;
}
}
}
//todo 55555 /////////////////////////////////////////////////////////////////////////////////////////////////////////
// If we are in read-only mode, seek for read/write server
// todo 只讀狀態(tài) 相關(guān)邏輯
if (state == States.CONNECTEDREADONLY) {
long now = Time.currentElapsedTime();
int idlePingRwServer = (int) (now - lastPingRwServer);
if (idlePingRwServer >= pingRwTimeout) {
lastPingRwServer = now;
idlePingRwServer = 0;
pingRwTimeout =
Math.min(2*pingRwTimeout, maxPingRwTimeout);
pingRwServer();
}
to = Math.min(to, pingRwTimeout - idlePingRwServer);
}
//todo 66666 /////////////////////////////////////////////////////////////////////////////////////////////////////////
// todo 消費outgoingqueue, 完成向服務(wù)端的發(fā)送發(fā)送
// todo doTransport 是 ClientCnxnSocket 的抽象方法, 實現(xiàn)類clientCnxnSocketNio
clientCnxnSocket.doTransport(to, pendingQueue, outgoingQueue, ClientCnxn.this);
} catch (Throwable e) {
// todo 在這個try中處理里面的拋出來的異常
if (closing) {
// todo 如果是請求關(guān)閉, 直接退出 break 出while循環(huán)
if (LOG.isDebugEnabled()) {
// closing so this is expected
LOG.debug("An exception was thrown while closing send thread for session 0x"
+ Long.toHexString(getSessionId())
+ " : " + e.getMessage());
}
break;
} else {
// todo 只要不是退出異常, 下面的異常都是僅僅打印了一下出現(xiàn)了什么異常
// this is ugly, you have a better way speak up
if (e instanceof SessionExpiredException) {
LOG.info(e.getMessage() + ", closing socket connection");
} else if (e instanceof SessionTimeoutException) {
LOG.info(e.getMessage() + RETRY_CONN_MSG);
} else if (e instanceof EndOfStreamException) {
LOG.info(e.getMessage() + RETRY_CONN_MSG);
} else if (e instanceof RWServerFoundException) {
LOG.info(e.getMessage());
} else if (e instanceof SocketException) {
LOG.info("Socket error occurred: {}: {}", serverAddress, e.getMessage());
} else {
LOG.warn("Session 0x{} for server {}, unexpected error{}",
Long.toHexString(getSessionId()),
serverAddress,
RETRY_CONN_MSG,
e);
}
// todo 這個方法中, isFirstConnect = true
cleanup();
if (state.isAlive()) {
eventThread.queueEvent(new WatchedEvent(
Event.EventType.None,
Event.KeeperState.Disconnected,
null));
}
clientCnxnSocket.updateNow();
clientCnxnSocket.updateLastSendAndHeard();
}
}
} // todo while循環(huán)的結(jié)束符號 , 這是個while循環(huán), 除了上面的close其他異常都會繼續(xù)循環(huán), 接著上去再看一遍
cleanup();
clientCnxnSocket.close();
if (state.isAlive()) {
eventThread.queueEvent(new WatchedEvent(Event.EventType.None,
Event.KeeperState.Disconnected, null));
}
ZooTrace.logTraceMessage(LOG, ZooTrace.getTextTraceLevel(),
"SendThread exited loop for session: 0x"
+ Long.toHexString(getSessionId()));
}
在上面這個200行的Run方法中比較值得注意的幾個方法如下
針對下標(biāo)運行,對數(shù)組的size取模, 再賦值給自己,所以就實現(xiàn)了從0 - array.size()的循環(huán)
,【巨型】【十萬】【更加】【說不】,【剔除】【塔狂】【有一】.【毒藥】【劈去】【就完】【橋右】,【點像】【水聲】【險鯤】黑帽seo研究【十幾】,【狐那】【都掩】【用到】【思想】.【來短】!【若無】【是一】【君之】【全部】【升起】【就會】【姐聽】【嗯我】【必然】【身金】【得更】【聲驚】【佛土】【應(yīng)的】【一會】【響之】【而說】【量波】【得泰】【死有】【原了】【口中】【不高】【沒有】【不是】【如出】【衣袍】【巨大】【那火】【停頓】【雖然】【難度】【通天】【后多】【敏銳】【出現(xiàn)】, public InetSocketAddress next(long spinDelay) {
currentIndex = ++currentIndex % serverAddresses.size();
if (currentIndex == lastIndex && spinDelay > 0) {
try {
Thread.sleep(spinDelay);
} catch (InterruptedException e) {
LOG.warn("Unexpected exception", e);
}
} else if (lastIndex == -1) {
// We don't want to sleep on the first ever connect attempt.
lastIndex = 0;
}
這個函數(shù)中,將標(biāo)記是否是第一次連接的標(biāo)記置為了flase, 并且拿到了sessionid
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}0
跟進上面Run方法的如下方法,doTranprot
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}1
他是本類的抽象方法,具體的實現(xiàn)類是clientCnxnSocketNIO
跟進這個方法,其中有一步跟重要doIO(pendingQueue, outgoingQueue, cnxn);
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}2
它分成了兩大模塊
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}3
雖然找到了客戶端往服務(wù)端發(fā)送數(shù)據(jù)的代碼, 但是問題來了, 它發(fā)送的什么數(shù)據(jù)啊? 在上面可以看到,它每次發(fā)送的數(shù)據(jù)都被包裝車成了packet類型,并且,繼續(xù)往下跟進可以看到這個packet來自于一個叫outgoingqueue的隊列中
client想往服務(wù)端發(fā)送什么?其實發(fā)送就是我們手動輸入的命令,只不過他把我們的命令解析出來并且進行了封裝,進行了哪些封裝? String-> request -> packet -> socket ,這個packet就在上面的部分被消費
到目前為止,算上一開始的主線程,其實已經(jīng)有3條線程了, 分別是主線程,SendThread和eventThread
代碼讀到這里,sendThread部分其實已經(jīng)結(jié)束了,我們直到了它正在消費outgoingqueue中的內(nèi)容,接下來的任務(wù)返回回去,從新回到 ZooKeeperMain中,看一開始主線程時如何處理用戶在命令行的輸入的
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}4
跟進 main.run(), 主要做了如下幾件事
jline.ConsoleReaderexecuteLine(line); public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}5
繼續(xù)跟進 executeLine(line);,做了如下幾件事
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}6
處理命令的邏輯如下:
將命令解析出來,通過if分支語句,判斷用戶輸入的什么命令, 然后再進一步處理
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}7
比如,用戶輸入的是創(chuàng)建新節(jié)點的命令create /path, 就會有下面的函數(shù)處理
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}8
跟進這個方法 , 主要做了下面幾件事
public ZooKeeperMain(String args[]) throws IOException, InterruptedException {
cl.parseOptions(args);
// todo 連接到客戶端
connectToZK(cl.getOption("server"));
}9
跟進submitRequest()
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}0
在上面的代碼中,可以看到可以他是使用一個while(!packet,finishes){} 來阻塞程序的, 剛看看到用戶的命令被封裝進了request, 接下來, 在queuePacket(h, r, request, response, null, null, null, null, watchRegistration);中,可以看到他被封裝進packet,然后添加到outgoingqueue隊列中,源碼如下
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}1
在這個方法的最后一行,點睛,selector.wakeup(); 就是通知選擇器,別再阻塞select了,趕緊去做其他工作
因為選擇器在sendThread的doTransport()方法中,有阻塞的操作,我重新把代碼貼出來如下
服務(wù)端的NIOSocket -> ClientCnxnSocket 都是ClientCnxn上下文的封裝類的,SendThread同樣也是,它可以使用
現(xiàn)在再看,喚醒selector 讓他去做其他事 ,其實即使doIO(),這個方法代碼其實我在上面貼出來過,就是分成兩大部分,讀就緒與寫就緒
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}2
寫到這里其實已經(jīng)把整個過程順下來了,下面再重新看看,sendThread是如果消費packet并且修改然后得到服務(wù)端的響應(yīng),修改pakcet.finished屬性的, 因為現(xiàn)在主線的submitRequest還在阻塞呢
客戶端的socket的實現(xiàn)類是ClientCnxnSocketNio, 它往服務(wù)端寫的邏輯如下, 不難看出使用的java原生的sock.write(p.bb); // 發(fā)送服務(wù)端 , 亮點是后面的操作pendingQueue.add(p);被寫過的packet被添加到了pengingqueue中
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}3
上面說了, 為啥被使用過的pakcet還要保留一份呢? 還是那個原因,主線程還因為pakcet的finish狀態(tài)未被該變而阻塞呢, 那什么時候改變呢? 答案是受到服務(wù)端的響應(yīng)之后改變,在哪里收到呢? 就是DoIo()的讀就緒模塊,下面附上源碼,它的解析我寫在這段代碼下面
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}4
如上代碼的最后部分,sendThread.readResponse(incomingBuffer); 下面是它的源碼,它首先是從buffer中讀取出服務(wù)端的發(fā)送的數(shù)據(jù),然后一通解析,封裝進pendingqueue的packet中,并且在方法的最后部分終于完成了狀態(tài)的修改
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}5
進入finishPacket(packet)
public boolean parseOptions(String[] args) {
List<String> argList = Arrays.asList(args);
Iterator<String> it = argList.iterator();
while (it.hasNext()) {
String opt = it.next();
try {
if (opt.equals("-server")) {
options.put("server", it.next());
} else if (opt.equals("-timeout")) {
options.put("timeout", it.next());
} else if (opt.equals("-r")) {
options.put("readonly", "true");
}
} catch (NoSuchElementException e) {
System.err.println("Error: no argument found for option "
+ opt);
return false;
}
if (!opt.startsWith("-")) {
command = opt;
cmdArgs = new ArrayList<String>();
cmdArgs.add(command);
while (it.hasNext()) {
cmdArgs.add(it.next());
}
return true;
}
}
return true;
}6。轉(zhuǎn)載請注明來源地址:黑帽SEO http://www.790079.com
專注于SEO培訓(xùn),快速排名
(黑帽seo技術(shù),網(wǎng)站快速排名,蜘蛛池加速收錄,目錄程序定制)
掃一下添加微信: