def start_aircast(hostname, port): sample_queue = Queue() io_loop = tornado.ioloop.IOLoop.current() stream_url = "http://{}:{}{}".format(hostname, port, STREAM_ROUTE) caster = Caster(stream_url) config = Config(sample_rate=44100, channels=2, bits_per_sample=16) broadcaster = Broadcaster(config, sample_queue, io_loop) shairport = Shairport(caster.device_name, config, sample_queue) app = make_app(broadcaster) def shairport_status_cb(event, _): if event == 'playing': caster.start_stream() shairport.add_callback(shairport_status_cb) broadcaster.start() shairport.start() app.listen(port) logger.info("AirCast ready. Advertising as '%s'", caster.device_name) try: io_loop.start() except KeyboardInterrupt: pass finally: io_loop.stop() shairport.stop() broadcaster.stop() shairport.join(5) broadcaster.join(5)
def main(): if len(sys.argv) != 2: print("Usage: {} [username]".format(sys.argv[0])); sys.exit(1); try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM); except: print('Could not create socket'); exit(1); port = PORT_MIN; while True: try: s.bind(('', port)); break; except: port += 1; if(port > PORT_MAX): print("Could not bind socket"); exit(1); # get user info addrinfo = socket.getaddrinfo(socket.gethostname(), port); addressFam = addrinfo[2]; addr = addressFam[4]; # make local peer localPeer = Peer(sys.argv[1], addr[0], addr[1]); while(not localPeer.isValidUsername()): newUsername = input("Please enter a username with at least one upper letter, one lower letter, one digit, and one of .-_: "); localPeer = Peer(newUsername, addr[0], addr[1]); hQueue = Queue(); crQueue = Queue(); csQueue = Queue(); peerList = []; peerList.append(localPeer); bc = Broadcaster(s, peerList); mr = MasterReceiver(s, hQueue, crQueue); pd = PeerDiscover(peerList, hQueue); mgr = Messenger(s, peerList, crQueue, csQueue); bc.daemon = True; mr.daemon = True; pd.daemon = True; mgr.daemon = True; bc.start(); mr.start(); pd.start(); mgr.start(); #print("Loading peers..."); msg = ""; while True: msg = input(localPeer.username + ": "); if len(msg) == 1 and msg[0] == 'q': break; elif(msg == "list"): print(" Users online: "); for i in range(1, len(peerList)): print(peerList[i].username); continue; elif len(msg) == 1 and msg[0] == 'p': peerList[0].printInfo(); continue; csQueue.put(msg); print("Logged off");
class NetMgr(Mgr): def __init__(self, engine,localOptions): Mgr.__init__(self, engine) self.engine = engine self.networkingEnabled = localOptions.enableNetworking if self.networkingEnabled: self.server = localOptions.server else: self.server = False self.ip = localOptions.ip self.remainingIDs = [] self.timeToWaitForPlayers = 10 def initialize(self): pass def loadLevel(self): if self.networkingEnabled: self.broadcaster = Broadcaster() self.listener = Listener() self.listener.start() self.broadcaster.start() if self.server or not self.networkingEnabled: self.remainingIDs.append([1,""]) self.remainingIDs.append([2,""]) def releaseLevel(self): if self.networkingEnabled: self.listener.stop() self.broadcaster.stop() self.listener.join() self.broadcaster.join() def tick(self, dtime): if self.networkingEnabled: if dtime < 1 and self.timeToWaitForPlayers > 0: self.timeToWaitForPlayers -= dtime # check to see if all players are ready to start the race if self.timeToWaitForPlayers > 0 and self.engine.gameMgr.allPlayersReady == False: allReady = True for ent in self.engine.entMgr.entities: if not ent.isReady: allReady = False if len(self.engine.entMgr.entities) > 1: self.engine.gameMgr.allPlayersReady = allReady elif self.timeToWaitForPlayers <= 0: self.engine.gameMgr.allPlayersReady = True # get messages incoming = self.listener.getMessages() outgoingMsgs = [] for msgs in incoming: msgType, msg = messages.unpack(msgs) if msgType == const.STATUS: found = False # check to see if the status message is regarding a ship that is already created for ent in self.engine.entMgr.entities: if ent.shipId == msg.shipId: found = True # if it is, send updates to entity if found and msg.shipId != self.engine.entMgr.playerIndex: self.engine.entMgr.entities[msg.shipId].updateQueue.append(msg) # if it isn't, create that entity elif not found: ent = Spaceship("Ship" + str(msg.shipId),msg.shipId, self.engine,const.MESHES[msg.shipId]) ent.pos = self.engine.entMgr.nextPos self.engine.entMgr.nextPos.x += 40 self.engine.entMgr.entities.append(ent) # only process requests before game has started elif msgType == const.REQUEST and self.timeToWaitForPlayers > 0: for ID in self.remainingIDs: # if the id has not been issued already, or if the ID corresponds to a user who has been given an ID (but did not recieve their ID), send the ID if ID[1] == "" or ID[1] == msg.userName: outgoingMsgs.append(messages.pack(messages.InfoMessage(msg.userName,ID[0]),const.INFO)) ID[1] = msg.userName # only process info messages when this player has not been handed a playerIndex elif msgType == const.INFO and not self.server and self.engine.entMgr.playerIndex == -1: self.engine.entMgr.playerIndex = msg.shipId found = False for ent in self.engine.entMgr.entities: if ent.shipId == msg.shipId: found = True if not found: ent = Spaceship("Ship" + str(msg.shipId),msg.shipId, self.engine,const.MESHES[msg.shipId]) ent.pos = self.engine.entMgr.nextPos self.engine.entMgr.nextPos.x += 40 self.engine.entMgr.entities.append(ent) # if this player has not been handed a playerIndex, request one. if self.engine.entMgr.playerIndex == -1: outgoingMsgs.append(messages.pack(messages.RequestMessage(str(socket.gethostname())),const.REQUEST)) # if the player has been handed a playerIndex, send the status of your ship else: myId = self.engine.entMgr.playerIndex myEnt = self.engine.entMgr.entities[myId] outgoingMsgs.append(messages.pack(messages.StatusMessage(myId,myEnt.pos,myEnt.dir,myEnt.colVel,myEnt.speed,myEnt.pitch, myEnt.roll,myEnt.isReady),const.STATUS)) # send outgoing messages for msg in outgoingMsgs: self.broadcaster.addMessage(msg)