#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-


'''

toUDP
v0.1.0

A basic exporter 

LICENCE : CC

by cocoa 


'''
from __future__ import print_function
import sys
import os
import argparse
import time 
import socket
import ast

argsparser = argparse.ArgumentParser(description="toUDP v0.1b help mode")
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose output")
argsparser.add_argument("-i","--ip",help="IP to bind to (0.0.0.0 by default)",default="0.0.0.0",type=str)
argsparser.add_argument("-p","--port",help="UDP port to bind to (9000 by default)",default="9003",type=str)
args = argsparser.parse_args()

verbose             = args.verbose
ip                  = args.ip
port                = int(args.port)
verbose             = args.verbose


name    = "exports::toUDP"

def debug(*args, **kwargs):
    if( verbose == False ):
        return
    print(*args, file=sys.stderr, **kwargs)

def ClientStart(ip, port):
    global sockclient

    sockclient = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)

def ClientSend(msgFromClient):

    bytesToSend         = str.encode(str(msgFromClient))
    serverAddressPort   = (ip, port)
    bufferSize          = 1024

    # Send to server using created UDP socket
    sockclient.sendto(bytesToSend, serverAddressPort)

    '''
    # If reply :
    msgFromServer = sockclient.recvfrom(bufferSize)

    msg = "Message from Server {}".format(msgFromServer[0])
    print(msg)
    '''

try:

    ClientStart(ip, port)
    while True:

        line = sys.stdin.readline()
        if line == "":
            time.sleep(0.01)
        line = line.rstrip('\n')
        #pointsList = ast.literal_eval(line)
        debug(name,": "+line)
        ClientSend(line)


except EOFError:
    debug("break")# no more information

