Esempio n. 1
0
 def load_entry(self, line):
     line = line.strip()
     # path
     pos = line.find(':')
     if pos == -1:
         raise IOError("Invalid xdfmeta file! (no colon in line)")
     path = line[:pos].decode("UTF-8")
     # prot
     line = line[pos + 1:]
     pos = line.find(',')
     if pos == -1:
         raise IOError("Invalid xdfmeta file! (no first comma)")
     prot_str = line[:pos]
     prot = ProtectFlags()
     prot.parse(prot_str)
     # time
     line = line[pos + 1:]
     pos = line.find(',')
     if pos == -1:
         raise IOError("Invalid xdfmeta file! (no second comma)")
     time_str = line[:pos]
     time = TimeStamp()
     time.parse(time_str)
     # comment
     comment = FSString(line[pos + 1:].decode("UTF-8"))
     # meta info
     mi = MetaInfo(protect_flags=prot, mod_ts=time, comment=comment)
     self.set_meta_info(path, mi)
Esempio n. 2
0
    def __init__(self, row=1, col=20, option=DataplotOption.TIMESTAMP_AUTO, time_type='millis'):
        """Constructor

        @param row: y axis data ~ data for 1 measurand
        @param col: x axis data ~ data reading point (numerical or time ticks in seconds or milliseconds)
        @param option: timestamp enable/disable auto/custom
        @param row: y data ~ data for 1 measurand

        @return:  Description
        @rtype :  Type

        @raise e:  Description
        """
        self.row = row
        self.col = col
        self.data_regs = []
        self.time_ticks = []
        # TODO: use numpy to save data in matrix
        self.data = []
        for i in range(row):
            self.data_regs.append(deque(maxlen=col))
        self.option = option
        if self.option == DataplotOption.TIMESTAMP_AUTO:
            self.timestamp = TimeStamp(time_type=time_type)
            self.time_ticks = deque(maxlen=col)
        if self.option == DataplotOption.TIMESTAMP_CUSTOM:
            self.row = len(self.data_regs)
            self.time_ticks = deque(maxlen=col)
Esempio n. 3
0
 def __init__(self,
              channel_name,
              ip,
              port,
              timeout=10.0,
              latency_threshold=1000):
     '''
     The constructor of the server. It takes ip and port number from user input
     '''
     self.channel_name = channel_name
     self.name = channel_name + "_server"
     self.ip = ip
     self.port = port
     self.timeout = timeout
     self.latency_threshold = latency_threshold  # latency threshold in millyseconds
     self.comm_dict = {
     }  # a look up table linking client name and corresponding communication channel
     self.server_socket = socket.socket(
         socket.AF_INET, socket.SOCK_STREAM)  # initiate basic socket
     self.server_socket.bind(
         (self.ip, self.port)
     )  # bind the server socket to the host ip and the corresponding port
     self.server_socket.listen(5)  # let the socket listening on the network
     self.ts = TimeStamp(self.name)
     self.rrt = 0
     self.clients = []
Esempio n. 4
0
    def tpc_begin(self, transaction, tid=None, status=' '):
        if self._is_read_only:
            raise POSException.ReadOnlyError()
        self._lock_acquire()
        try:
            if self._transaction is transaction:
                return
            self._lock_release()
            self._commit_lock_acquire()
            self._lock_acquire()
            self._transaction = transaction
            self._clear_temp()

            user = transaction.user
            desc = transaction.description
            ext = transaction._extension
            if ext:
                ext = cPickle.dumps(ext, 1)
            else:
                ext = ""
            self._ude = user, desc, ext

            if tid is None:
                now = time.time()
                t = TimeStamp(*(time.gmtime(now)[:5] + (now % 60,)))
                self._ts = t = t.laterThan(self._ts)
                self._serial = `t`
            else:
                self._ts = TimeStamp(tid)
                self._serial = tid

            self._tstatus = status
            self._begin(self._serial, user, desc, ext)
        finally:
            self._lock_release()
Esempio n. 5
0
 def load_entry(self, line):
   line = line.strip()
   # path
   pos = line.find(':')
   if pos == -1:
     raise IOError("Invalid xdfmeta file! (no colon in line)")
   path = line[:pos]
   # prot
   line = line[pos+1:]
   pos = line.find(',')
   if pos == -1:
     raise IOError("Invalid xdfmeta file! (no first comma)")
   prot_str = line[:pos]
   prot = ProtectFlags()
   prot.parse(prot_str)
   # time
   line = line[pos+1:]
   pos = line.find(',')
   if pos == -1:
     raise IOError("Invalid xdfmeta file! (no second comma)")
   time_str = line[:pos]
   time = TimeStamp()
   time.parse(time_str)
   # comment
   comment = line[pos+1:]
   # meta info
   mi = MetaInfo(protect_flags=prot, mod_ts=time, comment=comment)
   self.set_meta_info(path, mi)
Esempio n. 6
0
 def __init__(self, c_sock, c_addr, file='out/serverTimeData.txt'):
     threading.Thread.__init__(self)                         # create a new thread
     self._socket = c_sock
     self._addr = c_addr
     self._socket.setblocking(False)                         # non-blocking mode
     print('A Client has connected at address:\t', self._addr)
     self._ts = TimeStamp(datetime.datetime.now())
     self._file = open(file, 'a')
Esempio n. 7
0
 def __init__(self, name, ip, port, timeout=10.0):
     self.name = name
     self.ip = ip
     self.port = port
     self.timeout = timeout
     self.Client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.connected = False
     self.channel_name = None
     self.ts = TimeStamp(self.name)
     self._rrt = 0
