The book is connected to the back. After showing the top-level code of the app, let's look at how the modules are programmed.
In order to be able to understand the code of each module, we need to pave the way for the basic concepts and design specifications of the softchip architecture.
1, the random module does not hold other modules of the instance. Naturally no longer explicitly uses a handle to invoke no matter what method.
This means that there is no coupling between the encoding period and the compile period between the modules.
2, each module in the form only with the machine interface. The interface exists in API and Event two forms.
The API is mapped directly to the function entry address, which can be called directly by the user and has high performance during execution. It is mainly used for utility properties and for high performance applications.
The Event is also a synchronous call, but the execution period is less than the API. Its key role is to logically divide the lengthy call chain. In the traditional routines. A module is finished with its own processing. Often also hard to trigger other modules related to the action, which is logically equivalent to thinking about "other people's things." The introduction of an Event is equivalent to a phase state in which the original long call chain is cut into one stage in time.
Each module just needs to tell machine "x thing Y is finished", machinewill take the initiative to find who need to "x things Y" after the continuation. Machine Search might be based on an event listener statement made by the target module in its own interface.
3, the module interface by self-declaration, including:
3-1) Outapi: List of API names that you need to invoke.
3-2) InAPI: You can provide a list of API names for other module calls ( with corresponding function entry addresses ).
3-3) Inevent: A list of event names that you want to listen to ( with the corresponding event handler entry address ).
3-4) Outevent: Whether to send the event yourself.
Based on each module declaration. Machine itself to "insert" each module. Complete the binding of the execution period API and the distribution of the event.
4, generally speaking, each module basically only need to care about "
own thing ":
4-1) What API do I provide?
4-2) What API do I need to invoke?
4-3) What events do I monitor and handle?
4-4) What events do I have?
In turn, modules do not need to care about " other people's Affairs ":
4-5) who uses my API?
4-6) who provides the API for me to use?
4-7) who generated the event I was listening to? ( who was doing it before I worked?) )
4-8) The events I have generated who deal with it? (Who's going to carry on when I'm done? )
To give a simple example, in the Textchat design, Loginwindow login success, do not need to explicitly notify Chatwindow, because that is chatwindow things. Loginwindow just need to tell machine "Loginwindow login succeeded".
Machine will take the initiative to find Chatwindow out work, because Chatwindow had already told Machine, assume "Liginwindow login succeeded." please inform me.
OK, with the above cushion, the following code is easier to understand. The basic routines are:
1. Each module inherits the Chip class.
2, implement the _defineinterface() method to complete the interface declaration.
3. Implement the _poweron() method to receive "power-up" notifications from machine.
In addition, each module is completely free. Love how to play, how to play it.
A new version of the module relationship.
from softchip import Chip
Class Controller (Chip): Def __init__ (self, Chipid): chip.__init__ (Self, chipid) def _defineinterf Ace (self): outapilist = [u ' connect_server ', U ' disconnect_server ',] inapilist = [(U ' api_start_work ', self.__apistartwork),] ineventlist = [(U ' evt_logout_done ', s Elf.__evtonlogoutdone),] Self.__interface = {Chip.TYPE_INT_API:inAPIList, chip.ty Pe_out_api:outapilist, Chip.TYPE_OUT_EVENT:None,} return self.__interface def _poweron (SE LF): Self._ehandler = self.__interface[chip. Type_out_event] Apimap = self._defineinterface[chip. TYPE_OUT_API] Self.__apiconnect = Apimap[u ' connect_server '] self.__apidisconnect = Apimap[u ' Disconnect_se RVer '] def __apistartwork (self, ServerHost, serverport): succeeded = Self.__apiconnect (ServerHost, ServerPort) If not succeeded:sElf._ehandler (U ' evt_connect_failed ') return Self._ehandler (U ' evt_connect_succeeded ') def __evtonlogoutdon E (self): Self.__apidisconnect () class Communicationmanager (Chip): Def __init__ (self, chipid): Chip.__init_ _ (Self, chipid) def _defineinterface (self): inapilist = [(U ' connect_server ', self.__connect) , (U ' disconnect_server ', self.__disconnect), (U ' log_in ', Self.__login), (U ' log_out ', self.__logout), (U ' invite_friend ', self.__invitefriend), (U ' send _message ', self.__sendmessage),] Self.__interface = {Chip.TYPE_IN_API:inAPIList, Chip.TYPE_OUT_EVENT:None,} return self.__interface def _poweron (self): Self._ehandler = S Elf.__interface[chip.type_out_event] def __connect (ServerHost, ServerPort): Pass def __disconnect (ServerHost, ServerPort): pasS def __login (username, password): Pass def __logout (): Pass def __invitefriend (friendname): ... self.__messagereceivethread () def __sendmessage (message): Pass Def __MESSAGERECEIVETHR EAD (): while (True): ... message = xxx self._ehandler (u ' evt_message_arrived ', message). .. Class Loginwindow (Chip): Def __init__ (self, Chipid): chip.__init__ (Self, chipid) def _defineinterface (self): Outapilist = [u ' log_in ', U ' log_out ',] ineventlist = [(U ' Evt_conne Ct_succeeded ', self.__evtonconnectsucceeded), (U ' evt_chat_closed ', self.__evtonchatclosed), ] Self.__interface = {Chip.TYPE_OUT_API:outAPIList, Chip.TYPE_INT_EVENT:inEventList, Chip.TYPE_OUT_EVENT:None,} return self.__interface def _poweron (self): Self._ehandler = Self.__interface[chip.Type_out_event] Apimap = self._defineinterface[chip. TYPE_OUT_API] Self.__apilogin = Apimap[u ' log_in '] self.__apilogout = Apimap[u ' log_out '] def __evtonconne Ctsucceeded (self): self. Show () def __evtonchatclosed (self): Self.__apilogout () Self._ehandler (U ' evt_logout_done ') def __onlogi Nbuttonpressed (self): username = xxx. GetValue () password = xxx. GetValue () succeeded = Self.__apilogin (username, password) if not Succeeded:self._ehandler (U ' evt _login_failed ') Return Self._ehandler (U ' evt_login_succeeded ') self. Hide () class Chatwindow (Chip): Def __init__ (self, Chipid): chip.__init__ (Self, chipid) def _defineinterface (SE LF): Outapilist = [u ' invite_friend ', U ' send_message ', U ' log_out ', U ' di Sconnect_server ',] ineventlist = [(U ' evt_login_succeeded ', self.__evtonloginsucceeded), (U ' EVt_message_arrived ', self.__evtonmessagereceived),] self.__interface = {Chip.TYPE_OUT_API:ou Tapilist, Chip.TYPE_IN_API:inEventList, Chip.TYPE_OUT_EVENT:None,} return self._ _interface def _poweron (self): Self._ehandler = self.__interface[chip. Type_out_event] Apimap = self._defineinterface[chip. TYPE_OUT_API] Self.__apiinvite = Apimap[u ' invite_friend '] self.__apisendmsg = Apimap[u ' Send_message '] Self.__apilogout = Apimap[u ' log_out '] self.__apidisconnect = Apimap[u ' Disconnect_server '] def __evtO Nloginsucceeded (self): self. Show () def __evtonmessagereceived (self, message): XXX. Showtext (message) def __oninvitebuttonpressed (self): Friendname = xxx. GetValue () succeeded = Self.__apiinvite (Friendname, self.__onmessagereceived) if not succeeded:s Elf._ehandler (U ' evt_invite_failed ') return Self._ehandLer (U ' evt_invite_succeeded ') def __onsendbuttonpressed (self): message = XXX. GetValue () self.__apisendmsg (message) def __onwindowclosing (self): self._ehandle (U ' evt_chat_closed ')
In the next article in this series. We will briefly analyze the meaning of the softchip framework.
A fascinating story from Python (ii)