Пример #1
0
def main():
    try:
        with open('config.json', 'r') as f:
            config = json.load(f)
        ports = config['AddressBook']
        num_ports = len(ports)
    except Exception as e:
        raise e

    while True:
        customer = client()
        server_id = input('Whom to connect ? 1-%d: ' % num_ports )
        request = raw_input('How can we help you? --')
        if request == 'buy':
            uuid_ = uuid.uuid1()
            requestThread = KThread(target = customer.buyTickets, args =  (ports[server_id - 1], request, uuid_))
            timeout = 5
        else:
            requestThread = KThread(target = customer.show_state, args = (ports[server_id - 1],))
            timeout = 5
        start_time = time.time()
        requestThread.start()
        while time.time() - start_time < timeout:
            if not requestThread.is_alive():
                break
        if requestThread.is_alive():
            requestThread.kill()
            msgP = 'Timeout! Try again'
            print msgP
Пример #2
0
 def Subscribe(self,queue=None,callback=None):
    #
    # the queue and callback being subscribed to is not passed in here
    # but passed in through via "self"
    # the question is how to make this scale?
    #
    #
    #
    
    self.lockObj.acquire()     
    if (queue == None):    # done for compatibility with "simple" version
      queue = self.myqueue
    if (callback == None):
      callback = self._mycallback
      
    #
    # communication parameters within thread
    #
    self.thread_params.append(queue)       # 1
    self.thread_params.append(callback)    # 2
    #
    # control param for thread
    #
    self.ev_obj = Event()
    self.thread_params.append(self.ev_obj)      # 3 - top of stack
     
    subscribe = KThread(target = self._SubscribeThread)
    subscribe.start() 
    self.lockObj.release()    
    #
    # return thread and its control object
    #
    return subscribe, self.ev_obj
Пример #3
0
	def __init__(self, id_):
		self.id = id_
		self.config_file = 'config-%d' % self.id

		self.role = 'follower'
		self.commitIndex, self.lastApplied, self.leaderID = 0, 0, 0

		address = json.load(file('config.json'))
		self.initial_state = address['initial_state']
		self.addressbook = {}
		for id_ in address['running']:
			self.addressbook[id_] = address['AddressBook'][id_ - 1]

		# need to put it into file later on
		self.load()

		self.lastLogIndex = 0
		self.lastLogTerm, self.oldVotes, self.newVotes, self.numVotes = 0, 0, 0, 0

		self.port = self.addressbook[self.id]
		self.request_votes = self.peers[:]


		self.listener = KThread(target = self.listen, args= (acceptor,))
		self.listener.start()
		self.newPeers = []
class HttpServer(threading.Thread):
    """Thread class with a stop() method. The thread itself has to check
    regularly for the stopped() condition."""
    def stop(self):
        self.server.kill()
        print "HTTP server stopped"
        pass

    pass

    def stopped(self):
        return self._stop.isSet()
        pass

    pass

    def start(self):
        self.server = KThread(target=self.start_thread)
        self.server.start()
        pass

    pass

    def start_thread(self):
        PORT = 8000  # port used by the http server

        Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        httpd = SocketServer.TCPServer(('', PORT), Handler)

        print "HTTP serving at port", PORT
        httpd.serve_forever()
        pass

    pass
Пример #5
0
    def Subscribe(self, queue=None, callback=None):
        #
        # the queue and callback being subscribed to is not passed in here
        # but passed in through via "self"
        # the question is how to make this scale?
        #
        #
        #

        self.lockObj.acquire()
        if queue == None:  # done for compatibility with "simple" version
            queue = self.myqueue
        if callback == None:
            callback = self._mycallback

        #
        # communication parameters within thread
        #
        self.thread_params.append(queue)  # 1
        self.thread_params.append(callback)  # 2
        #
        # control param for thread
        #
        self.ev_obj = Event()
        self.thread_params.append(self.ev_obj)  # 3 - top of stack

        subscribe = KThread(target=self._SubscribeThread)
        subscribe.start()
        self.lockObj.release()
        #
        # return thread and its control object
        #
        return subscribe, self.ev_obj
Пример #6
0
 def Regression_Start(self,event):
     self.cleanProgress()
     self.ParseArgs()
     self.InitupdateProgress()
     self.t4 = KThread(target=self.regression_thread)
     self.t4.start()
     self.t2 = KThread(target=self.updateProgress)
     self.t2.start()
Пример #7
0
 def listen(self, on_accept):
     srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     srv.bind(("", self.port))
     print 'start listenning'
     while True:
         data, addr = srv.recvfrom(1024)
         thr = KThread(target=on_accept, args=(self, data, addr))
         thr.start()
     srv.close()
Пример #8
0
 def step_down(self):
     if self.role == 'candidate':
         print 'candidate step down when higher term'
         self.election.kill()
         self.last_update = time.time()
         self.role = 'follower'
     elif self.role == 'leader':
         self.leader_state.kill()
         self.follower_state = KThread(target=self.follower, args=())
         self.follower_state.start()
Пример #9
0
    def onRun(self, event):

        self.cleanProgress()
        self.ParseArgs()
        self.InitupdateProgress()

        self.t1 = KThread(target=self.Start_Test)
        self.t1.start()

        self.t2 = KThread(target=self.updateProgress)
        self.t2.start()
 def listen(self, on_accept):
     logger.debug('Server Listen Method')
     srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     srv.bind(("", self.port))
     print 'start listenning ', self.id, " ", self.currentTerm
     while True:
         data,addr = srv.recvfrom(1024)
         #print 'listening ', self.id
         thr = KThread(target=on_accept, args=(self,data,addr))
         thr.start()
     srv.close()
 def step_down(self):
     logger.info('Server step_down Method')
     if self.role == 'candidate':
         print 'candidate step down when higher term and becomes follower', self.id, " ", self.currentTerm
         self.election.kill()
         self.last_update = time.time()
         self.role = 'follower'
     elif self.role == 'leader':
         self.leader_state.kill()
         self.follower_state = KThread(target=self.follower, args=())
         self.follower_state.start()
    def startElection(self):
        logger.info('Server startElection Method server ID - {} Current Term = {}'.format(self.id, self.currentTerm))
        self.role = 'candidate'
        self.election = KThread(target =self.threadElection, args = ())
        if len(self.peers) != 0:
            self.currentTerm += 1
            self.votedFor = self.id
            self.save()
            self.numVotes = 1

            self.election.start()
Пример #13
0
	def start_election(self):
		self.role = 'candidate'
		self.election = KThread(target =self.thread_election,args = ())
		if len(self.peers) == 0:
			return
		self.currentTerm += 1
		self.votedFor = self.id
		serverConfig = ServerConfig(self.poolsize, self.currentTerm, self.votedFor, self.log, self.peers)
		with open(self.config_file, 'w') as f:
			pickle.dump(serverConfig, f)

		self.numVotes = 1
		self.election.start()
Пример #14
0
 def initiateVote(self):
     # Todo: mcip, make sure the term is the same while request vote????
     req_term = self.currentTerm
     for idx, addr in enumerate(self.addresses):
         if idx == self.id:
             continue
         # Create a thread for each request vote
         # Todo: mcip: req_term should be the same
         election_thread = KThread(target=self.thread_election,
                                   args=(
                                       idx,
                                       addr,
                                       req_term,
                                   ))
         election_thread.start()
def responseVote(server, Msg, addr):
    _sender = Msg.sender
    _term = Msg.term
    _msg = Msg.data
    if DEBUG or ACCEPTOR:
        print '---------Get vote response message---------'
    voteGranted = int(_msg)
    if voteGranted:
        if server.during_change == 0:
            if server.role == 'candidate':
                if _sender in server.request_votes:
                    server.request_votes.remove(_sender)
                server.numVotes += 1
                if server.numVotes == server.majority:
                    print 'Got majority votes, become leader at Term %d' % server.currentTerm
                    if server.election.is_alive():
                        server.election.kill()
                    # becomes a leader
                    server.role = 'leader'
                    server.follower_state.kill()
                    print 'new leader ', server.id, ' during change 0'
                    server.leader_state = KThread(target=server.leader,
                                                  args=())
                    server.leader_state.start()
        else:
            server.request_votes.remove(_sender)
            if _sender in server.peers:
                server.newVotes += 1
            majority = len(server.new) / 2 + 1
            if server.newVotes >= majority:
                print 'Got majority from new, become leader at Term %d' % server.currentTerm
                if server.election.is_alive():
                    server.election.kill()
                server.role = 'leader'
                print 'new leader ', server.id
                server.follower_state.kill()
                server.leader_state = KThread(target=server.leader, args=())
                server.leader_state.start()
    else:
        if _term > server.currentTerm:  # discover higher term
            server.currentTerm = _term
            server.save()
            if DEBUG or ACCEPTOR:
                print 'Server '
            if server.role == 'candidate':
                server.stepDown()

        print 'vote rejected by ', _sender, ' to ', server.id
Пример #16
0
	def __init__(self, id_):
		self.id = id_
		self.config_file = 'config-%d' % self.id

		#self.load()
		self.role = 'follower'
		self.commitIndex = 0
		self.lastApplied = 0

		self.leaderID = 0
		with open(r'C:\Users\Divya\Raft-python\config.json') as f:
			address = json.load(f)
		port_list = address['AddressBook']
		running = address['running']
		self.initial_state = address['initial_state']
		self.addressbook = {}
		for id_ in running:
			self.addressbook[id_] = port_list[id_ - 1]

		# need to put it into file later on
		self.load()


		self.port = self.addressbook[self.id]

		# self.nextIndex = {}
 	# 	self.matchIndex = {}
 	# 	for peer in self.peers:
 	# 		self.nextIndex[peer] = len(self.log) + 1
 	# 		self.matchIndex[peer] = 0

		self.request_votes = self.peers[:]

		self.numVotes = 0
		self.oldVotes = 0
		self.newVotes = 0

		self.lastLogIndex = 0
		self.lastLogTerm = 0

		self.listener = KThread(target = self.listen, args= (acceptor,))
		self.listener.start()

		self.during_change = 0
		self.newPeers = []
		self.new = None
		self.old = None
Пример #17
0
    def candidate(self):
        with self.candidateCond:
            self.candidateCond.wait()
        while True:
            self.logger.critical(
                f'[Role]: Running as a candidate, elec timeout <%.4f>' %
                self.currElectionTimeout)
            # Upon conversion to candidate, start election
            # Increment current term, vote for self, reset election timer, send requestVote RPCs to all other servers

            # self.logger.critical(f'RAFT[Vote]: Server <{self.id}> initiated voting for term <{self.currentTerm}> '
            #                      f'took <%.4f> seconds' % (time.time()-start_time))
            self.save(current_term=self.currentTerm + 1, voted_for=self.id)
            self.numVotes = 1
            self.currElectionTimeout = random.uniform(
                self.maxElectionTimeout / 2, self.maxElectionTimeout) / 1000
            self.election = KThread(target=self.initiateVote, args=())
            self.election.start()
            self.logger.info(
                f'[Vote]: Start, voted for self <{self.id}> term <{self.currentTerm}> '
                f'election timeout: <%.4f>' % self.currElectionTimeout)
            self.lastUpdate = time.time()
            while time.time(
            ) - self.lastUpdate <= self.currElectionTimeout and self.role == KVServer.candidate:
                with self.candidateCond:
                    self.candidateCond.wait(
                        self.currElectionTimeout -
                        (time.time() - self.lastUpdate))  # - elapse time
                if self.numVotes >= self.majority or self.role == KVServer.follower:
                    break
            self.save(current_term=self.currentTerm, voted_for=-1)
            if self.role == KVServer.leader:
                with self.leaderCond:
                    self.leaderCond.notify_all()
                with self.candidateCond:
                    # self.logger.critical(f"in candidate, larger than majority")
                    self.candidateCond.wait()
            elif self.role == KVServer.follower:
                with self.followerCond:
                    self.followerCond.notify_all()
                with self.candidateCond:
                    self.candidateCond.wait()
Пример #18
0
 def start_election(self):
     self.role = 'candidate'
     self.election = KThread(target=self.thread_election, args=())
     if len(self.peers) != 0:
         self.currentTerm += 1
         self.votedFor = self.id
         self.save()
         self.numVotes = 1
         if self.during_change == 1:
             self.newVotes = 0
             self.oldVotes = 0
             if self.id in self.new:
                 self.newVotes = 1
             if self.id in self.old:
                 self.oldVotes = 1
         elif self.during_change == 2:
             self.newVotes = 0
             if self.id in self.new:
                 self.newVotes = 1
         self.election.start()
Пример #19
0
 def create_threads(self, data):
     logging.info("Creating threads")
     #create threads
     global last_thread
     start_id = last_thread
     for i in range(start_id, start_id + int(data['count'])):
         thread_name = 'worker-{}'.format(i)
         self.threads.append(
             KThread(name=thread_name,
                     target=etcdUtils(self.ip, self.port,
                                      self.kv_root).set_worker,
                     args=(random.randint(5, 15), )))
    def __init__(self, id_):
        self.id = id_
        self.friendlyName = self._formFriendlyName(id_)
        self.configFile = 'config-%d' % self.id
        logger.debug('Server Init. Config File = {}'.format(self.configFile))
        self.role = 'follower'
        self.commitIndex = 0
        self.lastApplied = 0
        self.leaderID = 0
        address = json.load(file('config.json'))
        port_list = address['AddressBook']
        running = address['running']
        self.groupInfo = {}
        # self.initialState = 100
        self.addressbook = {}
        for id_ in running:
            self.addressbook[id_] = port_list[id_ - 1]
            
        # need to put it into file later on
        self.load()


        self.port = self.addressbook[self.id]
        self.request_votes = self.peers[:]

        self.numVotes = 0
        self.oldVotes = 0
        self.newVotes = 0

        self.lastLogIndex = 0
        self.lastLogTerm = 0

        self.listener = KThread(target = self.listen, args= (acceptor,))
        self.listener.start()
        logger.info('Started listening on port {}'.format(self.port))

        self.during_change = 0
        self.newPeers = []
        self.new = None
        self.old = None
Пример #21
0
 def append_entries(self):
     while self.role == KVServer.leader:
         # Todo: for debugging only
         # # self.debug1 += 1
         # self.logModify([self.debug1, "aa", "bb"], LogMod.APPEND)
         # self.logModify([self.debug1, "bb", "cc"], LogMod.APPEND)
         app_ent_term = self.currentTerm
         for idx, addr in enumerate(self.addresses):
             if idx == self.id:
                 continue
             # Create a thread for each append_entry message
             # Todo: mcip: append entries term is the same
             append_thread = KThread(target=self.thread_append_entry,
                                     args=(
                                         idx,
                                         addr,
                                         app_ent_term,
                                     ))
             append_thread.start()
         # Send append entry every following seconds, or be notified and wake up
         # Todo: will release during wait
         with self.appendEntriesCond:
             self.appendEntriesCond.wait(timeout=self.appendEntriesTimeout)
Пример #22
0
    def playClickedFile(self, event):   # play selected file in listbox with playerInstance
        try:
            if self.playerInstance.cur_song.playing:
                self.playerInstance.cur_song.stop()
                self.songGaugeThread.kill()
        except AttributeError:
            pass

        self.playerInstance.playFile(str(event.GetItem().GetText()))
        
        if self.playerInstance.cur_song.playing:
            #thread.start_new_thread(self.playingSongLoop,())
            self.songGaugeThread = KThread(target=self.playingSongLoop)
            self.songGaugeThread.start()
Пример #23
0
 def connect_client(self):
     s = socket.socket()
     port_client = server_conf[self.server]['port_client']
     s.bind(('', port_client))
     s.listen(200)
     self.txn_thread = {}
     while True:
         connection, address = s.accept()
         #  start thread to respond to client
         self.txn_thread[address] = KThread(target=self.getTxnData,
                                            args=(address, ))
         lock3.acquire()
         self.txnConnectionLog[address] = connection
         lock3.release()
         self.txn_thread[address].start()
