Beispiel #1
0
    protected bool StartMulticastSender(Multicast multicast, int portBase)
    {
        string shell = null;

        string appPath = HttpContext.Current.Server.MapPath("~") + Path.DirectorySeparatorChar + "data" + Path.DirectorySeparatorChar + "apps" + Path.DirectorySeparatorChar;
        string logPath = HttpContext.Current.Server.MapPath("~") + Path.DirectorySeparatorChar + "data" + Path.DirectorySeparatorChar + "logs" + Path.DirectorySeparatorChar;
        if (Environment.OSVersion.ToString().Contains("Unix"))
        {
             string dist = null;
             ProcessStartInfo distInfo = new ProcessStartInfo();
             distInfo.UseShellExecute = false;
             distInfo.FileName = "uname";
             distInfo.RedirectStandardOutput = true;
             distInfo.RedirectStandardError = true;

             using (Process process = Process.Start(distInfo))
             {
                  dist = process.StandardOutput.ReadToEnd();
             }

             if (dist.ToLower().Contains("bsd"))
                  shell = "/bin/csh";
             else
                  shell = "/bin/bash";
        }
        else
        {
            shell = "cmd.exe";
        }

        Task task = new Task();
        Utility settings = new Utility();
        string mcImage = multicast.GroupImage;
        int receivers = multicast.HostNames.Count;
        string imageName = multicast.GroupImage;

        Process sender = null;
        ProcessStartInfo senderInfo = new ProcessStartInfo();

        senderInfo.FileName = (shell);
        string udpFile = null;
        String[] partFiles = null;
        string compExt = null;
        string compAlg = null;
        string stdout = null;

        //Multicasting currently only supports the first active hd
        //Find First Active HD
        Image image = new Image();
        image.ID = image.GetImageID(imageName);
        image.Read(image);
        Image_Physical_Specs ips = new Image_Physical_Specs();
        if (!string.IsNullOrEmpty(image.ClientSizeCustom))
        {
            ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSizeCustom);
            try
            {
                ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSizeCustom);
            }
            catch { }
        }
        else
        {
            ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSize);
            try
            {
                ips = JsonConvert.DeserializeObject<Image_Physical_Specs>(image.ClientSize);
            }
            catch { }
        }
        string activeHD = null;
        int activeCounter = 0;
        foreach(var hd in ips.hd)
        {
            if (hd.active == "1")
            {
                activeHD = activeCounter.ToString();
                break;
            }
            activeCounter++;
        }

        if (activeCounter == 0)
        {
            multicast.GroupImage = settings.GetSettings("Image Store Path") + multicast.GroupImage;
        }
        else
        {
            multicast.GroupImage = settings.GetSettings("Image Store Path") + multicast.GroupImage + Path.DirectorySeparatorChar + "hd" + (activeCounter + 1).ToString();
        }

        try
        {
            // modified by cocoon
            string[] ImageExtensions = new string[3] { ".gz", ".lz4", ".none" }; // modified by cocoon

            foreach (var ext in ImageExtensions)
            {
                partFiles = Directory.GetFiles(multicast.GroupImage + Path.DirectorySeparatorChar, "*" + ext);
                if (partFiles != null && partFiles.Length > 0)
                {
                    compExt = ext;
                    if (compExt == ".lz4")
                    {
                        compAlg = Environment.OSVersion.ToString().Contains("Unix") ? "lz4 -d " : "lz4.exe -d ";
                        stdout = " - ";
                    }
                    else if (compExt == ".gz")
                    {
                        compAlg = Environment.OSVersion.ToString().Contains("Unix") ? "gzip -c -d " : "gzip.exe -c -d ";
                        stdout = "";
                    }
                    else
                    {
            compAlg = Environment.OSVersion.ToString().Contains("Unix") ? "cat " : "type ";
            stdout = "";
                    }
                    break;
                }
            }
            if (partFiles.Length == 0)
            {
                Utility.Message = "Image Files Could Not Be Located";
        return false;
            }
        }
        catch
        {
            Utility.Message = "Image Files Could Not Be Located";
            return false;
        }

        int x = 0;
        foreach (var part in ips.hd[activeCounter].partition)
        {
            udpFile = null;
            if (part.active == "1")
            {
                if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".ntfs" + compExt))
                    udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".ntfs" + compExt;
                else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".fat" + compExt))
                    udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".fat" + compExt;
                else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".extfs" + compExt))
                    udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".extfs" + compExt;
                else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".hfsp" + compExt))
                    udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".hfsp" + compExt;
                else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".imager" + compExt))
                    udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + "part" + part.number + ".imager" + compExt;
                else
                {
                    //Look for lvm
                    if (part.vg != null)
                    {
                        if (part.vg.lv != null)
                        {
                            foreach (var lv in part.vg.lv)
                            {
                                if (lv.active == "1")
                                {

                                    if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".ntfs" + compExt))
                                        udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".ntfs" + compExt;
                                    else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".fat" + compExt))
                                        udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".fat" + compExt;
                                    else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".extfs" + compExt))
                                        udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".extfs" + compExt;
                                    else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".hfsp" + compExt))
                                        udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".hfsp" + compExt;
                                    else if (File.Exists(multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".imager" + compExt))
                                        udpFile = multicast.GroupImage + Path.DirectorySeparatorChar + lv.vg + "-" + lv.name + ".imager" + compExt;
                                }
                            }
                        }
                    }
                }

                if (udpFile == null)
                    continue;
                else
                    x++;

                if (Environment.OSVersion.ToString().Contains("Unix"))
                {
                    if (x == 1)
                        senderInfo.Arguments = (" -c \"" + compAlg + udpFile + stdout + " | udp-sender" +
                                               " --portbase " + portBase + " --min-receivers " + receivers + " " + multicast.GroupSenderArgs + " --ttl 32");

                    else
                        senderInfo.Arguments += (" ; " + compAlg + udpFile + stdout + " | udp-sender" +
                                           " --portbase " + portBase + " --min-receivers " + receivers + " " + multicast.GroupSenderArgs + " --ttl 32");
                }
                else
                {
                    if (x == 1)
                        senderInfo.Arguments = (" /c " + appPath + compAlg + udpFile + stdout + " | " + appPath + "udp-sender.exe" +
                                               " --portbase " + portBase + " --min-receivers " + receivers + " " + multicast.GroupSenderArgs + " --ttl 32");
                    else
                        senderInfo.Arguments += (" & " + appPath + compAlg + udpFile + stdout + " | " + appPath + "udp-sender.exe" +
                                           " --portbase " + portBase + " --min-receivers " + receivers + " " + multicast.GroupSenderArgs + " --ttl 32");
                }
            }
        }

        if (Environment.OSVersion.ToString().Contains("Unix"))
        {
             senderInfo.Arguments += "\"";
        }

        string log = ("\r\n" + DateTime.Now.ToString("MM.dd.yy hh:mm") + " Starting Multicast Session " + multicast.GroupName +
                      " With The Following Command:\r\n\r\n" + senderInfo.FileName + senderInfo.Arguments + "\r\n\r\n");
        System.IO.File.AppendAllText(logPath + "multicast.log", log);

        try
        {
            sender = Process.Start(senderInfo);

        }
        catch (Exception ex)
        {
            Logger.Log(ex.ToString());
            Utility.Message = "Could Not Start Multicast Sender.  Check The Exception Log For More Info";
            System.IO.File.AppendAllText(logPath + "multicast.log", "Could Not Start Session " + multicast.GroupName + " Try Pasting The Command Into A Command Prompt");
            return false;
        }

        Thread.Sleep(2000);

        if (sender.HasExited)
        {
            Utility.Message = "Could Not Start Multicast Sender";
            System.IO.File.AppendAllText(logPath + "multicast.log", "Session " + multicast.GroupName + @" Started And Was Forced To Quit, Try Pasting The Command Into A Command Prompt");
            return false;
        }

        multicast.UpdatePID(sender.Id, multicast, portBase, mcImage);

        for (int i = 0; i < multicast.HostMacs.Count; i++)
        {
            task.WakeUp(multicast.HostMacs[i]);
        }

        Utility.Message = "Successfully Started Multicast " + multicast.GroupName;
        return true;
    }