Example #1
0
    def run(self):
        print "starts file update for all users"
        #sleep(1)
        encoder = json.JSONEncoder()
        bd = DB_Accesser()

        users = bd.Get_AllUsersInSession({"sessionID": self.sessionid})
        files = bd.Get_AllFilesFromSession({"sessionID": self.sessionid})

        data = {"sessionID": self.sessionid, "fileList": files}
        datastr = encoder.encode(data)
        cmdstr = unichr(UPDATE_FILES)

        for usr in users:

            print "update files for user " + usr["email"]

            skt = socketlist[usr["email"]][0]
            mtx = socketlist[usr["email"]][2]

            sendstr = "SHARINF" + cmdstr + datastr + "\endF"

            mtx.acquire()
            n_send = skt.sendall(sendstr)
            #while(n_send is not None):
            #    n_send = skt.sendall(sendstr)
            mtx.release()

        print "files update done!"
        return
Example #2
0
    def loadFileHandle(self, data):

        print "load file: " + self.user + " in session " + chr(
            data["sessionID"])
        db = DB_Accesser()
        sessionid = data["sessionID"]

        permissions = db.Get_Session({"sessionID": sessionid})
        permissions = permissions["SessionConfig"]["permissions"]

        admin = db.Get_SessionAdmin({"sessionID": sessionid})
        #admin = admin["email"]

        if permissions == RESTRICTED and self.user is not admin:
            self.returnInsucess(LOAD_FILE_SESSION,
                                "You dont have permissions to share")
            return

        db.Insert_File(data["File"])
        result = db.Insert_FileInSession(data)

        if result is True:
            self.updateFiles(sessionid)
            self.returnSuccess(LOAD_FILE_SESSION, None)
        else:
            self.returnInsucess(LOAD_FILE_SESSION)

        return
Example #3
0
    def sendSharinfFrame(self, cmd, data, usr=None, status=None):

        db = DB_Accesser()

        if usr == None:
            usr = self.user

        cmdstr = unichr(cmd)
        if data is not None:
            datastr = self.encoder.encode(data)

        skt = socketlist[usr][0]
        mtx = socketlist[usr][2]

        try:
            mtx.acquire()
            if (status is None):
                if (data is None):
                    skt.send("SHARINF" + cmdstr + "\endf")
                else:
                    skt.send("SHARINF" + cmdstr + datastr + "\endf")
            else:
                if (data is None):
                    skt.send("SHARINF" + cmdstr + status + "\endf")
                else:
                    skt.send("SHARINF" + cmdstr + status + datastr + "\endf")
            mtx.release()
        except:
            return
Example #4
0
    def removeFileHandle(self, data):

        db = DB_Accesser()

        sessionid = data["sessionID"]

        freerestr = db.Get_Session({"sessionID": sessionid})
        freerestr = freerestr["SessionConfig"]["permissions"]

        owner = db.Get_File(data["File"])
        owner = owner["owneremail"]

        admin = db.Get_SessionAdmin({"sessionID": sessionid})
        admin = admin["email"]

        if (permissions == RESTRICTED) and (self.user is not admin) or (
                self.user is not owner):
            self.returnInsucess(
                LOAD_FILE_SESSION,
                "You dont have permissions to share or you are not owner")
            return

        result = db.Remove_FileFromSession(data)

        if result is True:
            self.updateFiles(sessionid)
            self.returnSuccess(LOAD_FILE_SESSION, None)
        else:
            self.returnInsucess(LOAD_FILE_SESSION)

        return
Example #5
0
    def getUser(self):
        db = DB_Accesser()
        data = self.request.recv(5 * 1024)

        if data is None:
            return None

        if (data[:7] != "SHARINF"):
            print "No begin of frame"
            return None

        index = string.find(data, '\endF')

        if (index == -1):
            print "No end of frame"
            return None

        frame = sharinfFrame(data[7], data[8:index])
        cmdstr = unichr(CREATE_CONNECTION)

        if (frame == None or frame.cmd != CREATE_CONNECTION):
            #tempstr = "First I need a create connection request with information about the user..."
            self.request.send("SHARINF" + cmdstr + INSUCESS + "{\"error\":\"" +
                              tempstr + "\"}" + "\endF")
            #self.returnInsucess(CREATE_CONNECTION, "First I need a create connection request with information about the user...")
            return None

        db.Insert_User(frame.data)
        self.request.send("SHARINF" + cmdstr + SUCCESS + "\endF")
        #self.returnSuccess(CREATE_CONNECTION, None, us = frame.data["email"])

        print "getUser: "******"email"]
        return frame.data["email"]
