Beispiel #1
0
        public void EvalListen(RCRunner runner, RCClosure closure, RCSymbol right)
        {
            if (right.Count != 1)
            {
                throw new Exception("listen takes exactly one protocol,port");
            }
            string protocol = (string)right[0].Part(0);
            long   port     = (long)right[0].Part(1);
            RCBot  bot      = runner.GetBot(closure.Bot);
            long   handle   = bot.New();
            Server server;

            if (protocol.Equals("http"))
            {
                throw new NotImplementedException("http server not ready yet");
            }
            else if (protocol.Equals("tcp"))
            {
                server = new TcpServer(handle, port, new TcpProtocol());
            }
            else if (protocol.Equals("udp"))
            {
                throw new ArgumentException("No udp sockets yet");
            }
            else
            {
                throw new ArgumentException("Unknown protocol: " + protocol);
            }
            bot.Put(handle, server);
            server.Listen(runner, closure);
        }
Beispiel #2
0
            public void EvalTake(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right)
            {
                RCBot bot    = runner.GetBot(closure.Bot);
                Take  module = (Take)bot.GetModule(typeof(Take));

                module.DoTake(runner, closure, left, right);
            }
Beispiel #3
0
        protected void DoAccept(RCRunner runner, RCClosure closure, RCLong left, RCLong right)
        {
            RCBot bot = runner.GetBot(closure.Bot);

            for (int i = 0; i < left.Count; ++i)
            {
                Server server = (Server)bot.Get(left[i]);
                server.Accept(runner, closure, right[0]);
            }
        }
Beispiel #4
0
        protected void DoReceive(RCRunner runner, RCClosure closure, RCSymbol right)
        {
            RCBot        bot      = runner.GetBot(closure.Bot);
            TcpCollector gatherer = new TcpCollector(runner, closure, right);

            for (int i = 0; i < right.Count; ++i)
            {
                long   channel = (long)right[i].Part(0);
                Client client  = (Client)bot.Get(channel);
                client.Receive(gatherer, right[i]);
            }
        }
Beispiel #5
0
        protected void DoSend(RCRunner runner, RCClosure closure, RCLong left, RCBlock right)
        {
            RCSymbolScalar[] result = new RCSymbolScalar[left.Count];
            RCBot            bot    = runner.GetBot(closure.Bot);

            for (int i = 0; i < left.Count; ++i)
            {
                Client         client = (Client)bot.Get(left[i]);
                TcpSendState   state  = client.Send(runner, closure, right);
                RCSymbolScalar handle = new RCSymbolScalar(null, state.Handle);
                result[i] = new RCSymbolScalar(handle, state.Id);
            }
            runner.Yield(closure, new RCSymbol(result));
        }
Beispiel #6
0
        public void EvalClose(RCRunner runner, RCClosure closure, RCLong right)
        {
            // Implementing multiple would require some annoying scatter gather logic.
            // Plus what if one fails out of the list?
            if (right.Count != 1)
            {
                throw new Exception("open takes exactly one protocol,host,port");
            }
            RCBot  bot    = runner.GetBot(closure.Bot);
            Client client = (Client)bot.Get(right[0]);

            client.Close(runner, closure);
            runner.Yield(closure, right);
        }
Beispiel #7
0
        public void EvalOpen(RCRunner runner, RCClosure closure, RCLong left, RCSymbol right)
        {
            // Implementing multiple would require some annoying scatter gather logic.
            // Plus what if one fails out of the list?
            if (right.Count != 1)
            {
                throw new Exception("open takes exactly one protocol,host,[port]");
            }
            if (left.Count != 1)
            {
                throw new Exception("open takes a single timeout value");
            }

            RCSymbolScalar symbol   = right[0];
            string         protocol = (string)symbol.Part(0);
            int            timeout  = (int)left[0];
            // string host = (string) symbol[1];
            // long port = -1;
            // if (symbol.Length > 2)
            //  port = (long) symbol[2];
            RCBot  bot    = runner.GetBot(closure.Bot);
            long   handle = bot.New();
            Client client;

            if (protocol.Equals("http"))
            {
                client = new TcpHttpClient(handle, symbol);
            }
            else if (protocol.Equals("tcp"))
            {
                client = new TcpClient(handle, symbol, new TcpProtocol(), timeout);
            }
            else if (protocol.Equals("udp"))
            {
                throw new NotImplementedException("No udp sockets yet");
            }
            else if (protocol.Equals("cube"))
            {
                throw new NotImplementedException();
                // client = new CubeClient (handle, symbol);
            }
            else
            {
                throw new NotImplementedException("Unknown protocol: " + protocol);
            }
            bot.Put(handle, client);
            client.Open(runner, closure);
        }
