Beispiel #1
0
        public void CreateJobPacket()
        {
            DataAccess DB           = new DataAccess();
            string     prodSheetLoc = DB.GetHydroProdSheetLocation(PartNumber);
            //System.Windows.Forms.MessageBox.Show($"Prod Sheet Location = { prodSheetLoc }");
            bool isNewPath = true;

            if (string.IsNullOrEmpty(prodSheetLoc))
            {
                prodSheetLoc = @"C:\Dev\prodSheet18\masterAcmeHydroProdSheet.xlsx";
            }
            else
            {
                isNewPath = false;
            }
            SFPacket sfObj = new SFPacket()
            {
                item_no           = PartNumber,
                ord_no            = WorkOrder,
                cyc_per           = StdCycle,
                setup_std_lbr_hrs = SetupHours,
                RawMaterial       = RawMaterial,
                PieceWeight       = PieceWeight,
                qty = (int)OrderQty
            };

            ExcelInterop.popHydroProdSheet(prodSheetLoc, sfObj, isNewPath);
        }
Beispiel #2
0
 public void AssignPropWorkOrderChange()
 {
     if (!string.IsNullOrEmpty(WorkOrder))
     {
         DataAccess DB         = new DataAccess();
         SFPacket   packetData = DB.GetPrimaryOpData(WorkOrder);
         PartNumber  = packetData.item_no;
         OrderQty    = packetData.qty;
         StdCycle    = packetData.cyc_per;
         SetupHours  = packetData.setup_std_lbr_hrs;
         RawMaterial = packetData.RawMaterial;
         PieceWeight = packetData.PieceWeight;
     }
 }
        /// <summary>
        /// Takes an incoming SFPacket and decides where to go with it and performs its action
        /// </summary>
        /// <param name="incomingPacket">Packet received from the client</param>
        public void SortPacket(SFPacket incomingPacket)
        {
            _scheduler.myNeighbors.AddNeighbor(incomingPacket._sourceIP.ToString());
            switch(incomingPacket.GetPacketType())
            {
                case SFPacketType.FileTransfer:
                    _fileSaver.SavePacket(incomingPacket.GetPacketData(),incomingPacket._sourceIP.ToString());
                    break;
                case SFPacketType.SearchForFile:
                    ArrayList result = _myFiles.SearchFor(
                        System.Text.Encoding.ASCII.GetString(incomingPacket.GetPacketData()));
                    _outBoundManager.SendFileList(result,incomingPacket._sourceIP);
                    break;
                case SFPacketType.FileList:
                    MemoryStream stream = new MemoryStream(incomingPacket.GetPacketData());
                    BinaryFormatter bf = new BinaryFormatter();
                    ArrayList searchResults = (ArrayList)bf.Deserialize(stream);
                    _scheduler.fileSearchForm.AddResults(searchResults,incomingPacket._sourceIP);
                    break;

                case SFPacketType.FileDownloadRequest:
                    //Get the fileID
                    int fileId=BitConverter.ToInt32(incomingPacket.GetPacketData(),0);
                    //Lookup the file by its ID
                    MyFile file=_myFiles.GetFileByID(fileId);
                    //Send back the file
                    _outBoundManager.SendFile(fileId, file.FileLoc, incomingPacket._sourceIP);
                    break;

                case SFPacketType.GetNeighborList:
                    _outBoundManager.SendNeigbhorList(incomingPacket._sourceIP);
                    break;
                case SFPacketType.NeighborListResponse:
                    MemoryStream nlstream = new MemoryStream(incomingPacket.GetPacketData());
                    BinaryFormatter nlbf = new BinaryFormatter();
                    ArrayList neighbors = (ArrayList)nlbf.Deserialize(nlstream);
                    foreach (Neighbor nb in neighbors)
                        _scheduler.myNeighbors.AddNeighbor(nb.IPAddress);
                    break;

            }
        }
        public void SendFileList(ArrayList files,IPAddress destination)
        {
            byte[] data=null;
            int packetSize = Properties.Settings.Default.PacketDataSize;

            while (data == null || data.Length+2 > packetSize)
            {
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, files);
                data = ms.ToArray();

                if (data.Length+2 > packetSize)
                {
                    //If there are too many items because it goes over the packet size remove
                    //the last item in it
                    files.RemoveAt(files.Count - 1);
                }
            }

            long bytesRead = 0;
            byte[] buffer;
            int packetsSent = 0;

            try
            {
                while (bytesRead + (packetSize) < data.Length)
                {
                    buffer = new byte[packetsSent];
                    Array.Copy(data, buffer, packetsSent);
                    SFPacket packet = new SFPacket(SFPacketType.FileList, buffer);
                    _scheduler.SendPacket(packet, destination);
                    bytesRead += buffer.Length;
                    packetsSent++;
                }

                if (data.Length > bytesRead)
                {
                    byte[] toSend = new byte[data.Length];
                    Array.Copy(data,toSend,((int)(data.Length - bytesRead)));
                    SFPacket finalPacket = new SFPacket(SFPacketType.FileList, toSend);
                    _scheduler.SendPacket(finalPacket, destination);
                    packetsSent++;
                }

                System.Console.WriteLine("Packets Sent: " + packetsSent);
            }
            catch (Exception ex)
            {

            }
        }
 /// <summary>
 /// Sends a packet requesting a file download
 /// </summary>
 /// <param name="fileID">id of the file you want</param>
 /// <param name="dest">IPAddress of the destination</param>
 public void SendFileDownloadRequest(int fileID, IPAddress dest)
 {
     byte[] data = BitConverter.GetBytes(fileID);
     SFPacket packet=new SFPacket(SFPacketType.FileDownloadRequest,data);
     _scheduler.SendPacket(packet,dest);
 }
        private void ThreadedSendFile(object parameters)
        {
            object[] parms = (object[])parameters;
            int fileID=(int)parms[0];
            String file = (String)parms[1];
            IPAddress destination = (IPAddress)parms[2];

            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long bytesRead = 0;
            long fileSize = new FileInfo(file).Length;
            int packetSize=Properties.Settings.Default.PacketDataSize;
            byte[] buffer;
            int packetsSent = 0;
            int toSkip = BitConverter.GetBytes(fileID).Length;
            //try{
                while (bytesRead+(packetSize-toSkip)<fileSize)
                {
                    buffer = br.ReadBytes(packetSize-toSkip);
                    byte[] toSend=ShiftBytes(fileID, buffer);
                    SFPacket packet = new SFPacket(SFPacketType.FileTransfer,toSend );
                    _scheduler.SendPacket(packet, destination);
                    bytesRead += buffer.Length;
                    packetsSent++;
                }

                if (fileSize > bytesRead)
                {
                    buffer = br.ReadBytes((int)(fileSize - bytesRead));
                    byte[] toSend = ShiftBytes(fileID, buffer);
                    SFPacket finalPacket = new SFPacket(SFPacketType.FileTransfer, toSend);
                    _scheduler.SendPacket(finalPacket, destination);
                    packetsSent++;
                }

                byte[] bytes = Encoding.ASCII.GetBytes("EOT");
                byte[] EOT = ShiftBytes(fileID, bytes);
                SFPacket eotPacket = new SFPacket(SFPacketType.FileTransfer, EOT);
                _scheduler.SendPacket(eotPacket, destination);
                packetsSent++;

                br.Close();
                System.Console.WriteLine("Packets Sent: " + packetsSent);
            /*}
            catch (Exception ex)
            {

            }
             */
        }
 /// <summary>
 /// Sends a search request packet to given IP address
 /// </summary>
 /// <param name="query">The query to be sent</param>
 /// <param name="dest">IP address of the person you want to query</param>
 public void SendSearchRequest(String query, IPAddress dest)
 {
     byte[] bytes;
     bytes = Encoding.ASCII.GetBytes(query);
     if (bytes.Length < Properties.Settings.Default.PacketDataSize)
     {
         SFPacket packet = new SFPacket(SFPacketType.SearchForFile, bytes);
         _scheduler.SendPacket(packet, dest);
     }
     else
     {
         throw new Exception("Search Request data size is too large for one packet: " + query);
     }
 }
 public void SendNeighborDownloadRequest(IPAddress dest)
 {
     byte[] bytes;
     bytes = Encoding.ASCII.GetBytes("");
     if (bytes.Length < Properties.Settings.Default.PacketDataSize)
     {
         SFPacket packet = new SFPacket(SFPacketType.GetNeighborList, bytes);
         _scheduler.SendPacket(packet, dest);
     }
 }
        public void SendNeigbhorList(IPAddress dest)
        {
            byte[] data=null;
            int packetSize = Properties.Settings.Default.PacketDataSize;
            ArrayList neighbors = _scheduler.myNeighbors.GetListOfNeighbors();
            while (data == null || data.Length+2 > packetSize)
            {
                BinaryFormatter bf = new BinaryFormatter();
                MemoryStream ms = new MemoryStream();
                bf.Serialize(ms, neighbors);
                data = ms.ToArray();

                if (data.Length+2 > packetSize)
                {
                    //If there are too many items because it goes over the packet size remove
                    //the first item because we hope that was received earlier
                    neighbors.RemoveAt(0);
                }
            }

            long bytesRead = 0;
            byte[] buffer;
            int packetsSent = 0;

            try
            {
                while (bytesRead + (packetSize) < data.Length)
                {
                    buffer = new byte[packetsSent];
                    Array.Copy(data, buffer, packetsSent);
                    SFPacket packet = new SFPacket(SFPacketType.NeighborListResponse, buffer);
                    _scheduler.SendPacket(packet, dest);
                    bytesRead += buffer.Length;
                    packetsSent++;
                }

                if (data.Length > bytesRead)
                {
                    byte[] toSend = new byte[data.Length];
                    Array.Copy(data,toSend,((int)(data.Length - bytesRead)));
                    SFPacket finalPacket = new SFPacket(SFPacketType.NeighborListResponse, toSend);
                    _scheduler.SendPacket(finalPacket, dest);
                    packetsSent++;
                }

                System.Console.WriteLine("Packets Sent: " + packetsSent);
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// Go though the listener and decied what the request is and begin/continue transfer if needed
        /// </summary>
        public void ProcessRequest(object client)
        {
            int msgSize = 1 + Properties.Settings.Default.PacketDataSize;
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[msgSize];
            int bytesRead=0;

            while (bytesRead<Properties.Settings.Default.PacketDataSize)
            {
                int bytesReadThisTime = 0;
                try
                {
                    //blocks until a client sends a message
                    bytesReadThisTime= clientStream.Read(message,bytesRead, msgSize-(bytesRead));
                    bytesRead += bytesReadThisTime;
                }
                catch(Exception ex)
                {
                    throw ex;
                    //a socket error has occured
                    break;
                }

                if (bytesReadThisTime == 0)
                {
                    //the client has disconnected from the server
                    break;
                }

            }

            SFPacket packet = new SFPacket(message, bytesRead);
            String ip = tcpClient.Client.RemoteEndPoint.ToString();
            packet._sourceIP =IPAddress.Parse(ip.Substring(0,ip.IndexOf(':')));
            packetsReceived++;
            clientStream.Close();
            tcpClient.Close();

            _sorter.SortPacket(packet);

            Console.WriteLine("Received: " + bytesRead);
        }