Esempio n. 1
0
def client(ip, port):

	# Contacts the metadata server and ask for list of files.
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Creating socket

	try:
		sock.connect((ip, port)) #Connecting socket
	except:
		print 'Could not connect to the server. Are you sure it is on and that you have the right port?'

	p = Packet() #Making a packet
	p.BuildListPacket() #Building packet
	
	try:
		sock.sendall(p.getEncodedPacket()) #Sending encoded packet
	except:
		print 'Could not send the encoded packet!'

	files = sock.recv(1024) #Receiving list of files

	p.DecodePacket(files) #Decoding Packet

	try:
		for files, size in p.getFileArray():
			print files, size, ' bytes'
	except:
		print 'Could not read list of files.'
def register(meta_ip, meta_port, data_ip, data_port):
    """Creates a connection with the metadata server and
       register as data node
    """

    # Establish connection
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((meta_ip, meta_port))

    #Wow. Everything was all here. F*****g Packets, man.
    try:
        response = "NAK"
        sp = Packet()
        while response == "NAK":
            sp.BuildRegPacket(data_ip, data_port)
            sendall_with_size(sock, sp.getEncodedPacket())
            response = recv_with_size(sock)

            if response == "DUP":
                print "Duplicate Registration"

            if response == "NAK":
                print "Registratation ERROR"

    finally:
        sock.close()
Esempio n. 3
0
def client(ip, port):

	# Contacts the metadata server and ask for list of files.
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	s.connect((ip, port))

	# Creae a Packet and build it as a list packet
	# then send it to the metadata server
	packet = Packet()
	packet.BuildListPacket()
	s.sendall(packet.getEncodedPacket())

	# Now we take the response of the metadata server
	# the response is a packet with the list of files
	response = s.recv(1024)

	#print response
	
	if response == 'NAK':
		print 'there was an error with the request'
	else:
		packet.DecodePacket(response)

		# here I get the file array from the packet and
		# I will iterate to display the files and their size
		fileList = packet.getFileArray()
		#print fileList
		for f in fileList:
			print f[0], f[1], "bytes"
Esempio n. 4
0
 def createPacket(self):
     message = "Packet " + str(self.currentPacketID) + "'s data goes here!"
     newPacket = Packet(self.currentPacketID, now(), self.ID, 
                        self.destinationID, False, False, message, self.globs)
     self.currentPacketID += 1     
     activate(newPacket, newPacket.run())
     return newPacket
Esempio n. 5
0
def register(meta_ip, meta_port, data_ip, data_port):
	"""Creates a connection with the metadata server and
	   register as data node
	"""

	# Establish connection
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((meta_ip, meta_port))
		

	# Register data_node to meta_data
	try:
		response = "NAK"
		sp = Packet()
		while response == "NAK":
			sp.BuildRegPacket(data_ip, data_port)
			sock.sendall(sp.getEncodedPacket())
			response = sock.recv(1024)

			if response == "DUP":
				print "Duplicate Registration"

		 	if response == "NAK":
				print "Registratation ERROR"


	finally:
		sock.close()
Esempio n. 6
0
 def createAckPacket(self):
     message = "Acknowledgement packet " + str(self.currentPacketIDToAck) + "'s data goes here!"
     # create the packet object with the needed ack packet id, source id, etc.
     newPacket = Packet(self.currentPacketIDToAck, now(), self.ID, 
                        self.sourceID, False, True, message, self.globs)
     activate(newPacket, newPacket.run())
     return newPacket    
def register(meta_ip, meta_port, data_ip, data_port):
	"""Creates a connection with the metadata server and
	   register as data node
	"""

	# Establish connection
	
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	
	try:
		sock.connect((meta_ip, meta_port))
		response = "NAK"
		sp = Packet()
		while response == "NAK":
			sp.BuildRegPacket(data_ip, data_port)
			sock.sendall(sp.getEncodedPacket())
			response = sock.recv(1024)

			if response == "DUP":
				print "Duplicate Registration"

		 	if response == "NAK":
				print "Registratation ERROR"
	except:
		print 'ERROR: Unable to connect to server!\nPlease make sure the meta-data server is online.'
		sys.exit(0)

	finally:
		sock.close()
Esempio n. 8
0
 def initiateReroute(self):
     self.rerouting = True
     self.haveData = [False] * self.networkSize
     self.haveData[self.ID] = True
     self.haveAck = [False] * self.networkSize
     self.haveAck[self.ID] = True
     
     # calculate delayData
     self.delayData = []
     for i in range(self.networkSize):
         delays = self.delays[i]
         if delays != []:
             self.delayData.append((self.ID, i, sum(delays)/float(len(delays))))
         self.delays[i] = []
         
     self.updateNetwork(self.delayData)
     
     # broadcast rerouting signal with new delayData
     for i in range(self.networkSize):
         if i is not self.ID:
             if not (self.globs.NODES[i][1] or self.globs.NODES[i][2]): # if device i is a router
                 pkt = Packet("Delay Data at %d" % (self.ID), now(), self.ID, i, True, False, ("reroute", self.delayData), self.globs)
                 activate(pkt, pkt.run())
                 self.sendPacket(pkt, self.routingTable[i])
             else: # device is not a router, so don't need to communicate with it
                 self.haveData[i] = True
                 self.haveAck[i] = True
Esempio n. 9
0
def copyFromDFS(address, fname, path):
	""" Contact the metadata server to ask for the file blocks of
	    the file fname.  Get the data blocks from the data nodes.
	    Saves the data in path.
	"""

   	# Contact the metadata server to ask for information of fname
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect(address)


	# Create a packet object to get block ids from meta data
	p = Packet()
	p.BuildGetPacket(fname)
	sock.sendall(p.getEncodedPacket())


	# If there is no error response, retreive the data blocks
	r = sock.recv(1024)
	p.DecodePacket(r)
	dnList = p.getDataNodes()


	# Create file to store data from blocks
	f = open(path, 'wb')


	# Get data blocks from data servers
	for dnode in dnList:
		# Contact the data node
		sockdn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sockdn.connect((dnode[0], dnode[1]))


		# Create a packet object to get data from data node
		p.BuildGetDataBlockPacket(dnode[2])
		sockdn.sendall(p.getEncodedPacket())

		# Get the data size of the data that will be receive
		dsize = sockdn.recv(1024)
		dsize = int(dsize)

		sockdn.sendall("OK")


		# Get data in 1024 size parts
		data = ""
		while(len(data) < dsize):
			r = sockdn.recv(1024)
			data = data + r
			sockdn.sendall("OK")

		# Write data to file
		f.write(data)
		sockdn.close()


	f.close()
	def handle_list(self, db):
		"""Get the file list from the database and send list to client"""
		try:
			# Fill code here
			packet = Packet()
			packet.BuildListResponse(db.GetFiles())
			self.request.sendall(packet.getEncodedPacket())
		except:
			self.request.sendall("NAK")	
Esempio n. 11
0
	def handle_list(self, db):
		"""Get the file list from the database and send list to client"""
		try:
			# Fill code here
			response = Packet()
			response.BuildListResponse(db.GetFiles())
			self.request.sendall(response.getEncodedPacket()) 	
		except Exception as e:
			printExeption(e)
			self.request.sendall("NAK")	
Esempio n. 12
0
 def handle(self,now,event):
     if event == 'generate':
         if (now - self.start) > self.duration:
             return
             
         packet = Packet(src=self.node.name, dst='null')
         packet.load = self.mean_load
         packet.util = self.util
         self.scheduler.add(time=now, event=packet, handler=self.node.handle_message)
         now += random.expovariate(self.mean_load)
         self.scheduler.add(time=now, event='generate', handler=self.handle)
    def handle_list(self, db):
        """Get the file list from the database and send list to client"""
        try:
            #Packet for the ls.py response
            list_p = Packet()
            #GetFiles() already sends the files in the neat format
            list_p.BuildListResponse(db.GetFiles())
            self.sendall_with_size(list_p.getEncodedPacket())

        except:
            print "Problem in handle_list()."
            self.sendall_with_size("NAK")
