Commits

Ivan Vučica  committed 1542b68

Code for establishing session.

  • Participants
  • Parent commits 0b8cf84

Comments (0)

Files changed (3)

-7eb4f827c27d8766ef9145a90f4556b8b379ff57 zxmpp
+d967090b1162c6c2473ba7ef0e47e4bebafc968a zxmpp

File pong-invitations.js

         pongInvitations[fullJid] = invitation;
     }
     invitation.state = "gotdemand";
-    console.log("--- added, and now refreshing");
+
     pongInvitationUI_refreshNotifications();
 }
 function pongInvitation_receiveAccept(fullJid)
 {
+    
+    var invitation = pongInvitations[fullJid];
+    if(!invitation)
+    {
+        invitation = new Object();
+        invitation.fullJid = fullJid;
+        pongInvitations[fullJid] = invitation;
+    }
+    invitation.state = "accepted";
+    pongInvitationUI_refreshNotifications();
+    
+    zxmpp.setOwnPresence("dnd", "Playing TVZ Pong", 0);
+
+    pong_talkTo = fullJid;
+
 }
 function pongInvitation_receiveDecline(fullJid)
 {
-    
+    pongInvitationUI_removeNotificationForJid(fullJid);
+    delete pongInvitations[fullJid];
+
+    pongInvitationUI_refreshNotifications();
+}
+function pongInvitation_receiveError(fullJid)
+{
+    alert("ERROR FROM " + fullJid);
+    pongInvitationUI_removeNotificationForJid(fullJid);
+    delete pongInvitations[fullJid];
+
+    pongInvitationUI_refreshNotifications();
 }
 
 
 //////////// outgoing ///////////
 function pongInvitation_sendDemand(fullJid)
 {
+    var invitation = pongInvitations[fullJid];
+    if(!invitation)
+    {
+        invitation = new Object();
+        invitation.fullJid = fullJid;
+        pongInvitations[fullJid] = invitation;
+    }
+    invitation.state = "sendingdemand";
+    pongInvitationUI_refreshNotifications();
+
     
+    var packet = new zxmpp.packet(zxmpp);
+    
+    var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "invitation");
+    
+    /*
+     var message = new zxmpp.stanzaMessage(zxmpp);
+     message.appendToPacket(packet, zxmpp.fullJid, fullJid, "chat", false); // pass no body
+     packet.messageXML.appendChild(invitationNode);
+     */
+    
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /*id base name*/"pongdemand", "set", fullJid);
+    
+    var replyHandler = function pongInvitation_sendDemandReplyHandler(zxmpp, original, response)
+    {
+        if(response.type == "error")
+            pongInvitation_receiveError(original.to);
+        else if (response.type == "result")
+        {
+            var invitation = pongInvitations[fullJid];
+            if(!invitation)
+            {
+                invitation = new Object();
+                invitation.fullJid = fullJid;
+                pongInvitations[fullJid] = invitation;
+            }
+            invitation.state = "sentdemand";
+            pongInvitationUI_refreshNotifications();
+        }
+    };
+    
+    iq.onReply.push(replyHandler);
+    packet.iqXML.appendChild(invitationNode);
+    
+    packet.send("poll");
 }
 function pongInvitation_sendAccept(fullJid)
 {
+    var invitation = pongInvitations[fullJid];
+    if(!invitation)
+    {
+        invitation = new Object();
+        invitation.fullJid = fullJid;
+        pongInvitations[fullJid] = invitation;
+    }
+    invitation.state = "accepting";
+    pongInvitationUI_refreshNotifications();
+    
+    
+    var packet = new zxmpp.packet(zxmpp);
+    
+    var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "accept");
+    
+    /*
+     var message = new zxmpp.stanzaMessage(zxmpp);
+     message.appendToPacket(packet, zxmpp.fullJid, fullJid, "chat", false); // pass no body
+     packet.messageXML.appendChild(invitationNode);
+     */
+    
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /*id base name*/"pongaccept", "set", fullJid);
+    
+    var replyHandler = function pongInvitation_sendDemandReplyHandler(zxmpp, original, response)
+    {
+        if(response.type == "error")
+            pongInvitation_receiveError(original.to);
+        else if (response.type == "result")
+        {
+            var invitation = pongInvitations[fullJid];
+            if(!invitation)
+            {
+                invitation = new Object();
+                invitation.fullJid = fullJid;
+                pongInvitations[fullJid] = invitation;
+            }
+            invitation.state = "accepted";
+            pongInvitationUI_refreshNotifications();
+            
+            zxmpp.setOwnPresence("dnd", "Playing TVZ Pong", 0);
+
+            pong_talkTo = fullJid;
+            
+            for(var invite in pongInvitations)
+            {
+                if(invite.state != accepted)
+                    pongInvitation_sendDecline(invite);
+            }
+        }
+    };
+    
+    iq.onReply.push(replyHandler);
+    packet.iqXML.appendChild(invitationNode);
+    
+    packet.send("poll");
+    
+    
 }
 function pongInvitation_sendDecline(fullJid)
 {
+    
+    var invitation = pongInvitations[fullJid];
+    if(!invitation)
+    {
+        invitation = new Object();
+        invitation.fullJid = fullJid;
+        pongInvitations[fullJid] = invitation;
+    }
+    invitation.state = "declining";
+    pongInvitationUI_refreshNotifications();
+    
+    
+    var packet = new zxmpp.packet(zxmpp);
+    
+    var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "decline");
+    
+    /*
+     var message = new zxmpp.stanzaMessage(zxmpp);
+     message.appendToPacket(packet, zxmpp.fullJid, fullJid, "chat", false); // pass no body
+     packet.messageXML.appendChild(invitationNode);
+     */
+    
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /*id base name*/"pongdecline", "set", fullJid);
+    
+    var replyHandler = function pongInvitation_sendDemandReplyHandler(zxmpp, original, response)
+    {
+        pongInvitationUI_removeNotificationForJid(fullJid);
+        delete pongInvitations[fullJid];
+
+        pongInvitationUI_refreshNotifications();
+    };
+    
+    iq.onReply.push(replyHandler);
+    packet.iqXML.appendChild(invitationNode);
+    
+    packet.send("poll");
+}
+function pongInvitation_sendCancel(fullJid)
+{
+    pongInvitation_sendDecline(fullJid);
 }
 
 //////////// ui ////////////////
         
         if(invitation.state == "gotdemand")
         {
-            console.log("got demand " + fullJid);
             var notification = pongInvitationUI_notificationNodeForJid(fullJid);
             
             var newStatusText = "You received an invitation from " + fullJid + ".";
-            newStatusText += "<button>Accept</button>";
-            newStatusText += "<button>Decline</button>";
+            newStatusText += "<button onclick=\"pongInvitation_sendAccept('" + fullJid.replace("'", "\\'") + "');\">Accept</button>";
+            newStatusText += "<button onclick=\"pongInvitation_sendDecline('" + fullJid.replace("'", "\\'") + "');\">Decline</button>";
             notification.innerHTML = newStatusText;
         }
