public static IEnumerable <ThreadInfo> GetLocations(ThreadManager TM, string procedure, bool hideDescs, Dictionary <string, string> query, params SqlParameter[] parameters) { var currentThread = TM.CurrentThread; if (query.ContainsKey("version")) { double version; if (Double.TryParse(query["version"], out version)) { if (version >= Program.LocationsVersion) { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.NoContent))); } } } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, procedure, parameters)); DataTable table = TM.GetResult <DataTable>(currentThread); var response = new LocationsResponse(Program.LocationsVersion); foreach (DataRow row in table.Rows) { var location = new Location(row, hideDescs); if (location.Id >= 0) { if (location.IsMapArea()) { yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetMapAreaCorners", new SqlParameter("@maparea", location.Id))); DataTable cornerTable = TM.GetResult <DataTable>(currentThread); foreach (DataRow cornerRow in cornerTable.Rows) { location.MapArea.Corners.Add(new LatLong(cornerRow)); } } if (location.HasAltNames) { yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationAltNames", new SqlParameter("@location", location.Id))); location.AddAltNames(TM.GetResult <DataTable>(currentThread)); } if (location.HasLinks && !hideDescs) { yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationLinks", new SqlParameter("@location", location.Id))); location.AddLinks(TM.GetResult <DataTable>(currentThread)); } response.Locations.Add(location); } } response.Locations = LocationsHandler.ApplySearchFilter(query, location => location.Name, (location, name) => location.Name = name, response.Locations); yield return(TM.Return(currentThread, new JsonResponse(response))); }
protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; if (!(state is List <int>)) { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest))); } var tags = (List <int>)state; var locations = new Dictionary <int, LocationRank>(); int order = 0; foreach (int tag in tags) { yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetTaggedLocationIds", new SqlParameter("tag", tag))); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { int id = (int)row["location"]; if (!locations.ContainsKey(id)) { locations[id] = new LocationRank(id, order++); } locations[id].AddHit((int)row["priority"]); } } var response = new LocationIdsResponse(locations.Values.OrderBy(x => x, new LocationRankComparer()).Select(l => l.LocId).ToList()); yield return(TM.Return(currentThread, new JsonResponse(response))); }
public static IEnumerable <ThreadInfo> GetNames(ThreadManager TM, string procedure, Dictionary <string, string> query) { var currentThread = TM.CurrentThread; if (query.ContainsKey("version")) { double version; if (Double.TryParse(query["version"], out version)) { if (version >= Program.LocationsVersion) { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.NoContent))); } } } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, procedure)); var table = TM.GetResult <DataTable>(currentThread); var response = new LocationNamesResponse(Program.LocationsVersion); foreach (DataRow row in table.Rows) { var location = new LocationName(row); if (location.Id >= 0) { response.Names.Add(location); } } response.Names = LocationsHandler.ApplySearchFilter(query, locName => locName.Name, (locName, name) => locName.Name = name, response.Names); yield return(TM.Return(currentThread, new JsonResponse(response))); }
private IEnumerable <ThreadInfo> GetPartitionPaths(ThreadManager TM, Stack <PartitionPass> partitions, Dictionary <int, SortedSet <Node> > goalNodes) { var currentThread = TM.CurrentThread; var currentNode = partitions.Peek().Target; var paths = new List <List <PartitionPass> >(); // Travel to other partitions, and get the paths through them yield return(TM.Await(currentThread, GetNodePartitions(TM, currentNode, "spGetNodePartitions"))); var nodePartitions = TM.GetResult <SortedSet <int> >(currentThread).Except(partitions.Select((PartitionPass pp) => pp.Partition)); foreach (int partition in nodePartitions) { // See if any goal nodes are in this partition if (goalNodes.ContainsKey(partition)) { foreach (var goalNode in goalNodes[partition]) { partitions.Push(new PartitionPass(partition, goalNode, _settings.WeightedDist(currentNode, goalNode))); var path = partitions.Reverse().Skip(1).ToList(); paths.Add(path); partitions.Pop(); } } // Go to all the boundaries of this partition yield return(TM.Await(currentThread, GetPartitionBoundaries(TM, partition))); var targetNodes = TM.GetResult <SortedSet <Node> >(currentThread); if (goalNodes.ContainsKey(partition)) { targetNodes.ExceptWith(goalNodes[partition]); } targetNodes.Remove(currentNode); foreach (var targetNode in targetNodes) { partitions.Push(new PartitionPass(partition, targetNode, _settings.WeightedDist(currentNode, targetNode))); yield return(TM.Await(currentThread, GetPartitionPaths(TM, partitions, goalNodes))); paths.AddRange(TM.GetResult <List <List <PartitionPass> > >(currentThread)); partitions.Pop(); } } yield return(TM.Return(currentThread, paths)); }
/// <summary> /// Handles console commands /// </summary> /// <param name="TM">Thread Manager</param> /// <returns>No return</returns> public static IEnumerable <ThreadInfo> HandleConsoleRequests(ThreadManager TM) { var currentThread = TM.CurrentThread; while (true) { yield return(TM.WaitForConsole(currentThread)); string request = TM.GetResult <string>(currentThread); try { switch (request) { case "help": Console.WriteLine("update\nstatus\nexecutions\nqueues\nthreads\ndirections\nexit"); break; case "update": UpdateServerVersion(); break; case "status": TM.WriteExecutionStatus(); TM.WriteQueueStatus(); TM.WriteThreadStatus(); DirectionsFinder.WriteStatus(); break; case "executions": TM.WriteExecutionStatus(); break; case "queues": TM.WriteQueueStatus(); break; case "threads": TM.WriteThreadStatus(); break; case "directions": DirectionsFinder.WriteStatus(); break; case "exit": System.Environment.Exit(0); break; default: Console.WriteLine("Invalid request."); break; } } catch (Exception ex) { Console.WriteLine("An exception occurred: {0}\nStack trace:\n{1}", ex.Message, ex.StackTrace); } Console.WriteLine(); } }
protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; if (query.ContainsKey("version")) { double version; if (Double.TryParse(query["version"], out version)) { if (version >= Program.TagsVersion) { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.NoContent))); } } } var response = new TagsResponse(Program.TagsVersion); var categories = new Dictionary <string, TagCategory>(); categories.Add("", response.Root); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetTagCategories")); using (var table = TM.GetResult <DataTable>(currentThread)) { foreach (DataRow row in table.Rows) { var category = new TagCategory(row); categories[category.Parent ?? ""].Children.Add(category); categories.Add(category.Name, category); } } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetTags")); using (var table = TM.GetResult <DataTable>(currentThread)) { foreach (DataRow row in table.Rows) { var tag = new Tag(row); categories[(string)row["category"]].Tags.Add(tag); } } yield return(TM.Return(currentThread, new JsonResponse(response))); }
private static IEnumerable <ThreadInfo> HandleRequest(ThreadManager TM, HttpListenerContext context) { var currentThread = TM.CurrentThread; JsonResponse result = null; var path = Uri.UnescapeDataString(context.Request.Url.LocalPath).Split('/').SkipWhile(String.IsNullOrEmpty).TakeWhile(s => !String.IsNullOrEmpty(s)); Dictionary <string, string> query = new Dictionary <string, string>(); if (!String.IsNullOrEmpty(context.Request.Url.Query)) { var querySplit = Uri.UnescapeDataString(context.Request.Url.Query).Split('?', '&', ';').Skip(1); foreach (string field in querySplit) { int equalsPos = field.IndexOf('='); if (equalsPos == -1 || equalsPos == 0 || equalsPos == field.Length - 1) { result = new JsonResponse(HttpStatusCode.BadRequest); break; } query.Add(field.Substring(0, equalsPos), field.Substring(equalsPos + 1, field.Length - equalsPos - 1)); } } if (result == null) { yield return(TM.Await(currentThread, _handler.HandlePath(TM, path, query, new object()))); try { result = TM.GetResult <JsonResponse>(currentThread); } catch (Exception) { result = new JsonResponse(HttpStatusCode.InternalServerError); } } var encoding = new ASCIIEncoding(); byte[] b; if (result.Json != null) { b = encoding.GetBytes(result.Json.Serialize()); } else { b = encoding.GetBytes(result.Message); } context.Response.OutputStream.Write(b, 0, b.Length); context.Response.StatusCode = (int)result.StatusCode; context.Response.Close(); yield return(TM.Return(currentThread, null)); }
protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationDesc", new SqlParameter("@location", (int)state))); var table = TM.GetResult <DataTable>(currentThread); foreach (DataRow row in table.Rows) { yield return(TM.Return(currentThread, new JsonResponse(new LocationDescResponse(row)))); } yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest))); }
protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; var result = new PathDataResponse(Program.LocationsVersion); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPaths")); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { result.Paths.Add(new Path(row)); } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetNodes")); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { result.Nodes.Add(new Node(row)); } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPartitions")); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { result.Partitions.Add(new Partition(row)); } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPartitions")); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { result.Partitions.Add(new Partition(row)); } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionMessages")); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { result.Messages.Add(new DirectionMessage(row)); } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirections")); foreach (DataRow row in TM.GetResult <DataTable>(currentThread).Rows) { var direction = new Direction(row); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionPaths", new SqlParameter("direction", direction.Id))); foreach (DataRow pathRow in TM.GetResult <DataTable>(currentThread).Rows) { direction.Paths.Add((int)pathRow["path"]); } result.Directions.Add(direction); } yield return(TM.Return(currentThread, new JsonResponse(result))); }
private IEnumerable <ThreadInfo> GetPartitionBoundaries(ThreadManager TM, int partition) { var currentThread = TM.CurrentThread; var nodes = new SortedSet <Node>(new NodeComparer()); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPartitionBoundaries", new SqlParameter("@partition", partition))); using (var boundariesTable = TM.GetResult <DataTable>(currentThread)) { foreach (DataRow boundaryRow in boundariesTable.Rows) { nodes.Add(new Node(boundaryRow)); } } yield return(TM.Return(currentThread, nodes)); }
private IEnumerable <ThreadInfo> GetNodePartitions(ThreadManager TM, Node node, string procedure) { var currentThread = TM.CurrentThread; var partitions = new SortedSet <int>(); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, procedure, new SqlParameter("@node", node.Id))); using (var partitionsTable = TM.GetResult <DataTable>(currentThread)) { foreach (DataRow partitionRow in partitionsTable.Rows) { partitions.Add((int)partitionRow["partition"]); } } yield return(TM.Return(currentThread, partitions)); }
protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; var storedProcData = (SqlStoredProcData)state; var parameters = new SqlParameter[query.Count]; int i = 0; foreach (var kvp in query) { parameters[i] = new SqlParameter(kvp.Key, kvp.Value); i++; } yield return(TM.MakeDbCall(currentThread, Program.GetConnectionString(storedProcData.LoginData.Username, storedProcData.LoginData.Password), storedProcData.StoredProcName, parameters)); bool success = true; var response = new StoredProcedureResponse(); try { var table = TM.GetResult <DataTable>(currentThread); foreach (DataColumn column in table.Columns) { response.Columns.Add(column.ColumnName); } int columns = response.Columns.Count; foreach (DataRow row in table.Rows) { var rowList = new List <string>(); for (int j = 0; j < columns; j++) { rowList.Add(row.IsNull(j) ? null : row[j].ToString()); } response.Table.Add(rowList); } } catch { success = false; } if (success) { yield return(TM.Return(currentThread, new JsonResponse(response))); } else { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest))); } }
protected override IEnumerable <ThreadInfo> HandleIntPath(ThreadManager TM, int value, object state) { var currentThread = TM.CurrentThread; yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationDepartNode", new SqlParameter("@location", value))); var table = TM.GetResult <DataTable>(currentThread); if (table.Rows.Count > 0) { yield return(TM.Return(currentThread, new DirectionsFinder(table.Rows[0]))); } else { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest))); } }
protected override IEnumerable <ThreadInfo> HandleNoPath(ThreadManager TM, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; var loginData = (SqlLoginData)state; yield return(TM.MakeDbCall(currentThread, Program.GetConnectionString(loginData.Username, loginData.Password), "spTestConnection")); bool success = true; try { var result = TM.GetResult <DataTable>(currentThread); if (result.Rows.Count != 1 || (int)result.Rows[0][0] != 56) { success = false; } } catch { success = false; } if (success) { var alreadyLoggedIn = AdminHandler.Logins.Where(kvp => kvp.Value.Username == loginData.Username); if (alreadyLoggedIn.Any()) { AdminHandler.Logins.Remove(alreadyLoggedIn.First().Key); } Guid id = Guid.NewGuid(); while (AdminHandler.Logins.ContainsKey(id)) { id = Guid.NewGuid(); } AdminHandler.Logins[id] = loginData; yield return(TM.Return(currentThread, new JsonResponse(new AuthenticationResponse(loginData.Expiration, id)))); } else { yield return(TM.Return(currentThread, new JsonResponse(HttpStatusCode.BadRequest))); } }
public static IEnumerable <ThreadInfo> HandleClients(ThreadManager TM) { var currentThread = TM.CurrentThread; while (true) { int tries = 1; HttpListener listener = null; while (true) { try { listener = new HttpListener(); listener.Prefixes.Add("http://+:5600/"); Console.WriteLine("[{0}]\nAttempt #{1} to start HttpListener...", DateTime.Now, tries); listener.Start(); Console.WriteLine("HttpListener successfully started.\n"); break; } catch (Exception ex) { Console.WriteLine("HttpListener failed to start with exception:\nMessage: {0}\nStack trace:\n{1}\n", ex.Message, ex.StackTrace); tries++; } if (tries > 5) { Console.WriteLine("[{0}]\nMaximum number of attempts reached. Try again later.\n", DateTime.Now); yield return(TM.Return(currentThread)); } else { Console.WriteLine("[{0}]\nWaiting 5 seconds before attemping again...", DateTime.Now); yield return(TM.Sleep(currentThread, 5000)); } } Console.WriteLine("[{0}]\nWaiting for requests...\n", DateTime.Now); while (true) { yield return(TM.WaitForClient(currentThread, listener)); HttpListenerContext context = null; try { context = TM.GetResult <HttpListenerContext>(currentThread); if (context.Request.RawUrl.StartsWith("/admin/authenticate/")) { Console.WriteLine("[{0}]\nHandling request: {1}\n", DateTime.Now, "/admin/authenticate/..."); } else { Console.WriteLine("[{0}]\nHandling request: {1}\n", DateTime.Now, context.Request.RawUrl); } } catch (Exception ex) { Console.WriteLine("HttpListener threw an exception while waiting for a client:\nMessage: {0}\nStack trace:\n{1}\n\nGoing to restart the HttpListener...", ex.Message, ex.StackTrace); break; } TM.Enqueue(HandleRequest(TM, context)); } listener.Close(); } }
private IEnumerable <ThreadInfo> GetDirectionMessages(ThreadManager TM) { var currentThread = TM.CurrentThread; for (int i = 0; i < _paths.Count; i++) { _messageFindingDone = (double)(i * i) / (double)(_paths.Count * _paths.Count); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirection", new SqlParameter("@startpath", _paths[i].Id))); using (var directionTable = TM.GetResult <DataTable>(currentThread)) { foreach (DataRow directionRow in directionTable.Rows) { yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetDirectionPaths", new SqlParameter("@direction", (int)directionRow["id"]))); using (var pathTable = TM.GetResult <DataTable>(currentThread)) { bool match = true; int dir = 0; int j = i; foreach (DataRow pathRow in pathTable.Rows) { if (dir != 0) { if (j < 0 || j >= _paths.Count || (int)pathRow["path"] != _paths[j].Id) { match = false; break; } j += dir; } else { if (i > 0 && (int)pathRow["path"] == _paths[i - 1].Id) { dir = -1; j = i - 2; } else if (i < _paths.Count - 1 && (int)pathRow["path"] == _paths[i + 1].Id) { dir = 1; j = i + 2; } else { match = false; break; } } } if (match) { // TODO: Deal with within's bool forward = dir == 0 ? _paths[i].Forward : dir > 0; int index = Math.Min(j - dir, i) + (int)directionRow["nodeoffset"]; if (forward) { if (!directionRow.IsNull("message1")) { _paths[index].Dir = (string)directionRow["message1"]; _paths[index].Action = (string)directionRow["action1"]; } } else { if (!directionRow.IsNull("message1")) { _paths[index].Dir = (string)directionRow["message2"]; _paths[index].Action = (string)directionRow["action2"]; } } } } } } } yield return(TM.Return(currentThread)); }
private IEnumerable <ThreadInfo> GetShortestPath(ThreadManager TM, int to) { var currentThread = TM.CurrentThread; // See if the start node is already in the goal location yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationNodes", new SqlParameter("@location", to))); using (var table = TM.GetResult <DataTable>(currentThread)) { if (table.Rows.Count == 0) { // Error - no nodes in goal location yield return(TM.Return(currentThread)); } foreach (DataRow row in table.Rows) { if (new Node(row).Id == _start.Id) { // Already at goal yield return(TM.Return(currentThread)); } } } // Get the nodes on the boundary of the goal location var goalNodes = new Dictionary <int, SortedSet <Node> >(); yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetLocationBoundaryNodes", new SqlParameter("@location", to))); using (var table = TM.GetResult <DataTable>(currentThread)) { if (table.Rows.Count == 0) { // Error - no nodes on goal location's boundary yield return(TM.Return(currentThread)); } foreach (DataRow row in table.Rows) { var node = new Node(row); if (!goalNodes.ContainsKey(node.Partition.Value)) { goalNodes[node.Partition.Value] = new SortedSet <Node>(new NodeComparer()); } goalNodes[node.Partition.Value].Add(node); } } // Get the possible partition paths Stack <PartitionPass> partitions = new Stack <PartitionPass>(); partitions.Push(new PartitionPass(-1, _start, 0)); yield return(TM.Await(currentThread, GetPartitionPaths(TM, partitions, goalNodes))); var passes = TM.GetResult <List <List <PartitionPass> > >(currentThread); SortedSet <TargetPath> queue = new SortedSet <TargetPath>(new TargetPathComparer()); Dictionary <int, ShortestPath> shortestPaths = new Dictionary <int, ShortestPath>(); shortestPaths[_start.Id] = new ShortestPath(); foreach (var pass in passes) { queue.Add(new TargetPath(_start, 0, pass, queue, _settings)); } while (true) { var currentPath = queue.First(); _pathFindingDone = Math.Max(_pathFindingDone, currentPath.DistSoFar * currentPath.DistSoFar / (currentPath.BestDist * currentPath.BestDist)); if (goalNodes.Any(kvp => kvp.Value.Contains(currentPath.CurrentNode))) { var currentNode = currentPath.CurrentNode; var prevNode = shortestPaths[currentNode.Id].PrevNode; _paths.Add(new DirectionPath(currentNode.Lat, currentNode.Lon, null, true, null, currentNode.Altitude, currentNode.Location, currentNode.Outside, shortestPaths[currentNode.Id].Id, shortestPaths[currentNode.Id].Forward)); while (currentNode.Id != _start.Id) { _paths.Insert(0, new DirectionPath(prevNode.Lat, prevNode.Lon, null, false, null, prevNode.Altitude, prevNode.Location, prevNode.Outside, shortestPaths[currentNode.Id].Id, shortestPaths[currentNode.Id].Forward)); currentNode = prevNode; prevNode = shortestPaths[currentNode.Id].PrevNode; } _paths.First().Flag = true; yield return(TM.Return(currentThread)); } yield return(TM.MakeDbCall(currentThread, Program.ConnectionString, "spGetPathsFromNode", new SqlParameter("@node", currentPath.CurrentNode.Id), new SqlParameter("@part", currentPath.PartitionsLeft.First().Partition))); using (var table = TM.GetResult <DataTable>(currentThread)) { foreach (DataRow row in table.Rows) { var path = new PathToNode(row, currentPath.CurrentNode.Id, currentPath.PartitionsLeft.First().Partition); double pathDist = _settings.WeightedDist(currentPath.CurrentNode, path.Node); if (shortestPaths.ContainsKey(path.Node.Id)) { if (shortestPaths[path.Node.Id].TotalDist > currentPath.DistSoFar + pathDist) { shortestPaths[path.Node.Id].SetPrevNode(currentPath.CurrentNode, pathDist, path.Path.Id, path.Forward, shortestPaths); } else if (shortestPaths[path.Node.Id].TotalDist < currentPath.DistSoFar + pathDist) { continue; } // TODO } else { shortestPaths[path.Node.Id] = new ShortestPath(currentPath.CurrentNode, pathDist, path.Path.Id, path.Forward, shortestPaths); // TODO } if (path.Node.Id == currentPath.PartitionsLeft.First().Target.Id) { queue.Add(new TargetPath(path.Node, shortestPaths[path.Node.Id].TotalDist, currentPath.PartitionsLeft.Skip(1).ToList(), queue, _settings)); } else { queue.Add(new TargetPath(path.Node, shortestPaths[path.Node.Id].TotalDist, currentPath.PartitionsLeft, queue, _settings)); } } } queue.Remove(currentPath); } }
public IEnumerable <ThreadInfo> HandlePath(ThreadManager TM, IEnumerable <string> path, Dictionary <string, string> query, object state) { var currentThread = TM.CurrentThread; if (path.Any()) { if (Redirects.ContainsKey(path.First())) { yield return(TM.Await(currentThread, Redirects[path.First()].HandlePath(TM, path.Skip(1), query, state))); } else { int intValue; if (Int32.TryParse(path.First(), out intValue)) { yield return(TM.Await(currentThread, HandleIntPath(TM, intValue, state))); if (!(TM.GetResultNoException(currentThread) is JsonResponse)) { yield return(TM.Await(currentThread, IntRedirect.HandlePath(TM, path.Skip(1), query, TM.GetResult(currentThread)))); } } else { double floatValue; if (Double.TryParse(path.First(), out floatValue)) { yield return(TM.Await(currentThread, HandleFloatPath(TM, floatValue, state))); if (!(TM.GetResultNoException(currentThread) is JsonResponse)) { yield return(TM.Await(currentThread, FloatRedirect.HandlePath(TM, path.Skip(1), query, TM.GetResult(currentThread)))); } } else { yield return(TM.Await(currentThread, HandleUnknownPath(TM, path.First(), state))); if (!(TM.GetResultNoException(currentThread) is JsonResponse)) { yield return(TM.Await(currentThread, UnknownRedirect.HandlePath(TM, path.Skip(1), query, TM.GetResult(currentThread)))); } } } } } else { yield return(TM.Await(currentThread, HandleNoPath(TM, query, state))); } yield return(TM.Return(currentThread)); }
public IEnumerable <ThreadInfo> ScriptDatabase(ThreadManager TM, string connectionString, string dbName, string destination, DateTime start, string user) { var currentThread = TM.CurrentThread; yield return(TM.StartNewThread(currentThread, () => { using (var connection = new SqlConnection(connectionString)) using (var output = new StreamWriter(destination)) { connection.Open(); var server = new Server(new ServerConnection(connection)); var database = server.Databases[dbName]; var scripter = new Scripter(server); scripter.Options.ScriptSchema = true; scripter.Options.ScriptData = true; scripter.Options.ScriptDrops = false; scripter.Options.WithDependencies = true; scripter.Options.Indexes = true; scripter.Options.DriAllConstraints = true; // Not scripting: DdlTrigger, UnresolvedEntity var urns = new List <Urn>(); urns.AddRange(database.UserDefinedFunctions.Cast <UserDefinedFunction>().Where(f => !f.IsSystemObject).Select(f => f.Urn)); foreach (UserDefinedFunction function in database.UserDefinedFunctions) { if (!function.IsSystemObject) { urns.Add(function.Urn); } else { break; } } foreach (View view in database.Views) { if (!view.IsSystemObject) { urns.Add(view.Urn); } else { break; } } foreach (Table table in database.Tables) { if (!table.IsSystemObject) { urns.Add(table.Urn); } else { break; } } foreach (StoredProcedure proc in database.StoredProcedures) { if (!proc.IsSystemObject) { urns.Add(proc.Urn); } else { break; } } foreach (Default def in database.Defaults) { urns.Add(def.Urn); } foreach (Microsoft.SqlServer.Management.Smo.Rule rule in database.Rules) { urns.Add(rule.Urn); } foreach (Trigger trigger in database.Triggers) { if (!trigger.IsSystemObject) { urns.Add(trigger.Urn); } else { break; } } foreach (UserDefinedAggregate aggregate in database.UserDefinedAggregates) { urns.Add(aggregate.Urn); } foreach (Synonym synonym in database.Synonyms) { urns.Add(synonym.Urn); } foreach (UserDefinedDataType type in database.UserDefinedDataTypes) { urns.Add(type.Urn); } foreach (XmlSchemaCollection xsc in database.XmlSchemaCollections) { urns.Add(xsc.Urn); } foreach (UserDefinedType type in database.UserDefinedTypes) { urns.Add(type.Urn); } foreach (UserDefinedTableType type in database.UserDefinedTableTypes) { urns.Add(type.Urn); } foreach (PartitionScheme scheme in database.PartitionSchemes) { urns.Add(scheme.Urn); } foreach (PartitionFunction function in database.PartitionFunctions) { urns.Add(function.Urn); } foreach (PlanGuide guide in database.PlanGuides) { urns.Add(guide.Urn); } foreach (SqlAssembly assembly in database.Assemblies) { if (!assembly.IsSystemObject) { urns.Add(assembly.Urn); } else { break; } } output.WriteLine("-- Script generated at {0:MM/dd/yyyy HH:mm:ss} by {1} --", start, user); var strings = scripter.EnumScript(urns.ToArray()); foreach (string s in strings) { if (s.Contains("CREATE") || s.StartsWith("SET ANSI_NULLS")) { output.WriteLine("GO"); } output.WriteLine(s); } output.WriteLine("-- Scripting complete --"); return "Successfully scripted database."; } })); yield return(TM.Return(currentThread, new JsonResponse(new MessageResponse(TM.GetResult <string>(currentThread))))); }