# THIS FILE IS A PART OF VCStudio
# PYTHON 3

###############################################################################

# Multiuser is a complex system. It's so complex that my brain is melting at
# the moment. Anyways. Users need a way to talk to the server. And I'm going
# to provide it to the user using a multiuser Layer. See: 
# studio/studio_multiuserLayer.py.

# Tho there is one problem. The user should be able to read and write messages
# to the server. And you think. Right. There is a network/network_multiuser.py
# for this. Well. for the most stuff yes. 

# There are 2 types of protocols multiuser system uses. The TCP and the UDP.

# TCP protocol is used to tranfer sensetive stuff. Like files, scene text. Stuff
# that has to be at full. And where loosing packages is unexceptable.

# UDP will be used for the other stuff. Like log of the server. And messages to
# the server. Such as an Abort message.

# This file handle the UDP side of things.

###############################################################################

import os
import socket
import datetime

def listen(win):
    
    # Listen function will run in it's own thread and get the data from the 
    # server. It will be teminal like messages that we show in the multiuser
    # window.
    
    UDP_IP = "255.255.255.255"
    UDP_PORT = 54545
    
    while True:
        
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind((UDP_IP, UDP_PORT))
            #sock.settimeout(0.05)
            
            data, addr = sock.recvfrom(1024)
            data = data.decode('utf8')
            
            seconds_format = "%H:%M:%S"
            time = datetime.datetime.strftime(datetime.datetime.now(), seconds_format)
            
            if "VCStudio MULTIUSER SERVER TERMINAL" in data:
                #print(time+data.replace("VCStudio MULTIUSER SERVER TERMINAL", ""))
                win.multiuser["terminal"].append(time+data.replace("VCStudio MULTIUSER SERVER TERMINAL", ""))
                
                # Now i want to use a limit. Because hell I don't want 20000000 
                # bazillion messages stored in the memory at all times.
                
                win.multiuser["terminal"] = win.multiuser["terminal"][-300:]
                
                # And we want to scroll down on each message so
                
                win.scroll["multiuser_terminal"] = 0 - len(win.multiuser["terminal"])*50
            
            elif "VCStudio ABORT MULTIUSER" in data:
                win.multiuser["users"] = {}
            
            sock.close()
            
        except:
            pass
            
def message(message):
    
    # This is a function that will be called by multiple buttons. That want to
    # send the server any kind of message. But actually it's gonna send a message
    # to 255.255.255.255 port 54545 that the server is listening. At the moment
    # of writing this comment the server is programed only for 1 type of command
    # like this. It's the "VCStudio ABORT MULTIUSER". That makes the server stop
    # working. This command is a bit dangerous at the moment. Because it's running
    # on it's own thread in the server. Making it close even in the middle of a
    # request. Like it can corrupt a file that's being transferred. 
    
    UDP_IP = "255.255.255.255"
    UDP_PORT = 54545

    cs1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    cs1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    cs1.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    cs1.sendto(bytes(message, 'utf-8'), (UDP_IP, UDP_PORT))
    cs1.close()