您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 股票报告 > openwrt ubus简介
UbusBriefTheubusisdesignedforprovidingcommunicationbetweenvariousdaemonsandapplications.ThearchitectureasbelowUbusDaemonMessageDispatchUBUS_MSG_INVOKERegisteredObjectTableUbusConnectionInstanceserverUbusConnectionInstanceclientUbusConnectionInstanceUbusConnectionInstanceObjectInstanceUBUS_MSG_DTTAUBUS_MSG_NOTIFYSubscribedObjectTableUnixSocketUnixSocketRequestReplyCallMethodresponsecall……ObjectsandObjectpathsTheObjectpathsarebindingscannameobjectinstances,andallowapplicationstorefertothem.InOpenWRT,theobjectpathisnamespacelikenetwork.interface.lanMethodsandNotificationsMethodsareoperationsthatcanbeinvokedonanobject,withoptionalinputparametersandoutput.Notificationsarebroadcastsfromtheobjecttoanyinterestedobserversoftheobject.ThenotificationsmaycontainadatapayloadCallingamethodAmethodcallinubusconsistsoftwomessages;AcallmessagesfromprocessAtoprocessBandthereplymessagesfromprocessBtoprocessA.Thesendmessageandreplymessagesarebothroutedthroughtheubusdaemon.Thecallmessagecontainsthemethodarguments.Thereplymessagesmaybeerrormessages,ormaycontainmethodreturneddata.CallProcess1.Thecallmethodmessagescontainstheubusconnectioncontext,thedestinationobjectid,themethodname,themethodarguments.2.Themethodcallmessageissendtotheubusdaemon3.Theubusdaemonlookupthedestinationobjectid,ifaprocessownstheobjectinstance,thenthedaemonwillforwardthemethodcalltothefindprocess.Otherwisetheubusdaemoncreatesanerrormessagesandsendstheerrormessagebacktothemessagecallasreply.4.Thereceivingprocesswillparsetheubusobjectmessages,andfindthecallmethodandargumentsbelongtothemethod.Thenmatchtheobjectmethodsinobjectinstance,iffindmatchedmethod,willinvokethemethodandthensendthereplymessages.5.Ubusdaemonreceivethereplymessageandforwardthereplymessagetotheprocessthatmadethemethodcall.6.ThereplymessagesistransferredasubusblobmessagesstructurewhichisTLV(Type-Length-Value)basedbinarymessagestype.7.Theprocessreceivedthereplymessageshouldparsethemessageandformattohuman-nicemessagetypeasJSONorXML.NotifyNotificationsAnotificationinubusconsistsofasinglemessages,sendbyoneprocesstoanynumberofotherprocesses,whichmeansthenotificationisaunidirectionalbroadcast,noneedexpectedreplymessage.Thenotificationsenderdonotknowthenotificationsrecipients,itjustsendthenotificationontobusTheinterestrecipientsshouldsubscribethesenderobjectwiththebusdaemon.NotificationProcess1.Addnotificationobjectontoubusdaemon2.Thenotificationmessagecontainsubusconnectioncontext,thenotificationsenderobjectID,thenotificationtypeandoptionalargumentswiththetype.3.Anyprocessontheubuscansubscribethenotificationobject.Thebusmayhasalistofsubscribers,whichwillmatchtheobserverswhendaemonhandlethenotificationmessage.4.Theubusdaemoncheckthenotificationanddetermineswhichprocessesareinterestedinit.Thensendthenotificationtoalloftheinterestedprocesses.5.Eachsubscriberprocessreceivingthenotificationdecideswhattodowiththenotificationmessage.Blob_bufstructureonubusBlob_attrExtended(1bit)ID/Type(7bit)Data_len(24bit)DataBlob_msgExtended(1bit)ID/Type(7bit)Data_len(24bit)namelennamedataDataBlob_msgBlob_bufBlob_attrBlob_attrBlob_attrHowtouseubusServerMainprocessM1.DefineaobjectwithsomeabstractmethodsM2.Connecttheserverprocesstoubusdaemonandgetaubus_context,thecontextwillcontainedtheconnectedfd,registeredfdcallbackandanAVLtreetomanageallobjectsinformationwiththisconnectionM3.Usingulooputilitiestoaddtheubus_context,whichistoregistertheconnectedfdintoepollsetM4.AddthedefinedobjectintoubusdM5.ForeverlooptoepollthefdsetWhattodoinmethodhandlerH1.Parsetheblob_attrmsgintoablob_attrtable,whichcaneasyusingbyindexthetablebymsgIDH2.Getthemethodargumentsaccordingtomsgid,thehandlermaybecallmethodinanotherobjectsorinvokeashellscripttodosomeservice,etcH3.Preparetheresponsemsgintoblob_buffandsendtheresponsetoubusdaemon,whichwillforwardtheresponsetorequestclientifnotspecify“no_reply”or”deferred”flagH4.Ifspecify“deferred”flaginreqcontextinthemethodhandler,whichmeanstheserverprocesswillnotexpecttheresponseinthisrequesthandlerandjustcompletethisrequest.#includelibubox/blobmsg_json.h#includelibubus.hstaticstructubus_context*ctx;staticinttest_hello(structubus_context*ctx,structubus_object*obj,structubus_request_data*req,constchar*method,structblob_attr*msg){structhello_request*hreq;structblob_attr*tb[__HELLO_MAX];constchar*format=%sreceivedamessage:%s;constchar*msgstr=(unknown);//H1.Parsetheblob_attrmsg(blob_data(msg))intoablob_attr//table(tb),whichcaneasilyusebymsgIDtoindexthetableblobmsg_parse(hello_policy,ARRAY_SIZE(hello_policy),tb,blob_data(msg),blob_len(msg));//H2.GetmethodargumentsbymsgIDif(tb[HELLO_MSG])msgstr=blobmsg_data(tb[HELLO_MSG]);hreq=calloc(1,sizeof(*hreq)+strlen(format)+strlen(obj-name)+strlen(msgstr)+1);sprintf(hreq-data,format,obj-name,msgstr);//H4.Deferthereplyfortherequest//Thereplywillbemakingintimercallbackubus_defer_request(ctx,req,&hreq-req);hreq-timeout.cb=test_hello_reply;uloop_timeout_set(&hreq-timeout,1000);return0;}//Definehellomethodwithtest_hellohandle//hellopolicytellubusdtheobjectmethodparameterstyp
本文标题:openwrt ubus简介
链接地址:https://www.777doc.com/doc-4962718 .html