-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
157 lines (132 loc) · 5.82 KB
/
client.py
File metadata and controls
157 lines (132 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import struct
import socket
import sys
import time
from bitstring import BitArray, ConstBitArray, ConstBitStream
import itertools
import bitarray as ba
import numpy as np
import binascii
from functions import *
from operation import OPERATION
class Client:
def __init__(self, addr):
self.addr = addr
print("Initialize Client Protocol!")
def dec2bin(self, d):
# dec -> bin
b = bin(d)
return b
def intTObool(self, num):
bin_string = format(num, '04b')
return [x == '1' for x in bin_string[::-1]]
# Packing Frame data to Binary
def pack_message(self, OP, AN, ID):
operation = OPERATION(OP)
#Pakowanie do tablicy bool'i
message = intTOboolArr(operation.value, '05b') + intTOboolArr(AN, '04b') + intTOboolArr(ID,'03b') + intTOboolArr(0, '04b')
MESSAGE = boolList2BinString(message)
bitstring = BitArray(MESSAGE)
#print(str(bitstring))
return bitstring.tobytes()
# For unpacking massages from Binary to Frame structure
def unpack_message(self, data):
recvdata = bin(int(binascii.hexlify(data), 16))
#print recvdata
unpacked_data = recvdata[2:].zfill(16)
#print recvdata
unpacked_data = ba.bitarray(unpacked_data)
unpacked_data = unpacked_data.tolist()
#print unpacked_data
OP = OPERATION(boolArrTOint(unpacked_data[:5]))
AN = boolArrTOint(unpacked_data[5:9])
ID = boolArrTOint(unpacked_data[9:12])
return [OP, AN, ID]
def start(self):
print "Client is starting!"
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = (self.addr, 8000)
print >> sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)
#print(struct.Struct('5? 4? 3?').size)
#Zmienne Clienta
client_token = 0
TRIES=0
try:
#Packing Data to Binary
error = False
number = 16
while number < 0 or number > 15 or number % 2 == 0 or type(number) is not int:
if error:
number = input("Wrong number! Chose form 0 to 15: ")
else:
number = input('Pick one number from 0 to 15 as the number of tries:')
if number < 0 or number > 15 or number % 2 == 0 or type(number) is not int:
error = True
message = self.pack_message(OPERATION.GET_ID_TRIES, number, client_token)
#print >> sys.stderr, 'sending "%s"' % binascii.hexlify(message)
sock.sendall(message)
while True:
#Receive response with ID OR ID&TRIES
data = sock.recv(2)
received = self.unpack_message(data)
#print(received)
action = received[0]
answer = received[1]
token = received[2]
if action == OPERATION.SEND_ID:
client_token = token
TRIES = answer
#print('send' + str(received))
print "Waiting for second player to join and input number of tries!"
# time.sleep(10)
# message = self.pack_message(OPERATION.TRIES, 0, ID)
# sock.sendall(message)
# data = sock.recv(2)
# if len(data) == 12:
# received = self.unpack_message(data)
#if received[0] == OPERATION.TRIES: #and received[1] !=0:
#print "Tries: " + str(received)
while True:
time.sleep(2)
message = self.pack_message(OPERATION.TRIES, 1, client_token) #TRIES z AN ustawionym na 1 pyta o to czy drugi gracz wpisal juz swoja liczbe do wylosowania
sock.sendall(message)
data = sock.recv(2)
received = self.unpack_message(data)
#print "Tries log: " + str(received)
action = received[0]
answer = received[1]
if action == OPERATION.TRIES and answer !=0:
break
#Winning
if action == OPERATION.RESULT and answer == 1:
print "Congratulations! You have won!"
break
#Losing
if action == OPERATION.TRIES and answer == 0:
print "Game over! You used all of your tries!"
break
#Game in progress
if (action == OPERATION.TRIES or action == OPERATION.SEND_ID_TRIES) and answer > 0:
if action == OPERATION.SEND_ID_TRIES:
client_token = received[2]
print "Tries left: " + str(answer)
error = False
number = 16
while number < 0 or number > 15 or type(number) is not int:
if error:
number = input ("Wrong number! Chose from 0 to 15: ")
else:
number = input('Try to guess the number. Pick one from 0 to 15:')
if number < 0 or number > 15 or type(number) is not int:
error = True
#Sending pick
#print "ID: "+ str(client_token)
message = self.pack_message(OPERATION.GUESS, number, client_token)
sock.sendall(message)
#break
finally:
print >> sys.stderr, 'closing socket'
sock.close()