Esempio n. 14
0
	def handle_list(self, db):
		"""Get the file list from the database and send list to client"""
		try:
			# list will contain the names and sizes of all the files
			List = db.GetFiles()
			# now create a build list packet and send it to the client
			packet = Packet()
			packet.BuildListResponse(List)
			self.request.sendall(packet.getEncodedPacket())

		except:
			self.request.sendall("NAK")	
Esempio n. 15
0
def copyFromDFS(address, dfs_path, filename, password, crypto):
    """ Contact the metadata server to ask for the file blocks of
        the file fname.  Get the data blocks from the data nodes.
        Saves the data in path.
    """
    # Contact the metadata server to ask for information of fname

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(address)
    p = Packet()
    p.BuildGetPacket(dfs_path)
    sendall_with_size(sock, p.getEncodedPacket())

    # If there is no error response Retreive the data blocks

    # Save the file

    message = recv_with_size(sock)
    p.DecodePacket(message)
    #getDataNodes has ADDRESS, PORT, BLOCK_ID
    data_nodes = p.getDataNodes()

    """Not needed."""
    #In case the user specifies a diferent directory

    #filename = fname.split("/")

    #destination = "%s/%s" % (path, "copy_" + filename[-1])
    """Not needed."""

    wfile = open(filename, "w")
    for IP, PORT, BLOCK_ID in data_nodes:
        node_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        node_sock.connect((IP, PORT))

        p.BuildGetDataBlockPacket(BLOCK_ID)
        sendall_with_size(node_sock, p.getEncodedPacket())

        block = recv_with_size(node_sock)

        #added for decrypting
        if(crypto):
            decrpt_block = decrypt(password, block)

        else:
            decrpt_block = block

        wfile.write(decrpt_block)

        node_sock.close()

    wfile.close()
    def handle(self):
        msg = self.recv_with_size()
        print msg, type(msg)

        p = Packet()
        p.DecodePacket(msg)

        cmd = p.getCommand()
        if cmd == "put":
            self.handle_put(p)

        elif cmd == "get":
            self.handle_get(p)
	def handle(self):
		msg = self.request.recv(1024) #From copyclient
		print msg, type(msg)

		p = Packet()
		p.DecodePacket(msg)

		cmd = p.getCommand()
		if cmd == "put":	#Putting data
			self.handle_put(p)

		elif cmd == "get":	#Getting data
			self.handle_get(p)
Esempio n. 18
0
	def handle_list(self, db):
		"""Get the file list from the database and send list to client"""
		try:
			sp = Packet()
			lfiles = []
			for file, size in db.GetFiles():
				lfiles.append((file, size))

			else:
				sp.BuildListResponse(lfiles)
				self.request.sendall(sp.getEncodedPacket())

		except:
			self.request.sendall("NAK")	
Esempio n. 19
0
 def timerHandler(self):
     if not self.rerouting:
         for link in self.links:
             dst = link.end.ID
             if len(link.queue) >= link.bufferCapacity: # if the probe will be dropped
                 self.delays[dst].append(self.globs.PROBE_DROP_DELAY)
             pkt = Packet("Probe %d %d" % (self.ID, dst), now(), self.ID, dst, True, False, ("probe", None), self.globs)
             activate(pkt, pkt.run())
             self.sendPacket(pkt, link)
     else:
         for i in range(self.networkSize):
             if not self.haveAck[i]:
                 pkt = Packet("Delay Data at %d" % (self.ID), now(), self.ID, i, True, False, ("topology", self.delayData), self.globs)
                 activate(pkt, pkt.run())
                 self.sendPacket(pkt, self.routingTable[i])
Esempio n. 20
0
def write_packets(expected, outfile, r_in, r_out):
    r_in.setblocking(True)
    rcvd_bytes, address = r_in.recvfrom(1024)
    rcvd = Packet.byte_to_Packet(rcvd_bytes)
    if (rcvd.magicno == 0x497E and rcvd.packet_type == 0):  # only run if it works, otherwise packet dropped
        acknowledgement_packet = Packet.Packet(0x497E, 1, rcvd.seqno, 0, '')         
        awk_buffer = acknowledgement_packet.convert_to_bytes()          
        r_out.sendto(awk_buffer, (ip, Port.cr_in_port))      
        print("twas sent right?")
        print("seqno: " + str(rcvd.seqno))
        if rcvd.seqno == expected:
            # rcvd is the received data packet # empty data field
            expected = 1 - expected
            print("length: " + str(rcvd.data_len))
            if rcvd.data_len > 0:
                print("this is data: " + rcvd.data)
                outfile.write(rcvd.data)
            elif rcvd.data_len == 0:
                outfile.close()
                r_in.close()  # unnecessary?
                r_out.close()
                exit()
    else:       
        # we should test some with weird or missing magicnos, if we havent already
        print("Error: invalid magicno. Must be 0x497E")
    return expected;
Esempio n. 21
0
	def handle(self):
		# Receive a msg from the copy client
		msg = self.request.recv(1024)

		# Define a packet object and decode it
		p = Packet()
		p.DecodePacket(msg)

		# Copy client asking for data node to put data
		cmd = p.getCommand()
		if cmd == "put":
			self.handle_put(p)

		# Copy client asking for data node to get data
		elif cmd == "get":
			self.handle_get(p)
Esempio n. 22
0
	def handle(self):

		# Establish a connection with the local database
		db = mds_db("dfs.db")
		db.Connect()

		# Define a packet object to decode packet messages
		p = Packet()

		# Receive a msg from the list, data-node, or copy clients
		msg = self.request.recv(1024)
		print msg, type(msg)
		
		# Decode the packet received
		p.DecodePacket(msg)
	

		# Extract the command part of the received packet
		cmd = p.getCommand()

		# Invoke the proper action 
		if   cmd == "reg":
			# Registration client
			self.handle_reg(db, p)

		elif cmd == "list":
			pass
			# Client asking for a list of files
			# Fill code
		
		elif cmd == "put":
			pass
			# Client asking for servers to put data
			# Fill code
		
		elif cmd == "get":
			pass
			# Client asking for servers to get data
			# Fill code

		elif cmd == "dblks":
			pass
			# Client sending data blocks for file
			 # Fill code


		db.Close()
Esempio n. 23
0
	def handle_list(self, db):
		"""Get the file list from the database and send list to client"""

		try:
			# Search files information from data base
			dbfiles = db.GetFiles()

			# Create packet for sending to client
			p = Packet()
			p.BuildListResponse(dbfiles)

			# Sending encoded packet to list client
			self.request.sendall(p.getEncodedPacket())


		except:
			self.request.sendall("NAK")	
Esempio n. 24
0
 def addWaypoint(self, wayList):        #add waypoint list
     self.wayListCount=0
      #If this is the first waypoint we send, do not send a carrot.
     string = Packet.createWaypointPacket(wayList, self.firstWaypoint)
     #for waypoints in wayList:
     self.wayListCount = len(wayList)
         
     self.firstWaypoint=False
      
     
    
     
     if(self.waypointCount > 0): 
         string += Packet.getCurrentMode()
     else: 
         string += Packet.setInitalMode()
     self.setLine(string) 
   
     self.waypointCount=self.waypointCount + 1 
Esempio n. 25
0
def client(ip, port):

	# Contacts the metadata server and ask for list of files.
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((ip, port))

	# Creates a packet to ask the metadata to send the file list
	p = Packet()
	p.BuildListPacket()
	sock.sendall(p.getEncodedPacket())

	# Received the list of files and display the information
	r = sock.recv(1024)
		# print r, type(r)

	p.DecodePacket(r)
	filelist = p.getFileArray()

	for item in filelist:
		print item[0], item[1], "bytes"
