Exemplo n.º 1
0
        protected override Variable Evaluate(ParsingScript script)
        {
            bool            isList = false;
            List <Variable> args   = Utils.GetArgs(script,
                                                   Constants.START_ARG, Constants.END_ARG, out isList);

            Utils.CheckArgs(args.Count, 1, m_name);
            int limit = args[0].AsInt();

            Utils.CheckPosInt(args[0]);
            int numberRandoms = Utils.GetSafeInt(args, 1, 1);

            if (numberRandoms <= 1)
            {
                return(new Variable(m_random.Next(0, limit)));
            }

            List <int>      available = Enumerable.Range(0, limit).ToList();
            List <Variable> result    = new List <Variable>();

            for (int i = 0; i < numberRandoms && available.Count > 0; i++)
            {
                int nextRandom = m_random.Next(0, available.Count);
                result.Add(new Variable(available[nextRandom]));
                available.RemoveAt(nextRandom);
            }

            return(new Variable(result));
        }
Exemplo n.º 2
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List <Variable> args = script.GetFunctionArgs();

            Utils.CheckArgs(args.Count, 1, m_name, true);

            Variable id = args[0];

            Utils.CheckPosInt(id, script);

            int processId = (int)id.Value;

            try
            {
                Process process = Process.GetProcessById(processId);
                process.Kill();
                Interpreter.Instance.AppendOutput("Process " + processId + " killed", true);
            }
            catch (Exception exc)
            {
                throw new ArgumentException("Couldn't kill process " + processId +
                                            " (" + exc.Message + ")");
            }

            return(Variable.EmptyInstance);
        }
Exemplo n.º 3
0
        protected override Variable Evaluate(ParsingScript script)
        {
            List <Variable> args = script.GetFunctionArgs();

            if (m_decimal)
            {
                return(new Variable(m_random.NextDouble()));
            }

            Utils.CheckArgs(args.Count, 1, m_name);
            int limit = args[0].AsInt();

            Utils.CheckPosInt(args[0], script);
            int numberRandoms = Utils.GetSafeInt(args, 1, 1);

            if (numberRandoms <= 1)
            {
                return(new Variable(m_random.Next(0, limit)));
            }

            List <int>      available = Enumerable.Range(0, limit).ToList();
            List <Variable> result    = new List <Variable>();

            for (int i = 0; i < numberRandoms && available.Count > 0; i++)
            {
                int nextRandom = m_random.Next(0, available.Count);
                result.Add(new Variable(available[nextRandom]));
                available.RemoveAt(nextRandom);
            }

            return(new Variable(result));
        }
Exemplo n.º 4
0
        protected override Variable Evaluate(ParsingScript script)
        {
            Variable sleepms = Utils.GetItem(script);
            Utils.CheckPosInt(sleepms, script);

            Thread.Sleep((int)sleepms.Value);

            return Variable.EmptyInstance;
        }