Beispiel #8
0
        protected void DoReply(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right)
        {
            RCBot bot = runner.GetBot(closure.Bot);

            RCSymbolScalar[] result = new RCSymbolScalar[left.Count];
            for (int i = 0; i < left.Count; ++i)
            {
                long           channel = (long)left[i].Part(0);
                long           sid     = (long)left[i].Part(1);
                long           cid     = (long)left[i].Part(2);
                Server         server  = (Server)bot.Get(channel);
                TcpSendState   state   = server.Reply(runner, closure, sid, cid, right);
                RCSymbolScalar handle  = new RCSymbolScalar(null, state.Handle);
                result[i] = new RCSymbolScalar(handle, state.Id);
            }
            runner.Yield(closure, new RCSymbol(result));
        }
Beispiel #9
0
 public override RCClosure Next(RCRunner runner,
                                RCClosure tail,
                                RCClosure previous,
                                RCValue
                                result)
 {
     if (previous.Index < 2)
     {
         return(base.Next(runner, tail, previous, result));
     }
     else
     {
         RCBot bot    = runner.GetBot(tail.Bot);
         Take  module = (Take)bot.GetModule(typeof(Take));
         module.Untake(runner, tail);
         return(base.Next(runner, tail, previous, result));
     }
 }
Beispiel #10
0
        public void EvalCompile(RCRunner runner, RCClosure closure, RCString right)
        {
            string             code       = right[0];
            CSharpCodeProvider provider   = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            Uri           codebase        = new Uri(Assembly.GetExecutingAssembly().CodeBase);
            DirectoryInfo dir             = new FileInfo(codebase.LocalPath).Directory;

            parameters.ReferencedAssemblies.Add(dir.FullName + "/RCL.Kernel.dll");
            parameters.GenerateInMemory   = true;
            parameters.GenerateExecutable = false;
            CompilerResults results = null;

            try
            {
                RCSystem.Log.Record(closure, "compile", 0, "code", code);
                results = provider.CompileAssemblyFromSource(parameters, code);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (results != null)
                {
                    for (int i = 0; i < results.Errors.Count; ++i)
                    {
                        CompilerError error = results.Errors[i];
                        Console.Out.WriteLine(error.ToString());
                        RCSystem.Log.Record(closure, "compile", 0, "error", error.ToString());

                        /*
                         * error.Column;
                         * error.ErrorNumber;
                         * error.ErrorText;
                         * error.FileName;
                         * error.IsWarning;
                         * error.Line;
                         */
                    }
                }
            }
            if (results.Errors.Count > 0)
            {
                throw new Exception("compilation failed, show compile:error for details");
            }
            Type[]           types   = results.CompiledAssembly.GetTypes();
            RCArray <string> modules = new RCArray <string> ();
            RCBlock          result  = RCBlock.Empty;

            for (int i = 0; i < types.Length; ++i)
            {
                bool    isModule;
                RCBlock typeVerbs = RCSystem.Activator.CreateVerbTable(types[i], out isModule);
                result = new RCBlock(result, types[i].Name, ":", typeVerbs);
                if (isModule)
                {
                    modules.Write(types[i].Name);
                    RCBot bot = runner.GetBot(closure.Bot);
                    bot.PutModule(types[i]);
                }
            }
            runner.Yield(closure, result);
        }