Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
11380049_CW2/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
166 lines (131 sloc)
4.28 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivymd.app import MDApp | |
from kivy.properties import ObjectProperty,ListProperty,StringProperty | |
from kivy.uix.screenmanager import ScreenManager | |
from kivy.core.window import Window | |
from kivy.clock import Clock, mainthread | |
from kivymd.uix.boxlayout import MDBoxLayout | |
from serversetup import Setup | |
from home import Home, MyCheckbox | |
import socket,struct | |
from threading import Thread | |
class NetworkApp(MDApp): | |
active = ObjectProperty() | |
server = None | |
clients = ListProperty() | |
selected = ListProperty() | |
def build(self): | |
self.root = ScreenManager() | |
return self.root | |
def on_start(self): | |
self.setup = Setup() | |
self.home = Home() | |
self.root.add_widget(self.setup) | |
self.root.add_widget(self.home) | |
self.active = self.home.ids.home | |
Clock.schedule_interval(self.testConnection,0) | |
def testConnection(self,dt): | |
disconnected = [] | |
if self.clients: | |
for d in self.clients: | |
try: | |
d.sendall(b'') | |
except: | |
disconnected.append(d) | |
for d in disconnected: | |
self.clients.remove(d) | |
def acceptClients(self): | |
while self.server: | |
try: | |
client, add = self.server.accept() | |
self.clients.append(client) | |
Thread(target=self.getResponse,args=(client,)).start() | |
except Exception as e : | |
print(e) | |
break | |
def getResponse(self,client): | |
while True: | |
try: | |
size = struct.unpack("I", self.recvall(client, struct.calcsize("I")))[0] | |
data = self.recvall(client, size) | |
if data: | |
self.home.addRes(f'Device {self.clients.index(client)+1} : {data.decode()}\n') | |
except: | |
import traceback | |
if client in self.clients: | |
self.clients.remove(client) | |
break | |
def sendCommand(self,command): | |
if not self.clients: | |
self.home.addLogs('>> No Client Found\n') | |
return | |
if not self.selected: | |
self.home.addLogs('>> Please Select Any Client\n') | |
return | |
for r in self.selected: | |
msg = command.encode() | |
size = len(msg) | |
r.sendall(struct.pack("I", size)) | |
r.sendall(msg) | |
self.home.addLogs( f'>> To Device {self.clients.index(r)+1} "{command}" sent\n') | |
@mainthread | |
def on_clients(self,*args): | |
self.home.ids.grid.clear_widgets() | |
for i,client in enumerate(self.clients): | |
c = MyCheckbox(text=f'Device {i+1}',socket = self.clients[i]) | |
self.home.ids.grid.add_widget(c) | |
def on_stop(self): | |
# stop the server after exiting the application | |
if self.server: | |
self.server.close() | |
self.server = None | |
@mainthread | |
def on_checkbox_active(self,client, checkbox, value): | |
''' | |
Selecting and desecting devices | |
''' | |
if value: | |
self.selected.append(client.socket) | |
else: | |
self.selected.remove(client.socket) | |
def recvall(self,sock, size): | |
msg = b"" | |
while len(msg) < size: | |
packet = sock.recv(size - len(msg)) | |
if not packet: | |
return None | |
msg += packet | |
return msg | |
def shutdown(self): | |
self.server.close() | |
self.root.current = 'setup' | |
self.server = None | |
def setupServer(self): | |
host = self.setup.ids.host.text | |
port = self.setup.ids.port.text | |
if not host or not port: | |
self.setup.ids.error.text = 'Incomplete Data' | |
return | |
if not port.isnumeric(): | |
self.setup.ids.error.text = 'Invalid Port' | |
return | |
try: | |
self.server = socket.socket() | |
self.server.bind((host,int(port))) | |
self.server.listen(10) | |
self.root.current = 'home' | |
self.home.addLogs(f'>> Server Created ({host}:{port})\n') | |
Thread(target = self.acceptClients).start() | |
except Exception as e: | |
self.setup.ids.error.text = str(e) | |
def exit(self): | |
if self.server: | |
self.server.close() | |
Window.close() | |
def go(self,name,wdt): | |
if self.active: | |
self.active.background_color = [.5,.5,.5,.5] | |
self.active = wdt | |
self.active.background_color = [0,0,1,1] | |
self.home.ids.mngr.current = name | |
if __name__ == '__main__': | |
NetworkApp().run() |