Exemple #1
0
        /// <summary>
        /// 用户自定义Python脚本
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string RepairFun_PythonScript(string handler, string source)
        {
            if (source == "")
            {
                return("");
            }

            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = Python.CreateEngine();
            Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString(
                $"import textRepairPlugins.{handler} as customHandler\n" +
                "ResultStr = customHandler.process(SourceStr)\n"
                );
            Microsoft.Scripting.Hosting.ScriptScope scope = pythonEngine.CreateScope();
            scope.SetVariable("SourceStr", source);

            try
            {
                pythonScript.Execute(scope);
            }
            catch (Exception e)
            {
                return(e.Message);
            }
            return((string)scope.GetVariable("ResultStr"));
        }
        static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
            Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString(
                "helloWorldString = 'Hello World!'\n" +
                "print helloWorldString\n" +
                "print externalString"
                );

            Microsoft.Scripting.Hosting.ScriptScope scope = pythonEngine.CreateScope();
            scope.SetVariable("externalString", "How do you do?");

            pythonScript.Execute(scope);

            System.Console.Out.WriteLine();
            System.Console.Out.WriteLine("List of variables in the scope:");
            foreach (string name in scope.GetVariableNames())
            {
                System.Console.Out.Write(name + " ");
            }
            System.Console.Out.WriteLine();

            System.Console.Out.WriteLine();
            System.Console.Out.WriteLine("Variable values:");
            System.Console.Out.WriteLine("helloWorldString: " + scope.GetVariable("helloWorldString"));
            System.Console.Out.WriteLine("externalString: " + scope.GetVariable("externalString"));
        }
 static void Main(string[] args)
 {
     Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
     // We execute this script from Visual Studio so the program will be executed from bin\Debug or bin\Release
     Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromFile("..\\..\\HelloWorld.py");
     pythonScript.Execute();
 }
Exemple #4
0
        static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine =
                IronPython.Hosting.Python.CreateEngine();

            // Print the default search paths
            System.Console.Out.WriteLine("Search paths:");
            ICollection <string> searchPaths = pythonEngine.GetSearchPaths();

            foreach (string path in searchPaths)
            {
                System.Console.Out.WriteLine(path);
            }
            System.Console.Out.WriteLine();

            // Now modify the search paths to include the directory
            // where the standard library has been installed
            searchPaths.Add("..\\..\\Lib");
            pythonEngine.SetSearchPaths(searchPaths);

            // Execute the script
            // We execute this script from Visual Studio
            // so the program will executed from bin\Debug or bin\Release
            Microsoft.Scripting.Hosting.ScriptSource pythonScript =
                pythonEngine.CreateScriptSourceFromFile("..\\..\\HelloWorldBase64.py");
            pythonScript.Execute();
        }
Exemple #5
0
 static void Main(string[] args)
 {
     Microsoft.Scripting.Hosting.ScriptEngine pythonEngine =
         IronPython.Hosting.Python.CreateEngine();
     // We execute this script from Visual Studio
     // so the program will be executed from bin\Debug or bin\Release
     Microsoft.Scripting.Hosting.ScriptSource pythonScript =
         pythonEngine.CreateScriptSourceFromFile("..\\..\\python.py");
     Console.Beep(); //Plays Beep before running python
     pythonScript.Execute();
     ReadLine();
 }
