A wonderful story from Python (2)
Book connection. After displaying the code at the top layer of the App, let's look at how to program each module.
In order to understand the code of each module, we must first pave the waySoftchipThe basic concepts and design specifications of the architecture.
1. If any module does not hold instances of other modules, it naturally does not explicitly call any method using a handle.
This means that there is no coupling between the encoding and compilation phases.
2. Each module only has an interface with the Machine.APIAndEventTwo forms exist.
APIIt is directly mapped to the function entry address, which can be directly called by the user, resulting in high performance during runtime. It is mainly used for Utility methods and high-performance scenarios.
EventIt is also a synchronous call, but the runtime is lower than the API. Its key role is to logically cut lengthy call chains. In traditional routines, after a module completes its own processing, it often has to work hard to trigger the subsequent actions of other modules, which is logically equivalent to remembering "other things ".EventThe introduction of is equivalent to dividing the original long call chain into stages in time. Each module only needs to tell the Machine that "X is finished", and the Machine will automatically find out who needs to continue after "X is finished. The reason for Machine to look for subsequent operations is the event listening Statement made by the target module in its own interface.
3. The module interface is declared by yourself, including:
3-1) outAPI: List of API names to be called.
3-2) inAPI: You can provide a list of API names called by other modules (with the corresponding function entry address ).
3-3) inEvent: List of event names you want to listen to (with the corresponding event processing function entry address ).
3-4) outEvent: whether to send events.
Based on the declaration of each module, Machine automatically inserts each module to complete API binding and event distribution during runtime.
4. In summary, each module only needs to care about
Your own affairs":
4-1) What APIs do I provide?
4-2) What APIs do I need to call?
4-3) What events do I listen for and handle?
4-4) What events do I have?
In turn, the module does not need to care about"Other things":
4-5) who uses my API?
4-6) who provides APIs for me?
4-7) who generated the event I monitored? (Who did this before I worked ?)
4-8) who handles my events? (Who will continue after I finish ?)
For example, in TextChat design, after LoginWindow is successfully logged on, you do not need to explicitly notify ChatWindow because it is a ChatWindow task. LoginWindow only needs to tell the Machine that "LoginWindow logon is successful ". Machine will automatically find ChatWindow to work, because ChatWindow has already told Machine that if "LiginWindow login successful", please let me know.
OK. With the above preparations, the following code is easier to understand. The basic routine is:
1. Each module inheritsChipClass.
2. Implementation_ DefineInterface() Method to complete the interface declaration.
3. Implementation_ PowerOn() Method to receive power-on notifications from the Machine.
In addition, each module is completely free to play.
For more information about the new module relationship, see.
from Softchip import Chip
class Controller(Chip): def __init__(self, chipID): Chip.__init__(self, chipID) def _DefineInterface(self): outAPIList = [ u'connect_server', u'disconnect_server', ] inAPIList = [ (u'api_start_work', self.__apiStartWork), ] inEventList = [ (u'evt_logout_done', self.__evtOnLogoutDone), ] self.__interface = { Chip.TYPE_INT_API : inAPIList, Chip.TYPE_OUT_API : outAPIList, 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.__apiConnect = apiMap[u'connect_server'] self.__apiDisconnect = apiMap[u'disconnect_server'] 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 __evtOnLogoutDone(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 = self.__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 __MessageReceiveThread(): 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_connect_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 __evtOnConnectSucceeded(self): self.Show() def __evtOnChatClosed(self): self.__apiLogout() self._eHandler(u'evt_logout_done') def __OnLoginButtonPressed(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(self): outAPIList = [ u'invite_friend', u'send_message', u'log_out', u'disconnect_server', ] inEventList = [ (u'evt_login_succeeded', self.__evtOnLoginSucceeded ), (u'evt_message_arrived', self.__evtOnMessageReceived), ] self.__interface = { Chip.TYPE_OUT_API : outAPIList, 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 __evtOnLoginSucceeded(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: self._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')