# Copyright (c) 2020
# <NAME>, Uğurcan Akkök
# Bahcesehir University.
#
# All rights reserved.


import os
import json
import socket
import threading
import time
from datetime import timedelta


port = 5000
username = ""
MAX_BYTES = 65535


class Job(threading.Thread):
    def __init__(self, interval, execute, *args, **kwargs):
        threading.Thread.__init__(self)
        self.daemon = False
        self.stopped = threading.Event()
        self.interval = interval
        self.execute = execute
        self.args = args
        self.kwargs = kwargs

    def stop(self):
        self.stopped.set()
        self.join()

    def run(self):
        while not self.stopped.wait(self.interval.total_seconds()):
            self.execute(*self.args, **self.kwargs)


class AnnouncerJob(Job):
    def __init__(self, announcer, service_dict):
        Job.__init__(self, timedelta(seconds=10), execute=announce,
                     socket=announcer, service_dict=service_dict)
        self.service_dict = service_dict
        self.announcer = announcer
        return

    def update(self, service_dict):
        self.stop()
        self.__init__(self.announcer, service_dict)
        return


def announce(socket, service_dict):
    service = json.dumps(service_dict)
    socket.sendto(bytes(service, "utf-8"), ("192.168.3.11", port))
    print("Last sent was at ",
          time.ctime())
    print(service_dict["files"])
    return


def read_files():
    filenames = os.listdir("files")  # read all files in the directory
    return filenames


def main():
    # get files
    username = input("Your username: ")
    announcer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    announcer.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    file_list = read_files()
    service_dict = {"username": username, "files": file_list}
    announce(announcer, service_dict)
    job = AnnouncerJob(announcer, service_dict)
    job.start()
    # main loop
    while True:
        try:
            new_file_list = read_files()
            if new_file_list != file_list:
                service_dict["files"] = new_file_list
                job.update(service_dict)
                job.start()

        except Exception as e:
            print(str(e))
            pass
        time.sleep(10)


if __name__ == "__main__":
    main()

# Copyright (c) 2020
# <NAME>, Uğurcan Akkök
# Bahcesehir University.
#
# All rights reserved.


import os
import json
import socket
import threading
import time
from datetime import timedelta


port = 5000
username = ""
MAX_BYTES = 65535


class Job(threading.Thread):
    def __init__(self, interval, execute, *args, **kwargs):
        threading.Thread.__init__(self)
        self.daemon = False
        self.stopped = threading.Event()
        self.interval = interval
        self.execute = execute
        self.args = args
        self.kwargs = kwargs

    def stop(self):
        self.stopped.set()
        self.join()

    def run(self):
        while not self.stopped.wait(self.interval.total_seconds()):
            self.execute(*self.args, **self.kwargs)


class AnnouncerJob(Job):
    def __init__(self, announcer, service_dict):
        Job.__init__(self, timedelta(seconds=10), execute=announce,
                     socket=announcer, service_dict=service_dict)
        self.service_dict = service_dict
        self.announcer = announcer
        return

    def update(self, service_dict):
        self.stop()
        self.__init__(self.announcer, service_dict)
        return


def announce(socket, service_dict):
    service = json.dumps(service_dict)
    socket.sendto(bytes(service, "utf-8"), ("192.168.3.11", port))
    print("Last sent was at ",
          time.ctime())
    print(service_dict["files"])
    return


def read_files():
    filenames = os.listdir("files")  # read all files in the directory
    return filenames


def main():
    # get files
    username = input("Your username: ")
    announcer = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    announcer.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    file_list = read_files()
    service_dict = {"username": username, "files": file_list}
    announce(announcer, service_dict)
    job = AnnouncerJob(announcer, service_dict)
    job.start()
    # main loop
    while True:
        try:
            new_file_list = read_files()
            if new_file_list != file_list:
                service_dict["files"] = new_file_list
                job.update(service_dict)
                job.start()

        except Exception as e:
            print(str(e))
            pass
        time.sleep(10)


if __name__ == "__main__":
    main()

# Copyright (c) 2020 # <NAME>, Uğurcan Akkök # Bahcesehir University. # # All rights reserved. # read all files in the directory # get files # main loop