服务端:处理软件相关 RPC 请求
整体来说,和 Web 开发里的 Controller 很类似:接受请求,进行处理(中间可能会调用其它系统模块),做出响应。
钱包启动及运行时,会通过调用这里的接口,以同步链上数据。
软件 RPC
/* this file implements the rpcserver api, so as wallet and block explorer tools can work without migration */
// all components requiring access to blockchain must use , this struct to communicate
// this structure must be update while mutex
type RPCServer struct {
srv *http.Server
mux *http.ServeMux
Exit_Event chan bool // blockchain is shutting down and we must quit ASAP
sync.RWMutex
}
使用标准库 http.Server 实现网络连接、传输数据。
还有变量:
var Exit_In_Progress bool
var chain *blockchain.Blockchain
var logger *log.Entry
处理过程,几乎全部由 chain 完成(这里只是封装、调用)。即使需要用到其它组件(如 Mempool),也可由 chain 代劳。
架构设计:
路由层(外层请求过来,不同请求该怎么处理,由它路由)
Handle 层(通过路由过来的请求,由它处理)
业务层(提供 Blockchain 对象,由它实现封装,它与 Chain 或者其它系统模块交互)
服务用的是 http.Server,启动、运行实现起来都比较简单。