Esempio n. 1
0
    def crun(self, csocket):
        """Authenticates client and handles its jobs"""
        mysocket = pptransport.CSocketTransport(csocket, self.socket_timeout)
        #send PP version
        mysocket.send(version)
        #generate a random string
        srandom = "".join(
            [random.choice(string.ascii_letters) for i in range(16)])
        mysocket.send(srandom)
        answer = sha_new(ppc.b_(srandom + self.secret)).hexdigest()
        clientanswer = ppc.str_(mysocket.receive())
        if answer != clientanswer:
            self.logger.warning(
                "Authentication failed, client host=%s, port=%i" %
                csocket.getpeername())
            mysocket.send("FAILED")
            csocket.close()
            return
        else:
            mysocket.send("OK")

        ctype = ppc.str_(mysocket.receive())
        self.logger.debug("Control message received: " + ctype)
        self.ncon_add(1)
        try:
            if ctype == "STAT":
                #reset time at each new connection
                self.get_stats()["local"].time = 0.0
                #open('/tmp/pp.debug', 'a+').write('STAT: \n')
                mysocket.send(str(self.get_ncpus()))
                #open('/tmp/pp.debug', 'a+').write('STAT: get_ncpus\n')
                while 1:
                    mysocket.receive()
                    #open('/tmp/pp.debug', 'a+').write('STAT: recvd\n')
                    mysocket.send(str(self.get_stats()["local"].time))
                #open('/tmp/pp.debug', 'a+').write('STAT: _\n')
            elif ctype == "EXEC":
                while 1:
                    #open('/tmp/pp.debug', 'a+').write('EXEC: \n')
                    sfunc = mysocket.creceive()
                    #open('/tmp/pp.debug', 'a+').write('EXEC: '+repr((sfunc,))+'\n')
                    sargs = mysocket.receive()
                    #open('/tmp/pp.debug', 'a+').write('EXEC: '+repr((sargs,))+'\n')
                    fun = self.insert(sfunc, sargs)
                    sresult = fun(True)
                    #open('/tmp/pp.debug', 'a+').write('EXEC: '+repr((sresult,))+'\n')
                    mysocket.send(sresult)
                #open('/tmp/pp.debug', 'a+').write('EXEC: _\n')
        except:
            if self._exiting:
                return
            if pp.SHOW_EXPECTED_EXCEPTIONS:
                self.logger.debug(
                    "Exception in crun method (possibly expected)",
                    exc_info=True)
            self.logger.debug("Closing client socket")
            csocket.close()
            self.ncon_add(-1)
Esempio n. 2
0
    def crun(self, csocket):
        """Authenticates client and handles its jobs"""
        mysocket = pptransport.CSocketTransport(csocket, self.socket_timeout)
        #send PP version
        mysocket.send(version)
        #generate a random string
        srandom = "".join([random.choice(string.ascii_letters)
                for i in range(16)])
        mysocket.send(srandom)
        answer = sha_new(ppc.b_(srandom+self.secret)).hexdigest()
        clientanswer = ppc.str_(mysocket.receive())
        if answer != clientanswer:
            self.logger.warning("Authentication failed, client host=%s, port=%i"
                    % csocket.getpeername())
            mysocket.send("FAILED")
            csocket.close()
            return
        else:
            mysocket.send("OK")

        ctype = ppc.str_(mysocket.receive())
        self.logger.debug("Control message received: " + ctype)
        self.ncon_add(1)
        try:
            if ctype == "STAT":
                #reset time at each new connection
                self.get_stats()["local"].time = 0.0
               #open('/tmp/pp.debug', 'a+').write('STAT: \n')
                mysocket.send(str(self.get_ncpus()))
               #open('/tmp/pp.debug', 'a+').write('STAT: get_ncpus\n')
                while 1:
                    mysocket.receive()
                   #open('/tmp/pp.debug', 'a+').write('STAT: recvd\n')
                    mysocket.send(str(self.get_stats()["local"].time))
                   #open('/tmp/pp.debug', 'a+').write('STAT: _\n')
            elif ctype=="EXEC":
                while 1:
                   #open('/tmp/pp.debug', 'a+').write('EXEC: \n')
                    sfunc = mysocket.creceive()
                   #open('/tmp/pp.debug', 'a+').write('EXEC: '+repr((sfunc,))+'\n')
                    sargs = mysocket.receive()
                   #open('/tmp/pp.debug', 'a+').write('EXEC: '+repr((sargs,))+'\n')
                    fun = self.insert(sfunc, sargs)
                    sresult = fun(True)
                   #open('/tmp/pp.debug', 'a+').write('EXEC: '+repr((sresult,))+'\n')
                    mysocket.send(sresult)
                   #open('/tmp/pp.debug', 'a+').write('EXEC: _\n')
        except:
            if self._exiting:
                return
            if pp.SHOW_EXPECTED_EXCEPTIONS:
                self.logger.debug("Exception in crun method (possibly expected)", exc_info=True)
            self.logger.debug("Closing client socket")
            csocket.close()
            self.ncon_add(-1)
