def createCodeInterface(e,sw,idt,idx): global interface_defs,ifcnt print '..createCodeInterface():',e.name module = e.container # ifidx = e.ifidx ifidx = ifcnt ifcnt+=1 # ifname = "%s.%s"%(module.name,e.name) # r = tce_util.getInterfaceIndexWithName(ifname) # # if r != -1: # ifidx = r # # e.ifidx = ifidx ifidx = e.ifidx interface_defs[ifidx] = {'e':e,'f':{}} fileifx.write('<if id="%s" name="%s.%s"/>\n'%(ifidx,module.name,e.name)) fileifx.flush() # tce_util.rebuildFunctionIndex(e) # # #???????? createProxy(e,sw,ifidx) # r = tce_util.isExposeDelegateOfInterfaceWithName(ifname) # e.delegate_exposed = r # if not e.delegate_exposed: # print 'Interface:(%s) delegate not be exposed.'%ifname # return if not tce_util.generateSkeleton(e,lexparser.language): return sw.wln() sw.writeln('// class %s'%e.getName()) sw.writeln('function %s(){'%e.getName() ).idt_inc() sw.writeln("//# -- INTERFACE -- ") sw.writeln('this.delegate = new %s_delegate(this);'%e.getName()) # sw.scope_end().wln() # end construct function #??servant ???? for m in e.list: # function list sw.wln() params=[] for p in m.params: params.append( (p.id,p.type.getMappingTypeName(module)) ) list =[] for v,t in params: list.append('%s'%(v)) s = string.join( list,',') if s: s += ',' sw.writeln('//public %s %s(%stce.RpcContext ctx){'%(m.type.getMappingTypeName(module),m.name,s ) ) sw.writeln('this.%s = function(%sctx){'%(m.name,s ) ).idt_inc() #------------????????---------------------- if isinstance( m.type ,Builtin ): if m.type.name =='void': # sw.idt_dec().wln() sw.scope_end() continue else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(module)) elif isinstance(m.type,Sequence): sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) # elif isinstance(m.type,Dictionary): # sw.writeln("return %s;"%m.type.getTypeDefaultValue() ) else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) sw.scope_end2() sw.scope_end() # end class # sw.classfile_leave() # sw.pkg_end() #------- ?????? --------------------------- sw.wln() #???????? sw.writeln("function %s_delegate(inst) {"%e.getName()).idt_inc() sw.wln() sw.writeln('this.inst = inst;') sw.writeln('this.ifidx = %s;'%ifidx) # sw.wln() #???? # sw.writeln("public %s_delegate(%s inst,adapter:CommAdapter=null,conn:tce.RpcConnection=null){"%(e.getName(),e.getName() )).idt_inc() # sw.writeln("public %s_delegate(%s inst){"%(e.getName(),e.getName() )).idt_inc() # sw.writeln('this.ifidx = %s;'%ifidx) # sw.writeln('this.id = ""; ') #????? # sw.writeln("this.adapter = adapter;") # sw.writeln('this.index = "%s";'%e.getName() ) #???xml?????adapter # for opidx,m in enumerate(e.list): # function list # sw.writeln("this.optlist.put(%s,this.%s);"%(opidx,m.name)) #???? twoway ? oneway ???? # sw.writeln("this.inst = inst;") # sw.scope_end().wln() # finish construct() #??invoke()?? # sw.writeln("@Override") sw.writeln("this.invoke = function(m){").idt_inc() # sw.writeln('boolean r=false;') # sw.writeln('tce.RpcMessageXML m = (tce.RpcMessageXML)m_;') for opidx,m in enumerate(e.list): opidx = m.index sw.writeln('if(m.opidx == %s ){'%opidx).idt_inc() sw.writeln('return this.func_%s_delegate(m);'%opidx ) sw.scope_end() sw.writeln('return false;') sw.scope_end2() # end - invoke() sw.wln() #???? ???? for opidx,m in enumerate(e.list): # function list opidx = m.index sw.writeln('this.func_%s_delegate = function(m){'%(opidx) ).idt_inc() params=[ ] sw.writeln('var r = false;') sw.writeln('var pos =0;') sw.writeln('var array = null;') sw.writeln('var view = null;') # sw.writeln('r = false;') if m.params: sw.writeln('array = new Uint8Array(m.paramstream);') sw.writeln('view = new DataView(array.buffer);') for p in m.params: if isinstance(p.type,Builtin): sw.define_var(p.id,p.type.getMappingTypeName(module)) Builtin_Python.unserial(p.type,p.id,sw,'view','pos') elif isinstance(p.type,Sequence) or isinstance(p.type,Dictionary): # sw.define_var(p.id,p.type.getMappingTypeName(module),'new %s()'%p.type.getMappingTypeName(module)) if isinstance(p.type,Sequence) and p.type.type.name == 'byte': # sw.define_var(p.id,p.type.getMappingTypeName(module),p.type.getTypeDefaultValue(module)) sw.writeln('var %s = null;'%p.id) v1 = sw.newVariant('_len') sw.writeln('var %s = view.getUint32(pos);'%v1) sw.writeln('pos+=4;') v2 = sw.newVariant('_v') sw.writeln('var %s = new Uint8Array(view.buffer,pos,%s)'%(v2,v1)) sw.writeln('%s = %s.buffer.slice(pos,pos+%s);'%(p.id,v2,v1)) sw.writeln('pos += %s;'%v1) else: sw.define_var(p.id,p.type.getMappingTypeName(module),p.type.getTypeDefaultValue(module)) c = sw.newVariant('_array') sw.define_var(c,'%shlp'%p.type.name,'new %shlp(%s)'%(p.type.name,p.id)) sw.writeln('pos=%s.unmarshall(view,pos);'%c) else: sw.define_var(p.id,p.type.getMappingTypeName(module),'new %s()'%p.type.getMappingTypeName(module)) sw.writeln('pos= %s.unmarshall(view,pos);'%p.id) params.append(p.id) #params = map( lambda x: '_p_'+x,params) ps = string.join(params,',') if ps: ps = ps + ',' sw.define_var('servant',e.getName(),'this.inst') sw.writeln('var ctx = new tce.RpcContext();') sw.writeln('ctx.msg = m;') if isinstance(m.type,Builtin) and m.type.type =='void': # none return value sw.writeln("servant.%s(%sctx);"%(m.name,ps) ) else: sw.define_var('cr',m.type.getMappingTypeName(module)) sw.writeln("cr = servant.%s(%sctx);"%(m.name,ps) ) sw.writeln("if( (m.calltype & tce.RpcMessage.ONEWAY) !=0 ){").idt_inc() sw.writeln("return true;") #????????? sw.scope_end() sw.wln() #????? # if m.type.name !='void': sw.define_var('mr','tce.RpcMessage','new tce.RpcMessage(tce.RpcMessage.RETURN)') sw.writeln('mr.sequence = m.sequence;') sw.writeln('mr.callmsg = m;') # sw.writeln("m.sequence = ctx.msg.sequence;") #??????????????? # sw.writeln('try{').idt_inc() # sw.writeln('ByteArrayOutputStream bos = new ByteArrayOutputStream();') # sw.writeln('DataOutputStream dos = new DataOutputStream(bos);') # sw.writeln('String xml="";') sw.writeln('var size = 0;') sw.writeln('pos=0;') if isinstance( m.type ,Builtin ) and m.type.name!='void': Builtin_Python.getsize(m.type,'cr',sw) sw.writeln('array = new ArrayBuffer(size);') sw.writeln('view = new DataView(array);') Builtin_Python.serial(m.type,'cr',sw,'view','pos') elif isinstance(m.type,Sequence) or isinstance(m.type,Dictionary): if isinstance(m.type,Sequence) and m.type.type.name == 'byte': sw.writeln('size += 4 + cr.byteLength;') sw.writeln('array = new ArrayBuffer(size);') sw.writeln('view = new DataView(array);') sw.writeln('view.setUint32(pos,cr.byteLength);') sw.writeln('pos += 4;') v1 = sw.newVariant('_v') sw.writeln('var %s = new Uint8Array(view.buffer);'%(v1,)) v2 = sw.newVariant('_v') sw.writeln('var %s = new Uint8Array(cr);'%(v2,)) sw.writeln('%s.set(%s,pos);'%(v1,v2) ) # sw.writeln('pos += %s.buffer.byteLength;'%v2) else: v = sw.newVariant('_c') sw.define_var(v,'%shlp'%m.type.name,'new %shlp(cr)'%m.type.name) sw.writeln('size+=%s.getsize();'%v) sw.writeln('array = new ArrayBuffer(size);') sw.writeln('view = new DataView(array);') sw.writeln('pos+=%s.marshall(view,pos);'%v) else: sw.writeln('size += cr.getsize();') sw.writeln('array = new ArrayBuffer(size);') sw.writeln('view = new DataView(array);') sw.writeln("cr.marshall(view,pos);") sw.writeln('mr.paramsize = 1;') sw.writeln('mr.paramstream = array;') sw.idt_dec().writeln('}catch(e){').idt_inc() sw.writeln('console.log(e.toString());') sw.writeln('r = false;') sw.writeln('return r;') sw.scope_end() sw.writeln("r =m.conn.sendMessage(mr);") #???? sw.writeln("return r;") sw.scope_end2() # end servant function{} sw.wln() sw.scope_end() # end class define # sw.classfile_leave() # sw.pkg_end() # createProxy(e,sw,ifidx) return
def createCodeInterface(e, sw, idt, idx): global interface_defs, ifcnt ifidx = ifcnt ifcnt += 1 import tce_util # ifname = "%s.%s"%(e.container.name,e.name) # r = tce_util.getInterfaceIndexWithName(ifname) # # print 'get if-index:%s with-name:%s'%(r,ifname) # if r != -1: # ifidx = r # # e.ifidx = ifidx ifidx = e.index interface_defs[ifidx] = {"e": e, "f": {}} fileifx.write('<if id="%s" name="%s"/>\n' % (ifidx, e.name)) fileifx.flush() # tce_util.rebuildFunctionIndex(e) # expose = tce_util.isExposeDelegateOfInterfaceWithName(ifname) expose = tce_util.generateSkeleton(e, lexparser.language) # if e.delegate_exposed: if expose: sw.writeln("class %s_delegate;" % e.getName()) createServant(e, sw, idx, ifidx) createServantDelegate(e, sw, idx, ifidx) # ----------- RpcAsyncCallBackBase --------------- createAsyncCallBack(e, sw) # ----------- END RpcAsyncCallBackBase --------------- sw.wln() sw.writeln("typedef boost::shared_ptr<%sPrx> %sPrxPtr;" % (e.getName(), e.getName())) sw.writeln("class %sPrx: public tce::RpcProxyBase{" % e.getName()).idt_inc() sw.writeln("//# -- INTERFACE PROXY -- ") # sw.writeln('var conn:RpcConnection ;') # sw.writeln('void* delta;') sw.writeln("public:") sw.wln() sw.writeln("%sPrx(){}" % e.getName()) sw.writeln("%sPrx( boost::shared_ptr<tce::RpcConnection> & conn){" % (e.getName())).idt_inc() sw.writeln("this->conn = conn;") sw.scope_end() sw.writeln("~%sPrx(){ if(this->conn.get()){ this->conn->close();}}" % e.getName()) sw.writeln("public:").wln() # sw.writeln('boost::shared_ptr<tce::RpcConnection> conn') for opidx, m in enumerate(e.list): # function list opidx = m.index sw.wln() interface_defs[ifidx]["f"][opidx] = m # ????????? createFunc_Twoway(e, sw, m, idx, ifidx, opidx) createFunc_Async(e, sw, m, idx, ifidx, opidx) createFunc_Oneway(e, sw, m, idx, ifidx, opidx) createFunc_AsyncParserHlp(e, sw, m, idx, ifidx, opidx) createFunc_AsyncParser(e, sw, m, idx, ifidx, opidx) sw.writeln("public:") sw.writeln("static %sPrxPtr create(tce::RpcConnectionPtr& conn){" % (e.getName())).idt_inc() sw.writeln("return %sPrxPtr( new %sPrx(conn) );" % (e.getName(), e.getName())) sw.scope_end() sw.wln() # ????????true,??uri????????????? connection::isConnected()????? sw.writeln("// async ??????? ") sw.writeln( "static %sPrxPtr create(const std::string& uri,tce::RpcConnection::Types conntype=tce::RpcConnection::SOCKET, bool sync=false,const tce::Properties_t& props = tce::Properties_t()){" % (e.getName()) ).idt_inc() sw.writeln("tce::RpcConnectionPtr conn ;") sw.writeln("conn = tce::RpcCommunicator::instance().createConnection(conntype,uri,props);") sw.writeln("bool r = false;") sw.writeln("r = conn->connect();") sw.writeln("if(!r) return %sPrxPtr();" % e.getName()) sw.writeln("return %sPrxPtr( new %sPrx(conn) );" % (e.getName(), e.getName())) sw.scope_end() sw.struct_end() # end class '};' # sw.classfile_leave() # -- begin ???? --- # -- end ???? --- return
def createCodeInterface(e,sw,idt,idx): global interface_defs,ifcnt ifidx = ifcnt ifcnt+=1 # ifidx = e.ifidx #?????????( ?? ) module = e.container #-------- index of if-cls from extern setting in file import tce_util # ifname = "%s.%s"%(module.name,e.name) # r = tce_util.getInterfaceIndexWithName(ifname) # # print 'get if-index:%s with-name:%s'%(r,ifname) # if r != -1: # ifidx = r # #--- end # e.ifidx = ifidx # print 'if-index:',ifidx ifidx = e.index fileifx.write('<if id="%s" name="%s.%s"/>\n'%(ifidx,module.name,e.name)) fileifx.flush() # tce_util.rebuildFunctionIndex(e) interface_defs[ifidx] = {'e':e,'f':{}} createProxy(e,sw,ifidx) # if not e.delegate_exposed: #????????,??????????RPC???????filter # return # expose = tce_util.isExposeDelegateOfInterfaceWithName(ifname) # if not expose: # return # if not tce_util.generateSkeleton(e,lexparser.language): return sw.setstream(H_FILE) sw.wln() sw.resetVariant() sw.writeln('@class %s_delegate;'%e.getName()) sw.writeln('@interface %s : RpcServant'%e.getName()).idt_inc() sw.wln() sw.writeln('@property %s_delegate * delegate;'%e.getName() ) for m in e.list: # function list params=[] for n in range(len(m.params)): p = m.params[n] if n!=0: params.append('%s:(%s)%s'%(p.id,p.type.getMappingTypeName(module),p.id)) else: params.append( '(%s)%s'%(p.type.getMappingTypeName(module),p.id) ) ss = string.join( params,' ') if ss: ss =':'+ ss +' context:' else: ss = ':' sw.writeln('-(%s) %s%s(RpcContext*)ctx;'%(m.type.getMappingTypeName(module),m.name,ss)) sw.wln().idt_dec().writeln('@end') sw.setstream(MM_FILE) sw.wln() sw.resetVariant() sw.writeln('@implementation %s'%e.getName()) sw.wln() sw.writeln('-(id) init{').idt_inc() sw.writeln('self = [super init];') sw.writeln('self.delegate = [%s_delegate new];'%e.getName()) sw.writeln('self.delegate.servant = self;') sw.writeln('return self;') sw.scope_end() sw.wln() sw.writeln('-(RpcServantDelegate*) getDelegate{').idt_inc() sw.writeln('return self.delegate;') sw.scope_end() #??servant ???? for m in e.list: # function list sw.wln() params=[] for n in range(len(m.params)): p = m.params[n] if n!=0: params.append('%s:(%s)%s'%(p.id,p.type.getMappingTypeName(module),p.id)) else: params.append( '(%s)%s'%(p.type.getMappingTypeName(module),p.id) ) ss = string.join( params,' ') if ss: ss =':'+ ss +' context:' else: ss = ':' sw.writeln('-(%s) %s%s(RpcContext*)ctx{'%(m.type.getMappingTypeName(module),m.name,ss)).idt_inc() if isinstance( m.type ,Builtin ): if m.type.name =='void': sw.scope_end() continue else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(module)) elif isinstance(m.type,Sequence): sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) elif isinstance(m.type,Dictionary): sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) sw.scope_end() sw.wln().writeln('@end') #begin delegate() ---- sw.setstream(H_FILE) sw.wln() sw.writeln('@interface %s_delegate:RpcServantDelegate'%e.getName()).idt_inc() # sw.writeln("public class %s_delegate extends RpcServantDelegate {"%e.getName()).idt_inc() sw.wln() sw.writeln('@property %s servant;'%e.getTypeName(module)) #??invoke()?? sw.wln() sw.writeln('-(void) invoke:(RpcMessage*)m;') sw.wln() sw.idt_dec() sw.writeln('@end') sw.setstream(MM_FILE) sw.resetVariant() sw.wln() sw.writeln('@implementation %s_delegate'%e.getName()) sw.wln() sw.writeln('-(id) init{').idt_inc() sw.writeln('self=[super init];') sw.writeln('self.ifidx = %s;'%ifidx) sw.writeln('return self;') sw.scope_end().wln() sw.writeln('-(void) invoke:(RpcMessage*)m{').idt_inc() for opidx,m in enumerate(e.list): opidx = m.index sw.writeln('if(m.opidx == %s ){'%opidx).idt_inc() sw.writeln('[self func_%s_delegate:m];'%m.name ) sw.scope_end() sw.scope_end() #???? ???? for opidx,m in enumerate(e.list): # function list opidx = m.index # sw.writeln('boolean func_%s_delegate(RpcMessage m){'%(m.name) ).idt_inc() sw.wln() sw.resetVariant() m1 = sw.newVariant('m1') sw.writeln('-(void)func_%s_delegate:(RpcMessage*)%s{'%(m.name,m1) ).idt_inc() params=[ ] if m.params: # sw.writeln('RpcByteArray* bar =[RpcByteArray new];') sw.writeln('RpcByteArray* bar = %s.content;'%m1) for p in m.params: if isinstance(p.type,Builtin): sw.define_var(p.id,p.type.getMappingTypeName(module)) Builtin_Python.unserial(p.type,p.id,sw,'bar') elif isinstance(p.type,Sequence) or isinstance(p.type,Dictionary): impled = False #print p.type,p.type.type.name if isinstance(p.type,Sequence): if p.type.type.name == 'byte': sw.define_var(p.id,p.type.getMappingTypeName(module),p.type.getTypeDefaultValue(module)) sw.writeln('%s = [bar readData];'%p.id) impled = True if not impled: sw.define_var(p.id,p.type.getMappingTypeName(module),p.type.getTypeDefaultValue(module)) c = sw.newVariant('c') sw.define_var(c,'%shlp*'%p.type.name,'[%shlp new]'%(p.type.name)) sw.writeln('%s.data = %s;'%(c,p.id)) sw.writeln('[%s unmarshall: bar];'%c) else: sw.define_var(p.id,p.type.getMappingTypeName(module),p.type.getTypeDefaultValue(module)) sw.writeln('[%s unmarshall:bar];'%p.id) params.append(p.id) #params = map( lambda x: '_p_'+x,params) list =[] for n in range(len(m.params)): p = m.params[n] if n!=0: list.append('%s:%s'%(p.id,p.id) ) else: list.append('%s'%(p.id) ) ss = string.join( list,' ') # if ss: # ss+=' context:ctx' # else: # ss+=':ctx' if ss: ss =':'+ ss +' context:' else: ss = ':' succ = sw.newVariant('succ') sw.define_var(succ,'int','RPCERROR_SUCC'); cr = 'undefined' sw.writeln('RpcContext* ctx = [RpcContext new];') sw.writeln('ctx.msg = %s;'%m1) if m.type.name !='void': # none return value cr = sw.newVariant('cr') sw.define_var(cr,m.type.getMappingTypeName(module)) sw.writeln('@try{').idt_inc() if isinstance(m.type,Builtin) and m.type.type =='void': # none return value sw.writeln("[self.servant %s%sctx];"%(m.name,ss) ) else: sw.writeln("%s = [self.servant %s%sctx];"%(cr,m.name,ss) ) sw.idt_dec().writeln('} @catch(NSException *exception){').idt_inc() sw.writeln('%s=RPCERROR_REMOTEMETHOD_EXCEPTION;'%succ) sw.writeln('NSLog(@"%@",exception);') sw.scope_end() #--????????? sw.writeln("if( (%s.calltype & RpcMsgCallType_ONEWAY) !=0 ){"%m1).idt_inc() sw.writeln("return ;") #????????? sw.scope_end() sw.wln() #????? # sw.writeln('if(%s == nil){'%cr).idt_inc() # sw.writeln('%s = %s;'%(cr,m.type.getTypeDefaultValue(module))) # sw.scope_end() if True: # if m.type.name !='void': m2 = sw.newVariant('m2') sw.define_var(m2,'RpcMessage*','[[RpcMessage alloc] initWithCallType: RpcMsgCallType_RETURN]') sw.writeln('%s.sequence = %s.sequence;'%(m2,m1)) sw.writeln('%s.callmsg = %s;'%(m2,m1)) sw.writeln('%s.conn = %s.conn;'%(m2,m1)) sw.writeln('%s.ifidx = %s.ifidx;'%(m2,m1)) sw.writeln('%s.call_id = %s.call_id;'%(m2,m1)) sw.writeln('%s.errcode = %s;'%(m2,succ)) v = sw.newVariant('temp') sw.writeln('NSString* %s = [%s.extra.properties objectForKey:@"__user_id__"];'%(v,m1)) sw.writeln('if( %s !=nil){'%v).idt_inc() sw.writeln('[%s.extra.properties setObject: %s forKey:@"__user_id__"];'%(m2,v)) # sw.writeln('if(m.extra.getProperties().containsKey("__user_id__")){').idt_inc() # sw.writeln('mr.extra.setPropertyValue("__user_id__",m.extra.getPropertyValue("__user_id__"));') sw.scope_end() sw.writeln('if(%s.errcode !=RPCERROR_SUCC){'%m2).idt_inc() sw.writeln("[%s.conn sendMessage:%s];"%(m1,m2)) sw.writeln('return ;') sw.scope_end() if m.type.name !='void': # sw.writeln("m.sequence = ctx.msg.sequence;") #??????????????? # ?????? bar = sw.newVariant('bar') sw.writeln('RpcByteArray* %s=[RpcByteArray new];'%bar) if isinstance( m.type ,Builtin ) and m.type.name!='void': Builtin_Python.serial(m.type,cr,sw,bar) elif isinstance(m.type,Sequence) or isinstance(m.type,Dictionary): impled = False if isinstance(m.type,Sequence): if m.type.type.name == 'byte': sw.writeln('[%s writeData:%s];'%(bar,cr)) # sw.writeln('%s.writeInt(%s.length);'%('dos','cr')) # sw.writeln('%s.write(%s,0,%s.length);'%('dos','cr','cr')) impled = True if not impled: c = sw.newVariant('c') sw.define_var(c,'%shlp*'%m.type.name,'[%shlp new]'%m.type.name) sw.writeln('%s.data = %s;'%(c,cr) ) sw.writeln('[%s marshall:%s];'%(c,bar)) else: sw.writeln("[%s marshall:%s];"%(cr,bar) ) sw.writeln('%s.paramsize = 1;'%m2) sw.writeln('%s.content = %s;'%(m2,bar)) else: sw.writeln('%s.paramsize = 0;'%m2) sw.writeln("[%s.conn sendMessage:%s];"%(m1,m2)) sw.scope_end() # end servant function{} # sw.wln() # sw.scope_end() # end fun_xxx_delegate # sw.scope_end() # end invoke() function -- sw.wln() sw.writeln('@end') # sw.classfile_leave() return
def createCodeInterface(e,sw,idt,idx): global interface_defs,ifcnt ifidx = e.ifidx ifcnt+=1 import tce_util # ifname = "%s.%s"%(e.container.name,e.name) # r = tce_util.getInterfaceIndexWithName(ifname) # if r != -1: # ifidx = r # #--- end # e.ifidx = ifidx ifidx = e.index interface_defs[ifidx] = {'e':e,'f':{}} createInterfaceProxy(e,sw,ifidx) # expose = tce_util.isExposeDelegateOfInterfaceWithName(ifname) # if not expose: # return if not tce_util.generateSkeleton(e,lexparser.language): return sw.classfile_enter(e.getName()) sw.writeln('import tcelib.RpcServant;') sw.writeln('import tcelib.RpcContext;') sw.writeln('import %s.%s_delegate;'%(sw.pkg_current(),e.getName()) ) sw.wln() sw.writeln('public class %s extends RpcServant{'%e.getName() ) sw.idt_inc() sw.writeln("//# -- INTERFACE -- ") # sw.writeln('var delegatecls:Class = %s_delegate'%e.getName()) #?????delegate ??? sw.writeln("public function %s(){"%e.getName() ).idt_inc() sw.writeln('this.delegate = new %s_delegate(this);'%e.getName()) sw.scope_end().wln() for m in e.list: # function list sw.wln() params=[] for p in m.params: params.append( (p.id,p.type.getMappingTypeName(e.container)) ) list =[] for v,t in params: list.append('%s:%s'%(v,t)) s = string.join( list,',') if s: s += ',' sw.writeln('public function %s(%sctx:RpcContext = null):%s{'%(m.name,s,m.type.getMappingTypeName(e.container) ) ).idt_inc() #------------????????---------------------- if isinstance( m.type ,Builtin ): if m.type.type =='void': # sw.idt_dec().wln() sw.scope_end() continue else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(e.container)) elif isinstance(m.type,Sequence): sw.writeln("return %s;"%m.type.getTypeDefaultValue(e.container) ) elif isinstance(m.type,Dictionary): sw.writeln("return %s;"%m.type.getTypeDefaultValue(e.container) ) else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(e.container) ) sw.scope_end() sw.scope_end() # end class sw.classfile_leave() # sw.pkg_end() #---------------------------------- sw.classfile_enter("%s_delegate"%e.getName()) sw.writeln('import tcelib.*;') sw.writeln("import tcelib.utils.HashMap;") sw.writeln("import flash.utils.ByteArray;") sw.writeln("import flash.utils.Endian;") sw.writeln("import tcelib.RpcServantDelegate;") # sw.writeln("import %s;"%e.getName()) sw.wln() #???????? sw.writeln("public class %s_delegate extends RpcServantDelegate {"%e.getName()).idt_inc() sw.wln() # sw.writeln('var index:uint = %s;'%ifidx) # sw.writeln('var id:String;') # sw.writeln('var adapter:RpcCommAdapter = null;') sw.writeln('public var conn:RpcConnection = null;') # sw.writeln('var inst:%s = null;'%(e.getName())) sw.wln() sw.writeln("public function %s_delegate(inst:%s,adapter:RpcCommAdapter=null,conn:RpcConnection=null){"%(e.getName(),e.getName() )).idt_inc() # sw.writeln('this.id = ""; ') #????? sw.writeln("this.adapter = adapter;") sw.writeln('this.index = %s;'%ifidx) for opidx,m in enumerate(e.list): # function list opidx = m.index sw.writeln("this.optlist.put(%s,this.%s);"%(opidx,m.name)) #???? twoway ? oneway ???? sw.writeln("this.inst = inst;") sw.scope_end().wln() #???? ???? for opidx,m in enumerate(e.list): # function list opidx = m.index sw.writeln('public function %s(ctx:RpcContext = null):Boolean{'%(m.name) ).idt_inc() params=[ ] sw.define_var('d','ByteArray') sw.writeln("d = ctx.msg.paramstream; ") sw.writeln('var r:Boolean = false;') #sw.writeln("idx = 0") #?????????? _p_?? for p in m.params: if isinstance(p.type,Builtin): sw.define_var(p.id,p.type.getMappingTypeName(e.container)) Builtin_Python.unserial(p.type.type,p.id,'d',idt,sw) elif isinstance(p.type,Sequence) or isinstance(p.type,Dictionary): sw.define_var(p.id,p.type.getMappingTypeName(e.container),'new %s()'%p.type.getMappingTypeName(e.container)) sw.scope_begin() sw.define_var('_c','%shlp'%p.type.name,'new %shlp(%s)'%(p.type.name,p.id)) sw.writeln('r = _c.unmarshall(d);') sw.writeln('if(!r) return false;') sw.scope_end() else: sw.define_var(p.id,p.type.getMappingTypeName(e.container),'new %s()'%p.type.getMappingTypeName(e.container)) sw.writeln('r = %s.unmarshall(d);'%p.id) sw.writeln('if(!r) return false;') params.append(p.id) #params = map( lambda x: '_p_'+x,params) ps = string.join(params,',') if ps: ps = ps + ',' sw.define_var('servant',e.getName(),'this.inst as %s'%e.getName()) if isinstance(m.type,Builtin) and m.type.type =='void': sw.writeln("servant.%s(%sctx)"%(m.name,ps) ) else: sw.define_var('cr',m.type.getMappingTypeName(e.container)) sw.writeln("cr = servant.%s(%sctx)"%(m.name,ps) ) # sw.writeln("if cr == None:").idt_inc() # if isinstance(m.type,Sequence): # sw.writeln("cr = []") # sw.idt_dec() # elif isinstance(m.type,Dictionary): # sw.writeln("cr = {}") # sw.idt_dec() # else: # sw.writeln("pass").idt_dec() sw.writeln("if( ctx.msg.calltype & tcelib.RpcMessage.CALL_ONE_WAY){").idt_inc() sw.writeln("return false;") sw.scope_end() sw.wln() # sw.define_var('d','ByteArray','new ByteArray();') sw.writeln('d = new ByteArray();') sw.writeln('d.endian = Endian.BIG_ENDIAN;') sw.define_var('m','RpcMessageReturn','new RpcMessageReturn()') sw.writeln("m.sequence = ctx.msg.sequence;") #??????????????? if isinstance( m.type ,Builtin ): Builtin_Python.serial(m.type.type,'cr',idt,sw) elif isinstance(m.type,Sequence) or isinstance(m.type,Dictionary): sw.scope_begin() v = sw.newVariant('_c') sw.define_var(v,'%shlp'%m.type.name,'new %shlp(cr)'%m.type.name) sw.writeln('%s.marshall(d);'%v) sw.scope_end() else: sw.writeln("cr.marshall(d);") sw.writeln("if(d.length) m.addParam(d);") sw.writeln("ctx.conn.sendMessage(m);") sw.writeln("return true;") sw.scope_end() sw.wln() sw.scope_end() sw.classfile_leave()
def createCodeInterface(e, sw, idt, idx): global interface_defs, ifcnt ifidx = e.ifidx ifcnt += 1 import tce_util # ifname = "%s.%s"%(e.container.name,e.name) # r = tce_util.getInterfaceIndexWithName(ifname) # if r != -1: # ifidx = r # #--- end # e.ifidx = ifidx ifidx = e.index interface_defs[ifidx] = {'e': e, 'f': {}} createInterfaceProxy(e, sw, ifidx) # expose = tce_util.isExposeDelegateOfInterfaceWithName(ifname) # if not expose: # return if not tce_util.generateSkeleton(e, lexparser.language): return sw.classfile_enter(e.getName()) sw.writeln('import tcelib.RpcServant;') sw.writeln('import tcelib.RpcContext;') sw.writeln('import %s.%s_delegate;' % (sw.pkg_current(), e.getName())) sw.wln() sw.writeln('public class %s extends RpcServant{' % e.getName()) sw.idt_inc() sw.writeln("//# -- INTERFACE -- ") # sw.writeln('var delegatecls:Class = %s_delegate'%e.getName()) #?????delegate ??? sw.writeln("public function %s(){" % e.getName()).idt_inc() sw.writeln('this.delegate = new %s_delegate(this);' % e.getName()) sw.scope_end().wln() for m in e.list: # function list sw.wln() params = [] for p in m.params: params.append((p.id, p.type.getMappingTypeName(e.container))) list = [] for v, t in params: list.append('%s:%s' % (v, t)) s = string.join(list, ',') if s: s += ',' sw.writeln( 'public function %s(%sctx:RpcContext = null):%s{' % (m.name, s, m.type.getMappingTypeName(e.container))).idt_inc() #------------????????---------------------- if isinstance(m.type, Builtin): if m.type.type == 'void': # sw.idt_dec().wln() sw.scope_end() continue else: sw.writeln("return %s;" % m.type.getTypeDefaultValue(e.container)) elif isinstance(m.type, Sequence): sw.writeln("return %s;" % m.type.getTypeDefaultValue(e.container)) elif isinstance(m.type, Dictionary): sw.writeln("return %s;" % m.type.getTypeDefaultValue(e.container)) else: sw.writeln("return %s;" % m.type.getTypeDefaultValue(e.container)) sw.scope_end() sw.scope_end() # end class sw.classfile_leave() # sw.pkg_end() #---------------------------------- sw.classfile_enter("%s_delegate" % e.getName()) sw.writeln('import tcelib.*;') sw.writeln("import tcelib.utils.HashMap;") sw.writeln("import flash.utils.ByteArray;") sw.writeln("import flash.utils.Endian;") sw.writeln("import tcelib.RpcServantDelegate;") # sw.writeln("import %s;"%e.getName()) sw.wln() #???????? sw.writeln("public class %s_delegate extends RpcServantDelegate {" % e.getName()).idt_inc() sw.wln() # sw.writeln('var index:uint = %s;'%ifidx) # sw.writeln('var id:String;') # sw.writeln('var adapter:RpcCommAdapter = null;') sw.writeln('public var conn:RpcConnection = null;') # sw.writeln('var inst:%s = null;'%(e.getName())) sw.wln() sw.writeln( "public function %s_delegate(inst:%s,adapter:RpcCommAdapter=null,conn:RpcConnection=null){" % (e.getName(), e.getName())).idt_inc() # sw.writeln('this.id = ""; ') #????? sw.writeln("this.adapter = adapter;") sw.writeln('this.index = %s;' % ifidx) for opidx, m in enumerate(e.list): # function list opidx = m.index sw.writeln("this.optlist.put(%s,this.%s);" % (opidx, m.name)) #???? twoway ? oneway ???? sw.writeln("this.inst = inst;") sw.scope_end().wln() #???? ???? for opidx, m in enumerate(e.list): # function list opidx = m.index sw.writeln('public function %s(ctx:RpcContext = null):Boolean{' % (m.name)).idt_inc() params = [] sw.define_var('d', 'ByteArray') sw.writeln("d = ctx.msg.paramstream; ") sw.writeln('var r:Boolean = false;') #sw.writeln("idx = 0") #?????????? _p_?? for p in m.params: if isinstance(p.type, Builtin): sw.define_var(p.id, p.type.getMappingTypeName(e.container)) Builtin_Python.unserial(p.type.type, p.id, 'd', idt, sw) elif isinstance(p.type, Sequence) or isinstance( p.type, Dictionary): sw.define_var( p.id, p.type.getMappingTypeName(e.container), 'new %s()' % p.type.getMappingTypeName(e.container)) sw.scope_begin() sw.define_var('_c', '%shlp' % p.type.name, 'new %shlp(%s)' % (p.type.name, p.id)) sw.writeln('r = _c.unmarshall(d);') sw.writeln('if(!r) return false;') sw.scope_end() else: sw.define_var( p.id, p.type.getMappingTypeName(e.container), 'new %s()' % p.type.getMappingTypeName(e.container)) sw.writeln('r = %s.unmarshall(d);' % p.id) sw.writeln('if(!r) return false;') params.append(p.id) #params = map( lambda x: '_p_'+x,params) ps = string.join(params, ',') if ps: ps = ps + ',' sw.define_var('servant', e.getName(), 'this.inst as %s' % e.getName()) if isinstance(m.type, Builtin) and m.type.type == 'void': sw.writeln("servant.%s(%sctx)" % (m.name, ps)) else: sw.define_var('cr', m.type.getMappingTypeName(e.container)) sw.writeln("cr = servant.%s(%sctx)" % (m.name, ps)) # sw.writeln("if cr == None:").idt_inc() # if isinstance(m.type,Sequence): # sw.writeln("cr = []") # sw.idt_dec() # elif isinstance(m.type,Dictionary): # sw.writeln("cr = {}") # sw.idt_dec() # else: # sw.writeln("pass").idt_dec() sw.writeln("if( ctx.msg.calltype & tcelib.RpcMessage.CALL_ONE_WAY){" ).idt_inc() sw.writeln("return false;") sw.scope_end() sw.wln() # sw.define_var('d','ByteArray','new ByteArray();') sw.writeln('d = new ByteArray();') sw.writeln('d.endian = Endian.BIG_ENDIAN;') sw.define_var('m', 'RpcMessageReturn', 'new RpcMessageReturn()') sw.writeln("m.sequence = ctx.msg.sequence;") #??????????????? if isinstance(m.type, Builtin): Builtin_Python.serial(m.type.type, 'cr', idt, sw) elif isinstance(m.type, Sequence) or isinstance(m.type, Dictionary): sw.scope_begin() v = sw.newVariant('_c') sw.define_var(v, '%shlp' % m.type.name, 'new %shlp(cr)' % m.type.name) sw.writeln('%s.marshall(d);' % v) sw.scope_end() else: sw.writeln("cr.marshall(d);") sw.writeln("if(d.length) m.addParam(d);") sw.writeln("ctx.conn.sendMessage(m);") sw.writeln("return true;") sw.scope_end() sw.wln() sw.scope_end() sw.classfile_leave()
def createCodeInterface(e,sw,idt,idx): global interface_defs,ifcnt ifidx = ifcnt ifcnt+=1 # ifidx = e.ifidx #?????????( ?? ) module = e.container #-------- index of if-cls from extern setting in file import tce_util # ifname = "%s.%s"%(module.name,e.name) # r = tce_util.getInterfaceIndexWithName(ifname) # # print 'get if-index:%s with-name:%s'%(r,ifname) # if r != -1: # ifidx = r # #--- end # e.ifidx = ifidx # print 'if-index:',ifidx ifidx = e.index interface_defs[ifidx] = {'e':e,'f':{}} fileifx.write('<if id="%s" name="%s.%s"/>\n'%(ifidx,module.name,e.name)) fileifx.flush() # tce_util.rebuildFunctionIndex(e) createProxy(e,sw,ifidx) # if not e.delegate_exposed: #????????,??????????RPC???????filter # return # expose = tce_util.isExposeDelegateOfInterfaceWithName(ifname) # if not expose: # return if not tce_util.generateSkeleton(e,lexparser.language): return # begin servant ---- sw.classfile_enter(e.getName()) sw.writeln('import tce.*;') #???????? sw.writeln('import %s.%s_delegate;'%(sw.pkg_current(),e.getName()) ) sw.writeln('import %s.*;'%(sw.pkg_current()) ) sw.writeln('import java.util.*;') for ref in module.ref_modules.keys(): if sw.pkg_current()!=ref: sw.writeln('import %s;'%ref) sw.wln() sw.writeln('public class %s extends RpcServant{'%e.getName() ) sw.idt_inc() sw.writeln("//# -- INTERFACE -- ") # sw.writeln('var delegatecls:Class = %s_delegate'%e.getName()) # sw.writeln('public %s_delegate delegate = null;'%e.getName() ) #?????delegate ??? sw.writeln("public %s(){"%e.getName() ).idt_inc() sw.writeln('super();') sw.writeln('this.delegate = new %s_delegate(this);'%e.getName()) sw.scope_end().wln() # end construct function #??servant ???? for m in e.list: # function list sw.wln() params=[] for p in m.params: params.append( (p.id,p.type.getMappingTypeName(module)) ) list =[] for v,t in params: list.append('%s %s'%(t,v)) s = string.join( list,',') if s: s += ',' sw.writeln('public %s %s(%sRpcContext ctx){'%(m.type.getMappingTypeName(module),m.name,s ) ).idt_inc() #------------????????---------------------- if isinstance( m.type ,Builtin ): if m.type.name =='void': # sw.idt_dec().wln() sw.scope_end() continue else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(module)) elif isinstance(m.type,Sequence): sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) # elif isinstance(m.type,Dictionary): # sw.writeln("return %s;"%m.type.getTypeDefaultValue() ) else: sw.writeln("return %s;"%m.type.getTypeDefaultValue(module) ) sw.scope_end() sw.scope_end() # end class sw.classfile_leave() # sw.pkg_end() # end -servant ----- #begin delegate() ---- #------- ?????? --------------------------- sw.classfile_enter("%s_delegate"%e.getName()) sw.writeln('import tce.*;') sw.writeln('import java.io.*;') sw.writeln('import java.nio.*;') sw.writeln('import java.util.*;') sw.writeln("import %s.%s;"%(sw.pkg_current(),e.getName())) sw.wln() #???????? sw.writeln("public class %s_delegate extends RpcServantDelegate {"%e.getName()).idt_inc() sw.wln() sw.writeln('%s inst = null;'%(e.getName())) # sw.wln() #???? # sw.writeln("public %s_delegate(%s inst,adapter:CommAdapter=null,conn:RpcConnection=null){"%(e.getName(),e.getName() )).idt_inc() sw.writeln("public %s_delegate(%s inst){"%(e.getName(),e.getName() )).idt_inc() sw.writeln('this.ifidx = %s;'%ifidx) # sw.writeln('this.id = ""; ') #????? # sw.writeln("this.adapter = adapter;") # sw.writeln('this.index = "%s";'%e.getName() ) #???xml?????adapter # for opidx,m in enumerate(e.list): # function list # sw.writeln("this.optlist.put(%s,this.%s);"%(opidx,m.name)) #???? twoway ? oneway ???? sw.writeln("this.inst = inst;") sw.scope_end().wln() # finish construct() #??invoke()?? sw.writeln("@Override") # sw.writeln("public boolean invoke(RpcMessage m){").idt_inc() sw.writeln("public RpcMessage invoke(RpcMessage m) throws Exception{").idt_inc() # sw.writeln('boolean r=false;') # sw.writeln('RpcMessageXML m = (RpcMessageXML)m_;') sw.writeln('RpcMessage mr = null;') for opidx,m in enumerate(e.list): opidx = m.index sw.writeln('if(m.opidx == %s ){'%opidx).idt_inc() sw.writeln('mr = func_%s_delegate(m);'%opidx ) sw.scope_end() sw.writeln('return mr;') sw.scope_end() # end - invoke() sw.wln() #???? ???? for opidx,m in enumerate(e.list): # function list opidx = m.index sw.writeln('// func: %s'%m.name) sw.writeln('RpcMessage func_%s_delegate(RpcMessage m) throws Exception{'%(opidx) ).idt_inc() params=[ ] # sw.writeln('boolean r= false;') # sw.writeln('r = false;') if m.params: sw.writeln('ByteBuffer d = ByteBuffer.wrap(m.paramstream);') for p in m.params: if isinstance(p.type,Builtin): sw.define_var(p.id,p.type.getMappingTypeName(module)) Builtin_Python.unserial(p.type,p.id,sw,'d') elif isinstance(p.type,Sequence) or isinstance(p.type,Dictionary): impled = False #print p.type,p.type.type.name if isinstance(p.type,Sequence): if p.type.type.name == 'byte': size = sw.newVariant('_s') sw.writeln('int %s = %s.getInt();'%(size,'d')) sw.writeln('byte[] %s = new byte[%s];'%(p.id,size)) sw.writeln('%s.get(%s);'%('d',p.id)) impled = True if not impled: sw.define_var(p.id,p.type.getMappingTypeName(module),'new %s()'%p.type.getMappingTypeName(module)) c = sw.newVariant('_array') sw.define_var(c,'%shlp'%p.type.name,'new %shlp(%s)'%(p.type.name,p.id)) sw.writeln('%s.unmarshall(d);'%c) else: sw.define_var(p.id,p.type.getMappingTypeName(module),'new %s()'%p.type.getMappingTypeName(module)) sw.writeln('%s.unmarshall(d);'%p.id) params.append(p.id) #params = map( lambda x: '_p_'+x,params) ps = string.join(params,',') if ps: ps = ps + ',' sw.define_var('servant',e.getName(),'(%s)this.inst'%e.getName()) sw.writeln('RpcContext ctx = new RpcContext();') sw.writeln('ctx.msg = m;') if isinstance(m.type,Builtin) and m.type.type =='void': # none return value sw.writeln("servant.%s(%sctx);"%(m.name,ps) ) else: sw.define_var('cr',m.type.getMappingTypeName(module)) sw.writeln("cr = servant.%s(%sctx);"%(m.name,ps) ) sw.writeln("if( (m.calltype & tce.RpcMessage.ONEWAY) !=0 ){").idt_inc() # sw.writeln("return true;") #????????? sw.writeln("return null;") #????????? sw.scope_end() sw.wln() #????? # sw.define_var('mr','RpcMessage','new RpcMessage(RpcMessage.RETURN)') sw.writeln('mr.sequence = m.sequence;') sw.writeln('mr.callmsg = m;') sw.writeln('mr.conn = m.conn;') sw.writeln('mr.ifidx = m.ifidx;') sw.writeln('mr.call_id = m.call_id;') sw.writeln('if(m.extra.getProperties().containsKey("__user_id__")){').idt_inc() sw.writeln('mr.extra.setPropertyValue("__user_id__",m.extra.getPropertyValue("__user_id__"));') sw.scope_end() if m.type.name !='void': # sw.writeln("m.sequence = ctx.msg.sequence;") #??????????????? # # sw.writeln('try{').idt_inc() sw.writeln('ByteArrayOutputStream bos = new ByteArrayOutputStream();') sw.writeln('DataOutputStream dos = new DataOutputStream(bos);') # sw.writeln('String xml="";') if isinstance( m.type ,Builtin ) and m.type.name!='void': Builtin_Python.serial(m.type,'cr',sw,'dos') elif isinstance(m.type,Sequence) or isinstance(m.type,Dictionary): impled = False if isinstance(m.type,Sequence): if m.type.type.name == 'byte': sw.writeln('%s.writeInt(%s.length);'%('dos','cr')) sw.writeln('%s.write(%s,0,%s.length);'%('dos','cr','cr')) impled = True if not impled: v = sw.newVariant('_c') sw.define_var(v,'%shlp'%m.type.name,'new %shlp(cr)'%m.type.name) sw.writeln('%s.marshall(dos);'%v) else: sw.writeln("cr.marshall(dos);") sw.writeln('mr.paramsize = 1;') sw.writeln('mr.paramstream = bos.toByteArray();') # sw.idt_dec().writeln('}catch(Exception e){').idt_inc() # sw.writeln('r = false;') # sw.writeln('return r;') # sw.scope_end() # sw.writeln("r =m.conn.sendMessage(mr);") #???? # sw.writeln("return r;") sw.writeln("return mr;") sw.scope_end() # end servant function{} sw.wln() sw.scope_end() # end class define sw.classfile_leave() # sw.pkg_end() # end delegate() # createProxy(e,sw,ifidx) return