Esempio n. 26
0
def carry_on(exit_flag, inputfile, next1, num_of_packets):
    rcvd_bytes, address = Socket.s_in.recvfrom(1024)
    rcvd = Packet.byte_to_Packet(rcvd_bytes)  # converts bytes to the recieved ack_packet
    if rcvd.magicno == 0x497E and rcvd.packet_type == 1 and rcvd.data_len == 0:
        if rcvd.seqno == next1:
            next1 = 1 - next1
            if exit_flag == True:
                inputfile.close()
                print("It took " + str(num_of_packets) + " packets to send the file")
                exit()
                # else go back to beginning of outer loop, idk if we need an else statement or it will do it itself ya feel me
    return next1
Esempio n. 27
0
def carry_on(exit_flag, inputfile, next1, num_of_packets):
    print("what")
    rcvd_bytes, address = Socket.s_in.recvfrom(1024)
    rcvd = Packet.byte_to_Packet(rcvd_bytes)  # converts bytes to the recieved ack_packet
    if rcvd.magicno == 0x497E and rcvd.packet_type == 1 and rcvd.data_len == 0:
        if rcvd.seqno == next1:
            next1 = 1 - next1
            if exit_flag == True:
                inputfile.close()
                print("It took " + str(num_of_packets) + " packets to send the file")
                exit()
    return next1
Esempio n. 28
0
   def notify(self):
       #initialize variables
       self.choice= None
       self.info = None
       try:
           (self.choice, self.info) = Packet.decodePacket(self.hardwareLink.getLine(), self.wayListCount)   #get packet information
       except Exception:
           pass   
       
       if(self.choice == 0 ):                #check for bad packet
           self.badPacketCount = self.badPacketCount + 1
           self.VGO.setTelError(self.info)                #sends error packet to VGo
           print "BAD PACKET SUCKA!"
           
       while(self.shake_flag):                        #check for handshake 
            
           if(self.choice == 1):    #handshake acknowledged 
               self.shake_flag = False; 
               self.VGO.completeHandShake() 
               self.handShakeComplete = True               
           time.sleep(0.1) 
       
       #after handshake, system will just push to object 
       if(self.choice == 2):                        #Packet A
                    
           self.packetInfo = self.info 
           self.VGO.pushPacket(1,self.info) 
       elif(self.choice ==3):                        #Packet B 
           self.packetInfo = self.info 
           self.VGO.pushPacket(2,self.info)
       elif(self.choice ==4):                    #vehicle packets 
           self.packetInfo = self.info 
           self.setOldstats(self.status_varLat,self.status_varLong)
           self.setNewStats(self.info)
           self.VGO.pushPacket(3,self.info)
       elif(self.choice==5):
           self.VGO.pushPacket(4,self.info)
       elif(self.choice==6):                    #antenna packet
           #update anntena information
           varSide = 0
           self.varRun = float(self.VGO.getVehicleLatitude()) - float(self.VGO.getGroundLat())    #get horizontal component
           if(self.varRun<0):    #right side
               self.varAntennaSide=1
               
           self.varRise = self.VGO.getVehicleLongitude() - self.VGO.getGroundLong() #get vertical component
           try:
               self.theta = math.atan(self.varRise/self.varRun)/10            #calcuate angle between antenna and truck
           except ZeroDivisionError:
               pass

           self.setLine(self.theta)
Esempio n. 29
0
def client(ip, port):

	# Contacts the metadata server and ask for list of files.
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

	try:
		p = Packet()
		p.BuildListPacket()

		sock.connect((ip, port))
		sock.sendall(p.getEncodedPacket())

		msg = sock.recv(1024)
		
		if msg != "NAK":
			p.DecodePacket(msg)
			for f, size in p.getFileArray():
				print f, size
		else:
			print "Not Acknowledged"

	finally:
		sock.close()
Esempio n. 30
0
def client(ip, port):
    #socket for connecting to the metadata server
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))

    #packet object used to interact with the metadata server
    p = Packet()
    #list command
    p.BuildListPacket()

    #sending the size and later the command
    message = p.getEncodedPacket()
    sendall_with_size(sock, message)

    #dealing with the metadata server's response
    response = recv_with_size(sock)
    p.DecodePacket(response)

    #get file array is a cool guy and has all the files if successful
    for filename, size in p.getFileArray():
        print filename, size, "bytes"

    sock.close()
Esempio n. 31
0
def parse_command(command, socket, destination):

    commandSplit = command.split(' ')
    work = commandSplit[0]
    parameters = commandSplit[1:]

    if work == "PRINT":

        if len(parameters) == 0:
            print("Invalid usage of command 'PRINT'. Usage: PRINT <string>")

        else:
            workerCount = input(
                "Enter the number of workers to send the work to (ALL for every worker): "
            )
            if workerCount.upper() == "ALL":
                send_work(Packet.Packet(Packet.JOB_DESCRIPTION, data=command),
                          socket, destination)
            elif workerCount.isnumeric():
                send_work(
                    Packet.Packet(Packet.JOB_DESCRIPTION,
                                  data=(command + " SENDTO " + workerCount)),
                    socket, destination)
            else:
                print("Invalid entry")

    elif work == "DECRYPT" or work == "ENCRYPT":

        if len(parameters) < 2:
            print(
                f"Invalid usage of command '{work}'. Usage: {work} <filename> <key>"
            )

        else:
            Packet.send_file(socket, destination, parameters[0])
            workerCount = input(
                "Enter the number of workers to send the work to (ALL for every worker): "
            )
            if workerCount.upper() == "ALL":
                workDescription = Packet.Packet(
                    Packet.JOB_DESCRIPTION, 0,
                    "XOR " + ' '.join(parameters[1:]))
                send_work(workDescription, socket, destination)
                receive_results(socket, destination, "XOR")
            elif workerCount.isnumeric():
                workDescription = Packet.Packet(
                    Packet.JOB_DESCRIPTION, 0, "XOR " +
                    ' '.join(parameters[1:]) + " SENDTO " + workerCount)
                send_work(workDescription, socket, destination)
                receive_results(socket, destination, "XOR")
            else:
                print("Invalid entry")

    elif work == "FINDKEY":

        if len(parameters) != 3:
            print(
                "Invalid usage of command 'FINDKEY'. Usage: FINDKEY <encrypted> <expected> <key length>"
            )
        else:
            send_work(Packet.Packet(Packet.JOB_DESCRIPTION, data=command),
                      socket, destination)
            workerCount = input(
                "Enter the number of workers to send the work to (ALL for every worker): "
            )
            if workerCount.upper() == "ALL":
                send_work(Packet.Packet(Packet.JOB_DESCRIPTION, data=command),
                          socket, destination)
            elif workerCount.isnumeric():
                send_work(
                    Packet.Packet(Packet.JOB_DESCRIPTION,
                                  data=(command + " SENDTO " + workerCount)),
                    socket, destination)
            else:
                print("Invalid entry")
