123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- import os
- import sys
- import time
- import insure
- import socket
- import random
- import datetime
- import threading
- import subprocess
- project_name = "VCStudio_Multiuser_Project_Name_Failed"
- try:
- project_name = sys.argv[1]
- except:
- pass
- print("\n")
- ipget = subprocess.Popen(["hostname", "-I"],stdout=subprocess.PIPE, universal_newlines=True)
- ipget.wait()
- thisIP = ipget.stdout.read()[:-2]
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind(("", 64646))
- sock.listen(0)
- threads = {}
- users = {}
- messages = [["Multiuser Server", "Server Started"]]
- assets = {}
- story = ["0.0.0.0:0000", {}, "0000/00/00-00:00:00"]
-
- analytics = [{}, "0000/00/00-00:00:00"]
-
- def output(string):
-
-
-
- cs0 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- cs0.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- cs0.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
- cs0.sendto(bytes("VCStudio MULTIUSER SERVER TERMINAL | "+string, 'utf-8'), ("255.255.255.255", 54545))
- cs0.close()
-
- seconds_format = "%H:%M:%S"
- time = datetime.datetime.strftime(datetime.datetime.now(), seconds_format)
-
- print(time+" | "+string)
- output("multiuser server | started | "+thisIP+" | "+project_name)
- def main():
-
-
-
- while True:
-
-
-
- client, ipport = sock.accept()
- ip, port = ipport
-
- userid = str(ip)+":"+str(port)
-
-
- users[userid] = {
- "client" :client,
- "ip" :ip ,
- "port" :port ,
- "username":"" ,
- "request" :[] ,
- "asnswer" :[] ,
- "camera" :[0,0] ,
- "pause" :False
-
- }
-
-
-
-
- threads[userid] = threading.Thread(target=user, args=(userid, ))
- threads[userid].setDaemon(True)
- threads[userid].start()
-
-
- def user(userid):
-
-
- client = users[userid]["client"]
- ip = users[userid]["ip"]
- port = users[userid]["port"]
- username = users[userid]["username"]
-
-
-
-
-
-
- insure.send(client, "username?")
-
- data = insure.recv(client)
- users[userid]["username"] = data
- username = users[userid]["username"]
-
-
- insure.send(client, "project?")
- data = insure.recv(client)
-
-
-
- if data != project_name:
- insure.send(client, "wrong_project")
- client.close()
- return
-
- output(username+" | connected | "+userid)
-
-
-
-
-
- request_all([0,"users"])
-
- def get_story(client):
- global story
- insure.send(client, "story")
- clients_story = insure.recv(client)
-
- if clients_story[1] > story[2]:
- story = [userid, clients_story[0], clients_story[1]]
- users[userid]["camera"] = clients_story[0]["camera"].copy()
-
- insure.send(client, [story[0], story[1]])
- insure.recv(client)
-
- def get_assets(client):
- insure.send(client, "assets")
- clients_assets = insure.recv(client)
-
-
-
- for asset in clients_assets:
- if asset not in assets or assets[asset][1] < clients_assets[asset][1]:
- assets[asset] = clients_assets[asset]
-
-
- insure.send(client, assets)
- insure.recv(client)
-
-
- def get_analytics(client):
- global analytics
- insure.send(client, "analytics")
- clients_analytics = insure.recv(client)
- if clients_analytics[1] > analytics[1]:
- analytics = clients_analytics
-
- insure.send(client, analytics[0])
- insure.recv(client)
-
- get_story(client)
- get_assets(client)
- get_analytics(client)
-
-
- insure.send(client, "yours")
-
-
-
- while True:
-
- try:
-
-
-
- request = insure.recv(client)
-
-
- if request == "yours":
-
-
-
-
-
- if len(users[userid]["request"]) > 1:
-
- if users[userid]["request"][1] == "story":
- get_story(client)
- insure.send(client, "yours")
-
- elif users[userid]["request"][1] == "analytics":
- get_analytics(client)
- insure.send(client, "yours")
-
- elif users[userid]["request"][1] == "assets":
- get_assets(client)
- insure.send(client, "yours")
-
- elif users[userid]["request"][1] == "users":
- insure.send(client, "users")
-
- elif users[userid]["request"][1] == "at":
- insure.send(client, users[userid]["request"])
-
- elif users[userid]["request"][1] == "message":
- insure.send(client, ["messages",messages])
-
- elif users[userid]["request"][0] == "serve":
-
- serve(
- users[userid]["request"][1],
- userid,
- users[userid]["request"][2]
- )
-
-
- users[userid]["request"] = []
-
-
-
-
-
- if not assets:
- request_all([0,"assets"])
- print("assets requested")
-
-
-
- else:
- insure.send(client, "yours")
-
-
-
-
-
- else:
-
-
- if request == "story":
- get_story(client)
- request_all([userid, "story"])
-
- elif request == "analytics":
- get_analytics(client)
- request_all([userid, "analytics"])
-
- elif request[0] == "at":
- output(userid+" | at | "+request[1]+" | time | "+request[2])
- request_all([userid, "at", request[1], request[2]])
-
- elif request == "users":
- insure.send(client, users_list())
-
- elif request[0] == "message":
- messages.append([username, request[1]])
- request_all([0, "message"])
-
- elif request[0] == "get":
-
-
-
-
- try:
-
-
-
- while users[request[1]]["request"]:
- time.sleep(0.001)
-
- users[request[1]]["request"] = ["serve", request[2], userid]
-
-
-
-
-
-
- users[userid]["pause"] = True
- while users[userid]["pause"]:
- time.sleep(0.001)
-
-
-
-
- except Exception as e:
-
- output(userid+" | get | error | "+str(e))
- globals()["assets"] = {}
-
-
-
- insure.send(client, "yours")
-
- except Exception as e:
-
-
- request_all([0,"users"])
- output(username+" | "+userid+" | "+str(e)+" | line: "+str(sys.exc_info()[-1].tb_lineno))
- try:
- del users[userid]
- except Exception as e:
- output("deleting user error | "+str(e))
-
-
-
- globals()["assets"] = {}
-
-
-
- return
- def users_list():
-
-
-
- U = {}
-
- for user in list(users.keys()):
- U[user] = {
- "username":users[user]["username"],
- "camera" :users[user]["camera"]
- }
-
- return U
-
- def request_all(request):
-
- for user in users:
- if user != request[0]:
-
-
- users[user]["request"] = request
-
-
- def broadcast():
-
-
-
-
-
-
- cs1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- cs1.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- cs1.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
- message = "VCStudio MULTIUSER SERVER | "+thisIP+" | "+project_name
- cs1.sendto(bytes(message, 'utf-8'), ("255.255.255.255", 54545))
- cs1.close()
- def listen():
-
-
-
-
- message = ""
-
-
-
- try:
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- sock.bind(("255.255.255.255", 54545))
- sock.settimeout(0.05)
-
- data, addr = sock.recvfrom(1024)
- data = data.decode('utf8')
-
- sock.close()
-
- message = data
-
- except:
- pass
-
-
- if message == "VCStudio ABORT MULTIUSER":
- output("recieved abort message | closing")
- exit()
- def serve(folder, fromid, toid):
-
- output("serving | "+folder+" | from | "+fromid+" | to | "+toid)
-
-
-
-
- to = users[ toid ]["client"]
- fr = users[fromid]["client"]
-
-
-
- insure.send(fr, ["give", folder])
-
-
-
-
- insure.send(to, insure.recv(fr))
-
-
-
-
-
- getlist = insure.recv(to)
- insure.send(fr, getlist)
-
-
- for f in getlist:
- print("serving file:",f)
- insure.send(to, insure.recv(fr))
-
- insure.send(fr, insure.recv(to))
-
-
-
- users[toid]["pause"] = False
-
- threads["server_listen"] = threading.Thread(target=main, args=())
- threads["server_listen"].setDaemon(True)
- threads["server_listen"].start()
-
- while True:
-
-
-
-
- broadcast()
-
-
-
-
-
- listen()
|