Esempio n. 3
0
 def send(self, msg):
    #l = len(ppc.b_(msg)) if (self.has_wb or self.w.mode == 'wb') else len(ppc.str_(msg))
    #open('/tmp/pp.debug', 'a+').write(repr(('s', l, self.w, msg))+'\n')
     if self.has_wb or self.w.mode == 'wb':
         msg = ppc.b_(msg)
         self.wb.write(struct.pack("!Q", len(msg)))
         self.w.flush()
     else: #HACK: following may be > 8 bytes, needed for len(msg) >= 256
         msg = ppc.str_(msg)
         self.wb.write(ppc.str_(struct.pack("!Q", len(msg))))
         self.w.flush()
     self.wb.write(msg)
     self.w.flush()
Esempio n. 4
0
 def send(self, msg):
     #l = len(ppc.b_(msg)) if (self.has_wb or self.w.mode == 'wb') else len(ppc.str_(msg))
     #open('/tmp/pp.debug', 'a+').write(repr(('s', l, self.w, msg))+'\n')
     if self.has_wb or self.w.mode == 'wb':
         msg = ppc.b_(msg)
         self.wb.write(struct.pack("!Q", len(msg)))
         self.w.flush()
     else:  #HACK: following may be > 8 bytes, needed for len(msg) >= 256
         msg = ppc.str_(msg)
         self.wb.write(ppc.str_(struct.pack("!Q", len(msg))))
         self.w.flush()
     self.wb.write(msg)
     self.w.flush()
    def listen(self):
        """Listens for broadcasts from other clients/servers"""
        self.base.logger.debug("Listening (%s, %i)" % self.interface_addr)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.socket.settimeout(5)
        self.socket.bind(self.interface_addr)

        ppc.start_thread("broadcast",  self.broadcast)

        while True:
            try:
                if self.base._exiting:
                    return
                message, (host, port) = self.socket.recvfrom(1024)
                message = ppc.str_(message)
                remote_address = (host, self.broadcast_addr[1])
                hostid = host + ":" + str(self.broadcast_addr[1])
                self.base.logger.debug("Discovered host (%s, %i) message=%c"
                        % (remote_address + (message[0], )))
                if not self.base.autopp_list.get(hostid, 0) and self.isclient \
                        and message[0] == 'S':
                    self.base.logger.debug("Connecting to host %s" % (hostid, ))
                    ppc.start_thread("ppauto_connect1",  self.base.connect1,
                            remote_address+(False, ))
                if not self.isclient and message[0] == 'C':
                    self.base.logger.debug("Replying to host %s" % (hostid, ))
                    self.bsocket.sendto(ppc.b_("S"), self.broadcast_addr)
            except socket.timeout:
                pass
            except:
                self.base.logger.error("An error has occured during execution of "
                        "Discover.listen")
                sys.excepthook(*sys.exc_info())
Esempio n. 6
0
    def listen(self):
        """Listens for broadcasts from other clients/servers"""
        self.base.logger.debug("Listening (%s, %i)" % self.interface_addr)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.socket.settimeout(5)
        self.socket.bind(self.interface_addr)

        ppc.start_thread("broadcast",  self.broadcast)

        while True:
            try:
                if self.base._exiting:
                    return
                message, (host, port) = self.socket.recvfrom(1024)
                message = ppc.str_(message)
                remote_address = (host, self.broadcast_addr[1])
                hostid = host + ":" + str(self.broadcast_addr[1])
                self.base.logger.debug("Discovered host (%s, %i) message=%c"
                        % (remote_address + (message[0], )))
                if not self.base.autopp_list.get(hostid, 0) and self.isclient \
                        and message[0] == 'S':
                    self.base.logger.debug("Connecting to host %s" % (hostid, ))
                    ppc.start_thread("ppauto_connect1",  self.base.connect1,
                            remote_address+(False, ))
                if not self.isclient and message[0] == 'C':
                    self.base.logger.debug("Replying to host %s" % (hostid, ))
                    self.bsocket.sendto(ppc.b_("S"), self.broadcast_addr)
            except socket.timeout:
                pass
            except:
                self.base.logger.error("An error has occured during execution of "
                        "Discover.listen")
                sys.excepthook(*sys.exc_info())