Esempio n. 8
0
 def load_header(self, line):
     pos = line.find(':')
     if pos == -1:
         raise IOError("Invalid xdfmeta header! (no colon in line)")
     # first extract volume name
     vol_name = line[:pos]
     self.vol_name = vol_name.decode("UTF-8")
     line = line[pos + 1:]
     # now get parameters
     comp = line.split(',')
     if len(comp) != 4:
         raise IOError(
             "Invalid xdfmeta header! (wrong number of parameters found)")
     # first dos type
     dos_type_str = comp[0]
     if len(dos_type_str) != 4:
         raise IOError("Invalid xdfmeta dostype string")
     num = ord(dos_type_str[3]) - ord('0')
     if num < 0 or num > 5:
         raise IOError("Invalid xdfmeta dostype number")
     self.dos_type = DosType.DOS0 + num
     # then time stamps
     create_ts = TimeStamp()
     ok1 = create_ts.parse(comp[1])
     disk_ts = TimeStamp()
     ok2 = disk_ts.parse(comp[2])
     mod_ts = TimeStamp()
     ok3 = mod_ts.parse(comp[3])
     if not ok1 or not ok2 or not ok3:
         raise IOError("Invalid xdfmeta header! (invalid timestamp found)")
     self.vol_meta = RootMetaInfo(create_ts, disk_ts, mod_ts)
Esempio n. 9
0
 def __init__(self, id, host=cmd.host, port=cmd.port):
     threading.Thread.__init__(self)  # create a new thread
     self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self._socket.connect((host, port))
     self._socket.setblocking(False)  # non-blocking mode
     self._host_ip = host
     self._my_ip = socket.gethostbyname(socket.gethostname())
     self._ts = TimeStamp(datetime.datetime.now())
     self._file = open(fname0 + str(id) + fname1,
                       'a')  # append to this file
     self._peer = PeerNetwork(id,
                              self._file.name)  # has a listener for peers
     self._id = id
     self._me_str = 'Client ' + str(self._id) + ' '
Esempio n. 10
0
 def set_mod_time(self, mod_time):
   self.mod_time = mod_time
   if self.mod_time != None:
     self.mod_ts = TimeStamp()
     self.mod_ts.from_secs(mod_time)
   else:
     self.mod_ts = None
Esempio n. 11
0
 def set_disk_time(self, disk_time):
   self.disk_time = disk_time
   if self.disk_time != None:
     self.disk_ts = TimeStamp()
     self.disk_ts.from_secs(disk_time)
   else:
     self.disk_ts = None
Esempio n. 12
0
 def set_create_time(self, create_time):
   self.create_time = create_time
   if self.create_time != None:
     self.create_ts = TimeStamp()
     self.create_ts.from_secs(create_time)
   else:
     self.create_ts = None
Esempio n. 13
0
class CThread(threading.Thread):
    def __init__(self, c_sock, c_addr, file='out/serverTimeData.txt'):
        threading.Thread.__init__(self)                         # create a new thread
        self._socket = c_sock
        self._addr = c_addr
        self._socket.setblocking(False)                         # non-blocking mode
        print('A Client has connected at address:\t', self._addr)
        self._ts = TimeStamp(datetime.datetime.now())
        self._file = open(file, 'a')

    """ Send a TimeStamp, if it is time to do so. """
    def _send(self):
        msg = self._ts.message(self._addr, socket.gethostbyname(socket.gethostname()), datetime.datetime.now())
        if msg is not None:
            try:
                self._socket.sendall(bytes(msg, 'UTF-8'))           # time to send the TimeStamp
                self._file.write(msg + '\n')                        # write the TimeStamp to file
            except ConnectionAbortedError:
                print('A Client has disconnected from:\t', self._addr)
                return -1
            except ConnectionResetError:
                print('A Client has forcibly disconnected from:\t', self._addr)
                return -1
        return 0

    """ Receive a TimeStamp, if it has been sent, without blocking the thread. """
    def _receive(self, buff_size=2048):
        try:
            msg = self._socket.recv(buff_size).decode()
            self._file.write(cmd.drop_commands_for_ts(msg) + '\n')
            if cmd.got_server(msg):                             # save the ip_addr:port of the client's server
                client_addr.append(cmd.parse(msg))
                client_thread.append(self)
            if cmd.wants_peers(msg):                            # send the list of peers for the client to connect
                self._socket.sendall(bytes(cmd.client_to_str(client_addr), 'UTF-8'))
        except socket.error:
            pass

    """ Override Thread.run() to send/receive messages through the socket. """
    def run(self, buff_size=2048):
        while True:
            if self._send() < 0:                                # keep sending TimeStamps?
                self.exit()
                return
            self._receive()                                     # get a TimeStamp

    """ Has this connection closed? """
    def test(self):
        try:
            self._socket.send(cmd.test)
        except:
            return True
        return False

    """ Close the connection and file. """
    def exit(self):
        self._socket.close()
        self._file.close()
    def __init__(self, camera):
        super(VideoCapture, self).__init__(camera)
        self.frameQ = queue.Queue() # queue to store incoming video frame
        self.msgQ = queue.PriorityQueue() # priority queue to store processing result
        self.resultFrameQ = queue.PriorityQueue() # priority queue to store processed frame
        self.terminate = mt.Event() # a threading event (flag) to notify all threads the end of the process

        self.t_pring_msg = mt.Thread(target=self.print_msg).start() # thread to show processing result concurrently
        self.t_visual = mt.Thread(target=self.show).start() # thread to show processed frame concurrently

        self.ts = TimeStamp("VideoCapture TS") # time-stamp object
        self.cap_frame_count = 0
        self.cap_start = None

        # lists to store the excecuting time (in msec) of different funcitons
        self.time_collector_frameCap = []
        self.time_collector_print = []
        self.time_collector_show = []
Esempio n. 15
0
 def load_header(self, line):
   pos = line.find(':')
   if pos == -1:
     raise IOError("Invalid xdfmeta header! (no colon in line)")
   # first extract volume name
   self.vol_name = line[:pos]
   line = line[pos+1:]
   # now get parameters
   comp = line.split(',')
   if len(comp) != 4:
     raise IOError("Invalid xdfmeta header! (wrong number of parameters found)")
   # first dos type  
   dos_type_str = comp[0]
   if len(dos_type_str) != 4:
     raise IOError("Invalid xdfmeta dostype string")
   num = ord(dos_type_str[3]) - ord('0')
   if num < 0 or num > 5:
     raise IOError("Invalid xdfmeta dostype number")
   self.dos_type = DosType.DOS0 + num
   # then time stamps
   create_ts = TimeStamp()
   ok1 = create_ts.parse(comp[1])
   disk_ts = TimeStamp()
   ok2 = disk_ts.parse(comp[2])
   mod_ts = TimeStamp()
   ok3 = mod_ts.parse(comp[3])
   if not ok1 or not ok2 or not ok3:
     raise IOError("Invalid xdfmeta header! (invalid timestamp found)")
   self.vol_meta = RootMetaInfo(create_ts, disk_ts, mod_ts)