Example #6
0
    def fileListHandle(self, data):

        db = DB_Accesser()

        files = db.Get_AllFilesFromSession(data)

        datajson = {"fileList": files}

        if files is not None:
            self.returnSuccess(SESSION_FILELIST, datajson)
        else:
            self.returnInsucess(SESSION_FILELIST)
Example #7
0
    def userListHandle(self, data):

        db = DB_Accesser()

        users = db.Get_AllUsersInSession(data)

        datajson = {"userList": users}

        if users is not None:
            self.returnSuccess(SESSION_USERLIST, datajson)
        else:
            self.returnInsucess(SESSION_USERLIST)
Example #8
0
    def createSessionHandle(self, data):

        print "create session: " + self.user
        db = DB_Accesser()
        location = db.Get_Location(data["BeaconState"])

        if db.Get_ActiveSessionFromLocal(
            {"localizationID":
             location["BeaconState"]["localizationID"]}) is not None:
            self.returnInsucess(CREATE_SESSION,
                                "Beacon already has session active...")
            return

        if (location == None):
            self.returnInsucess("No beacon with that ID exists...")

        if (location["BeaconState"]["state"] == "Occupied"):
            self.returnInsucess("There is already a session in that beacon..")

        session = {
            "s_begin": datetime.now().date(),
            "activeFlag": True,
            "localizationID": location["BeaconState"]["localizationID"],
            "s_type": data["SessionConfig"]["type"],
            "permissions": data["SessionConfig"]["permissions"],
            "s_name": data["SessionConfig"]["s_name"]
        }

        sessioninfo = db.Insert_Session(session)

        if sessioninfo == None:
            self.returnInsucess(CREATE_SESSION, "Beacon nonexisting...")
            return

        db.Insert_UserInSession({
            "email": self.user,
            "sessionID": sessioninfo["sessionID"]
        })
        db.Set_SessionAdmin({
            "email": self.user,
            "sessionID": sessioninfo["sessionID"]
        })

        self.sessionIDi = sessioninfo["sessionID"]
        self.updateUsers(sessioninfo["sessionID"])
        self.returnSuccess(CREATE_SESSION, sessioninfo)
        self.sendSharinfFrame(YOURNEWADMIN,
                              {"sessionID": sessioninfo["sessionID"]})
        return
Example #9
0
    def beaconUndetectedHandle(self, data):

        print "Beacon undetected: " + self.user
        db = DB_Accesser()
        location = None

        location = db.Get_Location(beacon)

        if (location is not None):
            db.Remove_UserFromLocation({
                "email":
                self.user,
                "localizationID":
                location["BeaconState"]["localizationID"]
            })

        self.returnSuccess(BEACON_UNDETECTED)

        return
Example #10
0
    def getOutSessionHandle(self, data):

        print "leave session request: " + self.user + " in session " + chr(
            data["sessionID"])
        db = DB_Accesser()
        sessionid = data["sessionID"]

        userisadmin = db.Get_UserInSession({
            "email": self.user,
            "sessionID": sessionid
        })

        if (userisadmin is None):
            self.returnInsucess(GETOUT_SESSION,
                                "No user/session to get out...")
            return

        userisadmin = userisadmin["adminFlag"]
        db.Remove_UserFromSession({"email": self.user, "sessionID": sessionid})

        sessionusers = db.Get_AllUsersInSession({"sessionID": sessionid})

        print "sessionuser: "******"sessionID": sessionid})

    # elif userisadmin:
    # 	print "NEW ADMIN"
    # tempusr = sessionusers[0]["email"]
    # self.sendSharinfFrame(YOURNEWADMIN, {"sessionID": sessionid}, usr = tempusr)
    # #responseframe = self.getSharinfFrame(tempusr)
    # #if responseframe.data == SUCCESS:
    # db.Set_SessionAdmin({"email": tempusr, "sessionID": sessionid})

        print "leave session request DONE: " + self.user + " in session " + chr(
            data["sessionID"])
        self.sessionIDi = None
        self.returnSuccess(GETOUT_SESSION)

        return sessionid
