# File name: Transmitter (transmitter.py)  #                  
# Author: Jackie Ye & Reymon Mercado       #              
# Assignment: FINAL PROJECT for COMP 7005  #                 
# Instructor: Aman Abdulla                 #    
############################################

import socket
import sys
import MyPacket
import MySocket
import config
import pickle
import time

# Creates a TCP/IP socketT
socketT = MySocket.mysocket()

socketT.sock.connect((config.hostnameNE, config.portA))

argument = sys.argv

def sendEot(connection, seqNum):
	print "Sending FIN"
	eot = MyPacket.mypacket(3, seqNum, None, config.windowSize, None)
	eot = pickle.dumps(eot)
	connection.sendall(eot)

with open(argument[1], 'r') as f:
	seqNum = 1
	resend = False
	eotSent = False
# File name: Network Emulator (emulator.py)#                  
# Author: Jackie Ye & Reymon Mercado       #              
# Assignment: FINAL PROJECT for COMP 7005  #                 
# Instructor: Aman Abdulla                 #    
############################################

import socket
import sys
import MyPacket
import MySocket
import config
import random
import pickle

# Creates a TCP/IP socket called "socketT" for the transmitter (transmitter.py)
socketT = MySocket.mysocket() 

# Binds the TCP/IP socket to the port, "portA"
socketT.sock.bind(('', config.portA))

# Listens for incoming connections coming from the transmitter 
socketT.sock.listen(5)
print "Listening on port %s" % config.portA

count = 1

# Mainloop of the network emulator
while True:

	# Accepts connections from the specified socket for the transmitter, "socketT"
	conn, c_addr = socketT.sock.accept()
# loop to find if the sequence number is in the buffer already
def seqNumInArray(array, element):
	for i in xrange(config.windowSize):
		if isinstance(array[i], list):
			if array[i][0] == element:
				return True
	return False

def windowFull(array):
	for i in xrange(config.windowSize):
		if array[i] == None:
			return False
	return True

socket = MySocket.mysocket()
socket.sock.bind((config.hostnameNE, config.portB))
socket.sock.listen(5)
print "listening on port %s" % config.portB

# initialize sequence number
seqNum = 1
buffWin = [None] * config.windowSize

while True:
	print "Waiting for new connections"
	conn, c_adddr = socket.sock.accept()
	print c_adddr, conn

	while True:
		recvBuffer = conn.recv(config.BUFFER_SIZE)