Пример #24
0
    Admin
    Bassamazzam
    
    """ % (FROM, ", ".join(TO), SUBJECT,name ,username,password,host,ssh_port)



    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()
def st():
   line = sys.stdin.readline()
   while line:
      send(line)
      line = sys.stdin.readline()
sthr =KThread(target=st)
def quitf():
    thr.kill()
    sthr.kill()
    sys.exit()
def send(msg):
   ircsock.send(msg+"\n")
   if options.verbose == True:
      print "-> "+msg

def prmsg(msg):
   if options.verbose == True:
      print "<- "+msg

def commands(nick,channel,message):
   global botnick
Пример #25
0
class Server(object):
    def __init__(self, id_):
        self.id = id_
        self.config_file = 'config-%d' % self.id

        #self.load()
        self.role = 'follower'
        self.commitIndex = 0
        self.lastApplied = 0

        self.leaderID = 0

        address = json.load(file('config.json'))
        port_list = address['AddressBook']
        running = address['running']
        self.initial_state = address['initial_state']
        self.addressbook = {}
        for id_ in running:
            self.addressbook[id_] = port_list[id_ - 1]

        # need to put it into file later on
        self.load()

        self.port = self.addressbook[self.id]

        # self.nextIndex = {}
        # 	self.matchIndex = {}
        # 	for peer in self.peers:
        # 		self.nextIndex[peer] = len(self.log) + 1
        # 		self.matchIndex[peer] = 0

        self.request_votes = self.peers[:]

        self.numVotes = 0
        self.oldVotes = 0
        self.newVotes = 0

        self.lastLogIndex = 0
        self.lastLogTerm = 0

        self.listener = KThread(target=self.listen, args=(acceptor, ))
        self.listener.start()

        self.during_change = 0
        self.newPeers = []
        self.new = None
        self.old = None

    def listen(self, on_accept):
        srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        srv.bind(("", self.port))
        print 'start listenning'
        while True:
            data, addr = srv.recvfrom(1024)
            thr = KThread(target=on_accept, args=(self, data, addr))
            thr.start()
        srv.close()

    def follower(self):
        print 'Running as a follower'
        self.role = 'follower'
        self.last_update = time.time()
        election_timeout = 5 * random.random() + 5
        while time.time() - self.last_update <= election_timeout:
            pass
        self.start_election()
        while True:
            self.last_update = time.time()
            election_timeout = 5 * random.random() + 5
            while time.time() - self.last_update <= election_timeout:
                pass

            if self.election.is_alive():
                self.election.kill()
            self.start_election()

    def start_election(self):
        self.role = 'candidate'
        self.election = KThread(target=self.thread_election, args=())
        if len(self.peers) != 0:
            self.currentTerm += 1
            self.votedFor = self.id
            self.save()
            self.numVotes = 1
            if self.during_change == 1:
                self.newVotes = 0
                self.oldVotes = 0
                if self.id in self.new:
                    self.newVotes = 1
                if self.id in self.old:
                    self.oldVotes = 1
            elif self.during_change == 2:
                self.newVotes = 0
                if self.id in self.new:
                    self.newVotes = 1
            self.election.start()

    def thread_election(self):
        print 'timouts, start a new election with term %d' % self.currentTerm
        self.role = 'candidate'
        self.request_votes = self.peers[:]
        sender = self.id

        while 1:
            # print 'Send vote request to ', self.request_votes
            for peer in self.peers:
                if peer in self.request_votes:
                    Msg = str(self.lastLogTerm) + ' ' + str(self.lastLogIndex)
                    msg = RequestVoteMsg(sender, peer, self.currentTerm, Msg)
                    data = pickle.dumps(msg)
                    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    sock.sendto(data, ("", self.addressbook[peer]))
            time.sleep(1)  # wait for servers to receive

    def leader(self):
        print 'Running as a leader'
        self.role = 'leader'
        self.nextIndex = {}
        self.matchIndex = {}
        for peer in self.peers:
            self.nextIndex[peer] = len(self.log) + 1
            self.matchIndex[peer] = 0
        self.append_entries()

    def append_entries(self):

        receipts = self.peers[:]
        while 1:
            receipts = self.peers[:]
            if self.during_change != 0:
                for peer in receipts:
                    if peer not in self.nextIndex:
                        self.nextIndex[peer] = len(self.log) + 1
                        self.matchIndex[peer] = 0
            for peer in receipts:
                if len(self.log) >= self.nextIndex[peer]:
                    prevLogIndex = self.nextIndex[peer] - 1
                    if prevLogIndex != 0:
                        prevLogTerm = self.log[prevLogIndex - 1].term
                    else:
                        prevLogTerm = 0
                    entries = [self.log[self.nextIndex[peer] - 1]]
                else:
                    entries = []
                    prevLogIndex = len(self.log)
                    if prevLogIndex != 0:
                        prevLogTerm = self.log[prevLogIndex - 1].term
                    else:
                        prevLogTerm = 0

                Msg = AppendEntriesMsg(self.id, peer, self.currentTerm,
                                       entries, self.commitIndex, prevLogIndex,
                                       prevLogTerm)
                data = pickle.dumps(Msg)
                sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                sock.sendto(data, ("", self.addressbook[peer]))
            time.sleep(0.5)

    def step_down(self):
        if self.role == 'candidate':
            print 'candidate step down when higher term'
            self.election.kill()
            self.last_update = time.time()
            self.role = 'follower'
        elif self.role == 'leader':
            self.leader_state.kill()
            self.follower_state = KThread(target=self.follower, args=())
            self.follower_state.start()

    def load(self):
        initial_running = [1, 2, 3]
        # new_quorom = []

        try:
            with open(self.config_file) as f:
                serverConfig = pickle.load(f)
        except Exception as e:
            if self.id not in initial_running:
                serverConfig = ServerConfig(100, 0, -1, [], [])
            else:
                initial_running.remove(self.id)
                serverConfig = ServerConfig(
                    100, 0, -1, [], initial_running
                )  #Server config = obiect de stocare a datelor despre server

        self.poolsize = serverConfig.poolsize
        self.currentTerm = serverConfig.currentTerm
        self.votedFor = serverConfig.votedFor
        self.log = serverConfig.log
        self.peers = serverConfig.peers
        self.majority = (len(self.peers) + 1) / 2 + 1
        # self.new_quorom = new_quorom
        #self.majority_1 = (len(self.new_quorom) + 1)/2 + 1

    def save(self):
        serverConfig = ServerConfig(self.poolsize, self.currentTerm,
                                    self.votedFor, self.log, self.peers)
        with open(self.config_file, 'w') as f:
            pickle.dump(serverConfig, f)

    def run(self):
        time.sleep(1)
        self.follower_state = KThread(target=self.follower, args=())
        self.follower_state.start()
Пример #26
0
class mp3GUI(wx.Frame):
    fileList = []
    def __init__(self, parent, id, title,playerInstance):
        root = wx.App()
        wx.Frame.__init__(self, parent, id, title, size=(650, 380))

        hbox = wx.BoxSizer(wx.VERTICAL)

        panel = wx.Panel(self, -1)

        self.list = SortedListCtrl(panel,self.fileList)
        self.list.InsertColumn(0, 'file', 635)

        self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.playClickedFile)

        self.updateFileList()

        gauge = wx.Gauge(panel, range=100, size=(100, 10))
        hbox.Add(self.list, 1, wx.EXPAND)
        hbox.Add(gauge, 0, wx.EXPAND)
        panel.SetSizer(hbox)

        
        #panel2 = wx.Panel(self, 0)
        #hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        #gauge = wx.Gauge(panel2, range=1000, size=(200, 15))
        #hbox2.Add(gauge,1,wx.EXPAND)
        #panel2.SetSizer(hbox2)

        self.CreateStatusBar()
        self.SetStatusText("This is the statusbar")

        MN_ADD_DIR  =   101
        MN_ADD_FILE =   102
        MN_QUIT     =   100

        file_menu = wx.Menu()
        menubar = wx.MenuBar()
        file_menu.Append(MN_ADD_DIR, "Add &Directory ...", "Select a directory to add to the list")
        file_menu.Append(MN_ADD_FILE, "Add &Files ...", "Select files add to the list")
        file_menu.AppendSeparator()
        file_menu.Append(MN_QUIT, "&Quit", "Quit the application")

        self.Bind(wx.EVT_MENU, self.pickDirectory, id=MN_ADD_DIR)
        self.Bind(wx.EVT_MENU, self.pickFile, id=MN_ADD_FILE)
        self.Bind(wx.EVT_MENU, self.onExit, id=MN_QUIT)

        self.Bind(wx.EVT_CLOSE, self.onExit)    # user / app quits
        self.Bind(wx.EVT_QUERY_END_SESSION, self.onExit) # system shutdown, veto
        self.Bind(wx.EVT_END_SESSION, self.onExit) # system shutdown, no veto

        menubar.Append(file_menu, '&File')
        self.SetMenuBar(menubar)

        self.Centre()
        self.Show(True)
        self.playing = False
        self.root = root
        self.playerInstance = playerInstance
        self.gauge = gauge        

    def onExit(self,event):
        self.need_abort = True
        if self.playing or self.playerInstance.cur_song.playing:
            self.playerInstance.cur_song.stop()
            self.songGaugeThread.kill()
            print "\nStopped music."
        
        print "Closing application..."
        self.Destroy()

    def start(self):
        self.root.MainLoop()

    def updateFileList(self):
        items = self.list.itemDataMap

        for i in xrange(len(items)):
            index = self.list.InsertStringItem(sys.maxint, items[i])
            #self.list.SetStringItem(index, 1, data[1])
            #self.list.SetStringItem(index, 2, data[2])
            self.list.SetItemData(index, i)

    def GetItemText(self, item):
        return self.list.itemDataMap[item]

    def pickFile(self, event):             # Call "Choose file..." dialog
        temp_location = wx.EmptyString
        # for multiple formats: wildcard="*.mp3;*.wav"
        
        tfiles = wx.FileDialog(self, "Choose one or more .mp3 files", temp_location, wildcard="*.mp3", style=wx.FD_MULTIPLE)
        if (tfiles.ShowModal() == wx.ID_OK):
            filepaths = tfiles.GetPaths()
        print filepaths
        self.list.itemDataMap = list(set(filepaths))
        # append selected file and update the list
        #self.fileList.append(tfile)
        self.updateFileList()

    def pickDirectory(self, event):             # Call "Choose directory..." dialog
        temp_location = wx.EmptyString
        tdir = wx.DirDialog(self, "Choose a directory with mp3 files", temp_location, style=wx.DD_DIR_MUST_EXIST)
        if (tdir.ShowModal() == wx.ID_OK):
            path = tdir.GetPath()
        
            tfileList = []
            for (dirpath, dirnames, filenames) in os.walk(path):
                for tfile in filenames:
                    if tfile.endswith(".mp3") or tfile.endswith(".wav"):
                        tfileList.append(dirpath+"/"+tfile)
        #print tfileList
        self.list.itemDataMap = list(set(tfileList))
        # append selected file and update the list
        #self.fileList.append(tfile)
        self.updateFileList()

    def playClickedFile(self, event):   # play selected file in listbox with playerInstance
        try:
            if self.playerInstance.cur_song.playing:
                self.playerInstance.cur_song.stop()
                self.songGaugeThread.kill()
        except AttributeError:
            pass

        self.playerInstance.playFile(str(event.GetItem().GetText()))
        
        if self.playerInstance.cur_song.playing:
            #thread.start_new_thread(self.playingSongLoop,())
            self.songGaugeThread = KThread(target=self.playingSongLoop)
            self.songGaugeThread.start()
            

    def playingSongLoop(self):
        self.gauge.SetRange(self.playerInstance.cur_song.length)
        while self.playerInstance.cur_song.playing:
            self.gauge.SetValue(self.playerInstance.cur_song.position)
            self.playing = self.playerInstance.cur_song.playing
            time.sleep(0.8)
Пример #27
0
def main():
    try:
        with open('config.json', 'r') as f:
            config = json.load(f)
        ports = config['AddressBook']
        num_ports = len(ports)
    except Exception as e:
        raise e

    while True:
        customer = client()
        server_id = input(
            'Which datacenter do you want to connect to? 1-%d: ' % num_ports)
        request = raw_input('How can we help you? --')
        if request == 'show':
            requestThread = KThread(target=customer.show_state,
                                    args=(ports[server_id - 1], ))
            timeout = 5
        elif request.split()[0] == 'change':
            uuid_ = uuid.uuid1()
            msg_split = request.split()
            new_config_msg = msg_split[1:]
            new_config = [int(item) for item in new_config_msg]
            print new_config
            requestThread = KThread(target=customer.config_change,
                                    args=(ports[server_id - 1], new_config,
                                          uuid_))
            timeout = 20
        else:
            uuid_ = uuid.uuid1()
            requestThread = KThread(target=customer.buyTickets,
                                    args=(ports[server_id - 1], request,
                                          uuid_))
            timeout = 5
        start_time = time.time()
        requestThread.start()
        while time.time() - start_time < timeout:
            if not requestThread.is_alive():
                break
        if requestThread.is_alive():
            print 'Timeout! Try again'
            requestThread.kill()
Пример #28
0
             #   print element['whatsapp_id']
              #  print ("Inside For loop")
                while True :
                    try :
                        Awhatsapp.selecting_from_recent_contact(element['whatsapp_id'])
                        Awhatsapp.send_message(element['msg'])
                        break
                    except :
                        #print "IN except"
                        pass
           # A.start()
            flag =1
        time.sleep(10)
# thread.start_new_thread(f1,())
# thread.start_new_thread(f2,())
A= KThread(target=f1)
B= KThread(target=f2)
A.start()
B.start()
# f1()
# def a1():
#     print "in a1"
#     # while True :
#     #     pass
#     print "Func stopped"
# def b1():
#     print "in b1"
#     global A
#     i=0
#     while i<5 :
#         print i
 def run(self):
     logger.info('Server thread run Method')
     time.sleep(1)
     self.follower_state = KThread(target=self.follower, args=())
     self.follower_state.start()
Пример #30
0
def acceptor(server, data, addr):
    Msg = pickle.loads(data)
    _type = Msg.type

    # deal with config changes
    if _type == 'change':
        if Msg.phase == 1:
            print 'Config change phase 1'
            server.during_change = 1
            server.new = Msg.new_config
            server.old = server.peers[:]
            server.old.append(server.id)
            if Msg.addr != None:
                addr = Msg.addr
            newEntry = LogEntry(server.currentTerm, Msg, addr, Msg.uuid, 1)
            server.log.append(newEntry)
            server.peers = list(set(server.old + server.new))
            server.peers.remove(server.id)
            server.save()
            print 'Config change phase 1 applied'
            #return
        else:
            print 'Config change phase 2'
            server.during_change = 2
            server.new = Msg.new_config
            if Msg.addr != None:
                addr = Msg.addr
            newEntry = LogEntry(server.currentTerm, Msg, addr, Msg.uuid, 2)
            server.log.append(newEntry)
            server.peers = server.new[:]
            if server.id in server.peers:
                server.peers.remove(server.id)
            server.save()
            print 'Config change phase 2 applied, running peers'
            #return

        # if server.role == 'leader':
        # 	for peer in server.peers:
        # 		if peer not in server.nextIndex:
        # 			server.nextIndex[peer] = len(server.log) + 1
        # 			server.matchIndex[peer] = 0

        if server.role != 'leader':
            print 'redirect config change to the leader'
            if server.leaderID != 0:
                redirect_target = server.leaderID
            else:
                redirect_target = random.choice(server.peers)
            if Msg.addr != None:
                addr = Msg.addr
            redirect_msg = ConfigChange(Msg.new_config, Msg.uuid, Msg.phase,
                                        addr)
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.sendto(pickle.dumps(redirect_msg),
                     ("", server.addressbook[redirect_target]))
            s.close()

        #server.log.append(newEntry)
        #server.save()
        return

    # deal with client's message
    if _type == 'client' or _type == 'redirect':

        if _type == 'redirect':
            addr = Msg.addr

        msg_string = Msg.request_msg
        if msg_string == 'show':
            state = server.poolsize
            committed_log = ''
            for idx in range(0, server.commitIndex):
                entry = server.log[idx]
                if entry.type == 0:
                    committed_log += str(entry.command) + ' '
                elif entry.type == 1:
                    committed_log += 'new_old' + ' '
                else:
                    committed_log += 'new' + ' '

            all_log = ''
            for entry in server.log:
                if entry.type == 0:
                    all_log += str(entry.command) + ' '
                elif entry.type == 1:
                    all_log += 'new_old' + ' '
                else:
                    all_log += 'new' + ' '

            show_msg = 'state machine: ' + str(
                state
            ) + '\n' + 'committed log: ' + committed_log + '\n' + 'all log:' + all_log + '\n' + 'status: ' + str(
                server.during_change)
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.sendto(show_msg, addr)
            s.close()

        else:
            ticket_num = int(msg_string.split()[1])
            if server.role == 'leader':
                print "I am the leader, customer wants to buy %d tickets" % ticket_num

                if ticket_num > server.poolsize:
                    print 'Tickets not enough'
                    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    s.sendto('We do not have enough tickets', addr)
                    s.close()
                    return

                # check whether this command has already been
                for idx, entry in enumerate(server.log):
                    if entry.uuid == Msg.uuid:
                        if server.commitIndex >= idx + 1:
                            s = socket.socket(socket.AF_INET,
                                              socket.SOCK_DGRAM)
                            s.sendto('Your request has been fullfilled', addr)
                            s.close()
                        else:  # ignore
                            pass
                        return  # ignore this new command

                newEntry = LogEntry(server.currentTerm, ticket_num, addr,
                                    Msg.uuid)
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.sendto('The Leader gets your request', addr)
                s.close()
                server.log.append(newEntry)
                server.save()
            # we need to redirect the request to leader
            else:
                print 'redirect the request to leader'
                if server.leaderID != 0:
                    redirect_target = server.leaderID
                else:
                    redirect_target = random.choice(server.peers)
                redirect_msg = RequestRedirect(msg_string, Msg.uuid, addr)
                s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                s.sendto(pickle.dumps(redirect_msg),
                         ("", server.addressbook[redirect_target]))
                s.close()
        return

    _sender = Msg.sender
    _term = Msg.term

    if _type == 1:  # requestvote message
        if _sender not in server.peers:
            return
        _msg = Msg.data
        print '---------Get requestvote message---------'
        _msg = _msg.split()
        log_info = (int(_msg[0]), int(_msg[1]))
        if _term < server.currentTerm:
            print 'rejected due to old term'
            voteGranted = 0
        elif _term == server.currentTerm:
            if log_info >= (server.lastLogTerm, server.lastLogIndex) and (
                    server.votedFor == -1 or server.votedFor == _sender):
                voteGranted = 1
                server.votedFor = _sender
                server.save()
            else:
                voteGranted = 0
        else:
            # find higher term in RequestForVoteMessage
            server.currentTerm = _term
            server.save()
            server.step_down()

            if log_info >= (server.lastLogTerm, server.lastLogIndex):
                voteGranted = 1
                server.votedFor = _sender
                server.save()
            else:
                voteGranted = 0
        reply = str(voteGranted)
        reply_msg = VoteResponseMessage(server.id, _sender, server.currentTerm,
                                        reply)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto(pickle.dumps(reply_msg), ("", server.addressbook[_sender]))

    elif _type == 2:  # Vote response message
        _msg = Msg.data
        print '---------Get vote response message---------'
        voteGranted = int(_msg)
        if voteGranted:
            if server.during_change == 0:
                if server.role == 'candidate':
                    server.request_votes.remove(_sender)
                    server.numVotes += 1
                    if server.numVotes == server.majority:
                        print 'Get majority votes, become leader at Term %d' % server.currentTerm
                        if server.election.is_alive():
                            server.election.kill()
                    # becomes a leader
                        server.role = 'leader'
                        server.follower_state.kill()
                        server.leader_state = KThread(target=server.leader,
                                                      args=())
                        server.leader_state.start()
            elif server.during_change == 1:
                server.request_votes.remove(_sender)
                if _sender in server.old:
                    server.oldVotes += 1
                if _sender in server.new:
                    server.newVotes += 1
                majority_1 = len(server.old) / 2 + 1
                majority_2 = len(server.new) / 2 + 1
                if server.oldVotes >= majority_1 and server.newVotes >= majority_2:
                    print 'Get majority from both old and new, become leader at Term %d' % server.currentTerm
                    if server.election.is_alive():
                        server.election.kill()
                    server.role = 'leader'
                    server.follower_state.kill()
                    server.leader_state = KThread(target=server.leader,
                                                  args=())
                    server.leader_state.start()
            else:
                server.request_votes.remove(_sender)
                if _sender in server.peers:
                    server.newVotes += 1
                majority = len(server.new) / 2 + 1
                if server.newVotes >= majority:
                    print 'Get majority from new, become leader at Term %d' % server.currentTerm
                    if server.election.is_alive():
                        server.election.kill()
                    server.role = 'leader'
                    server.follower_state.kill()
                    server.leader_state = KThread(target=server.leader,
                                                  args=())
                    server.leader_state.start()

        else:
            if _term > server.currentTerm:  # discover higher term
                server.currentTerm = _term
                server.save()
                if server.role == 'candidate':
                    server.step_down()

            print 'vote rejected by %d' % _sender

    elif _type == 0:  # AppendEntries msg
        # print '---------Get AppendEntries message---------'
        entries = Msg.entries
        leaderCommit = Msg.commitIndex
        prevLogTerm = Msg.prevLogTerm
        prevLogIndex = Msg.prevLogIndex

        matchIndex = server.commitIndex

        # This is a valid new leader
        if _term >= server.currentTerm:
            server.currentTerm = _term
            server.save()
            server.step_down()
            if server.role == 'follower':
                server.last_update = time.time()
            if prevLogIndex != 0:
                if len(server.log) >= prevLogIndex:
                    if server.log[prevLogIndex - 1].term == prevLogTerm:
                        success = 'True'
                        server.leaderID = _sender
                        if len(entries) != 0:
                            server.log = server.log[:prevLogIndex] + entries
                            matchIndex = len(server.log)
                            if entries[0].type == 1:
                                server.during_change = 1
                                server.new = entries[0].command.new_config[:]
                                server.old = server.peers[:]
                                server.old.append(server.id)
                                server.peers = list(
                                    set(server.old + server.new))
                                server.peers.remove(server.id)
                            elif entries[0].type == 2:
                                server.during_change = 2
                                server.new = entries[0].command.new_config[:]
                                server.peers = server.new[:]
                                server.peers.remove(server.id)
                                print 'follower applied new config, running peers', server.peers
                                server.save()
                    else:
                        success = 'False'
                else:
                    success = 'False'
            else:
                success = 'True'
                if len(entries) != 0:
                    server.log = server.log[:prevLogIndex] + entries
                    if entries[0].type == 1:
                        server.during_change = 1
                        server.new = entries[0].command.new_config[:]
                        server.old = server.peers[:]
                        server.old.append(server.id)
                        server.peers = list(set(server.old + server.new))
                        server.peers.remove(server.id)
                    elif entries[0].type == 2:
                        server.during_change = 2
                        server.new = entries[0].command.new_config[:]
                        server.peers = server.new[:]
                        server.peers.remove(server.id)
                        print 'follower applied new config, running peers', server.peers

                    server.save()
                    matchIndex = len(server.log)
                server.leaderID = _sender
        else:
            success = 'False'

        if leaderCommit > server.commitIndex:
            lastApplied = server.commitIndex
            server.commitIndex = min(leaderCommit, len(server.log))
            if server.commitIndex > lastApplied:
                server.poolsize = server.initial_state
                for idx in range(1, server.commitIndex + 1):
                    if server.log[idx - 1].type == 0:
                        server.poolsize -= server.log[idx - 1].command
                    elif server.log[idx - 1].type == 2:
                        server.during_change = 0

        reply_msg = AppendEntriesResponseMessage(server.id, _sender,
                                                 server.currentTerm, success,
                                                 matchIndex)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto(pickle.dumps(reply_msg), ("", server.addressbook[_sender]))

    elif _type == 3:  # AppendEntriesResponse:
        #print '---------Get AppendEntries Response message---------'
        success = Msg.success
        matchIndex = Msg.matchIndex

        if success == 'False':
            if _term > server.currentTerm:
                server.currentTerm = _term
                server.save()
                server.step_down()
            else:
                server.nextIndex[_sender] -= 1
        else:
            if server.nextIndex[_sender] <= len(
                    server.log) and matchIndex > server.matchIndex[_sender]:
                server.matchIndex[_sender] = matchIndex
                server.nextIndex[_sender] += 1

            if server.commitIndex < max(server.matchIndex.values()):
                start = server.commitIndex + 1
                for N in range(start, max(server.matchIndex.values()) + 1):
                    if server.during_change == 0:
                        # not in config change
                        compare = 1
                        for key, item in server.matchIndex.items():
                            if key in server.peers and item >= N:
                                compare += 1
                        majority = (len(server.peers) + 1) / 2 + 1
                        if compare == server.majority and server.log[
                                N - 1].term == server.currentTerm:
                            for idx in range(server.commitIndex + 1, N + 1):
                                server.poolsize -= server.log[idx - 1].command
                                server.save()
                                s = socket.socket(socket.AF_INET,
                                                  socket.SOCK_DGRAM)
                                s.sendto('Your request is fullfilled',
                                         server.log[idx - 1].addr)
                                s.close()
                                print 'reply once'
                            server.commitIndex = N
                    elif server.during_change == 1:
                        majority_1 = len(server.old) / 2 + 1
                        majority_2 = len(server.new) / 2 + 1
                        votes_1 = 0
                        votes_2 = 0
                        if server.id in server.old:
                            votes_1 = 1
                        if server.id in server.new:
                            votes_2 = 1
                        for key, item in server.matchIndex.items():
                            if item >= N:
                                if key in server.old:
                                    votes_1 += 1
                                if key in server.new:
                                    votes_2 += 1
                        if votes_1 >= majority_1 and votes_2 >= majority_2 and server.log[
                                N - 1].term == server.currentTerm:
                            server.commitIndex = N
                            poolsize = server.initial_state
                            for idx in range(1, N + 1):
                                if server.log[idx - 1].type == 0:
                                    poolsize -= server.log[idx - 1].command
                            server.poolsize = poolsize
                            server.save()
                            s = socket.socket(socket.AF_INET,
                                              socket.SOCK_DGRAM)
                            s.sendto('Your request is fullfilled',
                                     server.log[idx - 1].addr)
                            s.close()
                            # print 'send old_new once'

                    else:
                        majority = len(server.new) / 2 + 1
                        votes = 0
                        if server.id in server.new:
                            votes = 1
                        for key, item in server.matchIndex.items():
                            if item >= N:
                                if key in server.new:
                                    votes += 1
                        if votes == majority and server.log[
                                N - 1].term == server.currentTerm:
                            print '----------here 2----------'
                            for idx in range(server.commitIndex + 1, N + 1):
                                if server.log[idx - 1].type == 0:
                                    server.poolsize -= server.log[idx -
                                                                  1].command
                                    server.save()
                                    s = socket.socket(socket.AF_INET,
                                                      socket.SOCK_DGRAM)
                                    s.sendto('Your request is fullfilled',
                                             server.log[idx - 1].addr)
                                    s.close()
                                    server.commitIndex = idx
                                elif server.log[idx - 1].type == 2:
                                    server.commitIndex = idx
                                    time.sleep(1)
                                    if not server.id in server.new:
                                        print 'I am not in the new configuration'
                                        server.step_down()
                                    server.during_change = 0
                                    server.save()
                                    s = socket.socket(socket.AF_INET,
                                                      socket.SOCK_DGRAM)
                                    s.sendto('Your request is fullfilled',
                                             server.log[idx - 1].addr)
                                    s.close()
Пример #31
0
class Server(object):
	def __init__(self, id_):
		self.id = id_
		self.config_file = 'config-%d' % self.id

		self.role = 'follower'
		self.commitIndex, self.lastApplied, self.leaderID = 0, 0, 0

		address = json.load(file('config.json'))
		self.initial_state = address['initial_state']
		self.addressbook = {}
		for id_ in address['running']:
			self.addressbook[id_] = address['AddressBook'][id_ - 1]

		# need to put it into file later on
		self.load()

		self.lastLogIndex = 0
		self.lastLogTerm, self.oldVotes, self.newVotes, self.numVotes = 0, 0, 0, 0

		self.port = self.addressbook[self.id]
		self.request_votes = self.peers[:]


		self.listener = KThread(target = self.listen, args= (acceptor,))
		self.listener.start()
		self.newPeers = []

	def listen(self, on_accept):
		print 'start listenning on port '+str(self.port)
		srv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		srv.bind(("", self.port))
		while True:
			data, addr = srv.recvfrom(1024)
			KThread(target=on_accept, args=(self, data, addr)).start()
		srv.close()

	def follower(self):
		print 'Running as a follower'
		self.role = 'follower'
		first = True
		while True:
			self.last_update = time.time()
			election_timeout = 5 * random.random() + 5
			while time.time() - self.last_update <= election_timeout:
				pass
			if not first and self.election.is_alive():
				self.election.kill()
			first = False
			self.start_election()

	def start_election(self):
		self.role = 'candidate'
		self.election = KThread(target =self.thread_election,args = ())
		if len(self.peers) == 0:
			return
		self.currentTerm += 1
		self.votedFor = self.id
		serverConfig = ServerConfig(self.poolsize, self.currentTerm, self.votedFor, self.log, self.peers)
		with open(self.config_file, 'w') as f:
			pickle.dump(serverConfig, f)

		self.numVotes = 1
		self.election.start()

	def thread_election(self):
		print 'timouts, start a new election with term %d' % self.currentTerm
		self.role = 'candidate'
		self.request_votes = self.peers[:]

		while True:
			for peer in self.peers:
	 			if peer in self.request_votes:
	 				Msg = str(self.lastLogTerm) + ' ' + str(self.lastLogIndex)
	 				msg = BaseMessage(self.id, peer, self.currentTerm, Msg, reqtype="RequestVote")
	 				data = pickle.dumps(msg)
	 				sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
	 				sock.sendto(data, ("", self.addressbook[peer]))
 			time.sleep(1) # wait for servers to receive

 	def leader(self):
 		print 'Running as a leader'
 		self.role = 'leader'
 		self.nextIndex = {}
 		self.matchIndex = {}
 		for peer in self.peers:
 			self.nextIndex[peer] = len(self.log) + 1
 			self.matchIndex[peer] = 0
		self.append_entries()

	def append_entries(self):
		receipts = self.peers[:]
		while True:
			receipts = self.peers[:]
			for peer in receipts:
				if len(self.log) < self.nextIndex[peer]:
					entries = []
					prevLogIndex = len(self.log)
					prevLogTerm = 0
					if prevLogIndex != 0:
						prevLogTerm = self.log[prevLogIndex-1].term
				else:
					prevLogIndex = self.nextIndex[peer] - 1
					prevLogTerm = 0
					if prevLogIndex != 0:
						prevLogTerm = self.log[prevLogIndex-1].term
					entries = [self.log[prevLogIndex]]


				Msg = AppendEntriesMsg(self.id, peer, self.currentTerm, entries, self.commitIndex, prevLogIndex, prevLogTerm)
				data = pickle.dumps(Msg)
				sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
				sock.sendto(data, ("", self.addressbook[peer]))
			time.sleep(0.5)

	def step_down(self):
		if self.role == 'leader':
			self.leader_state.kill()
			self.follower_state = KThread(target = self.follower, args = ())
			self.follower_state.start()
		if self.role == 'candidate':
			print 'candidate step down when higher term'
			self.election.kill()
			self.last_update = time.time()
			self.role = 'follower'

	def load(self):
		try:
			with open(self.config_file) as f:
				serverConfig = pickle.load(f)
		except Exception as e:
			initial_running = [i+1 for i in range(5)]
			initial_running.remove(self.id)
			serverConfig = ServerConfig(100, 0, -1, [], initial_running)

		self.currentTerm = serverConfig.currentTerm
		self.poolsize = serverConfig.poolsize
		self.votedFor = serverConfig.votedFor
		self.peers = serverConfig.peers
		self.majority = (len(self.peers) + 1)/2 + 1
		self.log = serverConfig.log

	def run(self):
		time.sleep(1)
		self.follower_state = KThread(target = self.follower, args = ())
		self.follower_state.start()
Пример #32
0
class MyForm(wx.Frame):


    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "NVIDIA LTE Throughput Tester",size=(1250,800))

        self.panel = wx.Panel(self, wx.ID_ANY)
        self.pl = wx.Panel(self.panel)
        #self.pl = wx.Pane()
        self.pl.SetBackgroundColour("#d8d8bf")

        self.Font_Result = wx.Font(12,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_BOLD)

        #self.pl2 = wx.Panel(self, wx.ID_ANY)

        #self.pl = wx.Panel(self, -1)
        #self.pl.SetBackgroundColour("#EFFEFE")

        # TestType Part
        sbTestType = wx.StaticBox(self.pl, -1, 'Test Type', size=(-1, -1))
        #sbTestType.SetForegroundColour(wx.BLUE)
        sbsTestType = wx.StaticBoxSizer(sbTestType, wx.HORIZONTAL)
        #self.stTestType = wx.StaticText(self.pl,-1,'Test Type')
        #sbsTestType.Add(self.stTestType, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsTestType = wx.BoxSizer ( wx.HORIZONTAL )
        self.rbNotest = wx.RadioButton(self.pl, -1, 'No Test')
        self.Bind(wx.EVT_RADIOBUTTON, self.TestType, id=self.rbNotest.GetId())
        self.rbAuto   = wx.RadioButton(self.pl, -1, 'Auto Test')
        self.Bind(wx.EVT_RADIOBUTTON, self.TestType, id=self.rbAuto.GetId())
        self.rbUnit = wx.RadioButton(self.pl, -1, 'Unit Test')
        self.Bind(wx.EVT_RADIOBUTTON, self.TestType, id=self.rbUnit.GetId())

       # bsTestType.Add(self.stTestType,0, wx.ALL,10)
        bsTestType.Add(self.rbAuto,0, wx.ALL,10)
        bsTestType.Add(self.rbUnit,0, wx.ALL,10)
        bsTestType.Add(self.rbNotest,0, wx.ALL,10)
        sbsTestType.Add(bsTestType, 0, wx.LEFT,10)

        #platform
        #sbPlatformType = wx.StaticBox(self.pl, -1, 'Platform', size=(-1, -1))
        #sbsPlatformType = wx.StaticBoxSizer(sbPlatformType, wx.HORIZONTAL)
        #bsPlatformType = wx.BoxSizer ( wx.HORIZONTAL )
        self.rbplatform = wx.RadioBox(self.pl, -1, "Platform", choices=["Win XP", "WoA"], majorDimension=0, style=wx.RA_SPECIFY_COLS)

        #self.rbWinxp = wx.RadioButton(self.pl, -1, 'Win XP')
        #self.Bind(wx.EVT_RADIOBUTTON, self.PlatformType, id=self.rbWinxp.GetId())
        #self.rbWoA = wx.RadioButton(self.pl, -1, 'WoA')
        #self.Bind(wx.EVT_RADIOBUTTON, self.PlatformType, id=self.rbWoA.GetId())
        #bsPlatformType.Add(self.rbWinxp,0, wx.ALL,10)
        #bsPlatformType.Add(self.rbplatform,0, wx.ALL,10)
        #sbsPlatformType.Add(bsPlatformType, 0, wx.LEFT,10)
        # BranchSelection Part
        sbBranchSelection = wx.StaticBox(self.pl, -1, 'BranchSelection', size=(-1, -1))
        #sbBranchSelection.SetForegroundColour(wx.BLUE)
        sbsBranchSelection = wx.StaticBoxSizer(sbBranchSelection, wx.HORIZONTAL)
        #self.stBranchSelection = wx.StaticText(self.pl,-1,'BranchSelection')
       # sbsBranchSelection.Add(self.stBranchSelection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsBranchSelection = wx.BoxSizer ( wx.HORIZONTAL )
        self.mainBr = wx.CheckBox(self.pl, -1 ,'main')
        self.cr3Br = wx.CheckBox(self.pl, -1 ,'cr3')
        self.FTBr = wx.CheckBox(self.pl, -1 ,'Sanity Test')
        bsBranchSelection.Add(self.mainBr,0, wx.ALL,10)
        bsBranchSelection.Add(self.cr3Br,0, wx.ALL,10)
        bsBranchSelection.Add(self.FTBr,0, wx.ALL,10)
        sbsBranchSelection.Add(bsBranchSelection, 0, wx.LEFT,10)


        # SelectScenario Part
        sbSelectScenario = wx.StaticBox(self.pl, -1, 'SelectScenario', size=(-1, -1))
        #sbSelectScenario.SetForegroundColour(wx.BLUE)
        sbsSelectScenario = wx.StaticBoxSizer(sbSelectScenario, wx.VERTICAL)
        #self.stSelectScenario = wx.StaticText(self.pl,-1,'SelectScenario')
       # sbsSelectScenario.Add(self.stSelectScenario, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsSelectScenario = wx.BoxSizer ( wx.VERTICAL )
        bsSelectScenariox = []
        bsSelectScenariox.append(wx.BoxSizer ( wx.HORIZONTAL ))
        bsSelectScenariox.append(wx.BoxSizer ( wx.HORIZONTAL ))
        self.scen = []



        for i in range(0,len(scenario_implemented)):
            if re.search('MIMO',scenario_implemented[i]):
                self.scen.append(wx.CheckBox(self.pl, -1 ,scenario_implemented[i]))
                #self.scen[i].SetFont(font)
                bsSelectScenariox[1].Add(self.scen[i],0, wx.ALL,1)
            else:
                self.scen.append(wx.CheckBox(self.pl, -1 ,scenario_implemented[i]))
                #self.scen[i].SetFont(font)
                bsSelectScenariox[0].Add(self.scen[i],0, wx.ALL,1)

        self.Alltest = wx.CheckBox(self.pl, -1 ,"All")
        bsSelectScenariox[1].Add(self.Alltest)

        for i in range(0,len(bsSelectScenariox)):
            bsSelectScenario.Add(bsSelectScenariox[i])

        sbsSelectScenario.Add(bsSelectScenario, 0, wx.LEFT,5)

        # BandSelection Part
        sbBandSelection = wx.StaticBox(self.pl, -1, 'BandSelection', size=(-1, -1))
        #sbBandSelection.SetForegroundColour(wx.BLUE)
        sbsBandSelection = wx.StaticBoxSizer(sbBandSelection, wx.VERTICAL)
        #self.stBandSelection = wx.StaticText(self.pl,-1,'BandSelection')
       # sbsBandSelection.Add(self.stBandSelection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsBandSelection = wx.BoxSizer ( wx.HORIZONTAL )
        self.band4 = wx.CheckBox(self.pl, -1 ,'4')
        self.band17 = wx.CheckBox(self.pl, -1 ,'17')
        bsBandSelection.Add(self.band4,0, wx.ALL,10)
        bsBandSelection.Add(self.band17,0, wx.ALL,10)
        sbsBandSelection.Add(bsBandSelection, 0, wx.CENTER,10)

        # AdditionalOption Part
        sbAdditionOption = wx.StaticBox(self.pl, -1, 'AdditionOption', size=(-1, -1))
        #sbAdditionOption.SetForegroundColour(wx.BLUE)
        sbsAdditionOption = wx.StaticBoxSizer(sbAdditionOption, wx.VERTICAL)
        #self.stAdditionOption = wx.StaticText(self.pl,-1,'AdditionOption')
       # sbsAdditionOption.Add(self.stAdditionOption, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsAdditionOption = wx.BoxSizer ( wx.HORIZONTAL )
        self.stchangelist = wx.StaticText(self.pl,-1,' CL ')

        self.changelist = wx.TextCtrl(self.pl,-1,size=(60,20), value=u"")


        #self.build = wx.CheckBox(self.pl, -1 ,'Build')
        self.flash = wx.CheckBox(self.pl, -1 ,'Flash')
        #self.graph = wx.CheckBox(self.pl, -1 ,'Graph')
        self.force = wx.CheckBox(self.pl, -1 ,'Force')
        self.cReg = wx.CheckBox(self.pl, -1 ,'Reg')
        #self.cReg.SetValue(True)
        #self.stNbComp = wx.StaticText(self.pl,-1,'Nb.Comparisons')
        #self.tcNbComp = wx.TextCtrl(self.pl,-1,size=(20,20), value=u"")
        bsAdditionOption.Add(self.stchangelist,0, wx.ALL,10)
        bsAdditionOption.Add(self.changelist,0, wx.ALL,10)
        #self.stOk_cl = wx.StaticText(self.pl,-1,'Ref_CL')
        #self.tOk_cl = wx.TextCtrl(self.pl,-1,size=(60,20), value=u"")
        #bsAdditionOption.Add(self.stOk_cl,0, wx.ALL,10)
        #bsAdditionOption.Add(self.tOk_cl,0, wx.ALL,10)
        #self.stKo_cl = wx.StaticText(self.pl,-1,'Reg_CL')
        #self.tKo_cl = wx.TextCtrl(self.pl,-1,size=(60,20), value=u"")
        #bsAdditionOption.Add(self.stKo_cl,0, wx.ALL,10)
       # bsAdditionOption.Add(self.tKo_cl,0, wx.ALL,10)

        #bsAdditionOption.Add(self.build,0, wx.ALL,10)
        bsAdditionOption.Add(self.flash,0, wx.ALL,10)
        #bsAdditionOption.Add(self.graph,0, wx.ALL,10)
        bsAdditionOption.Add(self.force,0, wx.ALL,10)
        bsAdditionOption.Add(self.cReg,0, wx.ALL,10)
        #bsAdditionOption.Add(self.stNbComp,0, wx.ALL,10)
        #bsAdditionOption.Add(self.tcNbComp,0, wx.ALL,10)
        sbsAdditionOption.Add(bsAdditionOption, 0, wx.LEFT,10)

        # StartSection Part
        sbStartSection = wx.StaticBox(self.pl, -1, 'Execution', size=(-1, -1))
        #sbStartSection.SetForegroundColour(wx.BLUE)
        sbsStartSection = wx.StaticBoxSizer(sbStartSection, wx.VERTICAL)
        #self.stStartSection = wx.StaticText(self.pl,-1,'StartSection')
       # sbsStartSection.Add(self.stStartSection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsStartSection = wx.BoxSizer ( wx.HORIZONTAL )
        bStart = wx.Button(self.pl,-1,label="Start")
        bStop = wx.Button(self.pl,-1,label="Stop")
        bResult = wx.Button(self.pl,-1,label="Result")
        bReg = wx.Button(self.pl,-1,label="REG")
        bSetting = wx.Button(self.pl,-1,label="Setting")
        self.Bind(wx.EVT_BUTTON, self.onRun, bStart)
        bsStartSection.Add(bStart,0, wx.ALL,10)
        self.Bind(wx.EVT_BUTTON, self.stopThread, bStop)
        bsStartSection.Add(bStop,0, wx.ALL,10)
        self.Bind(wx.EVT_BUTTON, self._onShowResult, bResult)
        bsStartSection.Add(bResult,0, wx.ALL,10)
        self.Bind(wx.EVT_BUTTON, self._onShowOptions, bSetting)
        self.Bind(wx.EVT_BUTTON, self.Regression_Start, bReg)
        bsStartSection.Add(bReg,0, wx.ALL,10)
        bsStartSection.Add(bSetting,0, wx.ALL,10)
        sbsStartSection.Add(bsStartSection, 0, wx.LEFT,10)


        # FlashSection Part
        sbFlashSection = wx.StaticBox(self.pl, -1, 'Flash', size=(-1, -1))
        #sbFlashSection.SetForegroundColour(wx.BLUE)
        sbsFlashSection = wx.StaticBoxSizer(sbFlashSection, wx.VERTICAL)
        #self.stFlashSection = wx.StaticText(self.pl,-1,'FlashSection')
       # sbsFlashSection.Add(self.stFlashSection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsFlashSection = wx.BoxSizer ( wx.HORIZONTAL )
        #self.entry = wx.TextCtrl(self.pl,-1,size=(600,20))
        self.entry = wx.TextCtrl(self.pl,-1,r"",wx.Point(20,20), wx.Size(500,20), \
                wx.TE_MULTILINE | wx.TE_RICH2)

        self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.entry)
        bFile = wx.Button(self.pl,-1,label="File")
        self.Bind(wx.EVT_BUTTON, self.OnOpenScen, bFile)
        bFlash = wx.Button(self.pl,-1,label="Flash")
        self.Bind(wx.EVT_BUTTON,self.flash_only,bFlash)

        bsFlashSection.Add(self.entry,0, wx.ALL,10)
        bsFlashSection.Add(bFile,0, wx.ALL,10)
        bsFlashSection.Add(bFlash,0, wx.ALL,10)
        sbsFlashSection.Add(bsFlashSection, 0, wx.LEFT,10)

        # LogSection Part
        sbLogSection = wx.StaticBox(self.pl, -1, 'LogSection', size=(-1, -1))
        #sbLogSection.SetForegroundColour(wx.BLUE)
        sbsLogSection = wx.StaticBoxSizer(sbLogSection, wx.VERTICAL)
        #self.stLogSection = wx.StaticText(self.pl,-1,'LogSection')
       # sbsLogSection.Add(self.stLogSection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsLogSection = wx.BoxSizer ( wx.HORIZONTAL )
        self.log = wx.TextCtrl(self.pl, wx.ID_ANY, size=(1150,300),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)
        #self.msg = wx.TextCtrl(self.pl, wx.ID_ANY, size=(800,100),style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL)

        bsLogSection.Add(self.log,1,wx.ALL,10)
        #bsLogSection.Add(self.msg,1,wx.ALL,10)

        sbsLogSection.Add(bsLogSection, 0, wx.LEFT,10)



        #TEST TYPE BRANCH BAND JOINING
        bsTTBS = wx.BoxSizer ( wx.HORIZONTAL)
        bsTTBS.Add(sbsTestType, 0, wx.EXPAND)
        bsTTBS.Add(sbsBranchSelection, 0, wx.EXPAND)
        bsTTBS.Add(sbsBandSelection, 0, wx.EXPAND)
        bsTTBS.Add(sbsAdditionOption, 0, wx.EXPAND)
        #bsTTBS.Add(sbsPlatformType, 0, wx.EXPAND)
        bsTTBS.Add(self.rbplatform, 0, wx.EXPAND)

        #StartSection and Flash Joining
        bsSSFS = wx.BoxSizer ( wx.HORIZONTAL)
        bsSSFS.Add(sbsStartSection, 0, wx.EXPAND)
        bsSSFS.Add(sbsFlashSection, 0, wx.EXPAND)


        # Fill the Frame
        self.sizer = wx.BoxSizer ( wx.VERTICAL)
        self.sizer.Add(bsTTBS, 0, wx.EXPAND)
        # sizer.Add(sbsTestType, 0, wx.EXPAND)
        # sizer.Add(sbsBranchSelection, 0, wx.EXPAND)
        self.sizer.Add(sbsSelectScenario, 0, wx.EXPAND)
        #sizer.Add(sbsBandSelection, 0, wx.EXPAND)
        #sizer.Add(sbsAdditionOption, 0, wx.EXPAND)
        self.sizer.Add(bsSSFS,0,wx.EXPAND)
        #self.sizer.Add(sbsStartSection, 0, wx.EXPAND)
        self.sizer.Add(sbsLogSection, 0, wx.EXPAND)

        self.addProgress()
        self.pl.SetSizer(self.sizer)
        self.Center()
        self.Show(True)

        self._resultPanel()
        self.Rsizer.Layout()
        self._Additional_options()
        self.Osizer.Layout()
        self.optionPanel.SetSize(self.GetClientSizeTuple())
        self.resultPanel.SetSize(self.GetClientSizeTuple())
        self.pl.SetSize(self.GetClientSizeTuple())

        #self._onShowMain(None)

        self.redir=RedirectText(self.log)
        sys.stdout=self.redir
        sys.stderr=self.redir


        #image_file = 'test.jpeg'
        #bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        #self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))

        #self.Refresh_Frame()

        self.flash_list = []

    def _resultPanel(self):

        #RESULT PANEL
        #self.resultPanel = wx.Panel(self.panel)
        self.resultPanel = ScrolledPanel(self.panel)
        self.resultPanel.SetupScrolling()

        # TestType Part
        sbTestType = wx.StaticBox(self.resultPanel, -1, 'Test Type', size=(-1, -1))
        #sbTestType.SetForegroundColour(wx.BLUE)
        sbsTestType = wx.StaticBoxSizer(sbTestType, wx.HORIZONTAL)
        #self.stTestType = wx.StaticText(self.resultPanel,-1,'Test Type')
        #sbsTestType.Add(self.stTestType, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsTestType = wx.BoxSizer ( wx.HORIZONTAL )
        self.rbMainR = wx.RadioButton(self.resultPanel, -1, 'Main')
        self.Bind(wx.EVT_RADIOBUTTON, self.TestType, id=self.rbMainR.GetId())
        self.rbCr3R   = wx.RadioButton(self.resultPanel, -1, 'Cr3')
        self.Bind(wx.EVT_RADIOBUTTON, self.TestType, id=self.rbCr3R.GetId())
        self.rbStR = wx.RadioButton(self.resultPanel, -1, 'ST')
        self.Bind(wx.EVT_RADIOBUTTON, self.TestType, id=self.rbStR.GetId())


       # bsTestType.Add(self.stTestType,0, wx.ALL,10)
        bsTestType.Add(self.rbCr3R,0, wx.ALL,10)
        bsTestType.Add(self.rbStR,0, wx.ALL,10)
        bsTestType.Add(self.rbMainR,0, wx.ALL,10)
        sbsTestType.Add(bsTestType, 0, wx.LEFT,10)

        # SelectScenario Part
        sbSelectScenario = wx.StaticBox(self.resultPanel, -1, 'SelectScenario', size=(-1, -1))
        #sbSelectScenario.SetForegroundColour(wx.BLUE)
        sbsSelectScenario = wx.StaticBoxSizer(sbSelectScenario, wx.VERTICAL)
        #self.stSelectScenario = wx.StaticText(self.resultPanel,-1,'SelectScenario')
       # sbsSelectScenario.Add(self.stSelectScenario, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)


        bsSelectScenario = wx.BoxSizer ( wx.VERTICAL )
        bsSelectScenariox = []
        bsSelectScenariox.append(wx.BoxSizer ( wx.HORIZONTAL ))
        bsSelectScenariox.append(wx.BoxSizer ( wx.HORIZONTAL ))
        self.scenR = []

        font = wx.Font(7,wx.FONTFAMILY_DEFAULT,wx.FONTSTYLE_NORMAL,wx.FONTWEIGHT_LIGHT)

        for i in range(0,len(scenario_implemented)):
            if re.search('MIMO',scenario_implemented[i]):
                self.scenR.append(wx.CheckBox(self.resultPanel, -1 ,scenario_implemented[i]))
                #self.scen[i].SetFont(font)
                bsSelectScenariox[1].Add(self.scenR[i],0, wx.ALL,1)
            else:
                self.scenR.append(wx.CheckBox(self.resultPanel, -1 ,scenario_implemented[i]))
                #self.scen[i].SetFont(font)
                bsSelectScenariox[0].Add(self.scenR[i],0, wx.ALL,1)

        self.AlltestR = wx.CheckBox(self.resultPanel, -1 ,"All")
        bsSelectScenariox[1].Add(self.AlltestR)

        for i in range(0,len(bsSelectScenariox)):
            bsSelectScenario.Add(bsSelectScenariox[i])

        sbsSelectScenario.Add(bsSelectScenario, 0, wx.LEFT,5)







##        bsSelectScenario = wx.BoxSizer ( wx.HORIZONTAL )
##        self.scenR = []
##        for i in range(0,len(scenario_implemented)):
##            self.scenR.append(wx.CheckBox(self.resultPanel, -1 ,scenario_implemented[i]))
##            bsSelectScenario.Add(self.scenR[i],0, wx.ALL,1)
##        sbsSelectScenario.Add(bsSelectScenario, 0, wx.LEFT,5)

        # BandSelection Part
        sbBandSelection = wx.StaticBox(self.resultPanel, -1, 'BandSelection', size=(-1, -1))
        #sbBandSelection.SetForegroundColour(wx.BLUE)
        sbsBandSelection = wx.StaticBoxSizer(sbBandSelection, wx.VERTICAL)
        #self.stBandSelection = wx.StaticText(self.resultPanel,-1,'BandSelection')
       # sbsBandSelection.Add(self.stBandSelection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsBandSelection = wx.BoxSizer ( wx.HORIZONTAL )
        self.band4R = wx.CheckBox(self.resultPanel, -1 ,'4')
        self.band17R = wx.CheckBox(self.resultPanel, -1 ,'17')
        bsBandSelection.Add(self.band4R,0, wx.ALL,10)
        bsBandSelection.Add(self.band17R,0, wx.ALL,10)
        sbsBandSelection.Add(bsBandSelection, 0, wx.CENTER,10)

        # GraphOption Part
        sbAdditionOption = wx.StaticBox(self.resultPanel, -1, 'GraphOption', size=(-1, -1))
        #sbAdditionOption.SetForegroundColour(wx.BLUE)
        sbsAdditionOption = wx.StaticBoxSizer(sbAdditionOption, wx.VERTICAL)
        #self.stAdditionOption = wx.StaticText(self.resultPanel,-1,'AdditionOption')
       # sbsAdditionOption.Add(self.stAdditionOption, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsAdditionOption = wx.BoxSizer ( wx.HORIZONTAL )
        self.stchangelistR = wx.StaticText(self.resultPanel,-1,' CL ')
        self.forceGraph = wx.CheckBox(self.resultPanel, -1 ,'Force')

        self.changelistR = wx.TextCtrl(self.resultPanel,-1,size=(60,20), value=u"")
        #self.Bind(wx.EVT_TEXT_ENTER, self.OnPressEnter, self.changelistR)
        self.stNbCompR = wx.StaticText(self.resultPanel,-1,'Nb.Comparisons')
        self.tcNbCompR = wx.TextCtrl(self.resultPanel,-1,size=(20,20), value=u"")
        bsAdditionOption.Add(self.stchangelistR,0, wx.ALL,10)
        bsAdditionOption.Add(self.changelistR,0, wx.ALL,10)
        bsAdditionOption.Add(self.stNbCompR,0, wx.ALL,10)
        bsAdditionOption.Add(self.tcNbCompR,0, wx.ALL,10)
        bsAdditionOption.Add(self.forceGraph,0, wx.ALL,10)

        sbsAdditionOption.Add(bsAdditionOption, 0, wx.LEFT,10)

        # StartSection Part
        sbStartSection = wx.StaticBox(self.resultPanel, -1, '', size=(-1, -1))
        #sbStartSection.SetForegroundColour(wx.BLUE)
        sbsStartSection = wx.StaticBoxSizer(sbStartSection, wx.VERTICAL)
        #self.stStartSection = wx.StaticText(self.resultPanel,-1,'StartSection')
       # sbsStartSection.Add(self.stStartSection, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
        bsStartSection = wx.BoxSizer ( wx.HORIZONTAL )
        bStart = wx.Button(self.resultPanel,-1,label="Generate")
        bStop = wx.Button(self.resultPanel,-1,label="Clear")
        bResult = wx.Button(self.resultPanel,-1,label="Main Window")
        self.Bind(wx.EVT_BUTTON, self._updateResultPanel, bStart)
        bsStartSection.Add(bStart,0, wx.ALL,10)
        self.Bind(wx.EVT_BUTTON, self._cleanResultPanel, bStop)
        bsStartSection.Add(bStop,0, wx.ALL,10)
        self.Bind(wx.EVT_BUTTON, self._onShowMain, bResult)
        bsStartSection.Add(bResult,0, wx.ALL,10)
        sbsStartSection.Add(bsStartSection, 0, wx.CENTER,10)

        #TEST TYPE BRANCH BAND JOINING
        bsTTBS = wx.BoxSizer ( wx.HORIZONTAL)
        bsTTBS.Add(sbsTestType, 0, wx.EXPAND)
        bsTTBS.Add(sbsBandSelection, 0, wx.EXPAND)
        bsTTBS.Add(sbsAdditionOption, 0, wx.EXPAND)

        #IMAGE
##        self.img = []
##        self.caption = []
##        gridSizer = wx.GridSizer(rows=20, cols=1)
##
##        InsideBoxer =[]
##        #self.stcHelp = wx.StaticText(self.resultPanel, label="help help help\n"*8)
##        #btn = wx.Button(self.resultPanel, label="close[x]")
##        #btn.Bind(wx.EVT_BUTTON, self._onShowMain)
##        sizer2 = wx.BoxSizer(wx.VERTICAL)
##        sizer2.Add((20,20),proportion=1)
##       # sizer2.Add(self.stcHelp)
##        for i in range(0,len(BAND_TANGO_ALLOWED)):
##            for j in range(0,len(scenario_implemented)):
##                InsideBoxer.append(wx.BoxSizer(wx.VERTICAL))
##                self.caption.append(wx.StaticText(self.resultPanel,-1," "))
##                self.img.append(wx.StaticBitmap(self.resultPanel, wx.ID_ANY, wx.BitmapFromImage(wx.EmptyImage(240,240))))
##                InsideBoxer[i*len(scenario_implemented)+j].Add(self.caption[i*len(scenario_implemented)+j],-1,wx.ALL|wx.ALIGN_CENTER_VERTICAL,10)
##
##                InsideBoxer[i*len(scenario_implemented)+j].Add(self.img[i*len(scenario_implemented)+j])
##                gridSizer.Add(InsideBoxer[i*len(scenario_implemented)+j])

        #gridSizer.Add(btn)

        #gridSizer.Add(sizer2, 0, wx.ALIGN_RIGHT)

        # Fill the Frame
        self.Rsizer = wx.BoxSizer ( wx.VERTICAL)
        self.Rsizer.Add(bsTTBS, 0, wx.EXPAND)
        self.Rsizer.Add(sbsSelectScenario, 0, wx.EXPAND)
        self.Rsizer.Add(sbsStartSection, 0, wx.EXPAND)
        #self.Rsizer.Add((5,5), proportion=1)
       # self.Rsizer.Add(gridSizer, 0, wx.EXPAND)

        self.resultPanel.SetSizer(self.Rsizer)
        self.resultPanel.Hide()
        self.resultPanel.Raise()
        #self.resultPanel.SetBackgroundColour((240,250,240))
        self.Bind(wx.EVT_SIZE, self._onSize)

    def _Additional_options(self):
        self.optionPanel = ScrolledPanel(self.panel)
        self.optionPanel.SetupScrolling()

        # Options Part
        sboptions = wx.StaticBox(self.optionPanel, -1, 'Options', size=(-1, -1))
        sbsoptions = wx.StaticBoxSizer(sboptions, wx.HORIZONTAL)
        bsoptions = wx.BoxSizer ( wx.HORIZONTAL )

        #AT PORT
        satport = wx.StaticText(self.optionPanel,-1,'AT PORT')
        bsoptions.Add(satport,0, wx.ALL,10)
        self.atport = wx.TextCtrl(self.optionPanel,-1,size=(60,20), value=u"")
        bsoptions.Add(self.atport,0, wx.ALL,10)
        #self.Bind(wx.EVT_RADIOBUTTON, self.options, id=self.rbStR.GetId())

        #MODEM PORT
        smodemport = wx.StaticText(self.optionPanel,-1,'MODEM PORT')
        bsoptions.Add(smodemport,0, wx.ALL,10)
        self.modemport = wx.TextCtrl(self.optionPanel,-1,size=(60,20), value=u"")
        bsoptions.Add(self.modemport,0, wx.ALL,10)

        #VID
        svid = wx.StaticText(self.optionPanel,-1,'VID ')
        bsoptions.Add(svid,0, wx.ALL,10)
        self.vid_no = wx.TextCtrl(self.optionPanel,-1,size=(60,20), value=u"")
        bsoptions.Add(self.vid_no,0, wx.ALL,10)
        #NEXT WIDGET

        #END

        sbsoptions.Add(bsoptions, 0, wx.LEFT,10)
        #Return
        sbreturn = wx.StaticBox(self.optionPanel, -1, '', size=(-1, -1))
        sbsreturn = wx.StaticBoxSizer(sbreturn, wx.HORIZONTAL)
        bsreturn = wx.BoxSizer ( wx.HORIZONTAL )
        bReturn = wx.Button(self.optionPanel,-1,label="Return")
        self.Bind(wx.EVT_BUTTON, self._onShowMain, bReturn)
        bsreturn.Add(bReturn,0, wx.ALL,10)
        sbsreturn.Add(bsreturn,0,wx.CENTER,10)

        self.Osizer = wx.BoxSizer ( wx.VERTICAL)
        self.Osizer.Add(sbsoptions, 0, wx.EXPAND)
        self.Osizer.Add(sbsreturn, 0, wx.EXPAND)
        self.optionPanel.SetSizer(self.Osizer)
        self.optionPanel.Hide()
        self.optionPanel.Raise()
        self.Bind(wx.EVT_SIZE, self._onSize)

    def _onShowMain(self, event):
        self.pl.SetPosition((0,0))
        self.resultPanel.Hide()
        self.optionPanel.Hide()
        self.pl.Show()
        self.Refresh_Frame()

    def _onShowResult(self, event):
        self.resultPanel.SetPosition((0,0))
        self.resultPanel.Show()
        self.pl.Hide()
        self.optionPanel.Hide()
        self.resultPanel.Refresh()
        self.Refresh_Frame()

    def _onShowOptions(self, event):
        self.optionPanel.SetPosition((0,0))
        self.optionPanel.Show()
        self.pl.Hide()
        self.resultPanel.Hide()
        self.optionPanel.Refresh()
        self.Refresh_Frame()

    def GetResultPanelOption(self):
        if (self.rbMainR.GetValue()):
            self.branch_selected = "main"
            self.branch_idx = 0
        elif (self.rbCr3R.GetValue()):
            self.branch_selected = "cr3"
            self.branch_idx = 1
        else :
            self.branch_selected = "ST"
            self.branch_idx = 2

        self.band_4graph =[]
        if self.band4R.IsChecked():
           self.band_4graph.append(4)
        if self.band17R.IsChecked():
            self.band_4graph.append(17)

        self.scenario_4graph = []
        if self.AlltestR.IsChecked():
            self.scenario_4graph = scenario_implemented
        else :
            for i in range(0,len(scenario_implemented)):
                if self.scenR[i].IsChecked() :
                    self.scenario_4graph.append(scenario_implemented[i])

        try:
            self.clR = int(self.changelistR.GetValue())
        except:
            self.clR = 0

        try:
            self.nbcmpR = int(self.tcNbCompR.GetValue())
        except:
            self.nbcmpR = 5

        self.image_3d()

    def init_2d(self,foo,x,y):
        return [[foo for i in range(x)] for j in range(y)]

    def image_3d(self):

        #cdir ='\\\serv2.icerasemi.com\home\gcflab\workspace\callbox-test_cr3\swtools\main.br\\auto_regression\callbox\chart\\'
        #cdir = '\\\\'+CHART_LOC+'\\'
        cdir = 'chart\\'
        self.image_list =[]
        for i in range(0,len(BRANCH_ALLOWED)):
            self.image_list.append(" ")
            self.image_list[i] = self.init_2d("x",len(scenario_implemented),len(BAND_TANGO_ALLOWED))


        for i in range(0,len(BRANCH_ALLOWED)):
            for j in range(0,len(BAND_TANGO_ALLOWED)):
                for k in range(0,len(scenario_implemented)):
                    file = scenario_implemented[k]+"_"+"Band"+str(BAND_TANGO_ALLOWED[j])+"_"+BRANCH_ALLOWED[i]+".jpeg"
                    self.image_list[i][j][k] = cdir+file



    def _updateResultPanel(self,event):

        self.GetResultPanelOption()
        self._cleanResultPanel()
        self.img = []
        self.caption = []
        size = len(self.band_4graph) * len(self.scenario_4graph)
        self.gridSizer = wx.GridSizer(rows=size, cols=1)
        InsideBoxer =[]
        if self.forceGraph.IsChecked() or self.clR !=0 :
            for i in self.band_4graph:
                for j in self.scenario_4graph:
                    Chart().chart_scenario(int(i),self.clR,self.nbcmpR,j,self.branch_selected)


        for j in range(0,len(self.band_4graph)):
            for k in range(0,len(self.scenario_4graph)):
                self.caption.append(wx.StaticText(self.resultPanel,-1,"BAND "+str(self.band_4graph[j])+" "+self.scenario_4graph[k]))
                self.caption[j*len(self.scenario_4graph)+k].SetFont(self.Font_Result)
                self.img.append(wx.StaticBitmap(self.resultPanel, wx.ID_ANY, wx.BitmapFromImage(wx.Image(self.image_list[self.branch_idx][self.find_index(BAND_TANGO_ALLOWED,self.band_4graph[j])][self.find_index(scenario_implemented,self.scenario_4graph[k])],wx.BITMAP_TYPE_ANY))))
                #print "img loc",self.image_list[self.branch_idx][l][self.find_index(scenario_implemented,self.scenario_4graph[k])]
                InsideBoxer.append(wx.BoxSizer(wx.VERTICAL))
                InsideBoxer[j*len(self.scenario_4graph)+k].Add(self.caption[j*len(self.scenario_4graph)+k])
                InsideBoxer[j*len(self.scenario_4graph)+k].Add(wx.StaticText(self.resultPanel,-1,""))
                InsideBoxer[j*len(self.scenario_4graph)+k].Add(self.img[j*len(self.scenario_4graph)+k])

                self.gridSizer.Add(InsideBoxer[j*len(self.scenario_4graph)+k])

                #self.img[j*len(self.scenario_4graph)+k].SetBitmap(wx.BitmapFromImage( wx.Image(self.image_list[self.branch_idx][j][k],wx.BITMAP_TYPE_ANY)))

                #self.caption[j*len(self.scenario_4graph)+k].SetLabel("Band "+str(self.band_4graph[j])+"  "+self.scenario_4graph[k])
                #self.img[j*len(self.scenario_4graph)+k].SetBitmap(wx.BitmapFromImage( wx.Image(self.image_list[self.branch_idx][j][k],wx.BITMAP_TYPE_ANY)))


        self.Rsizer.Add(self.gridSizer, 0, wx.EXPAND)
        self.Refresh_Frame()

    def _cleanResultPanel(self,event=0):
        try:
            self.Rsizer.Hide(self.gridSizer)
            self.Rsizer.Remove(self.gridSizer)
        except:
            print "Already Empty"
        self.gridSizer = []
        self.Refresh_Frame()

    def _onSize(self, event):
        event.Skip()
        self.resultPanel.SetSize(self.GetClientSizeTuple())
        self.pl.SetSize(self.GetClientSizeTuple())

    def ParseArgs(self):
        self.branch_4test =[]
        if self.mainBr.IsChecked():
            self.branch_4test.append('main')
        if self.cr3Br.IsChecked():
            self.branch_4test.append('cr3')
        if self.FTBr.IsChecked():
            self.branch_4test.append('ST')

        self.band_4test =[]
        if self.band4.IsChecked():
           self.band_4test.append(4)
        if self.band17.IsChecked():
            self.band_4test.append(17)
        #BAND_TANGO_ALLOWED = band_4test

        self.scenario_4test = []
        if self.Alltest.IsChecked():
            self.scenario_4test = scenario_implemented
        else :
            for i in range(0,len(scenario_implemented)):
                if self.scen[i].IsChecked() :
                    self.scenario_4test.append(scenario_implemented[i])

        try:
            self.cl = int(self.changelist.GetValue())
        except:
            self.cl = 0

        try:
            self.nbcmp = int(self.tcNbComp.GetValue())
        except:
            self.nbcmp = 0

        #Option Panel
        if (str(self.atport.GetValue()) != "") :
            common.PORT_COM_TANGO = str(self.atport.GetValue())
            print "AT Port to use",common.PORT_COM_TANGO

        if(str(self.modemport.GetValue()) != ""):
            common.MODEM_PORT = str(self.modemport.GetValue())
            print "Modem Port to use",common.MODEM_PORT

        if(str(self.vid_no.GetValue()) != ""):
            common.VID = str(self.vid_no.GetValue())
            print "VID number to use",common.VID

        try:
            if self.rbplatform.GetSelection() == 0:
                common.CARDHU = False
            if self.rbplatform.GetSelection() == 1:
                common.CARDHU = True
                common.MODEM_PORT = common.CARDHU_MODEM_TCP
        except:
            common.CARDHU = False

    def Refresh_Progress(self):
        self.sizer.Layout()
        self.pl.Layout()

    def Refresh_Frame(self):
        #self.log.Clear()
        self.sizer.Layout()
        self.Rsizer.Layout()
        self.Osizer.Layout()
        self.panel.Refresh()
        self.pl.Refresh()
        self.resultPanel.Refresh()
        self.SendSizeEvent()
        self.resultPanel.SetupScrolling()
        self.resultPanel.Layout()
        self.optionPanel.Layout()
        self.pl.Layout()
        self.panel.Layout()

        self.log.Refresh()
        self.redir=RedirectText(self.log)
        sys.stdout=self.redir
        sys.stderr=self.redir

    def onRun(self, event):

        self.cleanProgress()
        self.ParseArgs()
        self.InitupdateProgress()

        self.t1 = KThread(target=self.Start_Test)
        self.t1.start()

        self.t2 = KThread(target=self.updateProgress)
        self.t2.start()

    def stopThread(self,event):
        print "Number of Active Threads",threading.activeCount()
        if threading.activeCount()>1:
            self.t1.kill() # Autocallbox Thread
            self.t2.kill() # Read_Status Thread
            print "Number of Active Threads",threading.activeCount()
        print "Number of Active Threads",threading.activeCount()
        #self.cleanProgress()

    def addProgress(self):

        #ProgressSection
        self.cl_run = "       "
        self.branch_run = "     "
        self.band_run = "  "
        self.scen_run = "               "
        self.scenario_4test = ""
        #current Run
        sbCurrentRun = wx.StaticBox(self.pl, -1, 'Current Test', size=(-1, -1))
        #sbCurrentRun.SetForegroundColour(wx.GREEN)
        sbsCurrentRun = wx.StaticBoxSizer(sbCurrentRun, wx.VERTICAL)
        bsCurrentRun = wx.BoxSizer ( wx.HORIZONTAL )
        self.stCL = wx.StaticText(self.pl,-1,self.cl_run)
        self.stBranch = wx.StaticText(self.pl,-1,self.branch_run)
        self.stBand = wx.StaticText(self.pl,-1,self.band_run)
        self.stScen = wx.StaticText(self.pl,-1,self.scen_run)
        bsCurrentRun.Add(wx.StaticText(self.pl,-1,"CL : "))
        bsCurrentRun.Add(self.stCL,1,wx.ALL|wx.EXPAND,10)
        bsCurrentRun.Add(wx.StaticText(self.pl,-1,"BRANCH : "))
        bsCurrentRun.Add(self.stBranch,1,wx.ALL|wx.EXPAND,10)
        bsCurrentRun.Add(wx.StaticText(self.pl,-1,"BAND : "))
        bsCurrentRun.Add(self.stBand,1,wx.ALL|wx.EXPAND,10)
        bsCurrentRun.Add(wx.StaticText(self.pl,-1,"SCENARIO : "))
        bsCurrentRun.Add(self.stScen,1,wx.ALL|wx.EXPAND,10)
        sbsCurrentRun.Add(bsCurrentRun, 0, wx.LEFT,10)

        #Band4
        sbBand4Prog = wx.StaticBox(self.pl, -1, 'Band 4 Progress', size=(-1, -1))
        #sbBand4Prog.SetForegroundColour(wx.BLUE)
        sbsBand4Prog = wx.StaticBoxSizer(sbBand4Prog, wx.VERTICAL)
        bsBand4Prog = wx.BoxSizer ( wx.HORIZONTAL )
        self.stscen_4 = []
        for i in range(0,len(scenario_implemented)):
            self.stscen_4.append(wx.StaticText(self.pl, -1 ,""))
            bsBand4Prog.Add(self.stscen_4[i],0, wx.ALL,10)
        sbsBand4Prog.Add(bsBand4Prog, 0, wx.LEFT,10)

        #Band17
        sbBand17Prog = wx.StaticBox(self.pl, -1, 'Band 17 Progress', size=(-1, -1))
        #sbBand17Prog.SetForegroundColour(wx.BLUE)
        sbsBand17Prog = wx.StaticBoxSizer(sbBand17Prog, wx.VERTICAL)
        bsBand17Prog = wx.BoxSizer ( wx.HORIZONTAL )
        self.stscen_17 = []
        for i in range(0,len(scenario_implemented)):
            self.stscen_17.append(wx.StaticText(self.pl, -1 ,""))
            bsBand17Prog.Add(self.stscen_17[i],0, wx.ALL,10)
        sbsBand17Prog.Add(bsBand17Prog, 0, wx.LEFT,10)

        #PROGRESS SECTION JOIN
        self.bsPRG = wx.BoxSizer ( wx.VERTICAL)
        self.bsPRG.Add(sbsCurrentRun, 0, wx.EXPAND)
        self.bsPRG.Add(sbsBand4Prog, 0, wx.EXPAND)
        self.bsPRG.Add(sbsBand17Prog, 0, wx.EXPAND)

        self.sizer.Add(self.bsPRG,0,wx.EXPAND)

        #self.pl.Refresh()

        #self.Refresh_Frame()
    def InitupdateProgress(self):
        self.stCL.SetLabel(self.cl_run)
        self.stBranch.SetLabel(self.branch_run)
        self.stBand.SetLabel(self.band_run)
        self.stScen.SetLabel(self.scen_run)

        if self.band4.IsChecked():
            for i in range(0,len(self.scenario_4test)):
                self.stscen_4[i].SetLabel(self.scenario_4test[i])


        if self.band17.IsChecked():
            for i in range(0,len(self.scenario_4test)):
                self.stscen_17[i].SetLabel(self.scenario_4test[i])

        self.Refresh_Frame()

    def updateProgress(self):

        self.scen_list_4x = []
        self.scen_list_17x = []
        for scen in self.scenario_4test:
            self.scen_list_4x.append(scen)
            self.scen_list_17x.append(scen)

        while threading.activeCount()>1:
        #while self.t1.isAlive() or self.t4.isAlive():
            time.sleep(5)
            Read = self.Read_Status()
            if Read == True :
                self.stCL.SetLabel(self.cl_run)
                self.stBranch.SetLabel(self.branch_run)
                self.stBand.SetLabel(self.band_run)
                self.stScen.SetLabel(self.scen_run)

                for i in range(0,len(self.scenario_4test)):
                    if self.band4.IsChecked():
                        if self.scen_list_4x[i] == STATUS_OK:
                            self.stscen_4[i].SetBackgroundColour("#1ad821") # GREEN
                        elif self.scen_list_4x[i] == STATUS_REGRESSION:
                            self.stscen_4[i].SetBackgroundColour("#ddff0f") # Orange
                        elif self.scen_list_4x[i] == STATUS_ASSERT:
                            self.stscen_4[i].SetBackgroundColour("#ff1515") # Red
                        elif self.scen_list_4x[i] != self.scenario_4test[i]:
                            self.stscen_4[i].SetBackgroundColour("#0000ff") # Blue

                    if self.band17.IsChecked():
                        if self.scen_list_17x[i] == STATUS_OK:
                            self.stscen_17[i].SetBackgroundColour("#1ad821") # GREEN
                        elif self.scen_list_17x[i] == STATUS_REGRESSION:
                            self.stscen_17[i].SetBackgroundColour("#ddff0f") # Orange
                        elif self.scen_list_17x[i] == STATUS_ASSERT:
                            self.stscen_17[i].SetBackgroundColour("#ff1515") # Red
                        elif self.scen_list_17x[i] != self.scenario_4test[i]:
                            self.stscen_17[i].SetBackgroundColour("#0000ff") # Blue

                self.Refresh_Progress()
                #self.pl.Refresh()
                # self.panel.Fit()
                # self.SendSizeEvent()
                # #self._onSize(wxFrame::SendSizeEvent)

    def cleanProgress(self):
        self.cl_run = ""
        self.branch_run = ""
        self.band_run = ""
        self.scen_run = ""
        self.scenario_4test = ""

        self.stCL.SetLabel(self.cl_run)
        self.stBranch.SetLabel(self.branch_run)
        self.stBand.SetLabel(self.band_run)
        self.stScen.SetLabel(self.scen_run)

        for i in range(0,len(scenario_implemented)):
            self.stscen_4[i].SetLabel("")
            self.stscen_4[i].SetBackgroundColour("#bfbfbf")

        for i in range(0,len(scenario_implemented)):
            self.stscen_17[i].SetLabel("")
            self.stscen_17[i].SetBackgroundColour("#bfbfbf")

        self.Refresh_Frame()

    def mainThread(self):
        self.kill_thread = False
        self.t1 = threading.Thread(target=self.Start_Test)
        self.t1.start()

        while True:
            if self.kill_thread == True :
                #self.t1.join()
                print "Killing Thread"
                sys.exit(1)


    def ResultWindow(self, event):
        print "Result Window"

    def TestType(self, event):
        if (self.rbAuto.GetValue()):
            self.testchoice = 0;
        elif (self.rbUnit.GetValue()):
            self.testchoice = 1
        else:
            self.testchoice = 2

    def PlatformType(self, event):
        if (self.rbWinxp.GetValue()):
            self.platformchoice = 0;
        elif (self.rbWoA.GetValue()):
            self.platformchoice = 1
            
    def Start_Test(self):
        iCT = CallboxTest()
        iCT.Init_Auto(self.branch_4test,self.band_4test,self.scenario_4test)
        if self.testchoice == 0:
            print "Call Auto scheduler"
            iCT.start()
        elif self.testchoice == 1:
            if self.flash.IsChecked() == True and self.entry.GetLabel() != "":
                print "Run Test with Flashing"
                Untar().main(self.entry.GetLabel())
                Flash().flash_modem(99999,'cr3')
                iCT.Init_Auto(self.branch_4test,self.band_4test,self.scenario_4test)
                iCT.Run_Branch_Test(Forced=self.force.IsChecked(),flash=False,Reg=self.cReg.IsChecked(),CL=self.cl)
                print "Run Test Finished"
            else:
                print "Run Test without Flashing"
                iCT.Run_Branch_Test(Forced=self.force.IsChecked(),flash=self.flash.IsChecked(),Reg=self.cReg.IsChecked(),CL=self.cl)
                print "Run Test Finished"

        elif self.testchoice == 2:
           print "No Test"


    def find_index(self,lst,item):
        for i in range(0,len(lst)):
            if(lst[i]==item):
                #print "find index %d"%i
                return i
        return 0

    def Read_Status(self):
        file = 'status.txt'
        self.scen_list_4 =[]
        self.status_list_4 = []
        self.scen_list_17 =[]
        self.status_list_17 = []

        latest_CL = 0
        latest_br = ""
        CURRENT_SCEN = ""
        if os.path.exists(file):
            FILE_OK = open(file,'r')
            for line in (FILE_OK.readlines()):#reversed
                try:
                    band_run = re.search(re.compile(r"Band:(\S+)"),line).group(1)
                    scen = re.search(re.compile(r"Test:(\S+)"), line).group(1)
                    Status = re.search(re.compile(r"Status:(\S+)(])"), line).group(1)
                    if int(band_run) == 4 :
                        self.scen_list_4x[self.find_index(self.scenario_4test,scen)] = Status
                    elif int(band_run) == 17 :
                        self.scen_list_17x[self.find_index(self.scenario_4test,scen)] = Status
                except:
                    pass

            self.cl_run = re.search(re.compile(r"CL([0-9]+)"),line).group(1)
            self.branch_run = re.search(re.compile(r"Branch:(\S+)"),line).group(1)
            self.scen_run  = re.search(re.compile(r"Test:(\S+)"), line).group(1)
            self.band_run = re.search(re.compile(r"Band:(\S+)"),line).group(1)

            FILE_OK.close()
            return True
        else:
            return False

    def OnPressEnter(self,event):
        x = 1
        #self.stInfo.SetLabel( self.entry.GetValue())

    def OnOpenScen(self,event):
        wildcard = " (*.tar,*.gz,*.wrapped,*.rar)|*.tar;*.gz;*.wrapped;*.rar"
        dialog = wx.FileDialog(None, "Choose a file",os.getcwd(), "", wildcard, wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            filename = dialog.GetPath()
            self.entry.AppendText(filename)
            self.flash_list.append(filename)
        dialog.Destroy()

    def flash_only(self,event):
        self.t3 = KThread(target=self.flash_thread)
        self.t3.start()

    def flash_thread(self):
        if self.entry.GetLabel() != "" :
            Untar().main(self.entry.GetLabel())
            Flash().flash_modem(99999,'cr3')
        else:
            print "Please select a binary to flash"


    def Regression_Start(self,event):
        self.cleanProgress()
        self.ParseArgs()
        self.InitupdateProgress()
        self.t4 = KThread(target=self.regression_thread)
        self.t4.start()
        self.t2 = KThread(target=self.updateProgress)
        self.t2.start()

    def regression_thread(self):
        cl = int(self.changelist.GetValue())
        if cl != 0 :
            Regression()._run(self.branch_4test,self.band_4test,self.scenario_4test,cl)
            return
Пример #33
0
class CommObject:
    def __init__(self, host="localhost", userid="guest", password="******"):
        self.error = False
        self.debug = False
        self.host = host
        self.userid = userid
        self.password = password
        self.configList = []  # scalable config for rabbitmq
        self.chanList = []  # [id,obj] ordered pairs
        self.lockObj = Lock()  # used for thread safety
        self.thread_params = list()  # used for passing params into thread

        self.exchange = "myexchange"
        self.uuid = str(uuid.uuid1())  # make each queue unique
        self.myqueue = "myqueue" + self.uuid
        self.mykey = "myq.myx"
        self.running = False
        self.subscribe = None  # self.subscribe.start() to start thread for listening on exchange

        try:
            self.connection = amqp.Connection(
                userid=self.userid, password=self.password, host=self.host, virtualhost="/", ssl=False
            )
        except Exception as eobj:
            print eobj
            print "Is the server running? (sh rabbitmq-server-2.1.1/scripts/rabbitmq-server)"
            self.error = True
            #
            # an object is returned, not None
            #

    # end __init__
    # -----------------------------------------------------------
    def SimpleConfigure(self):
        try:
            self.channel = self.connection.channel()
        except Exception as eobj:
            print eobj
            self.connection.close()
            return -1

        print "channel = ", self.channel.channel_id

        try:
            self.channel.access_request("/data", active=True, write=True, read=True)
        except Exception as eobj:
            print eobj
            self.channel.close()
            return -1

        #
        # Note: When configuring/reconfiguring the server may not like changes
        #       to existing exchanges. Closing channel on exception clears server.

        #
        # with mulitiple listeners 'direct' ping-pongs 1:1 across n listeners
        #

        #
        # defaults on exchange_declare:are same as queue_declare:
        # passive=False, durable=False,
        # auto_delete=True, internal=False, nowait=False
        #
        # if you mix defaults between the two, then you will raise an exception
        #
        # auto_delete -> queue is deleted when last consumer closes
        # durable -> survives server reboot
        # passive -> when set tests presence of queue, if not present then "access refused" returned
        # internal -> special purpose, keep false
        # nowait -> server doesn't reply, keep false
        #
        queue_type = "fanout"  # 'direct' #
        try:
            self.channel.exchange_declare(self.exchange, type=queue_type, durable=False, auto_delete=True)
        except Exception as eobj:
            print eobj
            print "The server is unhappy."
            print "You re-declared an exchange and changed a parameter."
            print "Kill and restart the server."
            self.channel.close()
            return -1

        try:
            self.channel.queue_declare(self.myqueue, durable=False, auto_delete=True)
        except Exception as eobj:
            print eobj
            self.channel.close()
            return -1

        try:
            self.channel.queue_bind(self.myqueue, self.exchange, routing_key=self.mykey)
        except Exception as eobj:
            print eobj
            self.channel.close()
            return -1

    # end SimpleConfigure
    # -----------------------------------------------------------
    #
    # private
    #
    def _getChannelObject(self, channelList, chid):
        # channelList format:
        #   [ [<channel-id>, <channelObject>] ]
        ii = 0
        while ii < len(channelList):
            chpair = channelList[ii]
            if chid == chpair[0]:
                return chpair[1]

            ii = ii + 1

            # end while

        return None

    # end _getChannelObject
    # -----------------------------------------------------------

    def Configure(self, config=[1, "myexchange", "direct", "myqueue", "myq.myx"]):
        #
        # in: config format:
        #   [<channel>, <exchange>, <type>, <optional_queue>, <optional_routing_key>]
        #        0          1         2            3                4
        #
        # queue is not necessary for writes, only for reads
        # routing_key is used for filtering reads
        #
        # out: self.configList, self.chanList
        #   chanlList format:
        #   [ [<channel-id>, <channelObject>] ]
        #          0            1
        #
        #
        # entity-relationship for channels:
        #
        # 1 configuration : multiple channels
        # 1 channel : multiple exchanges
        # 1 exchange : 0,1 queue
        # 1 queue : 0,1 message tag
        #
        err_f = False
        channelList = self.chanList

        #
        # if channel does not exist then create channel
        # else skip create, use pre-existing channel in channelList
        #
        chobj = self._getChannelObject(channelList, config[0])
        if chobj == None:
            try:
                chobj = self.connection.channel()
            except Exception as eobj:
                print eobj
                err_f = True

        if not err_f:
            try:
                chobj.access_request("/data", active=True, write=True, read=True)
            except Exception as eobj:
                print eobj
                err_f = True
                channel.close()

        if not err_f:
            try:
                chobj.exchange_declare(exchange=config[1], type=config[2], durable=False, auto_delete=True)
            except Exception as eobj:
                print eobj
                print "The server is unhappy."
                print "You re-used an exchange: %s and changed the type parameter: %s." % (config[1], config[2])
                print "Closing the channel. Rename the exchange or kill and restart the server (server has memory)."
                chobj.close()
                err_f = True
        #
        # (type, queue, publish) = ('direct', routing key, match routing key)             -> yes delivery
        # (type, queue, publish) = ('direct', routing key, no or mismatched routing key) -> no
        # (type, queue, publish) = ('fanout', routing key, no or mismatched routing key) -> yes
        #
        if not err_f:
            if config[3] != None:  # queues are for reading from exchange
                try:
                    chobj.queue_declare(queue=config[3], durable=False, auto_delete=True)
                except Exception as eobj:
                    print eobj
                    err_f = True

                if not err_f:
                    try:
                        chobj.queue_bind(queue=config[3], exchange=config[1], routing_key=config[4])
                    except Exception as eobj:
                        print eobj
                        err_f = True

        newElement = []

        #
        # build object's channel list out of channel id from configuration list plus channel object
        # build objects's configuration list out of input list
        #
        if not err_f:
            newElement = [config[0]] + [chobj]
            self.chanList = self.chanList + [newElement]  # building list of lists

            self.configList = self.configList + [config]  # building list of lists

        return err_f

    # end Configure

    # -----------------------------------------------------------

    def ConfigureList(self, configSet):
        #
        # config format:
        #   [[<channel>, <exchange>, <type>, <optional_queue>, <optional_routing_key>], ...]
        #
        # channelList format:
        #   [ <channel-id>, <channelObject> ]
        #

        err_f = False

        ii = 0  # channelList is poplulated as channels are created
        while ii < len(configSet) and (not err_f):
            err_f = self.Configure(configSet[ii])
            if err_f:
                break

            ii = ii + 1
        # end while

        return err_f

    # end ConfigureList
    # -----------------------------------------------------------
    def SimplePut(self, msg_in):

        #
        # routing key is used for 'direct' exchanges
        #
        base_time = time.time()
        self.channel.basic_publish(amqp.Message(msg_in), self.exchange, self.mykey)
        #
        # (type, queue, publish) = ('direct', routing key, routing key)                  -> yes delivery
        # (type, queue, publish) = ('direct', routing key, no or mismatched routing key) -> no
        # (type, queue, publish) = ('fanout', routing key, no or mismatched routing key) -> yes
        #

        print "sec:", time.time() - base_time

    # end SimplePut
    # -----------------------------------------------------------
    def _searchListOfLists(
        self, listOfLists, index, key  # form: [ [...], [...],...]  # index into unwrapped list
    ):  # what to match
        match_f = False
        ii = 0

        while ii < len(listOfLists):
            theList = listOfLists[ii]
            if theList[index] == key:
                match_f = True
                break
            ii = ii + 1
        # end while

        return match_f, ii

    # end _searchListOfLists

    # -----------------------------------------------------------
    def Put(self, msg_in, exchange=None, routing_key=None):
        #
        # the purpose with the defaults is to allow the creation of simple communication links
        # with the minimal amount of programmatic effort (for testing), in parallel with
        # complex links that scale for large networks
        #
        err_f = False
        if exchange == None:
            exchange = self.exchange
            routing_key = self.mykey  # routing key is used for 'direct' exchanges
            ch_obj = self.channel
        else:
            # search the configure list for exchange, match index yields channel id and routing_key
            # search the channel list for channel id, match yields channel object

            # there are many ways for this function to fail by
            # passing in bad parameters
            try:
                match_f, e_index = self._searchListOfLists(self.configList, 1, exchange)
            except Exception as eobj:
                print eobj
                err_f = True
                return err_f

            if not match_f:
                print "Put(), exchange: %s not found in %s" % (exchange, self.configList)
                err_f = True
                return err_f

            chan_id = self.configList[e_index][0]
            routing_key = self.configList[e_index][4]

            # there are many ways for this function to fail by
            # passing in bad parameters
            try:
                match_f, c_index = self._searchListOfLists(self.chanList, 0, chan_id)
            except Exception as eobj:
                print eobj
                err_f = True
                return err_f

            if not match_f:
                print "Put(), channel %d not found in: %s" % (chan_id, self.chanList)
                err_f = True
                return err_f

            ch_obj = self.chanList[c_index][1]

        ch_obj.basic_publish(amqp.Message(msg_in), exchange, routing_key)

        return err_f

    # end Put

    # -----------------------------------------------------------
    def SimpleGet(self):
        try:
            response = self.channel.basic_get(self.myqueue, no_ack=False)
        except Exception as eobj:
            print self.myqueue, eobj
            response = None

        if response is not None:
            print 'ch: %d, body = "%s"' % (self.channel.channel_id, response.body)
            self.channel.basic_ack(response.delivery_tag)
        else:
            print "no message"

    # end SimpleGet
    # -----------------------------------------------------------
    def _queue2chobj(self, queue):
        ch_obj = None
        err_f = False
        #
        # given queue, search for channel id
        #
        try:
            match_f, q_index = self._searchListOfLists(self.configList, 3, queue)
        except Exception as eobj:
            print eobj
            err_f = True

        if not err_f:
            if not match_f:
                print "_queue2chobj(), queue %s not found in: %s" % (queue, self.configList)
                err_f = True

        if not err_f:
            chan_id = self.configList[q_index][0]

            # there are many ways for this function to fail by
            # passing in bad parameters
            try:
                match_f, c_index = self._searchListOfLists(self.chanList, 0, chan_id)
            except Exception as eobj:
                print eobj
                err_f = True

        if not err_f:
            if not match_f:
                print "_queue2chobj(), channel %d not found in: %s" % (chan_id, self.chanList)
                err_f = True

        if not err_f:
            ch_obj = self.chanList[c_index][1]

        return err_f, ch_obj

    # end _queue2chobj
    # -----------------------------------------------------------
    def Get(self, queue=None):
        err_f = False
        msg_f = False
        msg = None
        if queue == None:  # for simple configuration case
            queue = self.myqueue
            ch_obj = self.channel
        else:
            #
            # given queue, search for channel id
            #
            err_f, ch_obj = self._queue2chobj(queue)

            if not err_f:
                response = None
                try:
                    response = ch_obj.basic_get(queue, no_ack=False)
                except amqp.Timeout:
                    pass  # not an error
                except:
                    print "queue=", queue
                    print eobj
                    err_f = True

            if not err_f:
                if response is not None:
                    msg_f = True
                    msg = response.body
                    ch_obj.basic_ack(response.delivery_tag)
                else:
                    print "no data"

        return err_f, msg_f, msg

    # end Get
    # -----------------------------------------------------------
    #
    # private
    #
    def _mycallback(self, msg):
        print
        for key, val in msg.properties.items():
            print "%s: %s" % (key, str(val))
        for key, val in msg.delivery_info.items():
            print "> %s: %s" % (key, str(val))
        print "received <", msg.body, "> from channel #", msg.channel.channel_id
        msg.channel.basic_ack(msg.delivery_tag)

    # end __mycallback
    # -----------------------------------------------------------
    #
    # private
    #
    def _SimpleSubscribeThread(self):
        self.channel.basic_consume(queue=self.myqueue, callback=self._mycallback, no_ack=False)
        #
        # init loop
        #
        self.running = True
        #
        # loop - need a shared object (event) to determine external stop
        #
        while self.running:
            try:
                self.channel.wait(timeout=5)
            except amqp.Timeout:
                print "timeout"

    # end _SimpleSubscribeThread
    # -----------------------------------------------------------
    #
    # private
    #
    def _SubscribeThread(self):
        #
        # do once: get loop invarients
        #
        self.lockObj.acquire()
        if len(self.thread_params):
            #
            # note: it is very easy to mess up order when
            #       adding/changing parameters
            #
            evobj = self.thread_params.pop()  # 3
            callback = self.thread_params.pop()  # 2
            queue = self.thread_params.pop()  # 1

        self.lockObj.release()

        #
        # given queue, return channel
        #
        err_f, ch_obj = self._queue2chobj(queue)
        if err_f:
            print "Subscribe failed"
        else:
            ch_obj.basic_consume(queue=queue, callback=callback, no_ack=False)
            #
            # init loop
            #
            self.running = True
            #
            # loop - need a shared object (event) to determine external stop
            #
            while not self.ev_obj.is_set():  # while not signaled halt
                try:
                    ch_obj.wait(timeout=5)
                except amqp.Timeout:
                    if self.debug:
                        print "timeout"

    # end _SubscribeThread
    # -----------------------------------------------------------
    #
    def SimpleSubscribe(self):

        self.subscribe = KThread(target=self._SimpleSubscribeThread)
        self.subscribe.start()

    # end SimpleSubscribe

    # -----------------------------------------------------------
    #
    def Subscribe(self, queue=None, callback=None):
        #
        # the queue and callback being subscribed to is not passed in here
        # but passed in through via "self"
        # the question is how to make this scale?
        #
        #
        #

        self.lockObj.acquire()
        if queue == None:  # done for compatibility with "simple" version
            queue = self.myqueue
        if callback == None:
            callback = self._mycallback

        #
        # communication parameters within thread
        #
        self.thread_params.append(queue)  # 1
        self.thread_params.append(callback)  # 2
        #
        # control param for thread
        #
        self.ev_obj = Event()
        self.thread_params.append(self.ev_obj)  # 3 - top of stack

        subscribe = KThread(target=self._SubscribeThread)
        subscribe.start()
        self.lockObj.release()
        #
        # return thread and its control object
        #
        return subscribe, self.ev_obj

    # end Subscribe
    # -----------------------------------------------------------
    #
    def Unsubscribe(self, thread_object, event_object):
        #
        # object may no longer exists, so bound by "try"
        # also, belts and suspenders, i.e. signal before kill
        #
        try:
            event_object.set()  # signals to stop running
        except:
            print "attempting to signal a non-existent event"
        try:
            thread_object.kill()
        except Exception as eobj:
            print "attempting to signal a non-existent thread"

    # end SimpleUnsubscribe
    # -----------------------------------------------------------
    #
    def SimpleUnsubscribe(self):
        self.running = False
        try:
            self.subscribe.kill()  # might not have ever subscribed
        except Exception as eobj:
            pass

    # end SimpleUnsubscribe
    # -----------------------------------------------------------
    #
    def SimpleClose(self):
        self.SimpleUnsubscribe()
        # self.channel.close()     # sometimes hangs at sock.recv
        self.connection.close()  # connection close kills socket

    # end SimpleClose
    # -----------------------------------------------------------
    #
    def CloseChannel(self, chobj):
        # self.Unsubscribe(chobj)
        self.chobj.close()  # connection close kills socket

    # end Close
    # -----------------------------------------------------------
    #
    def CloseAll(self):
        ii = 0
        while ii < len(self.chanList):
            self.Close(self.chanList[ii][1])  # connection close kills socket
            ii = ii + 1
        # end while
        self.connection.close()
 def run(self):
     print 'Server thread run Method ', self.id, " ", self.currentTerm
     time.sleep(1)
     self.follower_state = KThread(target=self.follower, args=())
     self.follower_state.start()
Пример #35
0
class CommObject():
  def __init__(self, host='localhost', userid='guest', password='******'): 
    self.error = False
    self.debug = False
    self.host = host
    self.userid = userid
    self.password = password        
    self.configList = []            # scalable config for rabbitmq
    self.chanList = []              # [id,obj] ordered pairs
    self.lockObj = Lock()           # used for thread safety
    self.thread_params = list()     # used for passing params into thread
       
    self.exchange = 'myexchange'
    self.uuid = str(uuid.uuid1())  # make each queue unique
    self.myqueue = 'myqueue' + self.uuid
    self.mykey = 'myq.myx'
    self.running = False
    self.subscribe = None # self.subscribe.start() to start thread for listening on exchange
 
    try:     
      self.connection = amqp.Connection(userid=self.userid, 
                           password=self.password, host=self.host,
                           virtualhost='/', ssl=False)
    except Exception as eobj:
      print eobj
      print "Is the server running? (sh rabbitmq-server-2.1.1/scripts/rabbitmq-server)"
      self.error = True
      #
      # an object is returned, not None
      #
      
  # end __init__ 
  #-----------------------------------------------------------   
  def SimpleConfigure(self):  
    try:
      self.channel = self.connection.channel()
    except Exception as eobj:
      print eobj
      self.connection.close()
      return -1
      
    print 'channel = ', self.channel.channel_id
    
    try:
      self.channel.access_request('/data', active=True, write=True, read=True)
    except Exception as eobj:
      print eobj
      self.channel.close()
      return -1
      
    #
    # Note: When configuring/reconfiguring the server may not like changes
    #       to existing exchanges. Closing channel on exception clears server.
    
    #
    # with mulitiple listeners 'direct' ping-pongs 1:1 across n listeners
    #
    
    #
    # defaults on exchange_declare:are same as queue_declare:
    # passive=False, durable=False,
    # auto_delete=True, internal=False, nowait=False
    #
    # if you mix defaults between the two, then you will raise an exception
    # 
    # auto_delete -> queue is deleted when last consumer closes
    # durable -> survives server reboot
    # passive -> when set tests presence of queue, if not present then "access refused" returned
    # internal -> special purpose, keep false
    # nowait -> server doesn't reply, keep false
    #
    queue_type = 'fanout' # 'direct' # 
    try:
      self.channel.exchange_declare(self.exchange,type=queue_type,durable=False, auto_delete=True)
    except Exception as eobj:
      print eobj
      print "The server is unhappy."
      print "You re-declared an exchange and changed a parameter."
      print "Kill and restart the server."
      self.channel.close()
      return -1
      
    try:  
      self.channel.queue_declare(self.myqueue,durable=False, auto_delete=True)
    except Exception as eobj:
      print eobj
      self.channel.close()
      return -1
    
    try:    
      self.channel.queue_bind(self.myqueue,self.exchange,routing_key=self.mykey)  
    except Exception as eobj:
      print eobj
      self.channel.close()
      return -1
      
  # end SimpleConfigure
  #-----------------------------------------------------------
  #
  # private
  #
  def _getChannelObject(self,channelList,chid):
    # channelList format:
    #   [ [<channel-id>, <channelObject>] ]
    ii = 0
    while (ii<len(channelList)):
      chpair = channelList[ii]
      if (chid == chpair[0]):
        return chpair[1]
      
      ii = ii+1
      
      #end while 
      
    return None
  #end _getChannelObject
  #-----------------------------------------------------------
 
  def Configure(self, config=[1, 'myexchange', 'direct', 'myqueue', 'myq.myx']):
    #
    # in: config format:
    #   [<channel>, <exchange>, <type>, <optional_queue>, <optional_routing_key>] 
    #        0          1         2            3                4
    #
    # queue is not necessary for writes, only for reads
    # routing_key is used for filtering reads
    #
    # out: self.configList, self.chanList
    #   chanlList format:
    #   [ [<channel-id>, <channelObject>] ]
    #          0            1
    #
    #
    # entity-relationship for channels:
    #
    # 1 configuration : multiple channels
    # 1 channel : multiple exchanges
    # 1 exchange : 0,1 queue
    # 1 queue : 0,1 message tag
    #
    err_f = False
    channelList = self.chanList
   
    #
    # if channel does not exist then create channel
    # else skip create, use pre-existing channel in channelList
    #     
    chobj = self._getChannelObject(channelList,config[0])
    if (chobj == None):
      try:
         chobj = self.connection.channel()
      except Exception as eobj:
         print eobj
	 err_f = True
	 
    if (not err_f):  
      try:
        chobj.access_request('/data',active=True,write=True,read=True)
      except Exception as eobj:
         print eobj
         err_f = True
         channel.close()
       
    if (not err_f):	 
      try:
        chobj.exchange_declare(exchange=config[1],type=config[2],durable=False, auto_delete=True)
      except Exception as eobj:
        print eobj
        print "The server is unhappy."
        print "You re-used an exchange: %s and changed the type parameter: %s." % (config[1], config[2])
        print "Closing the channel. Rename the exchange or kill and restart the server (server has memory)."
        chobj.close()
	err_f = True
    #
    # (type, queue, publish) = ('direct', routing key, match routing key)             -> yes delivery
    # (type, queue, publish) = ('direct', routing key, no or mismatched routing key) -> no 
    # (type, queue, publish) = ('fanout', routing key, no or mismatched routing key) -> yes
    #	
    if (not err_f):
      if (config[3] != None): 	   # queues are for reading from exchange
        try:  
          chobj.queue_declare(queue=config[3],durable=False,auto_delete=True)
        except Exception as eobj:
          print eobj
          err_f = True
        
        if (not err_f):	   
          try:	
            chobj.queue_bind(queue=config[3],exchange=config[1],routing_key=config[4])  
          except Exception as eobj:
            print eobj
            err_f = True            
      
    newElement = []
    
    #
    # build object's channel list out of channel id from configuration list plus channel object
    # build objects's configuration list out of input list
    #
    if (not err_f):
      newElement = [config[0]] + [chobj]
      self.chanList = self.chanList + [ newElement ] # building list of lists
      
      self.configList = self.configList + [ config ] # building list of lists
      
    return err_f
    
  #end Configure
 
  #-----------------------------------------------------------
     
  def ConfigureList(self, configSet):
    #
    # config format:
    #   [[<channel>, <exchange>, <type>, <optional_queue>, <optional_routing_key>], ...]
    # 
    # channelList format:
    #   [ <channel-id>, <channelObject> ]
    #
    
    err_f = False
    
    ii=0                      # channelList is poplulated as channels are created
    while (ii<len(configSet) and (not err_f)):
      err_f = self.Configure(configSet[ii])
      if (err_f):
        break
	
      ii = ii +1
    #end while
    
    return err_f
    
  #end ConfigureList
  #-----------------------------------------------------------     
  def SimplePut(self,msg_in):
    
    #
    # routing key is used for 'direct' exchanges
    #
    base_time = time.time();
    self.channel.basic_publish(amqp.Message(msg_in),self.exchange,self.mykey)
    #
    # (type, queue, publish) = ('direct', routing key, routing key)                  -> yes delivery
    # (type, queue, publish) = ('direct', routing key, no or mismatched routing key) -> no 
    # (type, queue, publish) = ('fanout', routing key, no or mismatched routing key) -> yes
    #
    
    print "sec:", time.time() - base_time
  # end SimplePut
  #-----------------------------------------------------------
  def _searchListOfLists(self,listOfLists,  # form: [ [...], [...],...]
                              index,        # index into unwrapped list
			      key):         # what to match
    match_f = False
    ii =0
    
    while(ii<len(listOfLists)):
      theList = listOfLists[ii]
      if (theList[index] == key):
        match_f = True
	break
      ii = ii + 1	
    #end while
    
    return match_f, ii
  #end _searchListOfLists
  
  #-----------------------------------------------------------     
  def Put(self,msg_in,exchange=None, routing_key=None):
    #
    # the purpose with the defaults is to allow the creation of simple communication links
    # with the minimal amount of programmatic effort (for testing), in parallel with
    # complex links that scale for large networks
    #
    err_f = False
    if (exchange == None):
      exchange = self.exchange
      routing_key = self.mykey    # routing key is used for 'direct' exchanges
      ch_obj = self.channel
    else:
       # search the configure list for exchange, match index yields channel id and routing_key
       # search the channel list for channel id, match yields channel object
       
       # there are many ways for this function to fail by
       # passing in bad parameters
       try:
         match_f, e_index = self._searchListOfLists(self.configList,1,exchange)
       except Exception as eobj:
          print eobj
	  err_f = True
	  return err_f
	  	 
       if (not match_f):
         print "Put(), exchange: %s not found in %s" % (exchange,self.configList)
	 err_f = True
	 return err_f
	 
       chan_id = self.configList[e_index][0]
       routing_key = self.configList[e_index][4]
	 
       # there are many ways for this function to fail by
       # passing in bad parameters
       try:
         match_f, c_index = self._searchListOfLists(self.chanList,0,chan_id)
       except Exception as eobj:
          print eobj
	  err_f = True
	  return err_f
	  
       if (not match_f):
         print "Put(), channel %d not found in: %s" % (chan_id,self.chanList)
	 err_f = True
	 return err_f
       
       ch_obj = self.chanList[c_index][1]
   
    ch_obj.basic_publish(amqp.Message(msg_in),exchange,routing_key)
      
    return err_f
  # end Put
  
  #-----------------------------------------------------------  
  def SimpleGet(self):
    try:
      response = self.channel.basic_get(self.myqueue,no_ack=False)
    except Exception as eobj:
      print self.myqueue, eobj
      response = None
      
    if response is not None:
      print "ch: %d, body = \"%s\"" %(self.channel.channel_id, response.body) 
      self.channel.basic_ack(response.delivery_tag)
    else:
      print "no message" 
       
  # end SimpleGet
  #-----------------------------------------------------------
  def _queue2chobj(self,queue):
    ch_obj = None
    err_f = False
    #
    # given queue, search for channel id
    #
    try:
         match_f, q_index = self._searchListOfLists(self.configList,3,queue)
    except Exception as eobj:
          print eobj
	  err_f = True
      	  
    if (not err_f):  	 
        if (not match_f):
          print "_queue2chobj(), queue %s not found in: %s" % (queue,self.configList)
	  err_f = True
	  
    if (not err_f): 
        chan_id = self.configList[q_index][0]
	 
        # there are many ways for this function to fail by
        # passing in bad parameters
        try:
          match_f, c_index = self._searchListOfLists(self.chanList,0,chan_id)
        except Exception as eobj:
          print eobj
	  err_f = True
	  
    if (not err_f):  
        if (not match_f):
          print "_queue2chobj(), channel %d not found in: %s" % (chan_id,self.chanList)
	  err_f = True
	  
    if (not err_f):         
        ch_obj = self.chanList[c_index][1]
	
    return err_f, ch_obj
  # end _queue2chobj
  #-----------------------------------------------------------  
  def Get(self, queue=None):
    err_f = False
    msg_f = False
    msg = None
    if (queue == None):   # for simple configuration case
      queue = self.myqueue
      ch_obj = self.channel
    else:
      #
      # given queue, search for channel id
      #
      err_f, ch_obj = self._queue2chobj(queue)
	  
      if (not err_f):         
	response = None  
        try:
          response = ch_obj.basic_get(queue,no_ack=False)
        except amqp.Timeout:
	  pass                 # not an error
	except: 
         print "queue=", queue
	 print eobj
         err_f = True
     
      if (not err_f): 
        if response is not None:
	  msg_f = True
          msg = response.body
          ch_obj.basic_ack(response.delivery_tag)
        else:
          print "no data" 
      
    return err_f, msg_f, msg
  # end Get  
  #-----------------------------------------------------------     
  #
  # private
  #  
  def _mycallback(self,msg):
    print
    for key,val in msg.properties.items():
      print '%s: %s' % (key,str(val))
    for key,val in msg.delivery_info.items():
      print '> %s: %s' % (key, str(val))
    print 'received <', msg.body, '> from channel #', msg.channel.channel_id
    msg.channel.basic_ack(msg.delivery_tag)
  # end __mycallback
  #----------------------------------------------------------- 
  #
  # private
  #    
  def _SimpleSubscribeThread(self):
     self.channel.basic_consume(queue=self.myqueue, callback=self._mycallback, no_ack=False)
     #
     # init loop
     #
     self.running = True
     #
     # loop - need a shared object (event) to determine external stop
     # 
     while self.running:
        try:
           self.channel.wait(timeout = 5)   
        except amqp.Timeout:
           print "timeout"
      
  # end _SimpleSubscribeThread
  #----------------------------------------------------------- 
  #
  # private
  #    
  def _SubscribeThread(self):
    #
    # do once: get loop invarients 
    #      
    self.lockObj.acquire()
    if len(self.thread_params):
       #
       # note: it is very easy to mess up order when
       #       adding/changing parameters
       #
       evobj = self.thread_params.pop()     # 3
       callback = self.thread_params.pop()  # 2
       queue = self.thread_params.pop()     # 1
       
    self.lockObj.release()
    
    #
    # given queue, return channel 
    #
    err_f, ch_obj = self._queue2chobj(queue)
    if (err_f):
      print "Subscribe failed"
    else:
      ch_obj.basic_consume(queue=queue, callback=callback, no_ack=False)
      #
      # init loop
      #
      self.running = True
      #
      # loop - need a shared object (event) to determine external stop
      # 
      while not self.ev_obj.is_set():    # while not signaled halt
        try:
           ch_obj.wait(timeout = 5)   
        except amqp.Timeout:
	   if (self.debug):
             print "timeout"
      
  # end _SubscribeThread
  #----------------------------------------------------------- 
  #    
  def SimpleSubscribe(self):
  
     self.subscribe = KThread(target = self._SimpleSubscribeThread)
     self.subscribe.start() 
     
  # end SimpleSubscribe
  
  #----------------------------------------------------------- 
  #    
  def Subscribe(self,queue=None,callback=None):
     #
     # the queue and callback being subscribed to is not passed in here
     # but passed in through via "self"
     # the question is how to make this scale?
     #
     #
     #
     
     self.lockObj.acquire()     
     if (queue == None):    # done for compatibility with "simple" version
       queue = self.myqueue
     if (callback == None):
       callback = self._mycallback
       
     #
     # communication parameters within thread
     #
     self.thread_params.append(queue)       # 1
     self.thread_params.append(callback)    # 2
     #
     # control param for thread
     #
     self.ev_obj = Event()
     self.thread_params.append(self.ev_obj)      # 3 - top of stack
      
     subscribe = KThread(target = self._SubscribeThread)
     subscribe.start() 
     self.lockObj.release()    
     #
     # return thread and its control object
     #
     return subscribe, self.ev_obj
     
  # end Subscribe
  #----------------------------------------------------------- 
  #
  def Unsubscribe(self,thread_object, event_object):
     #
     # object may no longer exists, so bound by "try"
     # also, belts and suspenders, i.e. signal before kill
     #
     try: 
       event_object.set()    # signals to stop running
     except:
       print "attempting to signal a non-existent event"
     try:
       thread_object.kill()    
     except Exception as eobj:
       print "attempting to signal a non-existent thread"   
  # end SimpleUnsubscribe
  #----------------------------------------------------------- 
  #
  def SimpleUnsubscribe(self): 
     self.running = False
     try:
       self.subscribe.kill()    # might not have ever subscribed
     except Exception as eobj:
       pass   
  # end SimpleUnsubscribe
  #----------------------------------------------------------- 
  #   
  def SimpleClose(self):
    self.SimpleUnsubscribe()           
    #self.channel.close()     # sometimes hangs at sock.recv
    self.connection.close()   # connection close kills socket
  # end SimpleClose
  #----------------------------------------------------------- 
  #   
  def CloseChannel(self, chobj):
    #self.Unsubscribe(chobj)           
    self.chobj.close()   # connection close kills socket
  # end Close
  #----------------------------------------------------------- 
  #   
  def CloseAll(self):
    ii =0
    while (ii < len(self.chanList)):          
      self.Close(self.chanList[ii][1])   # connection close kills socket
      ii = ii + 1
    # end while
    self.connection.close() 
Пример #36
0
def main():
    client = Client(sys.argv[1])
    init = 1
    if sys.argv[1] == 'init':
        filename = input("Enter filename containing initial transactions: ")
        with open(filename) as f:
            for line in f:
                line = line.strip('\n')
                words = line.split(' ')
                txn = [words[0].lower(), words[1].lower(), words[2]]
                txn_id = uuid.uuid4().hex
                msg = 'txn' + '.' + txn_id + '.' + '*'.join(txn) + '/'

                lock.acquire()
                client.txnThreads[txn_id] = KThread(target=client.executeTxn,
                                                    args=(
                                                        txn_id,
                                                        msg,
                                                        init,
                                                    ))
                client.txnThreads[txn_id].start()
                lock.release()
                init += 1
                sleep(0.001)
    else:
        seq = 1
        txn_id = uuid.uuid4().hex
        msg = 'bal' + '.' + txn_id + '.' + client.name + '/'
        client.txnThreads[txn_id] = KThread(target=client.executeTxn,
                                            args=(
                                                txn_id,
                                                msg,
                                                seq,
                                            ))
        client.txnThreads[txn_id].start()

        sleep(1)
        while True:
            user_input = input(
                "Enter 1 for transaction and 2 for latest balance: ")
            if user_input == '1':
                txn = input("Enter transaction:")
                txn = txn.split(' ')

                if len(txn) == 3 and txn[0] == client.name and (
                        txn[1] == 'a' or txn[1] == 'b' or txn[1] == 'c'):
                    txn_id = uuid.uuid4().hex
                    msg = 'txn' + '.' + txn_id + '.' + '*'.join(txn) + '/'

                    client.txnThreads[txn_id] = KThread(
                        target=client.executeTxn, args=(
                            txn_id,
                            msg,
                            seq,
                        ))
                    client.txnThreads[txn_id].start()
                    seq += 1
                else:
                    print('Wrong Format! Try again')

            elif user_input == '2':
                txn_id = uuid.uuid4().hex
                msg = 'bal' + '.' + txn_id + '.' + client.name + '/'

                client.txnThreads[txn_id] = KThread(target=client.executeTxn,
                                                    args=(
                                                        txn_id,
                                                        msg,
                                                        seq,
                                                    ))
                client.txnThreads[txn_id].start()

            else:
                print('Try Again!')

            input("")
Пример #37
0
import os
from KThread import *

def func():
  print 'Function started'
  os.system('java -jar ' + 'TournamentServer-GUI/gui/test/lib/jstestdriver/JsTestDriver.jar --port 9876')
  print 'Function finished'

A = KThread(target=func)
A.start()
A.kill()

print 'End of main program'
Пример #38
0
 def SimpleSubscribe(self):
 
    self.subscribe = KThread(target = self._SimpleSubscribeThread)
    self.subscribe.start() 
Пример #39
0
 def run(self):
     time.sleep(1)
     self.follower_state = KThread(target=self.follower, args=())
     self.follower_state.start()
Пример #40
0
 def flash_only(self,event):
     self.t3 = KThread(target=self.flash_thread)
     self.t3.start()
Пример #41
0
    def SimpleSubscribe(self):

        self.subscribe = KThread(target=self._SimpleSubscribeThread)
        self.subscribe.start()
Пример #42
0
def acceptor(server, data, addr):
    Msg = pickle.loads(data)
    _type = Msg.type

    # deal with client's message
    if _type == 'client' or _type == 'redirect':
        msg_string = Msg.request_msg
        if _type == 'redirect':
            addr = Msg.addr
        if msg_string == 'show':
            show_command(server, Msg, addr)

        else:
            if server.role == 'leader':
                if buy_ticket(server, Msg, addr) == 0:
                    return
            # we need to redirect the request to leader
            else:
                fun_redirect(server, Msg, addr)
        return

    _sender = Msg.sender
    _term = Msg.term

    if _type == 1:  # requestvote message
        if _sender not in server.peers:
            return
        vote_response(server, Msg)

    elif _type == 2:  # Vote response message
        voteGranted = int(Msg.data)
        print '---------Get vote response message---------'
        if voteGranted:
            if server.role == 'candidate':
                server.numVotes += 1
                server.request_votes.remove(_sender)
                if server.numVotes == server.majority:
                    print 'Get majority votes, become leader at Term ', server.currentTerm
                    if server.election.is_alive():
                        server.election.kill()
                    # becomes a leader
                    server.follower_state.kill()
                    server.role = 'leader'
                    server.leader_state = KThread(target=server.leader,
                                                  args=())
                    server.leader_state.start()

        else:
            print 'vote rejected by ', _sender
            if server.currentTerm < _term:  # discover higher term
                server.currentTerm = _term
                serverConfig = ServerConfig(server.poolsize,
                                            server.currentTerm,
                                            server.votedFor, server.log,
                                            server.peers)
                with open(server.config_file, 'w') as f:
                    pickle.dump(serverConfig, f)
                if server.role == 'candidate':
                    server.step_down()

    elif _type == 0:  # AppendEntries msg
        """---->>> Done till here <<<---"""
        # print '---------Get AppendEntries message---------'
        prevLogTerm = Msg.prevLogTerm
        leaderCommit = Msg.commitIndex
        matchIndex = server.commitIndex
        prevLogIndex = Msg.prevLogIndex
        entries = Msg.entries

        # This is a valid new leader
        if server.currentTerm <= _term:
            server.currentTerm = _term
            serverConfig = ServerConfig(server.poolsize, server.currentTerm,
                                        server.votedFor, server.log,
                                        server.peers)
            with open(server.config_file, 'w') as f:
                pickle.dump(serverConfig, f)
            server.step_down()
            if server.role == 'follower':
                server.last_update = time.time()
            if prevLogIndex != 0:
                if len(server.log) >= prevLogIndex:
                    if server.log[prevLogIndex - 1].term == prevLogTerm:
                        success = 'True'
                        server.leaderID = _sender
                        if len(entries) != 0:
                            server.log = server.log[:prevLogIndex] + entries
                            matchIndex = len(server.log)
                            if entries[0].type == 1:
                                server.new = entries[0].command.new_config[:]
                                server.old = server.peers[:]
                                server.old.append(server.id)
                                server.peers = list(
                                    set(server.old + server.new))
                                server.peers.remove(server.id)
                            elif entries[0].type == 2:
                                server.new = entries[0].command.new_config[:]
                                server.peers = server.new[:]
                                server.peers.remove(server.id)
                                print 'follower applied new config, running peers', server.peers
                                serverConfig = ServerConfig(
                                    server.poolsize, server.currentTerm,
                                    server.votedFor, server.log, server.peers)
                                with open(server.config_file, 'w') as f:
                                    pickle.dump(serverConfig, f)
                    else:
                        success = 'False'
                else:
                    success = 'False'
            else:
                success = 'True'
                if len(entries) != 0:
                    server.log = server.log[:prevLogIndex] + entries
                    if entries[0].type == 1:
                        server.new = entries[0].command.new_config[:]
                        server.old = server.peers[:]
                        server.old.append(server.id)
                        server.peers = list(set(server.old + server.new))
                        server.peers.remove(server.id)
                    elif entries[0].type == 2:
                        server.new = entries[0].command.new_config[:]
                        server.peers = server.new[:]
                        server.peers.remove(server.id)
                        print 'follower applied new config, running peers', server.peers

                    serverConfig = ServerConfig(server.poolsize,
                                                server.currentTerm,
                                                server.votedFor, server.log,
                                                server.peers)
                    with open(server.config_file, 'w') as f:
                        pickle.dump(serverConfig, f)
                    matchIndex = len(server.log)
                server.leaderID = _sender
        else:
            success = 'False'

        if leaderCommit > server.commitIndex:
            lastApplied = server.commitIndex
            server.commitIndex = min(leaderCommit, len(server.log))
            if server.commitIndex > lastApplied:
                server.poolsize = server.initial_state
                for idx in range(1, server.commitIndex + 1):
                    if server.log[idx - 1].type == 0:
                        server.poolsize -= server.log[idx - 1].command

        reply_msg = AppendEntriesResponseMsg(server.id, _sender,
                                             server.currentTerm, success,
                                             matchIndex)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto(pickle.dumps(reply_msg), ("", server.addressbook[_sender]))

    elif _type == 3:  # AppendEntriesResponse:
        #print '---------Get AppendEntries Response message---------'
        success = Msg.success
        matchIndex = Msg.matchIndex

        if success == 'False':
            if _term > server.currentTerm:
                server.currentTerm = _term
                serverConfig = ServerConfig(server.poolsize,
                                            server.currentTerm,
                                            server.votedFor, server.log,
                                            server.peers)
                with open(server.config_file, 'w') as f:
                    pickle.dump(serverConfig, f)
                server.step_down()
            else:
                server.nextIndex[_sender] -= 1
        else:
            if server.nextIndex[_sender] <= len(
                    server.log) and matchIndex > server.matchIndex[_sender]:
                server.matchIndex[_sender] = matchIndex
                server.nextIndex[_sender] += 1

            if server.commitIndex < max(server.matchIndex.values()):
                start = server.commitIndex + 1
                for N in range(start, max(server.matchIndex.values()) + 1):
                    # not in config change
                    compare = 1
                    for key, item in server.matchIndex.items():
                        if key in server.peers and item >= N:
                            compare += 1
                    majority = (len(server.peers) + 1) / 2 + 1
                    if compare == server.majority and server.log[
                            N - 1].term == server.currentTerm:
                        for idx in range(server.commitIndex + 1, N + 1):
                            server.poolsize -= server.log[idx - 1].command
                            serverConfig = ServerConfig(
                                server.poolsize, server.currentTerm,
                                server.votedFor, server.log, server.peers)
                            with open(server.config_file, 'w') as f:
                                pickle.dump(serverConfig, f)
                            s = socket.socket(socket.AF_INET,
                                              socket.SOCK_DGRAM)
                            s.sendto('Your request is fullfilled',
                                     server.log[idx - 1].addr)
                            s.close()
                            print 'reply once'
                        server.commitIndex = N