/* * public static void ListFibers (RCRunner runner, RCClosure closure) * { * RCArray<Fiber> modules = new RCArray<Fiber> (); * RCArray<long> keys = new RCArray<long> (); * lock (runner._botLock) * { * foreach (KeyValuePair<long, RCBot> kv in runner._bots) * { * keys.Write (kv.Key); * modules.Write ((Fiber) runner._bots[kv.Key].GetModule (typeof (Fiber))); * } * } * RCArray<long> bots = new RCArray<long> (); * RCArray<long> fibers = new RCArray<long> (); * RCArray<string> states = new RCArray<string> (); * for (int i = 0; i < modules.Count; ++i) * { * lock (modules[i]._fiberLock) * { * foreach (KeyValuePair<long, Fiber.FiberState> kv in modules[i]._fibers) * { * bots.Write (keys[i]); * fibers.Write (kv.Key); * states.Write (kv.Value.State); * } * } * } * RCBlock result = RCBlock.Empty; * if (states.Count > 0) * { * result = new RCBlock (result, "bot", ":", new RCLong (bots)); * result = new RCBlock (result, "fiber", ":", new RCLong (fibers)); * result = new RCBlock (result, "state", ":", new RCString (states)); * } * * runner.Yield (closure, result); * } */ public static void ListFibers(RCRunner runner, RCClosure closure) { RCArray <Fiber> modules = new RCArray <Fiber> (); RCArray <long> keys = new RCArray <long> (); lock (runner._botLock) { foreach (KeyValuePair <long, RCBot> kv in runner._bots) { keys.Write(kv.Key); modules.Write((Fiber)runner._bots[kv.Key].GetModule(typeof(Fiber))); } } RCCube result = new RCCube("S"); for (int i = 0; i < modules.Count; ++i) { lock (modules[i]._fiberLock) { foreach (KeyValuePair <long, Fiber.FiberState> kv in modules[i]._fibers) { RCSymbolScalar sym = RCSymbolScalar.From("fiber", keys[i], kv.Key); result.WriteCell("bot", sym, keys[i]); result.WriteCell("fiber", sym, kv.Key); result.WriteCell("state", sym, kv.Value.State); result.Write(sym); } } } runner.Yield(closure, result); }
public override void VisitScalar <T> (string name, Column <T> column, int row) { // Just a touch of ugly, slow if (_unplug && typeof(T) == _defaultValue.GetType()) { RCSymbolScalar scalar = null; if (_source.Axis.Symbol != null) { scalar = _source.Axis.Symbol[column.Index[row]]; } if (_comparer.Compare(column.Data[row], _defaultValue) != 0) { _target.WriteCell(name, scalar, column.Data[row], column.Index[row], true, true); } else { } } else { RCSymbolScalar scalar = null; if (_source.Axis.Symbol != null) { scalar = _source.Axis.Symbol[column.Index[row]]; } _target.WriteCell(name, scalar, column.Data[row], column.Index[row], true, true); } }
public static void ListRequest(RCRunner runner, RCClosure closure) { RCArray <HttpServer> modules = new RCArray <HttpServer> (); RCArray <long> keys = new RCArray <long> (); lock (runner._botLock) { foreach (KeyValuePair <long, RCBot> kv in runner._bots) { keys.Write(kv.Key); modules.Write((HttpServer)runner._bots[kv.Key].GetModule(typeof(HttpServer))); } } RCCube result = new RCCube("S"); for (int i = 0; i < modules.Count; ++i) { lock (modules[i]._lock) { foreach (KeyValuePair <int, HttpServer.RequestInfo> kv in modules[i]._contexts) { RCSymbolScalar urlsym = RCSymbolScalar.From("request", keys[i], (long)kv.Key); result.WriteCell("bot", urlsym, keys[i]); result.WriteCell("handle", urlsym, (long)kv.Key); result.WriteCell("url", urlsym, kv.Value.Context.Request.RawUrl); result.Write(urlsym); } } } runner.Yield(closure, result); }
public override int CompareAxisRows(Timeline axis1, int i1, Timeline axis2, int i2) { long eventX = axis1.Event[i1]; long eventY = axis2.Event[i2]; int compareResult = eventX.CompareTo(eventY); if (compareResult == 0) { RCTimeScalar timeX = axis1.Time[i1]; RCTimeScalar timeY = axis2.Time[i2]; compareResult = timeX.CompareTo(timeY); if (compareResult == 0) { RCSymbolScalar symbolX = axis1.SymbolAt(i1); RCSymbolScalar symbolY = axis2.SymbolAt(i2); return(symbolX.CompareTo(symbolY)); } else { return(compareResult); } } else { return(compareResult); } }
public override int CompareAxisRows(Timeline axis1, int i1, Timeline axis2, int i2) { RCSymbolScalar symbolX = axis1.SymbolAt(i1); RCSymbolScalar symbolY = axis2.SymbolAt(i2); return(symbolX.CompareTo(symbolY)); }
public ListArgs(RCSymbolScalar spec, bool all, bool deep) { Spec = spec; Path = Command.PathSymbolToLocalString(spec); All = all; Deep = deep; }
public static void ListExec(RCRunner runner, RCClosure closure) { RCArray <Exec> modules = new RCArray <Exec> (); RCArray <long> keys = new RCArray <long> (); lock (runner._botLock) { foreach (KeyValuePair <long, RCBot> kv in runner._bots) { keys.Write(kv.Key); modules.Write((Exec)runner._bots[kv.Key].GetModule(typeof(Exec))); } } RCCube result = new RCCube("S"); for (int i = 0; i < modules.Count; ++i) { lock (modules[i]._lock) { foreach (KeyValuePair <long, Exec.ChildProcess> kv in modules[i]._process) { RCSymbolScalar sym = RCSymbolScalar.From("exec", keys[i], kv.Key); result.WriteCell("bot", sym, keys[i]); result.WriteCell("handle", sym, (long)kv.Key); result.WriteCell("pid", sym, (long)kv.Value._pid); result.WriteCell("program", sym, kv.Value._program); result.WriteCell("arguments", sym, kv.Value._arguments); result.WriteCell("exit", sym, kv.Value._exitCode); result.Write(sym); } } } runner.Yield(closure, result); }
public override void Receive(TcpCollector gatherer, RCSymbolScalar id) { // It's not honoring the id here. // We should make sure to return the response to the appropriate // request, not rely on the order. // _inbox.Remove(runner, closure); _inbox.Remove(id, gatherer); }
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)); }
public static string PathSymbolToLocalString(RCSymbolScalar symbol) { string path = PathSymbolToString(Path.DirectorySeparatorChar, symbol); // I think I might need to do something with GetPathRoot // string root = Path.GetPathRoot (path); // return root + path; return(path); }
public void EvalChart(RCRunner runner, RCClosure closure, RCCube right) { RCSymbolScalar parent = RCSymbolScalar.Empty; RCCube result = new RCCube(new RCArray <string> ("S")); long row = 0; long col = 0; DoChart(result, parent, ref row, col, right); runner.Yield(closure, result); }
public void EvalSymbolMonadicPlus(RCRunner runner, RCClosure closure, RCSymbol right) { RCSymbolScalar result = RCSymbolScalar.Empty; for (int i = 0; i < right.Count; ++i) { result = ScalarMath.Plus(result, right[i]); } runner.Yield(closure, new RCSymbol(result)); }
// I think host and port will get combined with "resource" as an RCSymbolScalar here. public TcpHttpClient(long handle, RCSymbolScalar hostandport) { _handle = handle; _host = (string)hostandport.Part(1); _port = (long)hostandport.Part(2); // _uri = new Uri("http://" + _host + ":" + _port.ToString ()); _inbox = new TcpReceiveBox(); _client = new WebClient(); _client.UploadDataCompleted += UploadDataCompleted; _client.DownloadDataCompleted += DownloadDataCompleted; }
public TcpCubeClient(long handle, RCSymbolScalar right) { object[] parts = right.ToArray(); for (int i = 1; i < parts.Length; ++i) { Path.Combine(_path, (string)parts[i]); } _dir = new DirectoryInfo(_path); _files = new Dictionary <string, FileStream> (); _handle = handle; }
public void List(RCRunner runner, RCClosure closure, RCSymbol key) { RCSymbolScalar[] array; lock (_lock) { Dictionary <RCSymbolScalar, RCValue> store = GetSection(key); array = new RCSymbolScalar[store.Keys.Count]; store.Keys.CopyTo(array, 0); } runner.Yield(closure, new RCSymbol(array)); }
public void EvalTuple(RCRunner runner, RCClosure closure, RCString left, RCString right) { RCArray <RCSymbolScalar> result = new RCArray <RCSymbolScalar> (); for (int i = 0; i < right.Count; ++i) { string[] parts = right[i].Split(left[0].ToCharArray()); RCSymbolScalar sym = RCSymbolScalar.From(parts); result.Write(sym); } runner.Yield(closure, new RCSymbol(result)); }
public override void VisitNull <T> (string name, Column <T> column, int row) { if (!_unplug) { RCSymbolScalar scalar = null; if (_source.Axis.Symbol != null) { scalar = _source.Axis.Symbol[_row]; } _target.WriteCell(name, scalar, _defaultValue, _row, true, true); } }
public TcpClient(long handle, RCSymbolScalar symbol, Tcp.Protocol protocol, int timeout) { _protocol = protocol; _handle = handle; _host = (string)symbol.Part(1); _port = (long)symbol.Part(2); _inbox = new TcpReceiveBox(); _outbox = new TcpOutBox(); _buffer = new TcpMessageBuffer(); _timeout = timeout; _timeoutTimer = null; }
public Queue <RCClosure> Dequeue(RCSymbolScalar scalar) { Queue <RCClosure> result = null; HashSet <long> candidates = null; HashSet <long> fibers = null; while (scalar != null) { _fibersBySymbol.TryGetValue(scalar, out fibers); if (fibers != null) { if (candidates == null) { candidates = new HashSet <long> (); } candidates.UnionWith(fibers); } scalar = scalar.Previous; } if (candidates == null) { return(null); } int count = _waitOrder.Count; for (int i = 0; i < count; ++i) { RCClosure next = _waitOrder.Dequeue(); if (candidates.Contains(next.Fiber)) { if (result == null) { result = new Queue <RCClosure> (); } HashSet <RCSymbolScalar> symbols = _symbolsByFiber[next.Fiber]; _symbolsByFiber.Remove(next.Fiber); foreach (RCSymbolScalar symbol in symbols) { _fibersBySymbol[symbol].Remove(next.Fiber); } result.Enqueue(next); } else { // Put it back in the queue, this preserves the original priority // because we go all the way through the queue each time. _waitOrder.Enqueue(next); } } return(result); }
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)); }
protected void UploadDataCompleted(object sender, UploadDataCompletedEventArgs e) { RCAsyncState state = (RCAsyncState)e.UserState; if (e.Error != null) { state.Runner.Report(state.Closure, e.Error); } RCSymbolScalar id = (RCSymbolScalar)state.Other; string text = _client.Encoding.GetString(e.Result); // TODO: make this into a real object with headers and body and stuff. _inbox.Add(id, new RCString(text)); }
public virtual RCBlock CompleteReceive(RCRunner runner, int count, long handle, long sid, out long cid) { RCBlock message = null; if (_read == 0) { // Inspect the header to see if we have enough buffer for the message // that is going to arrive. If not create a bigger buffer. _reading = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(_recvBuffer, 0)); _cid = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(_recvBuffer, 4)); if (_reading > _recvBuffer.Length - HEADER_SIZE) { Console.Out.WriteLine(" replacing buffer...", _read, _reading); byte[] replacement = new byte[_reading + HEADER_SIZE]; _recvBuffer.CopyTo(replacement, count); _recvBuffer = replacement; } } _read += count; cid = _cid; if (_read >= _reading + HEADER_SIZE) { string text = Encoding.ASCII.GetString(_recvBuffer, HEADER_SIZE, _read - HEADER_SIZE); bool fragment = false; // This one creates a new parser an so it is threadsafe for multiple calls. RCValue body = RCSystem.Parse(text, out fragment); if (body != null) { // throw new Exception("failed to parse:" + text); RCSymbolScalar correlation = new RCSymbolScalar(null, handle); if (sid > -1) { correlation = new RCSymbolScalar(correlation, sid); } correlation = new RCSymbolScalar(correlation, _cid); message = new RCBlock(null, "id", ":", new RCSymbol(correlation)); message = new RCBlock(message, "body", ":", body); } _read = 0; _reading = 0; _cid = -1; } return(message); }
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); }
public void EvalSymbolString(RCRunner runner, RCClosure closure, RCString right) { RCLexer lexer = new RCLexer(new RCArray <RCTokenType> ( RCTokenType.Number, RCTokenType.Boolean, RCTokenType.Symbol, RCTokenType.Name)); RCArray <RCSymbolScalar> result = new RCArray <RCSymbolScalar> (right.Count); for (int i = 0; i < right.Count; ++i) { RCSymbolScalar symbol = RCSymbolScalar.From(lexer, right[i]); result.Write(symbol); } runner.Yield(closure, new RCSymbol(result)); }
public static string PathSymbolToString(char separator, RCSymbolScalar symbol) { object[] parts = symbol.ToArray(); string path = ""; string zero = parts[0].ToString(); int startIndex = 0; if (zero == "home") { string home = Environment.GetEnvironmentVariable("RCL_HOME"); if (home == null) { home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + separator + "dev"; } path += home; if (parts.Length > 1) { path += separator; } startIndex = 1; } else if (zero == "root") { path += separator; startIndex = 1; } else if (zero == "work") { path += parts.Length == 1 ? "." : ""; startIndex = 1; } // Need to handle windows drive letter. // There is a function called "GetPathRoot." // I think it can be used to obtain the drive letter prefix, // but only if you have a path. for (int i = startIndex; i < parts.Length; ++i) { path += parts[i].ToString(); if (i < parts.Length - 1) { path += separator; } } return(path); }
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)); }
public TreeNode(TreeNode parent, string name, int number) { s = new RCSymbolScalar(parent.s, (long)number); if (name == null || name.Equals("")) { k = new RCSymbolScalar(parent.k, (long)number); } else { k = new RCSymbolScalar(parent.k, name); } if (parent.children == null) { parent.children = new RCArray <TreeNode> (8); } parent.children.Write(this); }
public void Add(RCSymbolScalar id, RCValue message) { lock (_lock) { TcpCollector waiter = null; // Console.Out.WriteLine ("Client message in:{0}", id); if (_waiters.TryGetValue(id, out waiter)) { _waiters.Remove(id); waiter.Accept(id, message); } else { _replies.Add(id, message); } } }
public void Remove(RCSymbolScalar id, TcpCollector waiter) { lock (_lock) { RCValue message = null; if (_replies.TryGetValue(id, out message)) { _replies.Remove(id); waiter.Accept(id, message); } else { _waiters.Add(id, waiter); } } // Console.Out.WriteLine ("Client waiting on {0}", new RCSymbol(new // RCArray<RCSymbolScalar>(_waiters.Keys))); }
public override int CompareAxisRows(Timeline axis1, int i1, Timeline axis2, int i2) { RCAssert.AxisHasT(axis1, "CompareAxisRows: axis1 must contain the T column"); RCAssert.AxisHasT(axis2, "CompareAxisRows: axis2 must contain the T column"); RCTimeScalar timeX = axis1.Time[i1]; RCTimeScalar timeY = axis2.Time[i2]; int compareResult = timeX.CompareTo(timeY); if (compareResult == 0) { RCSymbolScalar symbolX = axis1.SymbolAt(i1); RCSymbolScalar symbolY = axis2.SymbolAt(i2); return(symbolX.CompareTo(symbolY)); } else { return(compareResult); } }