第五章分布对象技术主要内容z远程过程调用z面向对象技术z分布对象技术基础zCORBAzJava/RMI远程过程调用远程过程调用(RemoteProcedureCall)z远程过程调用(RPC)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC协议假定某些传输协议的存在,如TCP或UDP,为通信程序之间携带信息数据。zRPC采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息的到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用过程接收答复信息,获得进程结果,然后调用执行继续进行。RemoteProcedureCallproc1(arg1,arg2)ProcessAProcessBaremoteprocedurereturnvalueexecutionflowLocalProcedureCallandRemoteProcedureCallproc1executionflowproc2hostAAlocalprocedurecallhostAhostBAremoteprocedurecall(thereturnexecutionpathisnotshown)proc1proc2proxyproxy1.proc1onhostAmakesacalltoproc2onhostB.2.TheruntimesupportmapsthecalltoacalltotheproxyonhostA.3.TheproxymarshallsthedataandmakesanIPCcalltoaproxyonhostB.7.Theproxyreceivedthereturnvalue,unmarshallsthedata,andforwardsthereturnvaluetoproc1,whichresumesitsexecutionflow.4.TheproxyonhostBunmarshallsthedatareceivedandissuesacalltoproc2.5.Thecodeinproc2isexecutedandreturnstotheproxyonhostB.6.TheproxymarshallsthereturnvalueandmakesanIPCcalltotheproxyonhostA.RPC主流标准zSinceitsintroductionintheearly1980s,theRemoteProcedureCallmodelhasbeenwidelyinuseinnetworkapplications.zTherearetwoprevalentAPIsforthisparadigm.ztheONCRPC(OpenNetworkComputingRemoteProcedureCall),evolvedfromtheRPCAPIoriginatedfromSunMicrosystemsintheearly1980s.zTheotherwell-knownAPIistheOpenGroupDistributedComputingEnvironment(DCE)RPC.zBothAPIsprovideatool,rpcgen,fortransformingremoteprocedurecallstolocalprocedurecallstothestub.Ordinaryprocedure/functioncallzcount=read(fd,buf,nbytes)(a)Parameterpassinginalocalprocedurecall:thestackbeforethecalltoread(fd,buf,nbytes)(b)ThestackwhilethecalledprocedureisactiveRemoteProcedureCallzInDistributedsystems:thecalleemaybeonadifferentsystem-RemoteProcedurecall(RPC,允许程序调用位于其它机器上的进程)-NOEXPLICITMESSAGEPASSING(对编程人员)从而达到隐藏通信(send和receive),实现分布式系统中的访问透明性zGoal:MakeRPClook(asmuchaspossible)likelocalprocedurecall-allowremoteservicestobecalledasprocedures-callershouldnotbeawareofthefactthatthecalleeis(executing)onadifferentmachine(orviceversa)zAlthoughnomessagepassing(atuserlevel),parametersmuststillbepassed&resultsmuststillbereturned!RemoteProcedureCall(Cont.)Solution—apairofStubszClient-sidestubzLookslikelocalserverfunctionzSameinterfaceaslocalfunctionzBundlesargumentsintomessage,sendstoserver-sidestubzWaitsforreply,un-bundlesresultszreturnszServer-sidestubzLookslikelocalclientfunctiontoserverzListensonasocketformessagefromclientstubzUn-bundlesargumentstolocalvariableszMakesalocalfunctioncalltoserverzBundlesresultintoreplymessagetoclientstubSolution—apairofStubs(Cont.)z比如服务端有某个函数Echo(),它为了能够被远程调用,需要通过编译器生成两个stub:客户端的一个stub:Client_StubEcho()服务器端的一个stub:Server_StubEcho()z在客户端,一个进程在执行过程中调用到了函数Echo(),此函数的具体实现是在远程的某台机器上,那么此进程实际上是调用了位于当地机器上的另外一个版本的Echo()(即Client_StubEcho())。当客户端的消息发送到服务器端时,服务器端也不是把消息直接就交给真正的Echo(),而是同样先交给一个不同版本的Echo()(即Server_StubEcho())ResultzThehardworkofbuildingmessages,formatting,uniformrepresentation,etc.,isburiedinthestubszWhereitcanbeautomated!zClientandserverdesignerscanconcentrateonthesemanticsofapplicationzProgramsbehaveinfamiliarwayStepsofaremoteprocedurecall1.Clientprocedurecallsclientstubinnormalway2.Clientstubbuildsmessage,callslocalOS3.Client’sOSsendsmessagetoremoteOS4.RemoteOSgivesmessagetoserverstub5.Serverstubunpacksparameters,callsserver6.Serverdoeswork,returnsresulttothestub7.Serverstubpacksitinmessage,callslocalOS8.Server’sOSsendsmessagetoclient’sOS9.Client’sOSgivesmessagetoclientstub10.Stubunpacksresult,returnstoclientPassingValueParameterszStepsinvolvedindoingremotecomputationthroughRPCRPCModelzAserverdefinestheserviceinterfaceusinganinterfacedefinitionlanguage(IDL)ztheIDLspecifiesthenames,parameters,andtypesforallclient-callableserverprocedureszAstubcompilerreadstheIDLdeclarationsandproducestwostubfunctionsforeachserverfunctionzServer-sideandclient-sideRPCModel(continued)zLinking:zServerprogrammerimplementstheservice’sfunctionsandlinkswiththeserver-sidestubszClientprogrammerimplementstheclientprogramandlinksitwithclient-sidestubszOperation:zStubsmanageallofthedetailsofremotecommunicationbetweenclientandserverRPCStubszAclient-sidestubisafunctionthatlookstotheclientasifitwereacallableserverfunctionzI.e.,sameAPIastheserver’simplementationofthefunctionzAserver-sidestublookslikeacallertotheserverzI.e.,likeahunkofcodeinvokingtheserverfunctionzTheclientprogramthinksit’sinvokingtheserverzbutit’scallingintotheclient-sidestubzTheserverprogramthinksit’scalledbytheclientzbutit’sreallycalledbytheserver-sidestubzThestubssendmessagestoeachothertomaketheRPChappentransparently(almost!)MarshallingArgumentszMarshallingisthepackingoffunctionparametersintoamessagepacketztheRPCstubscalltype-specificfunctionstomarshalorunmarshaltheparametersofanRPCzClientstubmarshalstheargumentsintoamessagezServerstubunmarshalstheargumentsandusesthemtoinvoketheservicefunctionzonreturn:ztheserverstubmarshalsreturnvaluesztheclientstubunmarshalsreturnvalues,andreturnstotheclientprogramRepresentationofDatazIDLmustalsodefinere