cs144 lab0 lab1記錄
這個叫什麼?Write Up嗎
lab0
lab0要做的事,實現buffer部分的功能,要自己寫變數以及實現介面。
成員函數 write()向buffer內寫數據,peek_out() pop_out() read() 函數從buffer內讀數據,
buffer長度capacity,需要變數記錄向buffer內一共寫入和讀取過多長的數據。
有些變數在後面的lab會用上,第一次寫真不容易想,好些都是參照別人的部落格寫的。
lab1
// Construct a `StreamReassembler` that will store up to `capacity` bytes. StreamReassembler(const size_t capacity); // Receive a substring and write any newly contiguous bytes into the stream, // while staying within the memory limits of the `capacity`. Bytes that would // exceed the capacity are silently discarded. // // `data`: the substring // `index` indicates the index (place in sequence) of the first byte in `data` // `eof`: the last byte of this substring will be the last byte in the entire stream void push_substring(const string &data, const uint64_t index, const bool eof); // Access the reassembled ByteStream (your code from Lab 0) ByteStream &stream_out(); // The number of bytes in the substrings stored but not yet reassembled size_t unassembled_bytes() const; // Is the internal state empty (other than the output stream)? bool empty() const;

push_substring,向buffer內寫入data,如「abcdefg」, 「cdf」 index = 2, eof代表傳完這段數據就沒有數據要傳了。由於buffer有容量限制,收到的數據最後index不能比first_unacceptable大,
對於index在first_unread和first_unassembled之間的部分(已經放進buffer了),直接丟掉,
對於index在first_unassembled和first_unacceptable之間的部分(還沒有放進buffer,暫存在map中的),直接丟掉,_head_index用來記錄下一個實際要讀進buffer的首字元index,例:傳「abc」和「bcd」兩段字元串,後面。。。
對於何時結束input,開始想的很複雜,其實有兩個條件:
1.收到with_eof訊號
2.收到的字元串長度如果超過了first_unacceptable,說明還不能結束,還有數據要讀
在lab1學會了看測試文件和報錯。測試文件放在tests目錄下,SubmitSegment 執行push_substring, BytesAssembled 用來測試上一步操作以後nwrite == _bytes, BytesAvailable 執行從buffer內讀操作,並判讀的數據是不是對的
把收到的數據放進map中有兩種思路:以capacity=3為例
1.「abcd」,index=0, 全部讀進map中,第二次收到「bcdef」,index=1時,因為第1、2、3位已經在map中(map.find() != map.end()),直接跳過,只讀第4位『f'
2.」abcd「,index=0, 只讀_first_unacceptable之前的部分,buffer內為空, 只讀"abc" , 'd'丟掉,下次再收到數據」bckef「,index=1時,此時index=3的是』k『
這兩種方法實現以後通過測試案例能發現很多問題,不全記錄了,然後一步步debug,一門實驗課提供這麼豐富的測試案例,愛了愛了。
一個有意思的測試案例,我採用的是第一種思路(我在參考時看到有的部落客用第二種思路,應該都可以,仔細debug),但是這時有一個問題:」abcd「, index=0,全部讀到map中,又來了一個」bckef」, index=1,如果因為index=3的字元已經讀到map中了,』k『就讀不進去了,就在我考慮換成第二種思路的時候,想到做一個判斷,如果新的字元不一樣,替換掉就好了!!!bingo,問題解決。lab1的案例都通過啦。
ps:第一次做這個lab的時候心想著:啊,不就是電腦網路的程式碼實現嗎,應該不會那麼難吧,比較做題家可是把概念什麼的都搞得很明白了呢,好吧,naive。然後到處找別人的參考,好傢夥,總是有一半以上的案例跑不通,但是也沒有想著仔細看程式碼,就迷迷糊糊的到了lab2,又稀里糊塗的搞完發現一點收穫也沒有,不純粹浪費時間嗎,一個星期前,從lab0開始重新仔仔細細的腳踏實地的走,通過每一個測試案例都有很多收穫,加油!
在部落格園記錄我在coding中程式碼與遇到問題的想法和心路。

