-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
151 lines (118 loc) · 4.17 KB
/
client.py
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
import socket
import time
import random
HEADER = 64
PORT = 5050
FORMAT = 'utf-8'
BID_LIMIT = 80
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
# Using 5 products and their initial price
PRODUCTS = ["Watch", "Ring", "Shoe", "Baseball", "Hat"]
PRODUCTS_PRICE = [500.0, 900.0, 400.0, 600.0, 350.0]
MAX_BIDS = 50
# Connect client to server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
# Initialize variables
clientId = -1
clientPresetValue = -1.0
bidWinners = [-1, -1, -1, -1, -1]
currentBidAmounts = [500.0, 900.0, 400.0, 600.0, 350.0]
lastBidBy = -1
numberOfClients = 0
totalBids = 0
# This function is to send message
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
# This function is to update data
def updateData(bidClient, prodId, bidAmt):
bidWinners[prodId] = bidClient
currentBidAmounts[prodId] = bidAmt
# This function is to check whether client is eligible to bid or not
def amIEleigibleClientToBid():
if (lastBidBy+1) % numberOfClients == clientId:
return True
return False
# This function is to check bid breaching
def isBidBreached():
if totalBids <= MAX_BIDS:
return True
return False
# This function is get random product
def getRandomProduct():
return random.randint(0, 4)
# This function is to make a bid
def make_a_bid():
productId = getRandomProduct()
print(f"Product selected randomly is {PRODUCTS[productId]}")
if bidWinners[productId] == clientId:
reason = "ALREADY_CURRENT_WINNER_FOR_RANDOMLY_SELECTED_PRODUCT"
print(f"[NO BID] with reason: {reason}")
send(f"NO_BID {reason}")
return
currentBid = currentBidAmounts[productId]
bidAmount = currentBid * (1.0 + (random.randint(5000, 20000) / 100000))
if bidAmount < clientPresetValue:
print(
f"[BID] for product {PRODUCTS[productId]} with amount {bidAmount}")
send(f"BID {productId} {bidAmount}")
else:
reason = "BID_PRCE_IS_HIGHER_THAN_CLIENT_PRESET_PRICE"
print(f"[NO BID] with reason: {reason}")
send(f"NO_BID {reason}")
return
# This function is to handle sever messages
def handle_server():
global clientId
global numberOfClients
global clientPresetValue
global lastBidBy
global totalBids
while True:
msg = client.recv(4096).decode(FORMAT)
if len(msg):
msgList = msg.split()
msgType = msgList[0]
if msgType == "CONNECTED":
clientId = int(msgList[1])
elif msgType == "PRESET":
clientPresetValue = float(msgList[1])
elif msgType == "START_BID":
bidStartClient = int(msgList[1])
numberOfClients = int(msgList[2])
if bidStartClient == clientId:
make_a_bid()
else:
lastBidBy = bidStartClient
elif msgType == "BID":
lastBidBy = int(msgList[1])
prodId = int(msgList[2])
bidAmt = float(msgList[3])
totalBids += 1
if lastBidBy == clientId:
updateData(lastBidBy, prodId, bidAmt)
if amIEleigibleClientToBid() and isBidBreached():
make_a_bid()
else:
print(
f"[BID] from the client {lastBidBy} with amount: {bidAmt} for product {PRODUCTS[prodId]}")
elif msgType == "NO_BID":
clientNum = int(msgList[1])
lastBidBy = clientNum
msgText = msgList[2]
if clientNum != clientId:
print(
f"[NO BID] from the client {clientNum} with reason: {msgText}")
if amIEleigibleClientToBid() and isBidBreached():
make_a_bid()
elif msgList[0] == "BIDDING_ENDED":
print("....BIDDING ENDED....")
return
handle_server()
print("Thank you!!!")