#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep  6 13:33:05 2018

@author: john
"""

# Python program to implement server side of chat room.
import socket
import select
import sys
import threading 
import json

#import _thread
#from _thread import *
IP_address = '192.168.168.184'
Port = 8179
def clientthread(conn,addr):
    count = 0 
    total_data = ""
    while True:
        try:
            conn.settimeout(1)
            print("..")
            message = ""
            message = conn.recv(100)
            while message is not "":                
                total_data+=message.decode("utf-8")
                message = "" 
                message = conn.recv(100)
            else:
                break
        except socket.error as ex:
            print(ex)
         
        f = json.loads(total_data)
        print(f["data"])
        #print (total_data)
        #total_data = ""
        #count=0
        print("END")
        #print(total_data)
        break
            
    
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)


server.bind((IP_address, Port))



server.listen()
#server.settimeout(1)

while True :
    try:
        server.settimeout(1)
        conn, addr = server.accept()
        print("EDO")
        t1 = threading.Thread(target = clientthread, args = (conn,addr)) 
        t1.start()
    except socket.error as e:
        
        print(e)









#"""
#listens for 100 active connections. This number can be
#increased as per convenience.
#"""
#server.listen(100)
#
#list_of_clients = []
#
#def clientthread(conn, addr):
#
#	# sends a message to the client whose user object is conn
#    conn.send(b"Welcome to this chatroom!")
#
#    while True:
#        try:
#            message = conn.recv(2048)
#            if message:
#                """prints the message and address of the	user who just sent the message on the server 	terminal"""
#                print ("<" + addr[0] + "> " + message.decode("utf-8"))
#					# Calls broadcast function to send message to all
#                message_to_send = "<" + addr[0] + "> " + message
#                broadcast(message_to_send, conn)
#            else:
#                """message may have no content if the connection is broken, in this case we remove the connection"""
#                remove(conn)
#
#        except:
#            continue
#
#"""Using the below function, we broadcast the message to all
#clients who's object is not the same as the one sending
#the message """
#def broadcast(message, connection):
#    for clients in list_of_clients:
#        if clients!=connection:
#            try:
#                clients.send(message.encode("utf-8"))
#            except:
#                clients.close()
#
#				# if the link is broken, we remove the client
#                remove(clients)
#
#"""The following function simply removes the object
#from the list that was created at the beginning of 
#the program"""
#def remove(connection):
#    if connection in list_of_clients:
#        list_of_clients.remove(connection)
#
#while True:
#
#    """Accepts a connection request and stores two parameters,	conn which is a socket object for that user, and addr 	which contains the IP address of the client that just 	connected"""
#    conn, addr = server.accept()
#
#    """Maintains a list of clients for ease of broadcasting a message to all available people in the chatroom"""
#    list_of_clients.append(conn)
#
#	# prints the address of the user that just connected
#    print (addr[0] + " connected")
#
#	# creates and individual thread for every user 
#	# that connects
#    _thread.start_new_thread(clientthread,(conn,addr)) 
#
#conn.close()
#server.close()
