def login(self, username, password): """ Initiate new user connection between proxy and server. Initiate Deferred object. Return Deferred. """ #TODO: Data format try: request = harbor_pb2.Request() request.cntrl = harbor_pb2.Request.LOGIN request.loginer.username = username request.loginer.password = password request = request.SerializeToString() self.write_to_server(request) result = yield self.server_proto.deferred # message arrived reply = harbor_pb2.Reply() reply.ParseFromString(result) if reply.success == True: # add client # Login succeeded self.isLoggedIn = True # Use utf-8 for now. But will use unicode later on interface = reply.user.usertype.encode('utf-8') username = reply.user.username.encode('utf-8') userid = reply.user.id # Assume there is only device for now devicenames, devicetypes = [], [] for device in reply.user.devices: devicenames.append(device.name.encode('utf-8')) devicetypes.append(device.type.encode('utf-8')) self.write_to_client( "%s %s login, owns %s:%s\r\n" % (interface, username, devicenames, devicetypes)) self.proc = self.handle_from_clientServer() #TODO else: # Login failed # TODO: omitted the case where client has already logged in # Retrive data reason = reply.reason.encode('utf-8') # Write reason to client self.write_to_client(reason) except Exception, err: """ The login fail due to server mechanism. Reply back the fail reason toClient Doesn't impose penalty on client """ err = str(err) self.write_to_client(err)
def login(self, username, password): """ Authentication use twisted portal """ try: # do login avatarInfo = yield self.factory.login(username, password) # login succeed self.avatarInterface, self.avatar, _ = avatarInfo print self.avatar.devices # update login flag self.isLoggedIn = True # compose reply message reply = harbor_pb2.Reply() reply.user.id = self.avatar.userId reply.user.username = self.avatar.username reply.user.usertype = "user" #TODO: need proper interface reply.success = True try: # connect to HarborServer, it is done after reply success to # client. This is because the Harbor connection might be fragile # or disconnected due to different reasons. But the connection # will be reopen if server is free agian. # TODO: repeated connection and remember connection state self.harborConn = yield self.factory.connect_to_harbor() # connection success # start receving message from harbor self.proc = self.recv_from_harbor() # fetch user devices results = yield self.factory.getUserDevice(self.avatar.userId) for r in results: # iterfate over tuple (deviceId, devicename, devicetype), = r self.avatar.devices[deviceId] = {devicename, devicetype} # assgin to pb new_device = reply.user.devices.add() new_device.id = deviceId new_device.name = devicename new_device.type = devicetype except Exception, err: # connect to HarborServer fail print err # TODO:because proxy doens't expect any msg finally: reply = reply.SerializeToString() # forward message to Harbor Server if self.harborConn: self.write_to_harbor(reply) # log user and reply back to client defer.maybeDeferred( self.factory.logClient( self.avatar.userId, self)).addBoth(lambda _: self.write_to_client(reply))
def handshake(self, data): """ record userid, and user devicesid found online devices from harbor and record in user devices keep updating user devices at loop Active Control Scheme:(current implementation) The user will take control of device whenever the device is free Passive Control Scheme:(future patch) The user will only take control of device when the user asked for it and the device is free """ try: reply = harbor_pb2.Reply() reply.ParseFromString(data) if reply.success == True: self.state = "user_control" # if this is a success message userId = reply.user.id self.controller = Controller(userId) self.factory.addClient(userId, self) print "user %s on harbor!" % userId # get user owned devices free, busy, offline = yield self.factory.matchClientDevices( reply.user.devices, self.controller.devices, self.controller.observing) # inform devices and clients for d_id in free: self.send_to_client("your deivce %s is free\n" % self.controller.devices[d_id].name) self.controller.subscribe(d_id, self) for d_id in busy: self.send_to_client("your deivce %s is busy\n" % self.controller.devices[d_id].name) self.controller.subscribe(d_id, self) # start updating client device list self.l_updateClientDevices = task.LoopingCall( self.updateClientDevices) # The update user device interval should be dynamic self.l_updateClientDevices.start(UPDATE_USER_DEVICE_INT) else: # if the reply is fail pass except Exception, err: err = str(err) print err
def register(self, username, password): """ Register user into database. Then call login user to login Trap error and reply back err msg """ try: r = yield self.factory.register(username, password) # register user success, login user afterwards self.login(username, password) except Exception, err: # register user fail log.msg(err, logLevel=logging.DEBUG) #TODO: convert from err to string err = str(err) reply = harbor_pb2.Reply() if "duplicate" in err.lower(): # If duplicate username reply_fail(self, reply_duplicate_username) else: # Other error reply_fail(self, reply_register_fail_general)
def register(self, username, password): """ """ try: request = harbor_pb2.Request() request.cntrl = harbor_pb2.Request.REGISTER request.registerr.username = username request.registerr.password = password request = request.SerializeToString() self.write_to_server(request) result = yield self.server_proto.deferred # message arrived reply = harbor_pb2.Reply() reply.ParseFromString(result) if reply.success == True: # Registered succeeded self.client.loginSucceed() # Use utf-8 for now. But will use unicode later on interface = reply.user.usertype.encode('utf-8') username = reply.user.username.encode('utf-8') self.write_to_client("%s %s registered and login" % (interface, username)) else: # Registered failed reason = reply.reason.encode('utf-8') # Write reason to client self.write_to_client(reason) except Exception, err: """ The login fail due to server mechanism. Reply back the fail reason toClient Doesn't impose penalty on client """ print err