Esempio n. 32
0
def starItem(pos, currStar, itemMaxStar, userMaxStar, itemid):
    print('{0} {1}'.format("Position: ".ljust(padding), str(pos)))
    slotStartingMeso = Character.GetMeso()
    slotStartingStar = currStar
    currCode = None

    if itemid in whitelist:
        return

    while currStar < userMaxStar and currStar < itemMaxStar and Inventory.GetItem(
            1, pos).valid:
        if GameState.IsInGame():
            print("#-----------------------Star-----------------------#")
            print('{0} {1}'.format("Starring From: ".ljust(padding),
                                   str(currStar)))
            print('{0} {1}'.format("User Max stars: ".ljust(padding),
                                   str(userMaxStar)))
            print('{0} {1}'.format("Item max stars: ".ljust(padding),
                                   str(itemMaxStar)))
            print('{0} {1}'.format("Item ID: ".ljust(padding), str(itemid)))

            beforeMeso = Character.GetMeso()

            cPacket = Packet.COutPacket(StarForceHeader)
            cPacket.Encode1(StarCatchingOpcode)
            Packet.SendPacket(cPacket)

            ciPacket = Packet.WaitForRecv(StarForceRecv, 10000)
            if ciPacket.GetRemaining() != 12 or ciPacket.ReadLong(
                    1) != StarCatchingOpcode:
                print('star catching packet error', flush=True)
                return

            level = ciPacket.ReadLong(1)
            # 0130 01 053ADEC9 FFF5 [01] 678C8108 00000001 FFFFFFFF 01 00
            # 0130 01 058B16B9 FFF5 [01] 54A8F589 00000001 FFFFFFFF 00 00
            # star the item
            oPacket = Packet.COutPacket(StarForceHeader)
            oPacket.Encode1(1)
            oPacket.Encode4(int(time.monotonic() * 1000))
            oPacket.Encode2(toHex(pos, 16))
            oPacket.Encode1(1)
            oPacket.Encode4(GenSecretCode(ciPacket.ReadLong(4)))
            oPacket.Encode4(1)
            oPacket.Encode4(0xFFFFFFFF)
            if safeguard and currStar in range(12, 17):
                oPacket.Encode1(1)
                print("SAFEGUARDING")
            else:
                oPacket.Encode1(0)
            oPacket.Encode1(1)
            Packet.SendPacket(oPacket)

            # wait for recv
            iPacket = Packet.WaitForRecv(StarForceRecv, 10000)

            afterMeso = Character.GetMeso()
            iCosted = beforeMeso - afterMeso
            print('{0} {1:,}'.format("Meso Cost of Star: ".ljust(padding),
                                     iCosted))
            print('{0} {1}'.format("iPacket remaining: ".ljust(padding),
                                   iPacket.GetRemaining()),
                  flush=True)

            # update current star counter
            currStar = Inventory.GetItem(1, pos).currentStar

            # get max star again in case item blew up
            # item blown up means itemMaxStar = 0
            itemMaxStar = Inventory.GetItem(1, pos).maxStar

    slotEndingMeso = Character.GetMeso()
    slotEndingStar = currStar
    slotTotalCost = slotStartingMeso - slotEndingMeso
    if (slotStartingMeso - slotEndingMeso) != 0:
        print('{0} {1:,} meso from star {2} to {3}\n'.format(
            "Total Cost: ".ljust(padding), slotTotalCost,
            str(slotStartingStar), str(slotEndingStar)),
              flush=True)
Esempio n. 33
0
def copyToDFS(address, from_path, to_path):
	"""
		Copy file from local machine in 'from_path' to the remote DFS
		in 'to_path' by dividing it to blocks and sending them to the
		available data nodes (managed by MDS)
	"""

	# Create a connection to the data server
	print "Connecting to MDS..."
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect(address)
	print "Connected!"


	# Create `put` packet with the server path and fsize, and send it to MDS
	fsize = os.path.getsize(from_path)
	p1 = Packet()
	p1.BuildPutPacket(to_path, fsize)
	sock.send(p1.getEncodedPacket())
	print "Sent `put` request to MDS for local file '%s'!" % from_path


	# Get the list of data nodes (if no error)
	msg = sock.recv(DNODE_BUFFER)
	sock.close()

	if msg == "DUP":
		print "Tried inserting a file that already exists! Exiting..."
		sys.exit(0)

	p2 = Packet()
	p2.DecodePacket(msg)
	nodelist = p2.getDataNodes()
	print "Received list of %d data nodes! Closing connection to MDS..." % len(nodelist)


	# Divide the file in blocks and send them to data nodes
	chunk, total = 1, 0
	cuota = int(ceil(float(fsize) / len(nodelist)))
	block_list = []

	fd = open(from_path, "rb")

	for ip, prt in nodelist:


		# Connect to data node and send data block `put` request
		sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sock2.connect((ip, prt))
		p3 = Packet()
		p3.BuildCommand("put")
		sock2.send(p3.getEncodedPacket())
		print "\n\t- Sent `put` request to data node at %s:%s!" % (ip, prt)


		# Blocking dummy variable so that messages don't corrupt themselves
		OK = sock2.recv(2) # 2 because it's only "OK"
		if OK != "OK":
			print "\nReply from %s:%s is corrupted! Exiting..." % (ip, prt)
			sock2.close()
			fd.close()
			sys.exit(0)


		# Send cuota little by little
		count = 0

		while count < cuota and total < fsize:

			data = fd.read(SUBCHUNK_BUFFER)
			sent = sock2.send(data)
			count += sent
			total += sent

			# Blocking dummy variable so that messages don't corrupt themselves
			MORE = sock2.recv(4) # 4 because it's only "MORE"
			if MORE != "MORE":
				print "\nReply from %s:%s is corrupted! Exiting..." % (ip, prt)
				sock2.close()
				fd.close()
				sys.exit(0)


		# Notify data node that chunk was sent
		print "\t- Sent chunk #%d!" % chunk
		chunk += 1
		sock2.send("DONE")


		# Receive block id from data node
		bid = sock2.recv(36) # 36 because uuids are 36 in length
		block_list.append((ip, prt, bid))
		print "\t- Received '%s'. Saving for later..." % bid


		# Disconnect from data node
		sock2.close()
		print "\t- Disconnecting from data node..."


	# Close local file
	fd.close()


	# Check if whole file was sent
	if total == fsize:
		print "\nWhole file sent!!!"
	else:
		print "The file wasn't sent completely! Exiting..."
		sys.exit(0)


	# Notify the MDS where the blocks are saved
	sock3 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock3.connect(address)
	print "Reconnecting to MDS..."
	p4 = Packet()
	p4.BuildDataBlockPacket(to_path, block_list)
	sock3.send(p4.getEncodedPacket())
	print "Sent block list! Disconnecting from MDS..."


	# Disconnect from MDS
	sock3.close()
	print "Done!"
Esempio n. 34
0
import Packet
'''
TESTS for errors or exceptions for the Packet class
this DOES NOT test that the methods do what they are intended to
'''

a = Packet.Packet()
''' Valid segment changes '''
print("\nTrying valid setSegment calls")
print("\nBEFORE", a.packet())
try:
    a.setSegment("ACK", 214764870)
    a.setSegment("SEQ", 1234567)
    a.setSegment("LENGTH", 718293)
    print('\n***TEST PASSED***')
except Exception as e:
    print(e)
    print("\nTEST FAILED - an Exception shouldn't have occured")
print("\nAFTER", a.packet())
''' invalid segment changes '''
print("\n\nTrying invalid setSegment calls")
try:
    a.setSegment("SEQ", 0x252525252)  #too big value
    print("TEST PART 1 FAILED - an Exception should have occured")
except Exception as e:
    print(e)
    print('***TEST PART 1 PASSED***')
try:
    a.setSegment("abc", 0x252522)  #invalid label
    print("TEST PART 2 FAILED - an Exception should have occured")