+        if(invitation.state == "accepting")
+        {
+            var notification = pongInvitationUI_notificationNodeForJid(fullJid);
+            
+            var newStatusText = "";
+            //newStatusText += "<img src=\"spinner.gif\">";
+            newStatusText += "Accepting...";
+            notification.innerHTML = newStatusText;
+        }
+        if(invitation.state == "accepted")
+        {
+            var notification = pongInvitationUI_notificationNodeForJid(fullJid);
+            
+            var newStatusText = "";
+            //newStatusText += "<img src=\"spinner.gif\">";
+            newStatusText += "Starting the game...";
+            notification.innerHTML = newStatusText;
+        }
+        if(invitation.state == "declining")
+        {
+            var notification = pongInvitationUI_notificationNodeForJid(fullJid);
+            
+            var newStatusText = "";
+            //newStatusText += "<img src=\"spinner.gif\">";
+            newStatusText += "Declining...";
+            notification.innerHTML = newStatusText;
+        }
+        
+        ////////////
+        
+        
+        if(invitation.state == "sendingdemand")
+        {
+            var notification = pongInvitationUI_notificationNodeForJid(fullJid);
+            
+            var newStatusText = "";
+            //newStatusText += "<img src=\"spinner.gif\">";
+            newStatusText += "Sending invitation to " + fullJid + "...";
+            newStatusText += "<button onclick=\"pongInvitation_sendCancel('" + fullJid.replace("'", "\\'") + "');\">Cancel</button>";
+            notification.innerHTML = newStatusText;
+        }
+        if(invitation.state == "sentdemand")
+        {
+            var notification = pongInvitationUI_notificationNodeForJid(fullJid);
+            
+            var newStatusText = "";
+            //newStatusText += "<img src=\"spinner.gif\">";
+            newStatusText += "Sent invitation to " + fullJid + ", waiting for reply...";
+            newStatusText += "<button onclick=\"pongInvitation_sendCancel('" + fullJid.replace("'", "\\'") + "');\">Cancel</button>";
+            notification.innerHTML = newStatusText;
+        }
+
     }
 }
