]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_spanningtree.cpp
Moved some stuff to debug
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
index abdb6d7988a27a61b1f6685ca8cfeea7fc780864..edfef803981dfb5a625611fe20bd2acd2aa248da 100644 (file)
@@ -162,6 +162,26 @@ class TreeServer
                }
                return false;
        }
+
+       // removes child nodes of this node, and of that node, etc etc
+       bool Tidy()
+       {
+               bool stillchildren = true;
+               while (stillchildren)
+               {
+                       stillchildren = false;
+                       for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
+                       {
+                               TreeServer* s = (TreeServer*)*a;
+                               s->Tidy();
+                               Children.erase(a);
+                               delete s;
+                               stillchildren = true;
+                               break;
+                       }
+               }
+               return true;
+       }
 };
 
 class Link
@@ -351,6 +371,60 @@ class TreeSocket : public InspSocket
                }
        }
 
+       void SquitServer(TreeServer* Current)
+       {
+               // recursively squit the servers attached to 'Current'
+               for (unsigned int q = 0; q < Current->ChildCount(); q++)
+               {
+                       TreeServer* recursive_server = Current->GetChild(q);
+                       this->SquitServer(recursive_server);
+               }
+               // Now we've whacked the kids, whack self
+               log(DEBUG,"Deleted %s",Current->GetName().c_str());
+               bool quittingpeople = true;
+               while (quittingpeople)
+               {
+                       quittingpeople = false;
+                       for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
+                       {
+                               if (!strcasecmp(u->second->server,Current->GetName().c_str()))
+                               {
+                                       log(DEBUG,"Quitting user %s of server %s",u->second->nick,u->second->server);
+                                       Srv->QuitUser(u->second,Current->GetName()+" "+std::string(Srv->GetServerName()));
+                                       quittingpeople = true;
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       void Squit(TreeServer* Current,std::string reason)
+       {
+               if (Current)
+               {
+                       std::deque<std::string> params;
+                       params.push_back(Current->GetName());
+                       params.push_back(":"+reason);
+                       DoOneToAllButSender(Current->GetParent()->GetName(),"SQUIT",params,Current->GetName());
+                       if (Current->GetParent() == TreeRoot)
+                       {
+                               Srv->SendOpers("Server \002"+Current->GetName()+"\002 SQUIT: "+reason);
+                       }
+                       else
+                       {
+                               Srv->SendOpers("Server \002"+Current->GetName()+"\002 split from server \002"+Current->GetParent()->GetName()+"\002 with reason: "+reason);
+                       }
+                       SquitServer(Current);
+                       Current->Tidy();
+                       Current->GetParent()->DelChild(Current);
+                       delete Current;
+               }
+               else
+               {
+                       log(DEBUG,"Squit from unknown server");
+               }
+       }
+
        bool ForceJoin(std::string source, std::deque<std::string> params)
        {
                if (params.size() < 1)
@@ -548,7 +622,7 @@ class TreeSocket : public InspSocket
                TreeServer* Node = new TreeServer(servername,description,ParentOfThis,NULL);
                ParentOfThis->AddChild(Node);
                DoOneToAllButSender(prefix,"SERVER",params,prefix);
-               Srv->SendOpers("*** Server "+prefix+" introduced server "+servername+" ("+description+")");
+               Srv->SendOpers("*** Server \002"+prefix+"\002 introduced server \002"+servername+"\002 ("+description+")");
                return true;
        }
 
@@ -579,6 +653,7 @@ class TreeSocket : public InspSocket
                                // node.
                                TreeServer* Node = new TreeServer(servername,description,TreeRoot,this);
                                TreeRoot->AddChild(Node);
+                               DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,servername);
                                this->DoBurst(Node);
                                return true;
                        }
@@ -604,7 +679,7 @@ class TreeSocket : public InspSocket
                {
                        if ((x->Name == servername) && (x->RecvPass == password))
                        {
-                               Srv->SendOpers("*** Verified incoming server connection from "+servername+"["+this->GetIP()+"] ("+description+")");
+                               Srv->SendOpers("*** Verified incoming server connection from \002"+servername+"\002["+this->GetIP()+"] ("+description+")");
                                this->InboundServerName = servername;
                                this->InboundDescription = description;
                                // this is good. Send our details: Our server name and description and hopcount of 0,
@@ -653,7 +728,7 @@ class TreeSocket : public InspSocket
 
        bool ProcessLine(std::string line)
        {
-               Srv->SendToModeMask("o",WM_AND,"inbound-line: '"+line+"'");
+               Srv->Log(DEBUG,"inbound-line: '"+line+"'");
 
                std::deque<std::string> params = this->Split(line);
                std::string command = "";
@@ -707,6 +782,12 @@ class TreeSocket : public InspSocket
                                        this->LinkState = CONNECTED;
                                        Node = new TreeServer(InboundServerName,InboundDescription,TreeRoot,this);
                                        TreeRoot->AddChild(Node);
+                                       params.clear();
+                                       params.push_back(InboundServerName);
+                                       params.push_back("*");
+                                       params.push_back("1");
+                                       params.push_back(InboundDescription);
+                                       DoOneToAllButSender(TreeRoot->GetName(),"SERVER",params,InboundServerName);
                                        this->DoBurst(Node);
                                }
                                else if (command == "ERROR")
@@ -747,6 +828,14 @@ class TreeSocket : public InspSocket
                                {
                                        return this->RemoteServer(prefix,params);
                                }
+                               else if (command == "SQUIT")
+                               {
+                                       if (params.size() == 2)
+                                       {
+                                               this->Squit(FindServer(params[0]),params[1]);
+                                       }
+                                       return true;
+                               }
                                else
                                {
                                        // not a special inter-server command.
@@ -794,14 +883,31 @@ class TreeSocket : public InspSocket
 
        virtual void OnTimeout()
        {
-               if (this->LinkState = CONNECTING)
+               if (this->LinkState == CONNECTING)
                {
-                       Srv->SendOpers("*** CONNECT: Connection to "+myhost+" timed out.");
+                       Srv->SendOpers("*** CONNECT: Connection to \002"+myhost+"\002 timed out.");
                }
        }
 
        virtual void OnClose()
        {
+               // Connection closed.
+               // If the connection is fully up (state CONNECTED)
+               // then propogate a netsplit to all peers.
+               std::string quitserver = this->myhost;
+               if (this->InboundServerName != "")
+               {
+                       quitserver = this->InboundServerName;
+               }
+               TreeServer* s = FindServer(quitserver);
+               if (s)
+               {
+                       std::deque<std::string> params;
+                       params.push_back(quitserver);
+                       params.push_back(":Remote host closed the connection");
+                       DoOneToAllButSender(Srv->GetServerName(),"SQUIT",params,quitserver);
+                       Squit(s,"Remote host closed the connection");
+               }
        }
 
        virtual int OnIncomingConnection(int newsock, char* ip)
@@ -971,54 +1077,78 @@ class ModuleSpanningTree : public Module
                return;
        }
 
-       void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[32][80])
+       // WARNING: NOT THREAD SAFE - DONT GET ANY SMART IDEAS.
+
+       void ShowMap(TreeServer* Current, userrec* user, int depth, char matrix[128][80])
        {
-               for (int t = 0; t < depth; t++)
-               {
-                       matrix[line][t] = ' ';
-               }
-               strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
-               line++;
-               //WriteServ(user->fd,"006 %s :%s%s",user->nick,header,Current->GetName().c_str());
-               for (unsigned int q = 0; q < Current->ChildCount(); q++)
+               if (line < 128)
                {
-                       ShowMap(Current->GetChild(q),user,depth+2,matrix);
+                       for (int t = 0; t < depth; t++)
+                       {
+                               matrix[line][t] = ' ';
+                       }
+                       strlcpy(&matrix[line][depth],Current->GetName().c_str(),80);
+                       line++;
+                       for (unsigned int q = 0; q < Current->ChildCount(); q++)
+                       {
+                               ShowMap(Current->GetChild(q),user,depth+2,matrix);
+                       }
                }
        }
 
-       // Yes, this is smart (and odd). You may all praise me now.
-       // After looking over how others did this, with tons of ugly
-       // maths combined with lots of recursion, i decided to approach
-       // this in a very different way. To cut down on the recursion,
-       // as users may be able to trigger this command, the algorithm
-       // renders the map 'backwards' without recursion after it has
-       // correctly placed the servers into the lines.
+       // Ok, prepare to be confused.
+       // After much mulling over how to approach this, it struck me that
+       // the 'usual' way of doing a /MAP isnt the best way. Instead of
+       // keeping track of a ton of ascii characters, and line by line
+       // under recursion working out where to place them using multiplications
+       // and divisons, we instead render the map onto a backplane of characters
+       // (a character matrix), then draw the branches as a series of "L" shapes
+       // from the nodes. This is not only friendlier on CPU it uses less stack.
+
        void HandleMap(char** parameters, int pcnt, userrec* user)
        {
-               char matrix[32][80];
-               for (unsigned int t = 0; t < 32; t++)
+               // This array represents a virtual screen which we will
+               // "scratch" draw to, as the console device of an irc
+               // client does not provide for a proper terminal.
+               char matrix[128][80];
+               for (unsigned int t = 0; t < 128; t++)
                {
-                       matrix[line][0] = '\0';
+                       matrix[t][0] = '\0';
                }
                line = 0;
+               // The only recursive bit is called here.
                ShowMap(TreeRoot,user,0,matrix);
+               // Process each line one by one. The algorithm has a limit of
+               // 128 servers (which is far more than a spanning tree should have
+               // anyway, so we're ok). This limit can be raised simply by making
+               // the character matrix deeper, 128 rows taking 10k of memory.
                for (int l = 1; l < line; l++)
                {
+                       // scan across the line looking for the start of the
+                       // servername (the recursive part of the algorithm has placed
+                       // the servers at indented positions depending on what they
+                       // are related to)
                        int first_nonspace = 0;
                        while (matrix[l][first_nonspace] == ' ')
                        {
                                first_nonspace++;
                        }
                        first_nonspace--;
+                       // Draw the `- (corner) section: this may be overwritten by
+                       // another L shape passing along the same vertical pane, becoming
+                       // a |- (branch) section instead.
                        matrix[l][first_nonspace] = '-';
                        matrix[l][first_nonspace-1] = '`';
                        int l2 = l - 1;
+                       // Draw upwards until we hit the parent server, causing possibly
+                       // other corners (`-) to become branches (|-)
                        while ((matrix[l2][first_nonspace-1] == ' ') || (matrix[l2][first_nonspace-1] == '`'))
                        {
                                matrix[l2][first_nonspace-1] = '|';
                                l2--;
                        }
                }
+               // dump the whole lot to the user. This is the easy bit, honest.
                for (int t = 0; t < line; t++)
                {
                        WriteServ(user->fd,"006 %s :%s",user->nick,&matrix[t][0]);