except Exception as e:
def readfileHoneyPatchSomePackets(webpageId, traceIndex):
    trace = Trace(webpageId)
    start = 0

    if config.DATA_SOURCE == 6:
        absPath = __constructAbsolutePathHoneyPatch(webpageId, traceIndex)
    elif config.DATA_SOURCE == 61:
        absPath = __constructAbsolutePathHoneyPatchMultiClass(
            webpageId, traceIndex)
    elif config.DATA_SOURCE == 62 or config.DATA_SOURCE == 63 or config.DATA_SOURCE == 64:
        absPath = __constructAbsolutePathHoneyPatchMultiClassAttackBenign(
            webpageId, traceIndex)

    packetCtr = 0

    if absPath:
        try:
            pcapReader = dpkt.pcap.Reader(file(absPath, "rb"))
        except:
            print absPath + ' has a problem reading file, testing'
            return trace
            #sys.exit(2)

        #print absPath + ' is ok'

        firstTCP = False
        firstPacketkPort = 70000

        try:
            for ts, buf in pcapReader:
                eth = dpkt.ethernet.Ethernet(buf)
                ip = eth.data
                tcp = ip.data

                if "TCP" not in str(type(
                        ip.data)) or tcp.sport == 80 or tcp.dport == 80:
                    continue

                if firstTCP == False:
                    firstTCP = True
                    firstPacketkPort = tcp.sport  # uplink

                if start == 0: start = ts
                direction = Packet.UP

                if (tcp.sport == 22):
                    direction = Packet.DOWN
                if (config.DATA_SOURCE == 3):
                    if (tcp.sport == 9001 or tcp.sport == 443):
                        direction = Packet.DOWN
                if (config.DATA_SOURCE == 4 or config.DATA_SOURCE == 41
                        or config.DATA_SOURCE == 42 or config.DATA_SOURCE == 6
                        or config.DATA_SOURCE == 61 or config.DATA_SOURCE == 62
                        or config.DATA_SOURCE == 63
                        or config.DATA_SOURCE == 64):
                    #if (tcp.sport==8080 or tcp.sport==443):
                    #direction = Packet.DOWN
                    if tcp.sport == firstPacketkPort:
                        direction = Packet.UP
                    else:
                        direction = Packet.DOWN

                delta = int(round(((ts - start) * 1000), 0))
                length = ip.len + Packet.HEADER_ETHERNET

                trace.addPacket(Packet(direction, delta, length))

                packetCtr += 1
                # Some packets from the trace are added
                if config.NUM_TRACE_PACKETS != -1 and packetCtr >= config.NUM_TRACE_PACKETS:
                    break
        except:
            print 'file ' + absPath + ' has a problem.'

    return trace
Esempio n. 36
0
    def selective_repeat(self):

        window = Window(self.window_size)
        ack = None
        new_ack = False

        with open('serverfiles\\' + self.file_requested, 'rb') as f:

            # Calculates the number of required packets to send the file in
            # Advance.
            size = len(f.read())
            f.seek(0)
            packet_num = math.ceil(size / 256)

            # Number of sent packets
            sent = 0

            line = f.read(256)

            while sent != packet_num:

                # returns 3 lists one for ready sockets to read from, one for
                # ready sockets to send on and one for exceptions.
                ready = select([self.sock], [self.sock], [])

                # Ready to send on sockets
                if ready[1]:

                    # If the next_sequence value is still in the window frame
                    # if this is false, then it means we sent all of the packets
                    # currently in the window and we're waiting for ACKs
                    if window.within_win():
                        if line:
                            pkt = Packet.create_udp_packet(
                                self.port, self.dest_addr[1],
                                window.next_seq(), line)
                            print("SERVER sending packet with seq",
                                  window.next_seq())
                            window.send_next_pkt(line)
                            if random.random() > self.loss_prob:
                                self.sock.sendto(pkt, self.dest_addr)
                            line = f.read(256)
                    # Update the window manually if we sent all of the window's
                    # Packets.
                    else:
                        window.update()

                    # If the packets are send but not acked after TIMEOUT time
                    # Send them again with probability of loss.
                    # RESETS TIMER.

                    for s in window.current_s():
                        if s.sent and not s.acked and (time.time() -
                                                       s.timeout) >= TIMEOUT:
                            pkt = Packet.create_udp_packet(
                                self.port, self.dest_addr[1], s.seq_no, s.data)
                            print("SERVER resending packet with seq", s.seq_no)
                            window.update_timer(s.seq_no)
                            if random.random() > self.loss_prob:
                                self.sock.sendto(pkt, self.dest_addr)

                if ready[0]:
                    ack = self.sock.recv(8)
                    sent += 1
                    ack_no = int.from_bytes(ack, 'big')
                    window.acknowledge(ack_no)

            self.sock.sendto(b'EOF', self.dest_addr)
Esempio n. 37
0
 def Start(self):
     """Starts sending first handshake packet"""
     self._connection.Send(Packet.HS_Version(1.0).wrap().encode())
Esempio n. 38
0
 def send(self, cmd):
     packet = Packet(source=Packet.FROM_CLIENT, data_cmd=cmd)
     packet.send(self.socket)
     packet.save(self.packet_repo)
     print packet.data
Esempio n. 39
0
def sending(sock, file):
    global Mutex
    global BASE
    global send_timer

    packets = []
    seq_number = 0

    # Generates all the packets from the picture
    while True:
        packet = Packet.make_packet(file, seq_number)
        if not packet:
            break
        packets.append(packet)
        seq_number += 1

    total_packets = len(packets)
    print("Packet total is ", total_packets)
    win_size = set_window(total_packets)
    next_packet_num = 0

    print('\nBeginning Receiving Thread')

    _thread.start_new_thread(receiving, (clientSocket, ))

    while BASE < total_packets:
        Mutex.acquire()

        # Send packets within the window
        while next_packet_num < BASE + win_size:
            print('Sending packet', next_packet_num)
            UDP.send(packets[next_packet_num], sock, serverAddress, loss_prob, cor_prob)
            next_packet_num += 1

            # Start the timer
            if not send_timer.running():
                print('Starting Timer')
                send_timer.start()

            start_sleep = False

            # Wait for an ack or Timeout occurs
            while send_timer.running() and not send_timer.timeout():
                if Mutex.locked():
                    Mutex.release()
                if not start_sleep:
                    print('Sending thread going to sleep')
                    start_sleep = True
                time.sleep(SLEEP_TIME)
                Mutex.acquire()

            # Handle the timeout case
            if send_timer.timeout():
                print('Timeout has occurred\n')
                send_timer.stop()
                next_packet_num = BASE
            else:
                print('Packets Acked... Shift Window\n')
                win_size = set_window(total_packets)
            Mutex.release()

        # All packets have been sent, send a empty packet
        print("File has been sent!!")
        UDP.send(bytes(), sock, serverAddress, 0, 0)
        fileSend.close()
Esempio n. 40
0
import Packet, datetime, GameState

if GameState.IsInGame() and datetime.datetime.utcnow().second % 3 == 0:
    IlliumPacket = Packet.COutPacket(0x02BA)
    IlliumPacket.EncodeBuffer('[62295010]')
    Packet.SendPacket(IlliumPacket)
    Ark = Packet.COutPacket(0x034B)
    Ark.EncodeBuffer("05 00 00 00 02 B6 C4 04")
    Packet.SendPacket(Ark)