Exemple #6
0
 static void Main(string[] args)
 {
     Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
     Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString("print 'Hello World!'");
     pythonScript.Execute();
 }
        /*-------------------------------------Network Thread------------------------------------------*/

        /* ---------------------------------------WARNING----------------------------------------------*/

        /* The code you are about to witness is a brainwave and if given more time it would be seperated
         * out into different functions and made more clear but I didn't have enough time				*/
        public async Task NetworkThread()
        {
            await Task.Run(() =>
            {
                try
                {
                    while (true)
                    {
                        RestRequest request = new RestRequest("api/client/getclientlist");
                        IRestResponse resp  = RestClient.Get(request);

                        clientList = JsonConvert.DeserializeObject <List <Client> >(resp.Content);
                        //Used to make the swarm more fair, start from the begginning and then start from the end
                        if (fairSwarm)
                        {
                            fairSwarm = false;
                            foreach (Client client in clientList)
                            {
                                if (me.port != client.port)
                                {
                                    ChannelFactory <PeerServerInterface> channelFactory;
                                    NetTcpBinding tcp          = new NetTcpBinding();
                                    tcp.MaxReceivedMessageSize = 2147483647;
                                    string clientURL           = String.Format("net.tcp://{0}:{1}/DataService", client.ipaddress, client.port);
                                    channelFactory             = new ChannelFactory <PeerServerInterface>(tcp, clientURL);
                                    channel = channelFactory.CreateChannel();

                                    currentJob = channel.DownloadJob();
                                    if (currentJob != null)
                                    {
                                        byte[] hash = CreateHash(currentJob.code);
                                        if (hash.SequenceEqual(currentJob.hash))
                                        {
                                            /*Sourced from
                                             * https://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-a-c-sharp-app
                                             */
                                            Job doJob = currentJob;
                                            if (string.IsNullOrEmpty(doJob.code))
                                            {
                                                throw new EmptyCodeException();
                                            }
                                            else
                                            {
                                                byte[] eoncodedBytes = Convert.FromBase64String(doJob.code);
                                                string code          = System.Text.Encoding.UTF8.GetString(eoncodedBytes);
                                                ChangeUIElements(true);
                                                Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
                                                Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString(code);
                                                var answer = pythonScript.Execute();
                                                Dispatcher.BeginInvoke(new Action(() =>
                                                {
                                                    try
                                                    {
                                                        doJob.answer = answer;
                                                        channel.UploadAns(doJob);
                                                        jobCount++;
                                                        ChangeJobCount();
                                                    }
                                                    catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)                                                    //Converting the answer to a string
                                                    {
                                                        doJob.answer = answer.ToString();
                                                        channel.UploadAns(doJob);
                                                        jobCount++;
                                                        ChangeJobCount();
                                                    }
                                                    ChangeUIElements(false);
                                                    Thread.Sleep(2000);                                                    //Just so it doesn't constantly keep checking it sleeps for 2 seconds to chill for a bit
                                                }));
                                            }

                                            currentJob = null;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            fairSwarm = true;
                            for (int i = clientList.Count - 1; i >= 0; i--)
                            {
                                Client client = clientList[i];
                                if (me.port != client.port)
                                {
                                    ChannelFactory <PeerServerInterface> channelFactory;
                                    NetTcpBinding tcp          = new NetTcpBinding();
                                    tcp.MaxReceivedMessageSize = 2147483647;
                                    string clientURL           = String.Format("net.tcp://{0}:{1}/DataService", client.ipaddress, client.port);
                                    channelFactory             = new ChannelFactory <PeerServerInterface>(tcp, clientURL);
                                    channel = channelFactory.CreateChannel();


                                    currentJob = channel.DownloadJob();
                                    if (currentJob != null)
                                    {
                                        byte[] hash = CreateHash(currentJob.code);
                                        if (hash.SequenceEqual(currentJob.hash))
                                        {
                                            /*Sourced from
                                             * https://stackoverflow.com/questions/7053172/how-can-i-call-ironpython-code-from-a-c-sharp-app
                                             */
                                            Job doJob = currentJob;
                                            if (string.IsNullOrEmpty(doJob.code))
                                            {
                                                throw new EmptyCodeException();
                                            }
                                            byte[] eoncodedBytes = Convert.FromBase64String(doJob.code);
                                            string code          = System.Text.Encoding.UTF8.GetString(eoncodedBytes);
                                            ChangeUIElements(true);
                                            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine = IronPython.Hosting.Python.CreateEngine();
                                            Microsoft.Scripting.Hosting.ScriptSource pythonScript = pythonEngine.CreateScriptSourceFromString(code);
                                            var answer = pythonScript.Execute();
                                            Dispatcher.BeginInvoke(new Action(() =>
                                            {
                                                //If have time encode ans but who cares for now
                                                try
                                                {
                                                    doJob.answer = answer;
                                                    channel.UploadAns(doJob);
                                                    jobCount++;
                                                    ChangeJobCount();
                                                }
                                                catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)                                                //Converting the answer to a string
                                                {
                                                    doJob.answer = answer.ToString();
                                                    channel.UploadAns(doJob);
                                                    jobCount++;
                                                    ChangeJobCount();
                                                }
                                                ChangeUIElements(false);
                                                Thread.Sleep(5000);
                                            }));

                                            currentJob = null;
                                        }
                                    }
                                }
                            }
                        }

                        UpdateScore();
                        Thread.Sleep(10000);
                    }
                }
                catch (System.ServiceModel.EndpointNotFoundException e)
                {
                    //Occurs when someone leaves the swarm
                    UpdateScore();
                    logger.LogFunc("Error occured in the Networking Thread, someone has left the swarm abruptly");
                }
                catch (IronPython.Runtime.UnboundNameException e)
                {
                    //When the input something not code
                    MessageBox.Show("It appears were given a job that was in the wrong format");
                    ChangeUIElements(false);
                    currentJob.error = true;
                    channel.UploadAns(currentJob);
                    logger.LogFunc("Error occured in the Networking Thread, the users input code is invalid");
                }
                catch (EmptyCodeException e)
                {
                    //when the input is empty
                    logger.LogFunc("Error occured in the Networking Thread, the user didn't enter any code");
                }
            });
        }