Example #11
0
    def joinSessionHandle(self, data):

        print "join session: " + self.user + " in session " + chr(
            data["sessionID"])
        db = DB_Accesser()
        sessionid = data["sessionID"]

        pubpriv = db.Get_Session({"sessionID": sessionid})
        pubpriv = pubpriv["SessionConfig"]["type"]

        if (pubpriv == PRIVATE):
            admin = db.Get_SessionAdmin({"sessionID": sessionid})
            admin = admin["email"]
            self.sendSharinfFrame(REQUESTJOINSESSION, {
                "sessionID": sessionid,
                "email": self.user
            },
                                  usr=admin)
            responseframe = self.getSharinfFrame(admin)

            if responseframe.data == INSUCESS:
                self.returnInsucess(JOIN_SESSION, "Administrar didnt accept")
                return

        result = db.Insert_UserInSession({
            "email": self.user,
            "sessionID": sessionid
        })

        if result is True:
            self.sessionIDi = sessionid
            self.updateUsers(sessionid)
            self.returnSuccess(JOIN_SESSION)
        else:
            self.returnInsucess(
                JOIN_SESSION,
                "It was not possible to join sesion probably beacause session does not exists or you are already there..."
            )

        return
Example #12
0
    def getSharinfFrame(self, usr=None):

        db = DB_Accesser()

        if usr == None:
            usr = self.user

        skt = socketlist[usr][0]
        mtx = socketlist[usr][1]

        try:
            mtx.acquire()
            self.data = skt.recv(5 * 1024)
            mtx.release()
        except:
            print "CONNECTION ABRUPLY TERMINATED BY " + self.user
            return None

        if self.data is None:
            return None

        print "Data received: " + self.data

        if (self.data[:7] != "SHARINF"):
            print "No begin of frame"
            return None

        index = string.find(self.data, '\endF')

        if (index == -1):
            print "No end of frame"
            return None

        try:
            returnfram = sharinfFrame(self.data[7], self.data[8:index])
            return returnfram
        except:
            print "RECEIVED BAD FRAME FROM: " + self.user
            return None
Example #13
0
    def beaconDetectedHandle(self, data):

        print "Beacon detected: " + self.user
        db = DB_Accesser()

        beaconlist = []
        sessionlist = []

        for beacon in data:

            location = db.Get_Location(beacon)
            while (location == None):
                db.Insert_Location(beacon)
                location = db.Get_Location(beacon)

            print location

            db.Insert_UserInLocation({
                "email":
                self.user,
                "localizationID":
                location["BeaconState"]["localizationID"]
            })
            beaconlist.append(location)
            tempsessid = db.Get_ActiveSessionFromLocal(
                {"localizationID": location["BeaconState"]["localizationID"]})
            if (tempsessid is not None):
                sessionlist.append(db.Get_Session(tempsessid))
            else:
                sessionlist.append(None)

        self.returnSuccess(BEACON_DETECTED, {
            "BeaconList": beaconlist,
            "SessionList": sessionlist
        })

        return
Example #14
0
from Sharinf_DB_Con import DB_Accesser

myReqHandler = DB_Accesser()

m_user = {"email": "*****@*****.**"}

#myReqHandler.Insert_User(m_user)
#myReqHandler.print_allUsers()

m_file = {
    "link": "https://drive.google.com/drive/my-sdcdsa",
    "owneremail": "*****@*****.**",
    "name": "Mole.png"
}

#print(myReqHandler.Insert_File(m_file))
#myReqHandler.print_allFiles()
#uuid cenas" AND l.major = 12 AND l.minor = 32;
m_location = {"uuid": "wedewqe4", "major": 12, "minor": 32, "lvirtual": 0}
Beacon = {"localizationID": 1}
print(myReqHandler.Insert_Location(m_location))
#print(myReqHandler.Get_Location(Beacon))

#myReqHandler.cursor.close()
#myReqHndler.cnx.close()
sess = {
    "permissions": 3,
    "activeFlag": True,
    "localizationID": 1,
    "s_type": 1,
    "s_name": "Sharinf_123"