Esempio n. 41
0
def copyToDFS(address, fname, path):
    """ Contact the metadata server to ask to copy file fname,
	    get a list of data nodes. Open the file in path to read,
	    divide in blocks and send to the data nodes. 
	"""
    print('hola')

    # Create a connection to the data server
    # Fill code
    sock = socket.socket()
    sock.connect(address)

    # Read file
    # Fill code
    fd = open(
        './' + path,
        "rb")  #  path is the name of the file stored in our local directory
    block_list = []  # block list of the file
    contents = fd.read()  # content from the file
    fd.close()

    # Create a Put packet with the fname and the length of the data,
    # and sends it to the metadata server
    # Fill code
    p = Packet()
    size = os.path.getsize('./' + path)
    p.BuildPutPacket(fname, size)
    file_attributes = p.packet  # contains the dictionary {"command": "put", "fname": fname, "size": size} from the property self.packet
    sock.sendall(
        p.getEncodedPacket()
    )  # send to meta data server the put packet; self.packet = {"command": "put", "fname": fname, "size": size}

    # If no error or file exists
    # Get the list of data nodes.
    # Divide the file in blocks
    # Send the blocks to the data servers

    # Fill code
    response = sock.recv(1024)  # data nodes from meta-data to save the file
    sock.close()
    sock = None
    if response != b'DUP':
        p.DecodePacket(response)
    else:
        print('DUP')
        return
    data_nodes = p.packet
    data_nodes = data_nodes["servers"]
    parts = int(
        len(contents) /
        len(data_nodes))  # each block list will contain this size "parts"
    print(data_nodes, 'response of data nodes')
    print(file_attributes, 'file_attributes')

    newDataNodes = []  # will contain the data nodes that are running
    for host, port in data_nodes:
        isRunning = isPortRunning(host, port)
        if isRunning:
            print(host, port, 'RUNNING')
            newDataNodes.append([host, port])

    data_nodes = newDataNodes
    print(data_nodes, 'data nodes running')

    # dividing the file over the number of data servers
    fd = open('./' + path, "rb")
    newContents = fd.read(parts)
    contents_left = len(contents) % parts  # the characters left
    for i in range(len(data_nodes)):
        if i == len(data_nodes) - 2:
            block_list.append(newContents)
            newContents = fd.read(parts + contents_left)
        else:
            block_list.append(newContents)
            newContents = fd.read(parts)
    print(block_list, 'blocks of data')
    fd.close()

    # sending the blocks to the data servers
    data = []
    for i in range(len(data_nodes)):
        host = data_nodes[i][0]
        port = data_nodes[i][1]
        block = block_list[i]

        sock = socket.socket()
        sock.connect((host, port))
        p.BuildPutPacket(path, size)
        # p.BuildPutResponse(block_list[i]) # block of data, i is the number of the new file
        sock.sendall(p.getEncodedPacket())
        response = sock.recv(1024)
        print(response, 'line 155')
        print(type(block_list[i]), 'line 116')
        try:
            sock.sendall(block_list[i].encode())
        except:
            sock.sendall(block_list[i])

        # if type(block_list[i]) == 'bytes':
        # 	sock.sendall(block_list[i])
        # else:
        # 	sock.sendall(block_list[i].encode())
        chunk_id = sock.recv(1024)
        chunk_id = chunk_id.decode()  # chunk_id or uuid
        data.append((host, port, chunk_id))
        sock.close()
        sock = None

    sock = socket.socket()
    sock.connect(
        address)  # connect socket to meta data server to send new block packet

    # Notify the metadata server where the blocks are saved.
    # Fill code
    p.BuildDataBlockPacket(fname, data)
    sock.sendall(p.getEncodedPacket())

    sock.close()
    sock = None
Esempio n. 42
0
def copyToDFS(address, fname, path):
    """ Contact the metadata server to ask to copu file fname,
	    get a list of data nodes. Open the file in path to read,
	    divide in blocks and send to the data nodes. 
	"""

    # Create a connection to the data server

    # Fill code
    sockt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sockt.connect(address)

    file_size = os.path.getsize(path)
    pack = Packet()
    pack.BuildPutPacket(fname, file_size,
                        0)  # THIS IS DUMMY BLOCK SIZE. Metadata won't need it.
    sockt.sendall(pack.getEncodedPacket())

    status = sockt.recv(3)
    print(status)

    dat = sockt.recv(4096)
    data = Packet()
    data.DecodePacket(dat)
    message = data.getDataNodes()
    print(message)
    # Read file
    read = open(path, 'r+b')
    block_ids = []
    # Fill code
    if status == "NAK":
        print("Error copying files.")
    else:
        bk_size = (file_size / len(message)) + 1

        for i in message:
            # sock_to_dnode.sendto
            # print(read.name, tuple(message[i]))
            sock_to_dnode = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            address1 = i[0]
            port1 = i[1]
            sock_to_dnode.connect((address1, port1))
            dnode_pack = Packet()
            dnode_pack.BuildPutPacket(fname, file_size, bk_size)
            sock_to_dnode.sendall(dnode_pack.getEncodedPacket())
            print("Put Packet sent.")

            if sock_to_dnode.recv(1024) == "OK":
                sock_to_dnode.sendall(read.read(bk_size))
                block_ids.append((address1, port1, sock_to_dnode.recv(1024)))
                print(block_ids)

            sock_to_dnode.close()

    sockt.close()
    read.close()

    # Create a Put packet with the fname and the length of the data,
    # and sends it to the metadata server

    # Fill code

    # If no error or file exists
    # Get the list of data nodes.
    # Divide the file in blocks
    # Send the blocks to the data servers

    # Fill code

    # Notify the metadata server where the blocks are saved.
    socket_blks = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket_blks.connect(address)

    bk_ids_to_meta = Packet()
    bk_ids_to_meta.BuildDataBlockPacket(fname, block_ids)
    socket_blks.sendall(bk_ids_to_meta.getEncodedPacket())
    socket_blks.close()
Esempio n. 43
0
def copyFromDFS(address, fname, path):
    """ Contact the metadata server to ask for the file blocks of
	    the file fname.  Get the data blocks from the data nodes.
	    Saves the data in path.
	"""

    # Contact the metadata server to ask for information of fname

    socke = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socke.connect(address)

    packt = Packet()
    packt.BuildGetPacket(fname)
    socke.sendall(packt.getEncodedPacket())

    # Fill code
    md_response = socke.recv(4096)

    if md_response == "NFOUND":
        print("NO, DALE REWIND!")
    else:

        # print "tamo aki"
        print md_response
        packt.DecodePacket(md_response)
        dnodes = packt.getDataNodes()
        fsize = packt.packet["fsize"]

        # print(dnodes)

        file_to_append = open(path, 'ab')

        blocks_string = ""

        for i in dnodes:
            print("address:", i[0])
            print("port:", i[1])
            print("blockid:", i[-1])

            address1 = i[0]
            port1 = i[1]
            block_id = i[-1]

            sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sok.connect((address1, port1))

            packert = Packet()
            packert.BuildGetDataBlockPacket(block_id)
            sok.sendall(packert.getEncodedPacket())

            block_size = (fsize / len(dnodes)) + 1
            # print (block_size)
            last_data_block = block_size - (fsize / block_size - 1)
            # print (last_data_block)
            bytes_atm = 0

            while bytes_atm < block_size:

                block = sok.recv(4096)
                # print (block)
                blocks_string += block
                bytes_atm += sys.getsizeof(block)
                # print bytes_atm

                if bytes_atm == last_data_block:
                    break

        file_to_append.write(blocks_string)
        file_to_append.close()
Esempio n. 44
0
 def issue_os_notification(self, title, content):
     self.main.Client.send(
         Packet.Packet(0, {
             "title": title,
             "content": content
         }))
Esempio n. 45
0
# -*- coding:utf-8 -*-

"""
 UDP 服务器
"""

import socket
import Packet

G_Packet = Packet.Packet()


def SocketHandler(sock):
    """
    协程处理
    :param sock:
    :return:
    """
    total_packet = 0
    while True:
        data, address = sock.recvfrom(8192)
        G_Packet.writeMulitBytes(data)
        G_Packet.position = 0
        while G_Packet.length() > 4:
            pack_size = G_Packet.readShort()
            if pack_size > G_Packet.length() - 2:
                G_Packet.position = G_Packet.length()
                break
            else:
                data = G_Packet.readMulitBytes(pack_size)
                total_packet += 1
Esempio n. 46
0
 def recv(self):
     msg = self.socket.recv(65535)
     packet = Packet(source=Packet.FROM_SERVER, data_cmd=msg)
     packet.save(self.packet_repo)
     print packet.data
