Profile豆浆油条BlogLists Tools Help
    October 25

    如何通信(3)

    小有收获,今天写出了一个根据之前的构想写出的简单通信模型,赶紧贴出来吧: 

    <node name="sendQueue">

    <attribute name="queue" value="[]" />

    <attribute name="workingitem" />

    <attribute name="lock" value="true"/>

    <remotecall name="remote" funcname="action" remotecontext="$once{canvas.eventrpc}" />

    <method name="enqueue" args="para">

    <![CDATA[

    this.lock=true;

    var inserted=false;

    for(var k=0;k<queue.length;k++){

    if (queue[k].action==para.action

    && queue[k].event.uuid==para.event.uuid){

    queue.splice(k, 1, para);

    inserted=true;

    debug.write("spliced!");

    break;

    }

    }

    if (!inserted)

    queue.push(para);

    this.lock=false;

    debug.write("enqueued", queue);

    ]]>

    </method>

    <method event="oninit">

    if(!this['del'])

    this.del = new LzDelegate(this, "work");

    </method>

    <method name="work">

    if (this.lock || !queue.length){

    nextQueue();

    return;

    }

    workingitem = queue.shift();

    debug.write("remotecall",workingitem);

    this.remote.invoke(Array(workingitem));

    </method>

    <method name="nextQueue" event="ondata" reference="remote">

    LzTimer.addTimer(this.del, 3000);

    </method>

    <method name="retry" event="onerror" reference="remote"

    args="err">

    debug.write("unshifting cause",err);

    queue.unshift(workingitem);

    workingitem=null;

    LzTimer.addTimer(this.del, 10000);

    </method>

    </node>

    <node name="receiveQueue">

    <remotecall name="remote" funcname="getEvents" remotecontext="$once{canvas.eventrpc}">

    <method event="ondata" args="d">

    debug.write("do merge!"); //TODO add merge method

    parent.plan();

    </method>

    </remotecall>

    <method name="getParam">

    return {year:2005, month:10}; //TODO build query args

    </method>

    <method event="oninit">

    if(!this['del'])

    this.del = new LzDelegate(this, "work");

    </method>

    <method name="work">

    this.remote.invoke(Array(getParam()));

    </method>

    <method name="plan">

    LzTimer.addTimer(this.del, 500000);

    </method>

    </node>

     
    收获是,我开始把这个Queue先做成了

    <class name="WorkQueue" extends="node"></class>

    结果,实例化出来的两个queue竟然在使用同一个queue和remote,还没想明白为什么。这个attribute也没有final,static,transient让我选,可能是node的特殊性吧,为啥以前这么在view上就不会有这样的问题。所以,我干脆把他们做成两个node,反正他们的行为和策略也不尽相同。
    的确是很短小啊,我记得我当年写过一个connectionImpl,相应部分的java代码虽然严谨但是的确是非常难写的,我今天再看了一次,那些notify然后到哪儿了我都说不清楚。不过感觉自己以前掌握的线程,锁,这些的应用还是很扎实的。嘿嘿(时隔一年就看不懂了)

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks (1)

    The trackback URL for this entry is:
    http://b051.spaces.live.com/blog/cns!9E2D0BC71827D4E9!237.trak
    Weblogs that reference this entry