Exemplo n.º 5
0
        protected override Variable Evaluate(ParsingScript script)
        {
            Variable portRes = Utils.GetItem(script);

            Utils.CheckPosInt(portRes, script);
            int port = (int)portRes.Value;

            try
            {
                IPHostEntry ipHostInfo    = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipAddress     = ipHostInfo.AddressList[0];
                IPEndPoint  localEndPoint = new IPEndPoint(ipAddress, port);

                Socket listener = new Socket(AddressFamily.InterNetwork,
                                             SocketType.Stream, ProtocolType.Tcp);

                listener.Bind(localEndPoint);
                listener.Listen(10);

                Socket handler = null;
                while (true)
                {
                    Interpreter.Instance.AppendOutput("Waiting for connections on " + port + " ...", true);
                    handler = listener.Accept();

                    // Data buffer for incoming data.
                    byte[] bytes    = new byte[1024];
                    int    bytesRec = handler.Receive(bytes);
                    string received = Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    Interpreter.Instance.AppendOutput("Received from " + handler.RemoteEndPoint.ToString() +
                                                      ": [" + received + "]", true);

                    byte[] msg = Encoding.UTF8.GetBytes(received);
                    handler.Send(msg);

                    if (received.Contains("<EOF>"))
                    {
                        break;
                    }
                }

                if (handler != null)
                {
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            }
            catch (Exception exc)
            {
                throw new ArgumentException("Couldn't start server: (" + exc.Message + ")");
            }

            return(Variable.EmptyInstance);
        }
Exemplo n.º 6
0
        protected override Variable Evaluate(ParsingScript script)
        {
            // Data buffer for incoming data.
            byte[] bytes = new byte[1024];

            List <Variable> args = script.GetFunctionArgs();

            Utils.CheckArgs(args.Count, 3, Constants.CONNECTSRV);
            Utils.CheckPosInt(args[1]);

            string hostname  = args[0].String;
            int    port      = (int)args[1].Value;
            string msgToSend = args[2].String;

            if (string.IsNullOrWhiteSpace(hostname) || hostname.Equals("localhost"))
            {
                hostname = Dns.GetHostName();
            }

            try
            {
                IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname);
                IPAddress   ipAddress  = ipHostInfo.AddressList[0];
                IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, port);

                // Create a TCP/IP  socket.
                Socket sender = new Socket(AddressFamily.InterNetwork,
                                           SocketType.Stream, ProtocolType.Tcp);

                sender.Connect(remoteEP);

                Interpreter.Instance.AppendOutput("Connected to [" + sender.RemoteEndPoint.ToString() + "]", true);

                byte[] msg = Encoding.UTF8.GetBytes(msgToSend);
                sender.Send(msg);

                // Receive the response from the remote device.
                int    bytesRec = sender.Receive(bytes);
                string received = Encoding.UTF8.GetString(bytes, 0, bytesRec);
                Interpreter.Instance.AppendOutput("Received [" + received + "]", true);

                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch (Exception exc)
            {
                throw new ArgumentException("Couldn't connect to server: (" + exc.Message + ")");
            }

            return(Variable.EmptyInstance);
        }
Exemplo n.º 7
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string filename = Utils.GetStringOrVarValue(script);
            int    size     = Constants.DEFAULT_FILE_LINES;

            bool sizeAvailable = Utils.SeparatorExists(script);

            if (sizeAvailable)
            {
                Variable length = Utils.GetItem(script);
                Utils.CheckPosInt(length);
                size = (int)length.Value;
            }

            string[]        lines   = Utils.GetFileLines(filename, -1, size);
            List <Variable> results = Utils.ConvertToResults(lines);

            return(new Variable(results));
        }
Exemplo n.º 8
0
        protected override Variable Evaluate(ParsingScript script)
        {
            string substring;

            // 1. Get the name of the variable.
            string varName = Utils.GetToken(script, Constants.NEXT_ARG_ARRAY);

            Utils.CheckNotEmpty(script, varName, m_name);

            // 2. Get the current value of the variable.
            ParserFunction func         = ParserFunction.GetFunction(varName);
            Variable       currentValue = func.GetValue(script);

            // 3. Take either the string part if it is defined,
            // or the numerical part converted to a string otherwise.
            string arg = currentValue.AsString();
            // 4. Get the initial index of the substring.
            Variable init = Utils.GetItem(script);

            Utils.CheckNonNegativeInt(init);

            // 5. Get the length of the substring if available.
            bool lengthAvailable = Utils.SeparatorExists(script);

            if (lengthAvailable)
            {
                Variable length = Utils.GetItem(script);
                Utils.CheckPosInt(length);
                if (init.Value + length.Value > arg.Length)
                {
                    throw new ArgumentException("The total substring length is larger than [" +
                                                arg + "]");
                }
                substring = arg.Substring((int)init.Value, (int)length.Value);
            }
            else
            {
                substring = arg.Substring((int)init.Value);
            }
            Variable newValue = new Variable(substring);

            return(newValue);
        }
Exemplo n.º 9
0
        protected override Variable Evaluate(ParsingScript script)
        {
            Variable id = Utils.GetItem(script);

            Utils.CheckPosInt(id);

            int processId = (int)id.Value;

            try {
                Process process = Process.GetProcessById(processId);
                process.Kill();
                Interpreter.Instance.AppendOutput("Process " + processId + " killed", true);
            } catch (Exception exc) {
                throw new ArgumentException("Couldn't kill process " + processId +
                                            " (" + exc.Message + ")");
            }

            return(Variable.EmptyInstance);
        }