Esempio n. 47
0
    total_pkts = 0
    local_pkts = 0
    for line in eval_file:
        lines_read += 1
        if ((STATUS_INTERVAL != 0) and (lines_read % STATUS_INTERVAL == 0)):
            count = 0
            for key1, val1 in unmatched_sent.items():
                count += len(val1)

            print("lines read: " + str(lines_read) + \
                  ", unmatched: " + str(count))

        if ">" not in line:
            continue
        toks = line.split()
        packet = Packet(toks)

        total_pkts += 1

        src = packet.get("src")
        dst = packet.get("dst")

        if src.startswith(prefix):
            # Sent packet.  We are expecting an ACK later.
            if (not packet.get("seq_begin")) or (not packet.get("seq_end")):
                # No seq number, must be an ACK generated in the target cluster
                continue

            if packet.get("seq_begin") == packet.get("seq_end"):
                packet.set("expected_ack", int(packet.get("seq_end")) + 1)
            else:
Esempio n. 48
0
# ---------------------------------------------------------------------------
# Module initialization
# ---------------------------------------------------------------------------

# Packet.IrigDataDll.szCmdWord.restype = ctypes.c_char_p

# This test code just opens an IRIG file and does a histogram of the
# data types

if __name__ == '__main__':
    print "IRIG 1106 Decode 1553"

    import Time

    # Make IRIG 106 library classes
    PktIO = Packet.IO()
    TimeUtils = Time.Time(PktIO)
    Decode1553 = Decode1553F1(PktIO)

    # Initialize counts variables
    TR = ("R", "T")
    #    Counts = {}
    PacketCount = 0
    MsgCount = 0
    DataType = Packet.DataType()

    if len(sys.argv) > 1:
        RetStatus = PktIO.open(sys.argv[1], Packet.FileMode.READ)
        if RetStatus != Status.OK:
            print "Error opening data file %s" % (sys.argv[1])
            sys.exit(1)
Esempio n. 49
0
 def udt_send(self, seq_no, data):
     if random.random() > self.loss_prob:
         pkt = Packet.create_udp_packet(self.port, self.dest_addr[1],
                                        seq_no, data)
         self.sock.sendto(pkt, self.dest_addr)
Esempio n. 50
0
 def send_find_a_relay_note_packet(self, relay_note, circuit_id):
     pkt = Packet.HandshakePacket(
         circuit_id,
         str(HANDSHAKE_COMMAND["FIND_A_RELAY_NOTE"]) + str(self.id))
     pkt.set_fixed_route_table(relay_note)
     self.send(relay_note[1], pkt)
Esempio n. 51
0
 def __init__(self):
     self.p = Packet()
Esempio n. 52
0
 def send_a_establish_rendezvous_packet(self, relay_note, circuit_id):
     pkt = Packet.HandshakePacket(
         circuit_id,
         str(HANDSHAKE_COMMAND["ESTABLISH_RENDEZVOUS"]) + str(circuit_id))
     pkt.set_fixed_route_table(relay_note)
     self.send(relay_note[1], pkt)
Esempio n. 53
0
def copyFromDFS(address, from_path, to_path):
	"""
		Contact the metadata server to ask for the file blocks,
		and then get the data blocks from the data nodes.
		Saves the data in 'to_path'.
	"""


	# Contact the MDS to ask for information of 'to_path'
	print "Connecting to MDS..."
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect(address)
	print "Connected!"


	# Create `get` packet with the remote file name, and send it to MDS
	p1 = Packet()
	p1.BuildGetPacket(from_path)
	sock.send(p1.getEncodedPacket())
	print "Sent `get` request to MDS for remote file '%s'!" % from_path


	# If there is no error, retrieve the data blocks
	msg = sock.recv(CHUNKLIST_BUFFER)
	sock.close()

	if msg == "NFOUND":
		print "File '%s' doesn't exist in DFS server! Exiting..." % from_path
		sys.exit(0)

	p2 = Packet()
	p2.DecodePacket(msg)
	metalist = p2.getDataNodes()
	fsize = p2.getFileSize()
	print "Received list of %d chunks! Closing connection to MDS..." % len(metalist)


	# Save the file in local machine (in 'to_path')
	chunk, total = 1, 0
	fd = open(to_path, "wb")

	for ip, prt, cid in metalist:


		# Connect to data node and send data block `get` request
		sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sock2.connect((ip, prt))
		p3 = Packet()
		p3.BuildGetDataBlockPacket(cid)
		sock2.send(p3.getEncodedPacket())
		print "\n\t- Sent `get` request to data node at %s:%s!" % (ip, prt)


		# Receive data little by little
		while True:

			data = sock2.recv(SUBCHUNK_BUFFER)
			if data == "DONE":
				break
			fd.write(data)
			total += len(data)

			# Blocking dummy variable so that messages don't corrupt themselves in data node
			sock2.send("MORE")


		# Disconnect from data node
		print "\t- Received chunk #%d!" % chunk
		chunk += 1
		sock2.close()
		print "\t- Disconnecting from data node..."


	# Close local file
	fd.close()


	# Check if file was recieved completely
	if total == fsize:
		print "\nWhole file received!!!"
	else:
		print "File wasn't received completely!"
Esempio n. 54
0
 def send_ack_pkt(self, pkt):
     seq = pkt.get_seq()
     recv = pkt.get_from()
     reply = Packet.ACKPacket(seq, self.id, recv)
     global_send(reply)
Terminal.SetRushByLevel(False)

while True:
    time.sleep(2)
    field_id = Field.GetID()
    jobid = Character.GetJob()
    level = Character.GetLevel()
    pos = Character.GetPos()
    if jobid == -1 or level == -1:
      #not in game
      continue
    if Terminal.IsRushing():
      continue
#if the script doesn't start right away, then go to any Victoria Island or Ossyria map
    elif field_id >= 100000000 and field_id <= 340000000:
      oPacket = Packet.COutPacket(0x019A)
      oPacket.Encode2(0x0002)
      Packet.SendPacket(oPacket)
      time.sleep(1)
      oPacket = Packet.COutPacket(0x022B) 
      oPacket.EncodeBuffer("[010000000100000004000000]")
      Packet.SendPacket(oPacket)
    elif field_id == 350140000:
      oPacket = Packet.COutPacket(0x00D3)
      oPacket.Encode4(int(time.monotonic()*1000))#S/O to Qybah for showing me how to do this
      oPacket.Encode4(0x00532A20)
      oPacket.Encode1(0xFD)
      oPacket.Encode1(0x00)
      oPacket.Encode4(0x000F009A)
      oPacket.Encode1(0x00)
      oPacket.Encode1(0x00)