-
-function pongInvitationUI_notificationNodeForJid(fullJid)
+function pongInvitationUI_removeNotificationForJid(fullJid)
+{
+    pongInvitationUI_notificationNodesForJid(fullJid).slideUp().remove();
+}
+function pongInvitationUI_notificationNodesForJid(fullJid)
 {
     var safeJid = pong_safeJid(fullJid);
     if($(".invitations > #" + safeJid).length)
     {
-        return $(".invitations > #" + safeJid)[0];
+        return $(".invitations > #" + safeJid);
     }
     
-    var newNode = $("<div/>", { id: safeJid, class: "invitation" });
+    var newNode = $("<div/>", { id: safeJid, class: "invitation", fullJid: fullJid });
     newNode.appendTo(".invitations");
+    newNode.hide().slideDown();
     return newNode;
-}
+}
+function pongInvitationUI_notificationNodeForJid(fullJid)
+{
+    return pongInvitationUI_notificationNodesForJid(fullJid)[0];
+}

File pong-network.js

     zxmpp.onRosterUpdate.push(pongZXMPP_onRosterUpdate);
     zxmpp.onMessage.push(pongZXMPP_onMessage);
     zxmpp.onPacket.push(pongZXMPP_onPacket);
+    zxmpp.addIqParser("invitation#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqInvitation);
+    zxmpp.addIqParser("accept#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqAccept);
+    zxmpp.addIqParser("decline#http://tvzpong.vucica.net/protocol/", pongZXMPP_onIqDecline);
 
     zxmpp.main(cfg, user, password);
     zxmpp.setOwnPresence("avail", "Working on TVZ Pong", 1);
     for(var resourceId in resources)
     {
         var resource = resources[resourceId];
-        var packet = new zxmpp.packet(zxmpp);
-        var message = new zxmpp.stanzaMessage(zxmpp);
-        message.appendToPacket(packet, zxmpp.fullJid, jid + "/" + resource, "chat", false); // pass no body
-        var invitationNode = packet.xml.createElementNS("http://tvzpong.vucica.net/protocol/", "invitation");
-        packet.messageXML.appendChild(invitationNode);
-        packet.send("poll");
+
+        pongInvitation_sendDemand(jid + "/" + resource);
     }
 }
+
 function pong_sendData(jid, data)
 {
     //var resources = tuntap_rosterContactResourcesSupportingTunTap(jid);
             pong_rosterContent[topPresence.bareJid].show = topPresence.show;
             pong_rosterContent[topPresence.bareJid].status = topPresence.status;
             pong_rosterRefresh();
-
-            
         }
 
     }
     }
 }
 
+function pongZXMPP_onIqInvitation(zxmpp, iqstanza, xml)
+{
+    pongInvitation_receiveDemand(iqstanza.from);
+    
+    // send confirmation
+    var packet = new zxmpp.packet(zxmpp);
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /* id */false, /* type */ "result", /* destination */iqstanza.from, iqstanza.id);
+    packet.send("poll");
+    return true;
+}
+function pongZXMPP_onIqAccept(zxmpp, iqstanza, xml)
+{
+    pongInvitation_receiveAccept(iqstanza.from);
+    
+    // send confirmation
+    var packet = new zxmpp.packet(zxmpp);
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /* id */false, /* type */ "result", /* destination */iqstanza.from, iqstanza.id);
+    packet.send("poll");
+    return true;
+}
+function pongZXMPP_onIqDecline(zxmpp, iqstanza, xml)
+{
+    pongInvitation_receiveDecline(iqstanza.from);
+    
+    // send confirmation
+    var packet = new zxmpp.packet(zxmpp);
+    var iq = new zxmpp.stanzaIq(zxmpp);
+    iq.appendIqToPacket(packet, /* id */false, /* type */ "result", /* destination */iqstanza.from, iqstanza.id);
+    packet.send("poll");
+    return true;
+}
+
+
 function pongZXMPP_onPacket(sender, packet)
 {
     if(packet && packet.incomingStanza && packet.incomingStanza.iqXML)