# Name:		sockClientPyDemo.py
# Author:	Dave Renner		dmrenne@comcast.net
# Date:		09/03/11	1300
# Rev:		09/24/12	1945
# Note:		this is a called module of 'pyDemo.py'
#
# Note:		'sockClientPyDemo.py' demos a socket client which sends
#			and receives socket messages to another process. 
#			We also require 'sockServerPyDemo.py' to receive and
#			acknowledge the	messages sent
#
#
import sys
from socket import *
import binascii

DEBUG1 = 1
MY_STD_BLOCKSIZE = 1024
serverHost = 'localhost'
serverPort = 50007                      # Mark Lutz's example
myLorettaMessage = [b'I want you all to call me Loretta']


# ############################## BOILERPLATE #############################
myConfigFile = 'pyDemoConfig.dat'
import pickle
# version has been stored in our calling module 'pyDemo.py'
PythonVersFile = 'version.obj'
versfile = open(PythonVersFile, 'rb')
PythonVers = pickle.load(versfile)
versfile.close()

if PythonVers == '30':
    import configparser
    config = configparser.ConfigParser()
    input_cmd = 'input'
else:
    import ConfigParser
    config = ConfigParser.ConfigParser()
    input_cmd = 'raw_input'

config.read(myConfigFile)
interactive = config.get('Mode', 'interactive')
continue_cmd = config.get('Strings', 'continue_cmd')

# easier just to paste this boilerplate function into all our modules
# than to import it
def printf(arg):
    cmdText = "print "
    arg_nonSlash = arg.strip('\\')
    if PythonVers == '30':
        cmdFull = cmdText + '("' + arg_nonSlash + '")'
    else:
        cmdFull = cmdText + '"' + arg_nonSlash + '"'
    exec( cmdFull )

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

printf("")
printf("")	
printf("We have now started the client process sockClientPyDemo.py.\
\\n\
\\nThe server process, sockServPyDemo.py\
\\nshould have been started first.\
\\n\
\\nIn all honesty, we must warn you at this point\
\\nthat sometimes this test doesn't work on Windows,\
\\ndue to its holding onto ports. I've tried all\
\\nsocket options to fix it and the results have still\
\\nbeen indeterminate.\
\\n\
\\nThat's why we're running this test last.\
\\nIn case it fails...\
\\n\
\\nWell, enough on that.\
\\n")
if interactive == '1':
	printf("")
	full_cmd = input_cmd + '("' + continue_cmd + '")'
	eval(full_cmd)	
	printf("")
	
printf("This 'client' will send a message indicating his/her\
\\nnew name to the server, which will display it for us.\
\\nThen the client will quit.\
\\nOK, here goes:")
printf("")

printf("IMPORTANT. You must have started 'sockServerPyDemo.py'\
\\nbefore you continue with this program.\
\\nHave you started 'sockServerPyDemo.py'?")

# BEEP!
#printf('\a')

if interactive == '1':
    if PythonVers == '30':
        resp = input('Enter Y for Yes, N for No: \n')
    else:
        resp = raw_input('Enter Y for Yes, N for No: \n')

    if resp == 'y':
        pass
    elif resp == 'Y':
        pass
    else:
        exit()

if PythonVers == '30':
    mySocket = socket(AF_INET, SOCK_STREAM)
else:
    mySocket = socket(AF_INET, SOCK_STREAM)

mySocket.connect( (serverHost, serverPort) )
if DEBUG1:
    printf('My Mafia client is connected. It belongs to the TCPIP family')
    printf("")

#respcat = ''

for line in myLorettaMessage:
    mySocket.send(line)
    if DEBUG1:
        printf("one-line 'Loretta' message sent")
        printf("")
    respbin = mySocket.recv(MY_STD_BLOCKSIZE)
    if DEBUG1:
        printf("received response:")
    resp_ascii = str(respbin)
    
    # clean up the binary response to straight ASCII	
    prs1 = resp_ascii.replace("b'", "")
    prs2 = prs1.replace("\\n","")
    prs3 = prs2.replace("'", "")
    printf(prs3)
mySocket.close()
