2014-03-25 13:43:11 -07:00
|
|
|
# Copyright 2014, Ian Gulliver
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
2014-05-27 14:43:59 -07:00
|
|
|
import json
|
2014-05-23 14:29:11 -07:00
|
|
|
import logging
|
2014-03-25 13:43:11 -07:00
|
|
|
import webapp2
|
|
|
|
|
|
2014-06-08 21:54:57 -07:00
|
|
|
from google.appengine.api import channel
|
2014-03-25 13:43:11 -07:00
|
|
|
from google.appengine.ext import db
|
|
|
|
|
|
|
|
|
|
from cosmopolite.lib import auth
|
|
|
|
|
from cosmopolite.lib import models
|
|
|
|
|
from cosmopolite.lib import utils
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OnChannelConnect(webapp2.RequestHandler):
|
|
|
|
|
@utils.local_namespace
|
|
|
|
|
@db.transactional()
|
|
|
|
|
def post(self):
|
2014-05-23 11:40:08 -07:00
|
|
|
instance_id = self.request.get('from')
|
2014-05-23 14:29:11 -07:00
|
|
|
instance = models.Instance.FromID(instance_id)
|
2014-05-26 17:28:59 -07:00
|
|
|
if not instance:
|
2014-05-27 14:43:59 -07:00
|
|
|
logging.warning('Channel opened with invalid instance_id: %s', instance_id)
|
|
|
|
|
message = {
|
|
|
|
|
'event_type': 'close',
|
|
|
|
|
}
|
|
|
|
|
channel.send_message(instance_id, json.dumps(msg, default=utils.EncodeJSON))
|
2014-05-26 17:28:59 -07:00
|
|
|
return
|
2014-05-23 11:23:30 -07:00
|
|
|
instance.active = True
|
|
|
|
|
instance.put()
|
2014-03-25 13:43:11 -07:00
|
|
|
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
class OnChannelDisconnect(webapp2.RequestHandler):
|
|
|
|
|
@utils.local_namespace
|
|
|
|
|
def post(self):
|
2014-05-23 11:40:08 -07:00
|
|
|
instance_id = self.request.get('from')
|
2014-05-26 17:28:59 -07:00
|
|
|
instance_key = db.Key.from_path('Instance', instance_id)
|
2014-03-25 13:43:11 -07:00
|
|
|
|
2014-05-28 14:44:13 -07:00
|
|
|
subscriptions = list(models.Subscription.all().filter('instance =', instance_key))
|
|
|
|
|
if subscriptions:
|
|
|
|
|
db.delete(subscriptions)
|
2014-05-06 22:46:07 -07:00
|
|
|
|
2014-05-26 17:28:59 -07:00
|
|
|
pins = models.Pin.all().filter('instance =', instance_key)
|
2014-05-25 23:40:56 -07:00
|
|
|
for pin in pins:
|
|
|
|
|
pin.Delete()
|
|
|
|
|
|
2014-05-28 14:44:13 -07:00
|
|
|
db.delete(instance_key)
|
2014-05-25 23:40:56 -07:00
|
|
|
|
2014-03-25 13:43:11 -07:00
|
|
|
|
|
|
|
|
app = webapp2.WSGIApplication([
|
|
|
|
|
('/_ah/channel/connected/', OnChannelConnect),
|
|
|
|
|
('/_ah/channel/disconnected/', OnChannelDisconnect),
|
|
|
|
|
])
|