def run_simulation(self): #First load the packages in package_map due to restrictions # SPACE | TIME COMPLEXITY: # O(N) | O(N^2 LOG N) for truck in self.preset_packages.keys(): current_truck = self.trucks[truck] package_ids = self.preset_packages[truck] for package_id in package_ids: package = self.packages_hash_table.get(package_id) package.truck = current_truck current_truck.load(package) #Now we load priority queues # SPACE | TIME COMPLEXITY: # O(N) | O(N^2 LOG N) for i in range(1, 4): for package in self.packages_hash_table.get_all_priority(i): if not truck1.is_full() and package.truck is None: truck1.load(package) elif not truck2.is_full() and package.truck is None: truck2.load(package) elif not truck3.is_full() and package.truck is None: truck3.load(package) truck1.start_delivery(Time(8, 00)) truck2.start_delivery(Time(8, 00)) truck3.start_delivery(Time(10, 20)) print_delivery_stats()
def deliver_package(self): lowest_distance = 9999 # initialize to a really high number so we can beat it #Go through the priority list (starts with 1 and ends with 3) # SPACE | TIME COMPLEXITY: # O(N) | O(N) for package in self.return_highest_priority_packages(): #Find the closest package we can deliver if return_distance(self.current_location, package.delivery_address) < lowest_distance: lowest_distance = return_distance(self.current_location, package.delivery_address) pop_index = self.packages_loaded.index(package) closest_package = self.packages_loaded[pop_index] #Once we go through every package and find the closest one we can now deliver it and set stats time_to_next_stop = (lowest_distance / self.avg_speed) * 60 self.distance_traveled += lowest_distance self.time_traveling += time_to_next_stop self.current_time.add(time_to_next_stop) closest_package.delivered_at = Time(self.current_time.hour, self.current_time.minute) print( 'Truck{} traveling from {} -> {}, it will travel {} miles and should take {} minutes to get there and arrive at {}' .format(self.id, self.current_location, self.packages_loaded[pop_index].delivery_address, lowest_distance, time_to_next_stop.__round__(2), str(self.current_time))) #if closest_package.priority < 3: # print(" {} has a deadline at {}, it was delivered at {}.".format(closest_package, closest_package.deadline, closest_package.delivered_at)) # Pop the package out of the loaded packages self.current_location = self.packages_loaded.pop( pop_index).delivery_address
def print_status_between_times(self, times): #SPACE | TIME COMPLEXITY: # O(N) | O(N) for i in range(0, 2): temp = times[i].split(':') hours = float(temp[0].strip()) temp = temp[1].split(' ') minutes = float(temp[0].strip()) if i == 0: time_1 = Time(hours, minutes) elif i == 1: time_2 = Time(hours, minutes) # Get all packages inside our hash table (it will be unsorted unsorted_packages = [] # Go through every element in hash table #SPACE | TIME COMPLEXITY: # O(N) | O(N^2) for i in range(0, len(self.packages_hash_table.table)): # Go through every element in that element (if it is empty len() will return 0) for j in range(0, len(self.packages_hash_table.table[i])): unsorted_packages.append(self.packages_hash_table.table[i][j]) # Now we have to sort unsorted_packages by using sorted() function and key is the package id def sort_key(package): return int(package.id) sorted_packages = sorted(unsorted_packages, key=sort_key) # SPACE | TIME COMPLEXITY: # O(N) | O(N) for package in sorted_packages: if time_1 <= package.delivered_at <= time_2: print(str(package) + " status: DELIVERED BETWEEN TIMES") elif time_1 >= package.delivered_at: print(str(package) + " status: ALREADY DELIVERED") elif time_2 <= package.left_hub: print(str(package) + " status: STILL IN HUB") elif time_1 <= package.left_hub <= time_2: print(str(package) + " status: LEFT HUB BETWEEN TIMES") elif time_1 >= package.left_hub and time_2 < package.delivered_at: print(str(package) + " status: IN TRANSIT") else: print( str(package) + "ERROR with times: " + str(package.left_hub) + " | " + str(package.delivered_at))
async def icmpHandler(log): global ddosWhitelist, ddosBlocklist, ddosWatchlist global firstPacket timeMatch = re.match(r"(\d+):(\d+):(\d+)\.(\d+)", log) addressMatch = re.search(r"(\S+)\s>\s([^:]+):", log) hour = timeMatch.group(1) minute = timeMatch.group(2) second = timeMatch.group(3) millisecond = timeMatch.group(4) source = addressMatch.group(1) destination = addressMatch.group(2) currentTime = Time(hour, minute, second, millisecond) sourceWhitelisted = source in ddosWhitelist["source"] destinationWhitelisted = destination in ddosWhitelist["destination"] if sourceWhitelisted or destinationWhitelisted: Print("Whitelisted") return sourceBlacklisted = source in ddosBlacklist["source"] destinationBlacklisted = destination in ddosBlacklist["destination"] if sourceBlacklisted or destinationBlacklisted: Print("Blacklisted") return sourceTracked = source in firstPacket sourceDiffTime = firstPacket[source]["time"].diffTime(currentTime) if sourceTracked else 0 sourceTotalTrack = firstPacket[source]["count"] if sourceTracked else 0 if not sourceTracked or (sourceTracked and diffTime >= 3 and sourceTotalTrack <= 15): firstPacket[source] = { "time" = Time, "count" = 1 } else: firstPacket[source]["count"] += 1 if sourceDiffTime < 3 and sourceTotalTrack > 15: sourceWatched = source in ddosWatchlist if not sourceWatched: # block by port ddosWatchlist[source] = "port" pass elif ddosWatchlist[source] == "port": # block by mac ddosWatchlist[source] = "mac" pass elif ddosWatchlist[source] == "mac": # block by ip ddosWatchlist[source] = "ip" pass elif ddosWatchlist[source] == "ip": ddosWatchlist.pop(source, None) ddosBlacklist["source"] += [source] pass
def print_status_at_time(self): hours = 999 query = input( 'Enter time in HH:MM format to query package statuses at a certain time: ' ) temp = query.split(':') hours = float(temp[0].strip()) temp = temp[1].split(' ') minutes = float(temp[0].strip()) time_requested = Time(hours, minutes) #Get all packages inside our hash table (it will be unsorted unsorted_packages = [] #Go through every element in hash table #SPACE | TIME COMPLEXITY: # O(N) | O(N^2) for i in range(0, len(self.packages_hash_table.table)): #Go through every element in that element (if it is empty len() will return 0) for j in range(0, len(self.packages_hash_table.table[i])): unsorted_packages.append(self.packages_hash_table.table[i][j]) #Now we have to sort unsorted_packages by using sorted() function and key is the package id def sort_key(package): return int(package.id) sorted_packages = sorted(unsorted_packages, key=sort_key) #SPACE | TIME COMPLEXITY: # O(N) | O(N) for package in sorted_packages: if time_requested < package.left_hub: print(str(package) + " status: AT HUB") elif package.left_hub < time_requested < package.delivered_at: print(str(package) + " status: IN TRANSIT") elif time_requested > package.delivered_at: print(str(package) + " status: DELIVERED") else: print("ERROR, ALL CASES FAILED!")
def start_delivery(self, time): # SPACE | TIME COMPLEXITY: # O(N) | O(N) for package in self.packages_loaded: package.left_hub = Time(time.hour, time.minute) self.current_time = time print( str(self) + " is leaving at " + str(self.current_time) + " and has a total of " + str(len(self.packages_loaded)) + " packages") #Go through all packages loaded # SPACE | TIME COMPLEXITY: # O(N) | O(N) while len(self.packages_loaded) != 0: self.deliver_package() #Once we are done delivering all packages print( str(self) + " has finished delivery at " + str(self.current_time) + " with a total of " + str(self.distance_traveled.__round__(2)) + " miles") print("")
async def icmpHandler(log): global ddosWhitelist, ddosBlocklist, ddosWatchlist global ddosFirstPacket try: timeMatch = re.match(r"(\d+):(\d+):(\d+)\.(\d+)", log) addressMatch = re.search(r"(\S+)\s>\s([^:]+): ([^,]+),", log) hour = timeMatch.group(1) minute = timeMatch.group(2) second = timeMatch.group(3) millisecond = timeMatch.group(4) source = addressMatch.group(1) destination = addressMatch.group(2) icmpType = addressMatch.group(3) currentTime = Time(hour, minute, second, millisecond) except Exception as e: print(log) print(e) if not timeMatch or not addressMatch: return if icmpType.find("echo reply") != -1: return sourceWhitelisted = source in ddosWhitelist["source"] sourceBlacklisted = source in ddosBlacklist["source"] sourceBlocked = source in ddosBlocklist sourceTracked = source in ddosFirstPacket sourceDiffTime = currentTime.diffTime( ddosFirstPacket[source]["time"]) if sourceTracked else 0 sourceTotalTrack = ddosFirstPacket[source]["count"] if sourceTracked else 0 destinationWhitelisted = destination in ddosWhitelist["destination"] destinationBlacklisted = destination in ddosBlacklist["destination"] if sourceWhitelisted or destinationWhitelisted: return if sourceBlacklisted or destinationBlacklisted: if sourceBlocked: return Print("Block by ip") Print("Blacklist") ddosBlocklist += [source] ddosBlacklist["source"] += [source] asyncio.create_task(blockBlacklist(source)) return if not sourceTracked or sourceDiffTime >= ddosInterval: ddosFirstPacket[source] = { "time": currentTime, "count": 1, } else: ddosFirstPacket[source]["count"] += 1 if sourceDiffTime < ddosInterval and sourceTotalTrack > ddosMaxPacket and not sourceBlocked: sourceWatched = source in ddosWatchlist if not sourceWatched: Print("Block by port") ddosBlocklist += [source] ddosWatchlist[source] = "port" asyncio.create_task( blockByPort(source, ddosBlocklist, ddosWatchlist)) elif ddosWatchlist[source] == "port": Print("Block by mac") ddosBlocklist += [source] ddosWatchlist[source] = "mac" asyncio.create_task( blockByMac(source, ddosBlocklist, ddosWatchlist)) elif ddosWatchlist[source] == "mac": Print("Block by ip") ddosBlocklist += [source] ddosWatchlist[source] = "ip" asyncio.create_task(blockByIp(source, ddosBlocklist, ddosWatchlist)) elif ddosWatchlist[source] == "ip": Print("Block by ip") Print("Blacklist") ddosWatchlist.pop(source, None) ddosBlocklist += [source] ddosBlacklist["source"] += [source] asyncio.create_task(blockBlacklist(source)) sourceWatched = source in ddosWatchlist if not sourceWatched: print( f"{str(sourceDiffTime):7.7}, {str(sourceTotalTrack):5.5} | {str(currentTime):15.15} | {source} > {destination} : {icmpType}" ) else: pass Print( f"{str(sourceDiffTime):7.7}, {str(sourceTotalTrack):5.5} | {str(currentTime):15.15} | {source} > {destination} : {icmpType}" )
import smtplib from timeclass import Time import random FROM_ADDR = "****" TO_ADDR = "****" EMAIL = "****" PASSWORD = "******" time = Time() with open("quotes.txt", mode="r") as quotes: data = quotes.read().splitlines() msg = f"Subject:Master, your daily quote\n\n{random.choice(data)}" with smtplib.SMTP("smtp.gmail.com") as connection: connection.starttls() connection.login(user=EMAIL, password=PASSWORD) if time.day != 6: connection.sendmail(from_addr=FROM_ADDR, to_addrs=TO_ADDR, msg=msg) print(time.day) print(random.choice(data))