public void EvalRename(RCRunner runner, RCClosure closure, RCString left, RCBlock right) { if (left.Count != right.Count && left.Count != 1) { throw new Exception("left and right arguments must have the same length"); } RCBlock result = RCBlock.Empty; if (left.Count == 1) { for (int i = 0; i < right.Count; ++i) { RCBlock name = right.GetName(i); result = new RCBlock(result, left[0], name.Evaluator, name.Value); } } else { for (int i = 0; i < left.Count; ++i) { RCBlock name = right.GetName(i); result = new RCBlock(result, left[i], name.Evaluator, name.Value); } } runner.Yield(closure, result); }
protected RCTemplate CreateTemplate(RCString right, long escapeCount) { bool multiline = right[0].IndexOf('\n') > -1; RCBlock def = new RCBlock("", ":", right); return(new RCTemplate(def, (int)escapeCount, multiline)); }
public void EvalFlip(RCRunner runner, RCClosure closure, RCBlock right) { RCBlock prototype = (RCBlock)right.Get(0); string[] names = new string[prototype.Count]; RCVectorBase[] columns = new RCVectorBase[prototype.Count]; for (int i = 0; i < prototype.Count; ++i) { RCBlock name = prototype.GetName(i); RCVectorBase scalar = (RCVectorBase)name.Value; columns[i] = RCVectorBase.FromScalar(scalar.Child(0)); names[i] = name.Name; } for (int i = 1; i < right.Count; ++i) { RCBlock row = (RCBlock)right.Get(i); for (int j = 0; j < row.Count; ++j) { RCBlock name = (RCBlock)row.GetName(j); int n = Array.IndexOf(names, name.Name); if (n >= 0) { RCVectorBase scalar = (RCVectorBase)name.Value; columns[n].Write(scalar.Child(0)); } } } RCBlock result = RCBlock.Empty; for (int i = 0; i < names.Length; ++i) { result = new RCBlock(result, names[i], ":", columns[i]); } runner.Yield(closure, result); }
public void EvalUnflip(RCRunner runner, RCClosure closure, RCBlock right) { // Take a block of arrays and turn them into a block of rows. RCBlock[] blocks = new RCBlock[right.Get(0).Count]; for (int i = 0; i < blocks.Length; ++i) { blocks[i] = RCBlock.Empty; } for (int i = 0; i < right.Count; ++i) { RCBlock name = right.GetName(i); RCVectorBase vector = (RCVectorBase)name.Value; for (int j = 0; j < vector.Count; ++j) { RCValue box = RCVectorBase.FromScalar(vector.Child(j)); blocks[j] = new RCBlock(blocks[j], name.Name, ":", box); } } RCBlock result = RCBlock.Empty; for (int i = 0; i < blocks.Length; ++i) { result = new RCBlock(result, "", ":", blocks[i]); } runner.Yield(closure, result); }
public override TcpSendState Send(RCRunner runner, RCClosure closure, RCBlock message) { long cid = Interlocked.Increment(ref _cid); // Console.Out.WriteLine ("New Cid:{0}", cid); byte[] payload = _protocol.Serialize(this, message); TcpSendState correlation = new TcpSendState(_handle, cid, message); RCAsyncState state = new RCAsyncState(runner, closure, correlation); if (_outbox.Add(state)) { // Send will add the header to the payload. // This is something I will probably want to change by adding some kind of // serializer/formatter abstraction. int size = _buffer.PrepareSend(correlation.Id, payload); _socket.BeginSend( _buffer.SendBuffer, 0, size, SocketFlags.None, new AsyncCallback(SendCompleted), state); // Console.Out.WriteLine ("Client sending {0}", correlation.Id); } else { // Console.Out.WriteLine ("Another message has to go first"); } return(correlation); }
protected virtual void WriteToFiles(RCRunner runner, RCClosure closure, RCSymbol symbol, RCBlock data) { for (int i = 0; i < data.Count; ++i) { FileStream stream = null; RCBlock column = data.GetName(i); if (!_files.TryGetValue(column.Name, out stream)) { string path = Path.Combine(_dir.Name, column.Name); stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write); _files[column.Name] = stream; } RCArray <byte> array = new RCArray <byte> (); column.Value.ToByte(array); // throw new NotImplementedException ("This is where it's at baby, get er done."); // RCAsyncState state = new RCAsyncState (runner, closure, stream); // stream.BeginWrite (array, 0, array.Length, new AsyncCallback (EndWrite), // state); } }
protected RCBlock DoShuffle(Random random, RCBlock right) { // wikipedia discusses a variant of this algorithm that allows you to // initialize the array and shuffle it in a single operation. // It would be cool to implement that. string[] names = new string[right.Count]; for (int i = 0; i < names.Length; ++i) { names[i] = right.GetName(i).Name; } for (int i = names.Length - 1; i > 0; i--) { int n = random.Next(i + 1); string temp = names[i]; names[i] = names[n]; names[n] = temp; } // Not close to optimal. Help. RCBlock result = null; for (int i = 0; i < names.Length; ++i) { RCBlock name = right.GetName(names[i]); result = new RCBlock(result, name.Name, name.Evaluator, name.Value); } return(result); }
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); }
public void EvalSwitch(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right) { Picker <RCSymbolScalar> picker = delegate(RCSymbolScalar val, out bool eval) { if (val.Length > 1) { throw new Exception( "switch only supports block lookups using tuples of count 1. But this could change."); } RCBlock variable = right.GetName((string)val.Key); RCValue code; // This behavior is sketchy and should be reevaluated - this should be an // exception if (variable == null) { code = RCBlock.Empty; eval = true; } else { code = variable.Value; eval = !variable.Evaluator.Pass; } return(code); }; DoSwitch <RCSymbolScalar> (runner, closure, left, right, picker); }
public void EvalLast(RCRunner runner, RCClosure closure, RCBlock right) { RCBlock last = right.GetName(right.Count - 1); RCBlock result = new RCBlock(last.Name, last.Evaluator.Symbol, last.Value); runner.Yield(closure, result); }
protected virtual void DoSwitch <T> (RCRunner runner, RCClosure closure, RCVector <T> left, RCBlock right, Picker <T> picker) { int i = closure.Index - 2; if (i < left.Count) { bool eval; RCValue code = picker(left[i], out eval); if (eval) { RCClosure child = new RCClosure(closure, closure.Bot, code, closure.Left, RCBlock.Empty, 0); code.Eval(runner, child); } else { // In this case the "code" can be a value, normally part of a generated program runner.Yield(closure, code); } } else { runner.Yield(closure, closure.Parent.Result); } }
public void EvalThen(RCRunner runner, RCClosure closure, RCBoolean left, RCBlock right) { if (left[0]) { int i = closure.Index - 2; if (i < left.Count) { RCClosure child = new RCClosure(closure, closure.Bot, right, closure.Left, RCBlock.Empty, 0); right.Eval(runner, child); } else { runner.Yield(closure, closure.Parent.Result); } } else { runner.Yield(closure, RCBoolean.False); } }
public void EvalBlackboard(RCRunner runner, RCClosure closure, RCBlock empty) { // Metadata about the blackboard contents. RCBlock result = RCBlock.Empty; lock (_readWriteLock) { RCBlock descriptor = RCBlock.Empty; foreach (KeyValuePair <object, Section> kv in _sections) { descriptor = new RCBlock(descriptor, "rows", ":", new RCLong(kv.Value._blackboard.Axis.Count)); descriptor = new RCBlock(descriptor, "cols", ":", new RCLong(kv.Value._blackboard.Cols)); result = new RCBlock(result, kv.Key.ToString(), ":", descriptor); } } runner.Yield(closure, result); }
protected virtual void DoEach <T> (RCRunner runner, RCClosure closure, RCBlock left, RCVector <T> right) { if (right.Count == 0) { runner.Yield(closure, RCBlock.Empty); return; } int i = closure.Index - 2; if (i < right.Count) { RCBlock result = new RCBlock("I", ":", new RCLong(i)); result = new RCBlock(result, "R", ":", RCVectorBase.FromArray(new RCArray <T> (right[i]))); left.Eval(runner, new RCClosure(closure, closure.Bot, left, closure.Left, result, 0)); } else { runner.Yield(closure, closure.Parent.Result); } }
public void EvalDyad(RCRunner runner, RCClosure closure, RCString left, object right) { RCBlock block = (RCBlock)right; RCOperator op = RCSystem.Activator.New(left[0], block.Get("l"), block.Get("r")); runner.Yield(closure, op); }
public void EvalEach(RCRunner runner, RCClosure closure, RCBlock left, RCBlock right) { if (right.Count == 0) { runner.Yield(closure, right); return; } long i = closure.Index - 2; if (i < right.Count) { RCBlock name = right.GetName(i); RCBlock result = new RCBlock(closure.Result, "L", ":", new RCString(name.Name)); result = new RCBlock(result, "I", ":", new RCLong(i)); result = new RCBlock(result, "R", ":", right.Get(i)); closure = new RCClosure(closure.Parent, closure.Bot, closure.Code, closure.Left, result, closure.Index); left.Eval(runner, new RCClosure(closure, closure.Bot, left, closure.Left, RCBlock.Empty, 0)); } else { runner.Yield(closure, closure.Parent.Result); } }
public TcpSendState Send(RCRunner runner, RCClosure closure, long cid, RCBlock message) { TcpSendState correlation = new TcpSendState(_handle, cid, message); RCAsyncState state = new RCAsyncState(runner, closure, correlation); byte[] payload = Encoding.ASCII.GetBytes(message.ToString()); // If other items are queued in the outbox Add will return false. // This message should be sent after the others. if (_outbox.Add(state)) { // Send will add the header to the payload. // This is something I will probably want to change by adding some kind of // serializer/formatter abstraction. int size = _buffer.PrepareSend(cid, payload); _socket.BeginSend( _buffer.SendBuffer, 0, size, SocketFlags.None, new AsyncCallback(SendCompleted), state); // Console.Out.WriteLine ("Server sending {0}", correlation.Id); } return(correlation); }
protected void SetHeaders(HttpWebRequest request, RCBlock head) { if (head != null) { for (int i = 0; i < head.Count; ++i) { RCBlock header = head.GetName(i); string name = header.RawName.ToLower(); if (name == "content-type") { request.ContentType = ((RCString)header.Value)[0]; } else if (name == "accept") { request.Accept = ((RCString)header.Value)[0]; } else if (name == "user-agent") { request.UserAgent = ((RCString)header.Value)[0]; } else if (name == "referer") { request.Referer = ((RCString)header.Value)[0]; } else { request.Headers.Set(header.RawName, ((RCString)header.Value)[0]); } } } }
public RestAsyncState(RCRunner runner, RCClosure closure, string url, string method, RCBlock head, RCString body, bool bodyOnly, long instance, long timeout, long retry, bool shouldRetry) : base(runner, closure, null) { Url = url; Method = method; Head = head; Body = body; BodyOnly = bodyOnly; Instance = instance; // RequestTimer = new Timer (AbortWebRequest); RetryTimer = new Timer(RetryWebRequest); OverallTimer = new Timer(AbortWebRequestAndFail); StartTime = DateTime.UtcNow; Timeout = new TimeSpan(0, 0, 0, 0, (int)timeout); Retry = new TimeSpan(0, 0, 0, 0, (int)retry); ShouldRetry = shouldRetry; }
public void EvalHttpHeader(RCRunner runner, RCClosure closure, RCLong right) { if (right.Count > 1) { throw new Exception("httpheader only allows one request per call"); } RequestInfo info; lock (_lock) { info = _contexts[(int)right[0]]; } NameValueCollection values = info.Context.Request.Headers; RCBlock result = RCBlock.Empty; result = new RCBlock(result, "Verb", ":", new RCString(info.Context.Request.HttpMethod)); result = new RCBlock(result, "RawUrl", ":", new RCString(info.Context.Request.RawUrl)); result = new RCBlock(result, "Url", ":", new RCString( info.Context.Request.Url.AbsolutePath)); for (int i = 0; i < values.AllKeys.Length; ++i) { string key = values.AllKeys[i]; result = new RCBlock(result, key, ":", new RCString(values[key])); } runner.Yield(closure, result); }
public void EvalHttpBody(RCRunner runner, RCClosure closure, RCLong right) { if (right.Count > 1) { throw new Exception("httpbody only allows one request per call"); } RequestInfo info; lock (_lock) { info = _contexts[(int)right[0]]; } string body = new StreamReader(info.Context.Request.InputStream).ReadToEnd(); // ParseQueryString really means ParseUrlEncodedForm. NameValueCollection values = HttpUtility.ParseQueryString(body); RCBlock result = RCBlock.Empty; for (int i = 0; i < values.AllKeys.Length; ++i) { string key = values.AllKeys[i]; result = new RCBlock(result, key, ":", new RCString(values[key])); } runner.Yield(closure, result); }
public override RCValue Finish(RCValue result) { RCBlock wrapper = new RCBlock("status", ":", new RCLong(0)); wrapper = new RCBlock(wrapper, "data", ":", result); return(wrapper); }
void watcher_Changed(object sender, FileSystemEventArgs e) { RCLFileSystemWatcher watcher = (RCLFileSystemWatcher)sender; RCBlock result = GetFileEventInfo(e); EnqueueAndDrain(watcher, result); }
public void EvalMatches(RCRunner runner, RCClosure closure, RCString left, RCString right) { RCBlock result = RCBlock.Empty; Regex[] regexes = new Regex[left.Count]; for (int i = 0; i < left.Count; ++i) { regexes[i] = new Regex(left[i].ToString(), RegexOptions.Multiline); } for (int i = 0; i < right.Count; ++i) { RCArray <string> resulti = null; for (int j = 0; j < regexes.Length; ++j) { MatchCollection matchesij = regexes[j].Matches(right[i]); if (matchesij.Count > 0) { if (resulti == null) { resulti = new RCArray <string> (matchesij.Count + 1); } for (int k = 0; k < matchesij.Count; ++k) { resulti.Write(matchesij[k].Value); } } } if (resulti == null) { resulti = new RCArray <string> (0); } result = new RCBlock(result, "", ":", new RCString(resulti)); } runner.Yield(closure, result); }
public void EvalBlock(RCRunner runner, RCClosure closure, RCCube right) { BlockWriter writer = new BlockWriter(right); RCBlock result = writer.Write(); runner.Yield(closure, result); }
public void EvalForce(RCRunner runner, RCClosure closure, RCSymbol left, RCBlock right) { long line = Write(runner, left, right, true); RCBlock logBlock = new RCBlock(right, "S", ":", left); RCSystem.Log.Record(closure, "board", 0, "write", logBlock); runner.Yield(closure, new RCLong(line)); }
public static void SetOptions(RCBlock options) { if (_options != null) { throw new Exception("Set options called more than once."); } _options = options; }
public void EvalUnwrap(RCRunner runner, RCClosure closure, RCBlock right) { if (right.Count != 1) { throw new Exception("monadic unwrap unwraps a block containing a single element"); } runner.Yield(closure, right.Get(0)); }
public void EvalCodebase(RCRunner runner, RCClosure closure, RCBlock right) { Uri codebase = new Uri(Assembly.GetExecutingAssembly().CodeBase); DirectoryInfo dir = new FileInfo(codebase.LocalPath).Directory; string unixFullName = dir.FullName.Replace("\\", "/"); runner.Yield(closure, new RCString(unixFullName)); }
public override TcpSendState Send(RCRunner runner, RCClosure closure, RCBlock message) { long cid = Interlocked.Increment(ref _cid); RCSymbolScalar id = new RCSymbolScalar(null, _handle); id = new RCSymbolScalar(id, cid); StringBuilder address = new StringBuilder(); // HttpVerb verb = (HttpVerb) Enum.Parse ( // typeof(HttpVerb), // ((RCSymbol) message.Get ("verb"))[0].Part (0).ToString ()); object[] resource = ((RCSymbol)message.Get("resource"))[0].ToArray(); address.Append("http://"); address.Append(_host); if (_port > 0) { address.Append(":"); address.Append(_port); } address.Append("/"); for (int i = 0; i < resource.Length; ++i) { address.Append(resource[i].ToString()); if (i < resource.Length - 1) { address.Append("/"); } } RCBlock query = (RCBlock)message.Get("query"); if (query != null) { address.Append("?"); for (int i = 0; i < query.Count; ++i) { RCBlock variable = query.GetName(i); address.Append(variable.Name); address.Append("="); address.Append(((RCString)variable.Value)[0]); if (i < query.Count - 1) { address.Append("&"); } } } // byte[] payload = _client.Encoding.GetBytes (message.Get ("body").ToString ()); Uri uri = new Uri(address.ToString()); System.Console.Out.WriteLine(address.ToString()); _client.DownloadDataAsync(uri, new RCAsyncState(runner, closure, id)); // runner.Yield (closure, new RCSymbol(id)); return(new TcpSendState(_handle, cid, message)); }