Esempio n. 56
0
def parse_fct(eval_file, fct_file, traffic_matrix, numOfSubtrees,
              numOfToRsPerSubtree, numOfServersPerRack, evaluatedPrefix,
              includeLocalFlows):
    flows = dict()  # [src_num, dst_num, base_seq]

    for line in eval_file:
        if ">" not in line:
            continue
        toks = line.split()
        pkt = Packet(toks)

        src = pkt.get("src")
        dst = pkt.get("dst")
        stoks = src.split(".")
        dtoks = dst.split(".")

        if src.startswith(evaluatedPrefix) and dst.startswith(evaluatedPrefix):
            if includeLocalFlows:
                if pkt.get("tor") != stoks[2] \
                        or int(pkt.get("svr")) != int(stoks[3])/4:
                    # don't double count local receives
                    continue
            else:
                continue

        # Set the base sequence # if it's the SYN
        # Unless it's an echo. Then ignore it.
        if ("S" in pkt.get("flags")) and (not pkt.get('ack_num')):
            src_num = (int(stoks[3]) // 4) + \
                      numOfServersPerRack * int(stoks[2]) + \
                      numOfToRsPerSubtree * numOfServersPerRack * int(stoks[1])
            dst_num = (int(dtoks[3]) // 4) + \
                      numOfServersPerRack * int(dtoks[2]) + \
                      numOfToRsPerSubtree * numOfServersPerRack * int(dtoks[1])

            if src not in flows:
                flows[src] = dict()
            if dst in flows[src]:
                # SYN retransmit
                assert flows[src][dst][2] == int(pkt.get("seq_begin"))
            flows[src][dst] = [src_num, dst_num, int(pkt.get("seq_begin"))]

        # check if this is the end of a flow
        if pkt.get('ack_num') and (dst in flows) and (src in flows[dst]):
            dst_num, src_num, seq_base = flows[dst][src]
            ack_num = int(pkt.get("ack_num"))

            # If we're not expecting anything else, the other side is probably
            # still sending
            if len(traffic_matrix[dst_num][src_num]) <= 0:
                # Not sure why this is 2 instead of 1...
                if seq_base + 2 < ack_num:
                    print("Extra packet!", seq_base, pkt)
                continue

            start_time, flow_size = traffic_matrix[dst_num][src_num][0]
            end_seq = seq_base + flow_size

            while ack_num >= end_seq:
                if ack_num - end_seq > 500:
                    # Missed a few packets.  A few of these are expected
                    print("Skipping ahead!", ack_num - end_seq, \
                          "seq_base", seq_base, "flow_size", flow_size, pkt)

                # write
                fct_file.write("%d %d %f %f %f\n" %
                               (dst_num, src_num, start_time, pkt.get('time'),
                                pkt.get('time') - start_time))
                traffic_matrix[dst_num][src_num].pop(0)

                seq_base = seq_base + flow_size
                flows[dst][src] = [dst_num, src_num, seq_base]

                # If this was the last one we were expecting, we're done
                if len(traffic_matrix[dst_num][src_num]) <= 0:
                    break

                start_time, flow_size = traffic_matrix[dst_num][src_num][0]
                end_seq = seq_base + flow_size
Esempio n. 57
0
def packet_maker(seq, message):
    """Creates a packet with sequence number seq
    and of data = message"""

    packet = Packet.Packet(1, seq, len(message), message)
    return packet
Esempio n. 58
0
 def _shutdown(self,connection,send=False):
    """ Shuts down and stops the connection"""
    if send:
       connection.Send(Packet.Error().wrap().encode())
    connection.Shutdown()
    connection.Stop()
Esempio n. 59
0
    try:
        created_socket.connect((ip, port))
    except socket.error, e:
        print("Connection with the server failed. Error: " + str(e)
              )  #error if connection fails
        sys.exit(1)

    print("Connection to the MetaData Server was successful."
          )  #if it doesn't fail, the connection was succesful!

    # We have this try where it will continue checking till the reply is not equal to NAK.
    # When that occurs it will check if there are any existing files and print them out.
    # If there are no files then it will say so and exit the while if eitehr of these happen.
    try:
        packet = Packet()
        reply = "NAK"

        while reply == "NAK":
            packet.BuildListPacket()
            created_socket.sendall(packet.getEncodedPacket())
            reply = created_socket.recv(1024)
            if reply != "NAK":
                packet.DecodePacket(reply)
                file_list = packet.getFileArray()

                if not file_list:
                    print "No files exists here."

                else:
                    for a in file_list:
Esempio n. 60
0
def import_ch10(irig_filename, hdf5_filename, status_callback=None):
    # Make IRIG 106 library classes
    pkt_io = Packet.IO()
    time_utils = Time.Time(pkt_io)
    decode1553 = MsgDecode1553.Decode1553F1(pkt_io)

    # Initialize variables
    packet_count = 0
    packet_count_1553 = 0
    msg_count_1553 = 0

    # Open the IRIG file
    ret_status = pkt_io.open(irig_filename, Packet.FileMode.READ)
    if ret_status != Status.OK:
        print "Error opening data file %s" % (irig_filename)
        sys.exit(1)

    # If using status callback then get file size
    if status_callback != None:
        file_size = os.stat(irig_filename).st_size

#        ret_status = time_utils.SyncTime(False, 10)
#        if ret_status != Py106.Status.OK:
#            print ("Sync Status = %s" % Py106.Status.Message(ret_status))
#            sys.exit(1)

# Set the default 1553 message table layout version
    layout_version = 2

    # Open the PyTable tables
    ch10_h5_file = tables.openFile(hdf5_filename,
                                   mode="w",
                                   title="Ch 10 Data File")

    # Create the 1553 message table
    if layout_version == 1:
        ch10_bus_data = ch10_h5_file.createVLArray("/",
                                                   "Bus_Data",
                                                   tables.ObjectAtom(),
                                                   title="1553 Bus Data")
    elif layout_version == 2:
        ch10_bus_data = ch10_h5_file.createVLArray("/",
                                                   "Bus_Data",
                                                   tables.UInt16Atom(),
                                                   title="1553 Bus Data")

    ch10_bus_data.attrs.layout_version = layout_version

    # Create the 1553 message index table
    ch10_bus_data_index = ch10_h5_file.createTable("/", "Bus_Data_Index",
                                                   IndexMsg1553,
                                                   "1553 Bus Data Index")

    # Iterate over all the IRIG packets
    for PktHdr in pkt_io.packet_headers():
        packet_count += 1

        # Update the callback function if it exists
        if status_callback != None:
            (status, offset) = pkt_io.get_pos()
            progress = float(offset) / float(file_size)
            status_callback(progress)

        if PktHdr.DataType == Packet.DataType.IRIG_TIME:
            pkt_io.read_data()
            time_utils.SetRelTime()

        if PktHdr.DataType == Packet.DataType.MIL1553_FMT_1:

            packet_count_1553 += 1
            pkt_io.read_data()
            for Msg in decode1553.msgs():
                msg_count_1553 += 1

                # Extract the import 1553 info
                WC = decode1553.word_cnt(Msg.pCmdWord1.contents.Value)

                # Put the 1553 message data into our storage class
                msg_1553 = Msg1553()
                msg_1553.msg_time = time_utils.RelInt2IrigTime(
                    Msg.p1553Hdr.contents.Field.PktTime)
                msg_1553.chan_id = numpy.uint16(pkt_io.Header.ChID)
                msg_1553.header_flags = Msg.p1553Hdr.contents.Field.Flags
                msg_1553.cmd_word_1 = numpy.uint16(
                    Msg.pCmdWord1.contents.Value)
                msg_1553.stat_word_1 = numpy.uint16(
                    Msg.pStatWord1.contents.Value)
                if (Msg.p1553Hdr.contents.Field.Flags.RT2RT == 0):
                    msg_1553.cmd_word_2 = numpy.uint16(0)
                    msg_1553.stat_word_2 = numpy.uint16(0)
                else:
                    msg_1553.cmd_word_2 = numpy.uint16(
                        Msg.pCmdWord2.contents.Value)
                    msg_1553.stat_word_2 = numpy.uint16(
                        Msg.pStatWord2.contents.Value)
                msg_1553.data = numpy.array(Msg.pData.contents[0:WC])
                msg_1553.layout_version = ch10_bus_data.attrs.layout_version
                DataMsg = msg_1553.encode_tuple()

                ch10_bus_data.append(DataMsg)

                # Store the 1553 command word index
                row_offset = ch10_bus_data.nrows - 1
                time_tuple_utc = msg_1553.msg_time.time.timetuple()
                timestamp_utc = calendar.timegm(time_tuple_utc)
                timestamp_utc += msg_1553.msg_time.time.microsecond / 1000000.0

                new_row = ch10_bus_data_index.row
                new_row['offset'] = row_offset
                new_row['time'] = timestamp_utc
                new_row['channel_id'] = pkt_io.Header.ChID
                new_row['rt'] = Msg.pCmdWord1.contents.Field.RTAddr
                new_row['tr'] = Msg.pCmdWord1.contents.Field.TR
                new_row['subaddr'] = Msg.pCmdWord1.contents.Field.SubAddr
                new_row.append()

            # Done with 1553 messages in packet

    ch10_h5_file.flush()
    pkt_io.close()

    return ch10_h5_file