Ejemplo n.º 1
0
        public static void connectThreadLoop()
        {
            bool newConnectData = false;

            while (connectLoop)
            {
                Connect.TransmitFrame tf = new Connect.TransmitFrame
                {
                    source = connectNodeName
                };
                Connect.TransmitData td = new Connect.TransmitData
                {
                    source = connectNodeName
                };

                if (ConnectResource.getAasxStatus == "start")
                {
                    td.destination = ConnectResource.getAasxServerName;
                    td.type        = ConnectResource.getAasxFileType;
                    td.extensions  = ConnectResource.getAasxServerIndex.ToString();
                    tf.data.Add(td);
                    ConnectResource.getAasxStatus = "send";
                    Console.WriteLine("Request file");
                }
                if (ConnectResource.refresh)
                {
                    aasDirectory   = new List <aasDirectoryParameters> {
                    };
                    td.destination = "";
                    td.type        = "getDirectory";
                    td.extensions  = "";
                    tf.data.Add(td);
                    ConnectResource.refresh = false;
                    Console.WriteLine("Refresh");
                }

                HttpClient httpClient;
                if (clientHandler != null)
                {
                    httpClient = new HttpClient(clientHandler);
                }
                else
                {
                    httpClient = new HttpClient();
                }

                string publish     = JsonConvert.SerializeObject(tf, Formatting.Indented);
                var    contentJson = new StringContent(publish, System.Text.Encoding.UTF8, "application/json");

                string content = "";
                try
                {
                    var result = httpClient.PostAsync(connectServer + "/publish", contentJson).Result;
                    content = Connect.ContentToString(result.Content);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Publish: " + e.Message);
                    content = "";
                }

                if (content != "")
                {
                    Console.Write(count++ + " Received content ");
                    newConnectData = false;
                    string node = "";

                    try
                    {
                        Connect.TransmitFrame tf2 = new Connect.TransmitFrame();
                        tf2 = Newtonsoft.Json.JsonConvert.DeserializeObject <Connect.TransmitFrame>(content);

                        node = tf2.source;
                        Console.WriteLine(node);
                        foreach (Connect.TransmitData td2 in tf2.data)
                        {
                            switch (td2.type)
                            {
                            case "directory":
                                if (td2.destination == connectNodeName)
                                {
                                    aasDirectoryParameters adp = new aasDirectoryParameters();
                                    Console.WriteLine("Received directory: " + td2.source);

                                    try
                                    {
                                        adp = Newtonsoft.Json.JsonConvert.DeserializeObject <aasDirectoryParameters>(td2.publish[0]);
                                    }
                                    catch
                                    {
                                        adp = null;
                                        Console.WriteLine("Error receive directory");
                                    }
                                    if (adp != null)
                                    {
                                        aasDirectory.Add(adp);
                                    }
                                    // tf2.data.Remove(td2);
                                }
                                break;

                            case "getaasxFile":
                                if (ConnectResource.getAasxStatus == "send" && td2.destination == connectNodeName && td2.source == ConnectResource.getAasxServerName)
                                {
                                    var parsed3 = JObject.Parse(td2.publish[0]);

                                    string fileName = parsed3.SelectToken("fileName").Value <string>();
                                    string fileData = parsed3.SelectToken("fileData").Value <string>();

                                    ConnectResource.getAasxFileName      = fileName;
                                    ConnectResource.getAasxFileData      = fileData;
                                    ConnectResource.getAasxFileLenBase64 = fileData.Length;

                                    if (ConnectResource.getAasxStatus == "send")
                                    {
                                        ConnectResource.getAasxStatus = "end";
                                    }

                                    Console.WriteLine("Received: " + fileName);
                                }
                                break;

                            case "getaasxBlock":
                                if (ConnectResource.getAasxStatus == "send" && td2.destination == connectNodeName && td2.source == ConnectResource.getAasxServerName)
                                {
                                    var parsed3 = JObject.Parse(td2.publish[0]);

                                    string fileName        = parsed3.SelectToken("fileName").Value <string>();
                                    string fileData        = parsed3.SelectToken("fileData").Value <string>();
                                    int    fileLenBase64   = parsed3.SelectToken("fileLenBase64").Value <int>();
                                    int    fileLenBinary   = parsed3.SelectToken("fileLenBinary").Value <int>();
                                    int    fileTransmitted = parsed3.SelectToken("fileTransmitted").Value <int>();

                                    if (getaasxFile_destination == "")     // first block
                                    {
                                        getaasxFile_destination              = connectNodeName;
                                        getaasxFile_fileName                 = fileName;
                                        getaasxFile_fileLenBase64            = fileLenBase64;
                                        getaasxFile_fileLenBinary            = fileLenBinary;
                                        ConnectResource.getAasxFileName      = getaasxFile_fileName;
                                        ConnectResource.getAasxFileData      = "";
                                        ConnectResource.getAasxFileLenBase64 = fileLenBase64;
                                        ConnectResource.getAasxFileLenBinary = fileLenBinary;
                                    }
                                    getaasxFile_fileData.Add(fileTransmitted, fileData);

                                    //
                                    bool copied = true;
                                    while (copied)
                                    {
                                        copied = false;
                                        foreach (var fd in getaasxFile_fileData)
                                        {
                                            if (getaasxFile_copied == fd.Key)
                                            {
                                                copied = true;
                                                Console.WriteLine("Copy block: " + fd.Key + "/" + fd.Value.Length);
                                                ConnectResource.getAasxFileData += fd.Value;
                                                getaasxFile_copied += fd.Value.Length;
                                            }
                                        }
                                    }
                                    //

                                    fileTransmitted += fileData.Length;
                                    Console.WriteLine("Transmitted: " + fileTransmitted + "/" + fileLenBase64);

                                    if (fileTransmitted == fileLenBase64)
                                    {
                                        foreach (var fd in getaasxFile_fileData)
                                        {
                                            if (getaasxFile_copied <= fd.Key)
                                            {
                                                Console.WriteLine("Copy data: " + fd.Key + "/" + fileLenBase64);
                                                ConnectResource.getAasxFileData += fd.Value;
                                            }
                                        }

                                        ConnectResource.getAasxStatus = "end";
                                        Console.WriteLine("Received: " + fileName + " " + fileTransmitted + " "
                                                          + ConnectResource.getAasxFileData.Length);

                                        getaasxFile_destination = "";
                                        getaasxFile_fileName    = "";
                                        getaasxFile_fileData.Clear();
                                        getaasxFile_fileLenBase64 = 0;
                                        getaasxFile_fileLenBinary = 0;
                                        getaasxFile_copied        = 0;
                                    }
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error receive");
                    }
                    if (newConnectData)
                    {
                    }
                }

                if (getaasxFile_destination != "") // block transfer
                {
                    Thread.Sleep(200);
                }
                else
                {
                    Thread.Sleep(connectUpdateRate);
                }
            }
        }
Ejemplo n.º 2
0
        public static void connectThreadLoop()
        {
            bool newConnectData = false;

            while (connectLoop)
            {
                Connect.TransmitFrame tf = new Connect.TransmitFrame
                {
                    source = connectNodeName
                };
                Connect.TransmitData td = new Connect.TransmitData
                {
                    source = connectNodeName
                };

                if (ConnectResource.getAasxStatus == "start")
                {
                    td.destination = ConnectResource.getAasxServerName;
                    td.type        = "getaasx";
                    td.extensions  = ConnectResource.getAasxServerIndex.ToString();
                    tf.data.Add(td);
                    ConnectResource.getAasxStatus = "send";
                    Console.WriteLine("Request file");
                }
                if (ConnectResource.refresh)
                {
                    aasDirectory   = new List <aasDirectoryParameters> {
                    };
                    td.destination = "";
                    td.type        = "getDirectory";
                    td.extensions  = "";
                    tf.data.Add(td);
                    ConnectResource.refresh = false;
                    Console.WriteLine("Refresh");
                }

                HttpClient httpClient;
                if (clientHandler != null)
                {
                    httpClient = new HttpClient(clientHandler);
                }
                else
                {
                    httpClient = new HttpClient();
                }

                string publish     = JsonConvert.SerializeObject(tf, Formatting.Indented);
                var    contentJson = new StringContent(publish, System.Text.Encoding.UTF8, "application/json");

                string content = "";
                try
                {
                    var result = httpClient.PostAsync(connectServer + "/publish", contentJson).Result;
                    content = Connect.ContentToString(result.Content);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error Publish: " + e.Message);
                    content = "";
                }

                if (content != "")
                {
                    Console.WriteLine(count++ + " Received content");
                    newConnectData = false;
                    string node = "";

                    try
                    {
                        Connect.TransmitFrame tf2 = new Connect.TransmitFrame();
                        tf2 = Newtonsoft.Json.JsonConvert.DeserializeObject <Connect.TransmitFrame>(content);

                        node = tf2.source;
                        foreach (Connect.TransmitData td2 in tf2.data)
                        {
                            switch (td2.type)
                            {
                            case "directory":
                                if (td2.destination == connectNodeName)
                                {
                                    aasDirectoryParameters adp = new aasDirectoryParameters();
                                    Console.WriteLine("Received directory: " + td2.source);

                                    try
                                    {
                                        adp = Newtonsoft.Json.JsonConvert.DeserializeObject <aasDirectoryParameters>(td2.publish[0]);
                                    }
                                    catch
                                    {
                                        adp = null;
                                        Console.WriteLine("Error receive directory");
                                    }
                                    if (adp != null)
                                    {
                                        aasDirectory.Add(adp);
                                    }
                                    tf.data.Remove(td2);
                                }
                                break;

                            case "getaasxFile":
                                if (ConnectResource.getAasxStatus == "send" && td2.destination == connectNodeName && td2.source == ConnectResource.getAasxServerName)
                                {
                                    var parsed3 = JObject.Parse(td2.publish[0]);

                                    string fileName = parsed3.SelectToken("fileName").Value <string>();
                                    string fileData = parsed3.SelectToken("fileData").Value <string>();

                                    ConnectResource.getAasxFileName = fileName;
                                    ConnectResource.getAasxFileData = fileData;

                                    ConnectResource.getAasxStatus = "end";

                                    Console.WriteLine("Received: " + fileName);
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error receive");
                    }
                    if (newConnectData)
                    {
                    }
                }

                Thread.Sleep(connectUpdateRate);
            }
        }