Esempio n. 16
0
 def __init__(self, name, owner, terminate, frameQ, msgQ, resultFrameQ):
     super(FrameProcessor, self).__init__()
     self.name = name
     self.event = mt.Event()
     self.owner = owner
     self.terminate = terminate
     self.msgQ = msgQ
     self.frameQ = frameQ
     self.resultFrameQ = resultFrameQ
     self.ts = TimeStamp("FrameProcessor")
     self.font = cv2.FONT_HERSHEY_SIMPLEX
     self.time_collector = []
     self.start()
Esempio n. 17
0
def ConvertToSpectrogram(filepath, seconds=None, pieces=None, subdir=True):
	# get details about the file
	command = f"{SOX_PATH} --i \"{filepath}\""
	output = subprocess.check_output(command)
	details = soxiMatch(output)

	# decide how long the snapshots should be
	increment = 5000  # default is 5 seconds
	if seconds:
		increment = seconds * 1000
	if pieces:
		increment = (details["Samples"] // details["SampleRate"] // pieces) * 1000

	directory, name, ext = splitDirNameExt(filepath)
	# destination should look like: D:\Music\SongName\
	dest_dir = directory
	if subdir:
		dest_dir = buildDirPath(directory, name)
	makeDirectory(dest_dir)

	begin = TimeStamp(0)
	step = TimeStamp(increment)
	end = TimeStamp.parse(details["Duration"])
	i = 1
	pad = paddingZeros(ceil(end // step))
	# it does not matter if (step > end)
	while begin < end:
		for channel in range(1, details["Channels"] + 1):
			dest_file = buildDirPath(dest_dir, f"{name}_ch{channel}_part{i:0{pad}}.png")
			# -n (I don't know why, but it needs this flag)
			# remix {channel} (select a specific channel)
			# -S {begin} (process starting with the specified position)
			# -d {step} (duration of the segment)
			# -m (Spectrogram will be monochrome)
			# -r (Raw spectrogram: suppress the display of axes and legends)
			# -o (Name of the spectrogram output PNG file)
			command = f"\"{SOX_PATH}\" \"{filepath}\" -n " \
					  f"remix {channel} " \
					  f"spectrogram -S {begin} -d {step} -m -r -o \"{dest_file}\""
			subprocess.run(command)

		# increment stuff (note: in the main loop)
		begin.add(step)
		i += 1
Esempio n. 18
0
def readFromTimeStampSKR(firstTimeStampIn):

    # read the first line of QKDSequece.log
    line = datafile.readline()
    print("Reading SKR data from QKDSequence.log file...")

    # list to hold QBER data entries
    dataEntriesSKR = []

    # pass each line in file until program reaches first timestamp of interest
    while line != "":
        entryComps = line.split("INFO")

        # process timestamp of each entry
        timeStampComps = entryComps[0].split()
        calendarComps = timeStampComps[0].split("-")
        year = int(calendarComps[0])
        month = int(calendarComps[1])
        day = int(calendarComps[2])
        timeComps = timeStampComps[1].split(":")
        hour = int(timeComps[0])
        minute = int(timeComps[1])
        second = int(timeComps[2])

        entryTimestamp = TimeStamp(year, month, day, hour, minute, second)

        if entryTimestamp.__str__() == enteredTimestamp.__str__():
            break
        line = datafile.readline()

    # read each line until the end of the file
    while line != "":

        # split the entry into a timestamp and a message
        entryComps = line.split("INFO")

        # process timestamp of each entry
        timeStampComps = entryComps[0].split()
        calendarComps = timeStampComps[0].split("-")
        year = int(calendarComps[0])
        month = int(calendarComps[1])
        day = int(calendarComps[2])
        timeComps = timeStampComps[1].split(":")
        hour = int(timeComps[0])
        minute = int(timeComps[1])
        second = int(timeComps[2])

        entryTimestamp = TimeStamp(year, month, day, hour, minute, second)

        # Not all message types follow the same structure. If the entry
        # components list is only of length 1, it is not SKR data and can be ignored
        if len(entryComps) > 1:
            # split the message into message type and message title/value
            messageComps = entryComps[1].split(":")

        # make sure that there is more than one component in the message
        if len(messageComps) > 1:
            # if the entry contains secret key rate data, record this entry
            if "Secret Key Rate" in messageComps[1]:
                # splits privacy amplification data into title of value and value
                type = "Secret Key Rate"

                # extract the SKR value from the entry
                titleAndValue = messageComps[1].split("=")
                valueAndUnit = titleAndValue[1].split()
                value = float(valueAndUnit[0])
                entrySKR = DataEntry(entryTimestamp, type, value)
                dataEntriesSKR.append(entrySKR)

        # read next line of data
        line = datafile.readline()

    datafile.close()

    # Create a file to store SKR Data
    SKRfile = open("SKR.txt", "w+")

    for entry in dataEntriesSKR:
        SKRfile.write(str(entry.timestamp.getRelativeSeconds(enteredTimestamp)) \
          + "\t" + str(entry.value) + "\n")
        print(str(entry))

    SKRfile.close()
Esempio n. 19
0
def readFromStartSKR():

    # read the first line
    line = datafile.readline()

    dataEntriesSKR = []

    # variables used to save first timestamp of file in order to calculate the
    # relative time between a given entry and the first entry in the file
    first = True
    firstTimeStamp = None

    # read each line until the end of the file
    while line != "":

        # split the entry into a timestamp and a message
        entryComps = line.split("INFO")

        # process timestamp of each entry
        timeStampComps = entryComps[0].split()
        calendarComps = timeStampComps[0].split("-")
        year = int(calendarComps[0])
        month = int(calendarComps[1])
        day = int(calendarComps[2])
        timeComps = timeStampComps[1].split(":")
        hour = int(timeComps[0])
        minute = int(timeComps[1])
        second = int(timeComps[2])

        entryTimestamp = TimeStamp(year, month, day, hour, minute, second)

        # save the timestamp of the first entry
        if first:
            firstTimestamp = entryTimestamp
            first = False

        # Not all message types follow the same structure. If the entry
        # components list is only of length 1, it is not SKR data and can be ignored
        if len(entryComps) > 1:
            # split the message into message type and message title/value
            messageComps = entryComps[1].split(":")

        # if the entry contains secret key rate data, record this entry
        if "Secret Key Rate" in messageComps[1]:
            # splits privacy amplification data into title of value and value
            type = "Secret Key Rate"

            # extract the SKR value from the entry
            titleAndValue = messageComps[1].split("=")
            valueAndUnit = titleAndValue[1].split()
            value = float(valueAndUnit[0])
            entrySKR = DataEntry(entryTimestamp, type, value)
            dataEntriesSKR.append(entrySKR)

        # read next line of data
        line = datafile.readline()

    datafile.close()

    # Create a file to store SKR Data
    SKRfile = open("SKR.txt", "w+")

    for entry in dataEntriesSKR:
        SKRfile.write(str(entry.timestamp.getRelativeSeconds(firstTimestamp)) \
          + "\t" + str(entry.value) + "\n")
        print(str(entry))

    SKRfile.close()
Esempio n. 20
0
# program will read from QKDSequence.log file
datafile = open("QKDSequence.log", "r")

# prompt user to choose first timestamp of interest or read from the beginning
# of the file
userInput = input("Would you like to read from the beginning of the " \
    + "QKDSequece.log file? ('y' or 'n'): ")
if userInput == "y":
    readFromStartSKR()
elif userInput == "n":

    # prompt user to enter the first timestamp of interest
    output = "Enter the following information regrading the first timestamp " \
        + "of interest..."
    print(output)

    inputYear = input("Year (4 digits please!): ")
    inputMonth = input("Month (Ex. February = 02): ")
    inputDay = input("Day: ")
    inputHour = input("Hour (military time): ")
    inputMinute = input("Minute: ")
    inputSecond = input("Second: ")

    enteredTimestamp = TimeStamp(inputYear, inputMonth, inputDay, inputHour, \
        inputMinute, inputSecond)
    readFromTimeStampSKR(enteredTimestamp)

else:
    print("Invalid Response: ")
class VideoCapture(PiRGBAnalysis):
    '''
    A costum, file-like output. It is a subclass of PiRGBAnalysis.
    The analyse function will provide an np.array for image processing
    '''

    def __init__(self, camera):
        super(VideoCapture, self).__init__(camera)
        self.frameQ = queue.Queue() # queue to store incoming video frame
        self.msgQ = queue.PriorityQueue() # priority queue to store processing result
        self.resultFrameQ = queue.PriorityQueue() # priority queue to store processed frame
        self.terminate = mt.Event() # a threading event (flag) to notify all threads the end of the process

        self.t_pring_msg = mt.Thread(target=self.print_msg).start() # thread to show processing result concurrently
        self.t_visual = mt.Thread(target=self.show).start() # thread to show processed frame concurrently

        self.ts = TimeStamp("VideoCapture TS") # time-stamp object
        self.cap_frame_count = 0
        self.cap_start = None

        # lists to store the excecuting time (in msec) of different funcitons
        self.time_collector_frameCap = []
        self.time_collector_print = []
        self.time_collector_show = []

    def analyse(self, frame):
        frame_time = 1000*self.ts.time("second")+self.ts.time("msec")

        ## uncomment this to shoe frame capture fps
        # if self.cap_frame_count == 0:
        #     self.cap_start = time.time()
        # self.cap_frame_count += 1

        time_frame_tuple = (frame_time, frame)
        self.process(time_frame_tuple)

        ## uncomment this to shoe frame capture fps
        # if time.time() - self.cap_start >= 1:
        #     print(f"[{self.ts.time()}][analyse] capture fps: {self.cap_frame_count}")
        #     self.cap_frame_count = 0
        


    def print_msg(self):
        while not self.terminate.is_set():
            try:
                _, msg = self.msgQ.get()
                print(f"[{self.ts.time()}][print_msg] {msg}")
            except:
                pass

    def show(self):
        # cv2.namedWindow("result", cv2.WINDOW_NORMAL)
        # cv2.resizeWindow("result", self.frame_shape[0]//2, self.frame_shape[1]//2)

        while not self.terminate.is_set():
            try:
                _, result_frame = self.resultFrameQ.get_nowait()
                cv2.imshow("result", result_frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    self.terminate.set()
            except:
                pass
    
    def process(self, time_frame_tuple):
       '''
        The frame processing function
        '''
        incoming_time, frame = time_frame_tuple
        org_frame = frame.copy() # copy an original frame for further usage
        
        #### 
        # Implement your frmae process here...
        process_message = None 
        ###
        
        try:
            self.msgQ.put_nowait((incoming_time, process_message))
            self.resultFrameQ.put_nowait((incoming_time, org_frame))
        except:
            pass
Esempio n. 22
0
class Server:
    def __init__(self,
                 channel_name,
                 ip,
                 port,
                 timeout=10.0,
                 latency_threshold=1000):
        '''
        The constructor of the server. It takes ip and port number from user input
        '''
        self.channel_name = channel_name
        self.name = channel_name + "_server"
        self.ip = ip
        self.port = port
        self.timeout = timeout
        self.latency_threshold = latency_threshold  # latency threshold in millyseconds
        self.comm_dict = {
        }  # a look up table linking client name and corresponding communication channel
        self.server_socket = socket.socket(
            socket.AF_INET, socket.SOCK_STREAM)  # initiate basic socket
        self.server_socket.bind(
            (self.ip, self.port)
        )  # bind the server socket to the host ip and the corresponding port
        self.server_socket.listen(5)  # let the socket listening on the network
        self.ts = TimeStamp(self.name)
        self.rrt = 0
        self.clients = []

    def estabish_connection(self):

        try:
            print(
                f"[{self.ts.time()}][estabish_connection] Number of connected client(s): {len(self.comm_dict)}. Listening to estabish connection..."
            )
            conn2client, client_address = self.server_socket.accept(
            )  # block until server accept the connection to client
            client_ip = client_address[0]
            print(
                f"[{self.ts.time()}][estabish_connection] client_ip = {client_ip}"
            )
            self.comm_dict[client_ip] = conn2client
            print(
                f"[{self.ts.time()}][estabish_connection] Connectted by {client_address}. Number of connected client(s): {len(self.comm_dict)}"
            )
            return True, conn2client, client_address

        except socket.error as msg:  # if the initial sever socket encouter an error
            print(
                '\033[91m' +
                f"[{self.ts.time()}][estabish_connection] Error when establish connection: {msg}. Closing the server..."
                + '\033[39m')
            self.server_socket.close()  # close the initial sever socket
            return False, None, None

    def comm_quality_check(self, client_ip):

        conn2client = self.comm_dict[client_ip]
        server_check_msg = f"{self.channel_name}#{self.ts.time()}#{self.name}#".ljust(
            128, ' ')
        conn2client.sendall(bytes(server_check_msg, 'utf-8'))  # first sending
        rrt_start = datetime.now()
        try:
            data = conn2client.recv(128)  # first receiving (block)
            self.rrt = (datetime.now() - rrt_start) / timedelta(milliseconds=1)
            received_time = self.ts.time()
            client_msgs = str(data, 'utf-8').split('#')[:-1]
            print(f"[{self.ts.time()}][Server] client_msgs = {client_msgs}")
            client_name = client_msgs[0]
            client_ts = client_msgs[1]
            print(
                f"[{self.ts.time()}][{self.name}] Checking communication qulity to {client_name}..."
            )

            reply_msg = f"{self.channel_name}#{self.ts.time()}#{received_time}#{self.rrt}#".ljust(
                128, ' ')
            conn2client.sendall(bytes(reply_msg, 'utf-8'))  # second sending
            print(
                f"[{self.ts.time()}][{self.name}] Round trip time (rrt) = {self.rrt}"
            )
            if self.rrt >= self.latency_threshold:
                # show in yellow string if the latency exceeds the threshold
                print(
                    '\033[93m' +
                    f'[{self.ts.time()}][Server] The conneciton delay to {client_name} is over {self.latency_threshold} ms'
                    + '\033[39m')
            return True, client_name
        except socket.error as msg:  # if error happens
            print(
                '\033[91m' +
                f"[{self.ts.time()}] Error when checking communication quality: {msg} ..."
                + '\033[39m')
            return False, None

    def recv(self, client_ip):
        '''
        recv+
        '''
        commObj = self.comm_dict[client_ip]
        data = commObj.recv(128)
        server_min, server_sec, server_msec = self.ts.time(
            ["minute", "second", "msec"])
        result = [None, (server_min, server_sec), None, None]
        if data:  # if there is message sent by client

            client_msgs = str(
                data,
                'utf-8').split('#')[:-1]  # received and decode the message
            client_name = client_msgs[0]
            client_TimeStamp = client_msgs[
                1]  # extract the local time of the server when sending message
            result[0] = client_name
            detect_result = client_msgs[2:]

            if len(detect_result) == 2:
                # which means there should be position and orientation info
                # return [client_name, (server_min, server_sec), detect_position, detect_orientation]

                ## convert the time the client send the message and the time server received the message
                ## to seconds, then subtracting them to find the latency
                # client_min = int(client_TimeStamp[-9:-7])
                # client_sec = int(client_TimeStamp[-6:-4])
                # client_msec = int(client_TimeStamp[-3:])
                # client_t = (client_min*60 + client_sec)*1000 + client_msec
                # server_t = (server_min*60 + server_sec)*1000 + server_msec
                # latency = abs(client_t - server_t)
                # if (latency - self.rrt/2) >= self.latency_threshold: # show in yellow string if the latency exceeds the threshold
                #     print('\033[93m' +
                #     f'[{self.ts.time()}][recv] The conneciton delay to {client_name} is over {self.latency_threshold} second(s)'
                #     + '\033[39m')

                detect_position, detect_orientation = detect_result[
                    0], detect_result[1]
                detect_position = np.fromstring(detect_position[1:-1],
                                                dtype=float,
                                                sep=" ")
                result[2] = detect_position
                detect_orientation = np.fromstring(detect_orientation[1:-1],
                                                   dtype=float,
                                                   sep=" ")
                result[3] = detect_orientation

            else:
                # print(f"[{self.ts.time()}][recv] No detect result")
                result[2] = detect_result

        else:  # return [None, (server_min, server_sec), None, None]
            print(
                '\033[91m' +
                f'[{self.ts.time()}][recv] The conneciton to has been closed' +
                '\033[39m')

        # print(f"[{self.ts.time()}][recv] {client_name} result = {result}")
        return result

    def send(self, message):
        msg2client = f"{self.channel_name}#{self.ts.time()}#{message}#".ljust(
            128, ' ')
        print(f"[{self.ts.time()}][send] Send instruction {msg2client}")
        for client in self.comm_dict:
            self.comm_dict[client].send(bytes(msg2client, 'utf-8'))

    def request_close_client(self, conn2client, client_address, reason):
        request_close_client_msg = \
          f"Server requests client {client_address} to close due to {reason}"
        print(request_close_client_msg)
        conn2client.sendall(bytes(request_close_client_msg, 'utf-8'))
Esempio n. 23
0
class RootMetaInfo:
  def __init__(self, create_ts=None, disk_ts=None, mod_ts=None):
    self.set_create_ts(create_ts)
    self.set_disk_ts(disk_ts)
    self.set_mod_ts(mod_ts)
  
  def __str__(self):
    res = []
    res.append(self.get_create_time_str())
    res.append(self.get_disk_time_str())
    res.append(self.get_mod_time_str())
    return "  ".join(res)
      
  # create_ts
  def set_create_time(self, create_time):
    self.create_time = create_time
    if self.create_time != None:
      self.create_ts = TimeStamp()
      self.create_ts.from_secs(create_time)
    else:
      self.create_ts = None
  
  def set_create_ts(self, create_ts):
    self.create_ts = create_ts
    if self.create_ts != None:
      self.create_time = self.create_ts.get_secsf()
    else:
      self.create_time = None
  
  def get_create_time(self):
    return self.create_time
  
  def get_create_ts(self):
    return self.create_ts

  def get_create_time_str(self):
    if self.create_ts != None:
      return str(self.create_ts)
    else:
      return ts_empty_string

  def set_current_as_create_time(self):
    create_time = time.mktime(time.localtime())
    self.set_create_time(create_time)

  # disk_ts
  def set_disk_time(self, disk_time):
    self.disk_time = disk_time
    if self.disk_time != None:
      self.disk_ts = TimeStamp()
      self.disk_ts.from_secs(disk_time)
    else:
      self.disk_ts = None
  
  def set_disk_ts(self, disk_ts):
    self.disk_ts = disk_ts
    if self.disk_ts != None:
      self.disk_time = self.disk_ts.get_secsf()
    else:
      self.disk_time = None
  
  def get_disk_time(self):
    return self.disk_time
  
  def get_disk_ts(self):
    return self.disk_ts

  def get_disk_time_str(self):
    if self.disk_ts != None:
      return str(self.disk_ts)
    else:
      return ts_empty_string

  def set_current_as_disk_time(self):
    disk_time = time.mktime(time.localtime())
    self.set_disk_time(disk_time)

  # mod_ts
  def set_mod_time(self, mod_time):
    self.mod_time = mod_time
    if self.mod_time != None:
      self.mod_ts = TimeStamp()
      self.mod_ts.from_secs(mod_time)
    else:
      self.mod_ts = None
  
  def set_mod_ts(self, mod_ts):
    self.mod_ts = mod_ts
    if self.mod_ts != None:
      self.mod_time = self.mod_ts.get_secsf()
    else:
      self.mod_time = None
  
  def get_mod_time(self):
    return self.mod_time
  
  def get_mod_ts(self):
    return self.mod_ts
  
  def get_mod_time_str(self):
    if self.mod_ts != None:
      return str(self.mod_ts)
    else:
      return ts_empty_string
 
  def set_current_as_mod_time(self):
    mod_time = time.mktime(time.localtime())
    self.set_mod_time(mod_time)
Esempio n. 24
0
# program will read from rawlogs file
datafile = open("rawlogs.txt", "r")

# prompt user to enter the first timestamp of interest
output = "Enter the following information regrading the first timestamp of " \
    + "interest..."
print(output)

inputYear = input("Year (4 digits please!): ")
inputMonth = input("Month (Ex. February = 02): ")
inputDay = input("Day: ")
inputHour = input("Hour (military time): ")
inputMinute = input("Minute: ")
inputSecond = input("Second: ")

firstTimestamp = TimeStamp(inputYear, inputMonth, inputDay, inputHour, \
    inputMinute, inputSecond)

# read the first line of rawlogs
line = datafile.readline()
print("Reading QBER data from rawlogs file...")

# list to hold QBER data entries
dataEntries = []

# pass each line in rawlogs until program reaches first timestamp of interest
while line != "":
    dataEntryComps = line.split()
    dataTimestamp = dataEntryComps[0]
    if dataTimestamp.__str__() == firstTimestamp.__str__():
        break
    line = datafile.readline()
Esempio n. 25
0
 def change_create_ts_by_string(self, create_ts_str):
   t = TimeStamp()
   t.parse(create_ts_str)
   return self.change_meta_info(RootMetaInfo(create_ts=t))
Esempio n. 26
0
class Client(threading.Thread):
    def __init__(self, id, host=cmd.host, port=cmd.port):
        threading.Thread.__init__(self)  # create a new thread
        self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._socket.connect((host, port))
        self._socket.setblocking(False)  # non-blocking mode
        self._host_ip = host
        self._my_ip = socket.gethostbyname(socket.gethostname())
        self._ts = TimeStamp(datetime.datetime.now())
        self._file = open(fname0 + str(id) + fname1,
                          'a')  # append to this file
        self._peer = PeerNetwork(id,
                                 self._file.name)  # has a listener for peers
        self._id = id
        self._me_str = 'Client ' + str(self._id) + ' '

    """ Send a TimeStamp, if it is time to do so. """

    def _send(self):
        msg = self._ts.message(self._my_ip, self._host_ip,
                               datetime.datetime.now())
        if msg is not None:
            self._socket.sendall(bytes(msg,
                                       'UTF-8'))  # time to send the TimeStamp
            self._file.write(msg + '\n')  # write to file

    """ Receive a TimeStamp, if it has been sent, without blocking the thread. """

    def _receive(self, buff_size=2048):
        try:
            msg = self._socket.recv(buff_size).decode()
            msg = cmd.clean(msg)
            self._file.write(msg + '\n')  # write to file
        except ConnectionAbortedError:
            print('Server has closed the connection.')
            self.exit()
        except ConnectionResetError:
            print('Server has forcibly closed the connection.')
            self.exit()
        except BlockingIOError:
            # print('The connection is waiting for I/O operation.')
            pass

    """ Send the server this Client's listener address. """

    def _invite_peers(self):
        self._socket.sendall(
            bytes(cmd.s_cmd + str(self.get_server_address()), 'UTF-8'))
        print(self._me_str, 'has listener socket at address',
              self.get_server_address())

    """ Connect to Peers in the network, and send the files. """

    def _connect_to_peers(self):
        peer_list = self._get_peers()
        print(self._me_str + str(peer_list) + '\t' + str(len(peer_list)))
        connected = False
        while not connected:  # attempt the connection to the full peer list
            try:
                self._peer.connect_and_send(peer_list)
                connected = True
            except ConnectionRefusedError:
                continue

    """ Get the list of peers from the server in a readable format. """

    def _get_peers(self, buff_size=2048):
        self._socket.sendall(bytes(cmd.g_cmd, 'UTF-8'))
        pl = ''
        while pl == '':  # get the list of Peers' listener addresses
            try:
                msg = self._socket.recv(buff_size).decode()
                pl, ts = cmd.get_pl_and_ts(msg)
                if ts != '':
                    self._file.write(
                        ts + '\n')  # write the TimeStamp to file, if any
            except:
                continue
        return pl  # convert the list to (host, port) tuples

    """ Override Thread.run() to send/receive messages through the socket. """

    def run(self):
        self._peer.start()  # tell the server this Client's listener address
        self._invite_peers()
        while not self._ts.finished(datetime.datetime.now()):
            self._receive()  # get a TimeStamp; exit on error
            self._send()  # send the TimeStamp
        print(self._me_str + 'finished exchanging TimeStamps with the Server.')
        self._connect_to_peers()
        print(self._me_str + 'finished sending TimeStamp file to Peers.')
        # self.exit()

    """ Close the connection and file. """

    def exit(self):
        self._peer.exit()
        self._socket.close()
        self._file.close()
        print(self._me_str, 'disconnected at', datetime.datetime.now())

    def get_server_address(self):
        return self._peer.get_host()
Esempio n. 27
0
 def change_mod_ts_by_string(self, mod_ts_str):
     t = TimeStamp()
     t.parse(mod_ts_str)
     return self.change_meta_info(RootMetaInfo(mod_ts=t))
Esempio n. 28
0
class Client:
    def __init__(self, name, ip, port, timeout=10.0):
        self.name = name
        self.ip = ip
        self.port = port
        self.timeout = timeout
        self.Client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connected = False
        self.channel_name = None
        self.ts = TimeStamp(self.name)
        self._rrt = 0

    def connect2Server(self):
        try:
            self.Client_socket.connect((self.ip, self.port))
            time_connected = self.ts.datetime()

            self.channel_name, _, _ = self.recv()
            receive_time = self.ts.datetime()

            # = channel_name
            print(
                f"[{self.ts.datetime()}][Client][connect2Server] Connection to '{self.channel_name}' server is established at time {time_connected}"
            )
            self.send(receive_time)
            rrt_start = datetime.now()

            _, _, server_rrt = self.recv()
            print(
                f"[{self.ts.datetime()}][Client][connect2Server] server_rrt = {server_rrt}"
            )
            self.rrt = (datetime.now() - rrt_start) / timedelta(milliseconds=1)

            self.connected = True

        except socket.error as err_msg:
            print(
                '\033[91m' +
                f"[{self.ts.datetime()}] Connection to ({self.ip}: {self.port}) cannot establish: {err_msg}"
                + '\033[39m')

    def send(self, message, show_msg=False):
        try:
            # self.Client_socket.sendall(bytes(self.ts.datetime()+message, 'utf-8'))
            msg2send = f"{self.name}#{self.ts.datetime()}#{message}#".ljust(
                128, ' ')
            self.Client_socket.sendall(bytes(msg2send, 'utf-8'))
            if show_msg:
                print(f"[{self.ts.datetime()}] Sent message \"{message}\"")
            return True
        except socket.error as err_msg:
            print(
                '\033[91m' +
                f"[{self.ts.datetime()}] Cannot send message to ({self.ip}: {self.port}): {err_msg}"
                + '\033[39m')
            # self.Client_socket.close() # directly closing due to unavailablility to communicate
            return False

    def recv(self):
        data = self.Client_socket.recv(128)
        if data:
            # msg = f"[{self.ts.datetime()}]" + str(data, 'utf-8')
            msgs = str(data, 'utf-8').split('#')[:-1]
            channel_name = msgs[0]
            sent_time = msgs[1]
            if len(msgs) >= 2:
                msg = msgs[2:]
                if len(msg) == 1 and msg[0] == "END":
                    msg = "END"
            print(f"[{self.ts.datetime()}][Client][recv] msg = {msg}")

            if self.channel_name is not None and channel_name != self.channel_name:
                print(
                    f"[{self.ts.datetime()}][Client][recv] Error: Receive from channel {channel_name}"
                )

            return channel_name, sent_time, msg
        else:
            print(
                f"{self.ts.datetime()}][Client][recv] No raw data receive, return END..."
            )
            return self.channel_name, None, "END"

    def close_connection(self):
        # self.Client_socket.sendall(bytes(f"{self.ts.datetime()}Connection closed from Client", "utf-8"))
        print(f"[{self.ts.datetime()}][Client] Connection close notified")
        self.Client_socket.close()
Esempio n. 29
0
 def change_create_ts_by_string(self, create_ts_str):
     t = TimeStamp()
     t.parse(create_ts_str)
     return self.change_meta_info(RootMetaInfo(create_ts=t))
Esempio n. 30
0
class DataPlot(object):

    """Docstring for DataPlot.
    - Container for Data (e.g. Data from multiple sensor)
    - implement as deque(works like shift register or FIFO)
    - first element in queue is the oldest, last is the newest
    - all data must have the same x-axis !!
    """

    def __init__(self, row=1, col=20, option=DataplotOption.TIMESTAMP_AUTO, time_type='millis'):
        """Constructor

        @param row: y axis data ~ data for 1 measurand
        @param col: x axis data ~ data reading point (numerical or time ticks in seconds or milliseconds)
        @param option: timestamp enable/disable auto/custom
        @param row: y data ~ data for 1 measurand

        @return:  Description
        @rtype :  Type

        @raise e:  Description
        """
        self.row = row
        self.col = col
        self.data_regs = []
        self.time_ticks = []
        # TODO: use numpy to save data in matrix
        self.data = []
        for i in range(row):
            self.data_regs.append(deque(maxlen=col))
        self.option = option
        if self.option == DataplotOption.TIMESTAMP_AUTO:
            self.timestamp = TimeStamp(time_type=time_type)
            self.time_ticks = deque(maxlen=col)
        if self.option == DataplotOption.TIMESTAMP_CUSTOM:
            self.row = len(self.data_regs)
            self.time_ticks = deque(maxlen=col)

    def __str__(self):
        self.get_data_as_matrix()
        if self.option == DataplotOption.TIMESTAMP_AUTO or self.option == DataplotOption.TIMESTAMP_CUSTOM:
            ret = 'data: ' + str(self.data[:-1])
        # last row are time ticks
            ret = ret + ('\ntime: ' + str(self.data[-1]))
        elif self.option == DataplotOption.TIMESTAMP_NONE:
            ret = 'data: ' + str(self.data)
        return ret

    def append(self, y, x=[], single=True):
        """append new data vector to class

        @param y:  vector y=[y0, y1, y2, ...] or matrix y=[y00 y01 y02, y10 y11 y12, ...]
        @type  param:  list

        """
        if self.option != DataplotOption.TIMESTAMP_CUSTOM and np.array(x).size != 0:
            raise Exception(
                DataPlotException.DATAPLOT_APPEND_PARAM_X_NOT_SUPPORTED_BY_OPTION.value)
        if single:
            for i in range(self.row):
                self.data_regs[i].append(y[i])
            if self.option == DataplotOption.TIMESTAMP_AUTO:
                self.time_ticks.append(self.timestamp.tick())
            elif self.option == DataplotOption.TIMESTAMP_CUSTOM:
                self.time_ticks.append(x)
        else:  # multi mode
            for i in range(self.row):
                self.data_regs[i].extend(y[i])
            if self.option == DataplotOption.TIMESTAMP_AUTO:
                # not supported be cause timestamp can generate multiple ticks but it make no sense
                raise Exception(
                    DataPlotException.DATAPLOT_APPEND_MULTIMODE_UNSUPPORTED_BY_OPTION.value)

            # TODO: bug, last
            elif self.option == DataplotOption.TIMESTAMP_CUSTOM:
                if np.array(x).size == 0 and len(y[0]) != 0:
                    raise Exception(
                        DataPlotException.DATAPLOT_APPEND_MULTIMODE_X_NONE_BY_TIMESTAMP_CUSTOM.value)
                elif len(x) != len(y[0]):
                    raise Exception(
                        DataPlotException.DATAPLOT_APPEND_MULTIMODE_X_AND_Y_DIFF_LEN.value)
                else:
                    self.time_ticks.extend(x)

    def get_data_as_matrix(self):
        """get data as 2d list (last row will be time ticks if timestamp enable)

        """
        self.data = []
        for i in range(self.row):
            self.data.append(list(self.data_regs[i]))
        if self.option == DataplotOption.TIMESTAMP_AUTO or self.option == DataplotOption.TIMESTAMP_CUSTOM:
            self.data.append(list(self.time_ticks))
        return self.data

    def get_col(self, index):
        """get data vector at specific index as list

        @param index:  col index
        """
        ret = []
        for i in range(self.row):
            ret.append(list(self.data_regs[i])[index])
        if self.option == DataplotOption.TIMESTAMP_AUTO or self.option == DataplotOption.TIMESTAMP_CUSTOM:
            ret.append(list(self.time_ticks)[index])
        return ret

    def get_row(self, index):
        """get row data (measurand's y_data) as list

        @param index:  row index
        """
        return list(self.data_regs[index])

    def get_time_ticks(self):
        if self.option == DataplotOption.TIMESTAMP_NONE:
            raise Exception(
                DataPlotException.DATAPLOT_GET_TIME_TICKS_NOT_AVAILABLE.value)
        return list(self.time_ticks)

    def clear_data_regs(self):
        for i in range(self.row):
            self.data_regs[i].clear()
        if self.option in [DataplotOption.TIMESTAMP_CUSTOM, DataplotOption.TIMESTAMP_AUTO]:
            self.time_ticks.clear()
Esempio n. 31
0
 def change_mod_ts_by_string(self, tm_str):
   t = TimeStamp()
   t.parse(tm_str)
   self.change_meta_info(MetaInfo(mod_ts=t))
Esempio n. 32
0
 def change_mod_ts_by_string(self, mod_ts_str):
   t = TimeStamp()
   t.parse(mod_ts_str)
   return self.change_meta_info(RootMetaInfo(mod_ts=t))    
Esempio n. 33
0
 def change_mod_ts_by_string(self, tm_str):
     t = TimeStamp()
     t.parse(tm_str)
     self.change_meta_info(MetaInfo(mod_ts=t))
Esempio n. 34
0
    def copyTransactionsFrom(self, other, verbose=0):
        """Copy transactions from another storage.

        This is typically used for converting data from one storage to another.
        """
        _ts=None
        ok=1
        preindex={};
        preget=preindex.get   # waaaa
        # restore() is a new storage API method which has an identical
        # signature to store() except that it does not return anything.
        # Semantically, restore() is also identical to store() except that it
        # doesn't do the ConflictError or VersionLockError consistency
        # checks.  The reason to use restore() over store() in this method is
        # that store() cannot be used to copy transactions spanning a version
        # commit or abort, or over transactional undos.
        #
        # We'll use restore() if it's available, otherwise we'll fall back to
        # using store().  However, if we use store, then
        # copyTransactionsFrom() may fail with VersionLockError or
        # ConflictError.
        if hasattr(self, 'restore'):
            restoring = 1
        else:
            restoring = 0
        fiter = other.iterator()
        for transaction in fiter:
            tid=transaction.tid
            if _ts is None:
                _ts=TimeStamp(tid)
            else:
                t=TimeStamp(tid)
                if t <= _ts:
                    if ok: print ('Time stamps out of order %s, %s' % (_ts, t))
                    ok=0
                    _ts=t.laterThan(_ts)
                    tid=`_ts`
                else:
                    _ts = t
                    if not ok:
                        print ('Time stamps back in order %s' % (t))
                        ok=1

            if verbose: print _ts

            self.tpc_begin(transaction, tid, transaction.status)
            for r in transaction:
                oid=r.oid
                if verbose: print `oid`, r.version, len(r.data)
                if restoring:
                    self.restore(oid, r.serial, r.data, r.version,
                                 r.data_txn, transaction)
                else:
                    pre=preget(oid, None)
                    s=self.store(oid, pre, r.data, r.version, transaction)
                    preindex[oid]=s

            self.tpc_vote(transaction)
            self.tpc_finish(transaction)

        fiter.close()