Esempio n. 7
0
 def creceive(self, preprocess=None):
     msg = self.receive()
    #if hasattr(self, 'r'):
    #    open('/tmp/pp.debug', 'a+').write(repr(('cr',  self.r, msg))+'\n')
    #else:
    #    open('/tmp/pp.debug', 'a+').write(repr(('cr', self.socket, msg))+'\n')
     msg = ppc.b_(msg)
     if msg[:1] == ppc.b_('H'):
         hash1 = ppc.str_(msg[1:])
     else:
         msg = msg[1:]
         hash1 = self.hash(msg)
         if preprocess is None: preprocess = lambda x:x
         self.rcache[hash1] = tuple(map(preprocess, (msg, )))[0]
     return self.rcache[hash1]
Esempio n. 8
0
 def creceive(self, preprocess=None):
     msg = self.receive()
     #if hasattr(self, 'r'):
     #    open('/tmp/pp.debug', 'a+').write(repr(('cr',  self.r, msg))+'\n')
     #else:
     #    open('/tmp/pp.debug', 'a+').write(repr(('cr', self.socket, msg))+'\n')
     msg = ppc.b_(msg)
     if msg[:1] == ppc.b_('H'):
         hash1 = ppc.str_(msg[1:])
     else:
         msg = msg[1:]
         hash1 = self.hash(msg)
         if preprocess is None: preprocess = lambda x: x
         self.rcache[hash1] = tuple(map(preprocess, (msg, )))[0]
     return self.rcache[hash1]
Esempio n. 9
0
 def authenticate(self, secret):
     remote_version = ppc.str_(self.receive())
     if version != remote_version:
         logging.error("PP version mismatch (local: pp-%s, remote: pp-%s)"
             % (version, remote_version))
         logging.error("Please install the same version of PP on all nodes")
         return False
     srandom = ppc.b_(self.receive())
     secret = ppc.b_(secret)
     answer = sha_new(srandom+secret).hexdigest()
     self.send(answer)
     response = ppc.b_(self.receive())
     if response == ppc.b_("OK"):
         return True
     else:
         return False
Esempio n. 10
0
 def authenticate(self, secret):
     remote_version = ppc.str_(self.receive())
     if version != remote_version:
         logging.error("PP version mismatch (local: pp-%s, remote: pp-%s)" %
                       (version, remote_version))
         logging.error("Please install the same version of PP on all nodes")
         return False
     srandom = ppc.b_(self.receive())
     secret = ppc.b_(secret)
     answer = sha_new(srandom + secret).hexdigest()
     self.send(answer)
     response = ppc.b_(self.receive())
     if response == ppc.b_("OK"):
         return True
     else:
         return False
Esempio n. 11
0
    def run(self):
        try:
            #execution cycle
            while 1:
                __fname, __fobjs = self.t.creceive(preprocess)

                __sargs = self.t.receive()

                for __fobj in __fobjs:
                    try:
                        six.exec_(__fobj)
                        globals().update(locals())
                    except:
                        print("An error has occured during the " + \
                              "function import")
                        sys.excepthook(*sys.exc_info())

                __args = pickle.loads(ppc.b_(__sargs))

                __f = locals()[ppc.str_(__fname)]
                try:
                    __result = __f(*__args)
                except:
                    print("An error has occured during the function execution")
                    sys.excepthook(*sys.exc_info())
                    __result = None

                __sresult = pickle.dumps((__result, self.sout.getvalue()),
                                         self.pickle_proto)

                self.t.send(__sresult)
                self.sout.truncate(0)
        except:
            print("A fatal error has occured during the function execution")
            sys.excepthook(*sys.exc_info())
            __result = None
            __sresult = pickle.dumps((__result, self.sout.getvalue()),
                                     self.pickle_proto)
            self.t.send(__sresult)
Esempio n. 12
0
    def run(self):
        try:
            #execution cycle
            while 1:
                __fname, __fobjs = self.t.creceive(preprocess)

                __sargs = self.t.receive()

                for __fobj in __fobjs:
                    try:
                        six.exec_(__fobj)
                        globals().update(locals())
                    except:
                        print("An error has occured during the " + \
                              "function import")
                        sys.excepthook(*sys.exc_info())

                __args = pickle.loads(ppc.b_(__sargs))
            
                __f = locals()[ppc.str_(__fname)]
                try:
                    __result = __f(*__args)
                except:
                    print("An error has occured during the function execution")
                    sys.excepthook(*sys.exc_info())
                    __result = None

                __sresult = pickle.dumps((__result, self.sout.getvalue()),
                        self.pickle_proto)

                self.t.send(__sresult)
                self.sout.truncate(0)
        except:
            print("A fatal error has occured during the function execution")
            sys.excepthook(*sys.exc_info())
            __result = None
            __sresult = pickle.dumps((__result, self.sout.getvalue()),
                    self.pickle_proto)
            self.t.send(__sresult)