示例#1
0
/*
 *
 * avl_insert_immediate(tree, afterp, newnode):
 *	insert newnode immediately into tree immediately after afterp.
 *	after insertion, newnode is right child of afterp.
 */
void
avl_insert_immediate(
    avltree_desc_t *tree,
    avlnode_t *afterp,
    avlnode_t *newnode)
{
    /*
     * Clean all pointers for sanity; some will be reset as necessary.
     */
    newnode->avl_nextino = NULL;
    newnode->avl_parent = NULL;
    newnode->avl_forw = NULL;
    newnode->avl_back = NULL;
    newnode->avl_balance = AVL_BALANCE;

    if (afterp == NULL) {
        tree->avl_root = newnode;
        tree->avl_firstino = newnode;
        return;
    }

    ASSERT(afterp->avl_forw == NULL);
    avl_insert_grow(tree, afterp, newnode, AVL_FORW); /* grow to right */
    CERT(afterp->avl_forw == newnode);
    avl_balance(&tree->avl_root, afterp, AVL_FORW);
    avl_checktree(tree, tree->avl_root);
}
示例#2
0
static
avlnode_t *
avl_insert_find_growth(
    register avltree_desc_t *tree,
    register __psunsigned_t start,	/* range start at start, */
    register __psunsigned_t end,	/* exclusive */
    register int   *growthp)	/* OUT */
{
    avlnode_t *root = tree->avl_root;
    register avlnode_t *np;

    np = root;
    ASSERT(np); /* caller ensures that there is atleast one node in tree */

    for ( ; ; ) {
        CERT(np->avl_parent || root == np);
        CERT(!np->avl_parent || root != np);
        CERT(!(np->avl_back) || np->avl_back->avl_parent == np);
        CERT(!(np->avl_forw) || np->avl_forw->avl_parent == np);
        CERT(np->avl_balance != AVL_FORW || np->avl_forw);
        CERT(np->avl_balance != AVL_BACK || np->avl_back);
        CERT(np->avl_balance != AVL_BALANCE ||
             np->avl_back == NULL || np->avl_forw);
        CERT(np->avl_balance != AVL_BALANCE ||
             np->avl_forw == NULL || np->avl_back);

        if (AVL_START(tree, np) >= end) {
            if (np->avl_back) {
                np = np->avl_back;
                continue;
            }
            *growthp = AVL_BACK;
            break;
        }

        if (AVL_END(tree, np) <= start) {
            if (np->avl_forw) {
                np = np->avl_forw;
                continue;
            }
            *growthp = AVL_FORW;
            break;
        }
        /* found exact match -- let caller decide if it is an error */
        return(NULL);
    }
    return(np);
}
示例#3
0
//===================
//     PRIVATE
//===================
bool WebServer::setupWebSocket(quint16 port){
  WSServer = new QWebSocketServer("sysadm-server", QWebSocketServer::SecureMode, this);
  //SSL Configuration
  QSslConfiguration config = QSslConfiguration::defaultConfiguration();
	QFile CF( QStringLiteral(SSLCERTFILE) ); 
	  if(CF.open(QIODevice::ReadOnly) ){
	    QSslCertificate CERT(&CF,QSsl::Pem);
	    config.setLocalCertificate( CERT );
	    CF.close();   
	  }else{
	    qWarning() << "Could not read WS certificate file:" << CF.fileName();
	  }
	QFile KF( QStringLiteral(SSLKEYFILE));
	  if(KF.open(QIODevice::ReadOnly) ){
	    QSslKey KEY(&KF, QSsl::Rsa, QSsl::Pem);
	    config.setPrivateKey( KEY );
	    KF.close();	
	  }else{
	    qWarning() << "Could not read WS key file:" << KF.fileName();
	  }
	config.setPeerVerifyMode(QSslSocket::VerifyNone);
	config.setProtocol(SSLVERSION);
  WSServer->setSslConfiguration(config);
  //Setup Connections
  connect(WSServer, SIGNAL(newConnection()), this, SLOT(NewSocketConnection()) );
  connect(WSServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(NewConnectError(QAbstractSocket::SocketError)) );
  //  -- websocket specific signals
  connect(WSServer, SIGNAL(closed()), this, SLOT(ServerClosed()) );
  connect(WSServer, SIGNAL(serverError(QWebSocketProtocol::CloseCode)), this, SLOT(ServerError(QWebSocketProtocol::CloseCode)) );
  connect(WSServer, SIGNAL(originAuthenticationRequired(QWebSocketCorsAuthenticator*)), this, SLOT(OriginAuthRequired(QWebSocketCorsAuthenticator*)) );
  connect(WSServer, SIGNAL(peerVerifyError(const QSslError&)), this, SLOT(PeerVerifyError(const QSslError&)) );
  connect(WSServer, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(SslErrors(const QList<QSslError>&)) );
  connect(WSServer, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(ConnectError(QAbstractSocket::SocketError)) );
  //Now start the server
  return WSServer->listen(QHostAddress::Any, port);
}
示例#4
0
avlnode_t *
avl_insert(
    register avltree_desc_t *tree,
    register avlnode_t *newnode)
{
    register avlnode_t *np;
    register __psunsigned_t start = AVL_START(tree, newnode);
    register __psunsigned_t end = AVL_END(tree, newnode);
    int growth;

    ASSERT(newnode);
    ASSERT(start <= end);

    /*
     * Clean all pointers for sanity; some will be reset as necessary.
     */
    newnode->avl_nextino = NULL;
    newnode->avl_parent = NULL;
    newnode->avl_forw = NULL;
    newnode->avl_back = NULL;
    newnode->avl_balance = AVL_BALANCE;

    if ((np = tree->avl_root) == NULL) { /* degenerate case... */
        tree->avl_root = newnode;
        tree->avl_firstino = newnode;
        return newnode;
    }

    if ((np = avl_insert_find_growth(tree, start, end, &growth)) == NULL) {
        if (start != end)  { /* non-zero length range */
            fprintf(stderr,
                    _("avl_insert: Warning! duplicate range [%llu,%llu]\n"),
                    (unsigned long long)start,
                    (unsigned long long)end);
        }
        return(NULL);
    }

    avl_insert_grow(tree, np, newnode, growth);
    if (growth == AVL_BACK) {
        /*
         * Growing to left. if np was firstino, newnode will be firstino
         */
        if (tree->avl_firstino == np)
            tree->avl_firstino = newnode;
    }
#ifdef notneeded
    else if (growth == AVL_FORW)
        /*
         * Cannot possibly be firstino; there is somebody to our left.
         */
        ;
#endif

    newnode->avl_parent = np;
    CERT(np->avl_forw == newnode || np->avl_back == newnode);

    avl_balance(&tree->avl_root, np, growth);

    avl_checktree(tree, tree->avl_root);

    return newnode;
}
示例#5
0
/*
 * Balance buffer AVL tree after attaching a new node to root.
 * Called only by avl_insert.
 */
static void
avl_balance(
    register avlnode_t **rootp,
    register avlnode_t *np,
    register int growth)
{
    /*
     * At this point, np points to the node to which
     * a new node has been attached.  All that remains is to
     * propagate avl_balance up the tree.
     */
    for ( ; ; ) {
        register avlnode_t *parent = np->avl_parent;
        register avlnode_t *child;

        CERT(growth == AVL_BACK || growth == AVL_FORW);

        /*
         * If the buffer was already balanced, set avl_balance
         * to the new direction.  Continue if there is a
         * parent after setting growth to reflect np's
         * relation to its parent.
         */
        if (np->avl_balance == AVL_BALANCE) {
            np->avl_balance = growth;
            if (parent) {
                if (parent->avl_forw == np)
                    growth = AVL_FORW;
                else {
                    ASSERT(parent->avl_back == np);
                    growth = AVL_BACK;
                }

                np = parent;
                continue;
            }
            break;
        }

        if (growth != np->avl_balance) {
            /*
             * Subtree is now balanced -- no net effect
             * in the size of the subtree, so leave.
             */
            np->avl_balance = AVL_BALANCE;
            break;
        }

        if (growth == AVL_BACK) {

            child = np->avl_back;
            CERT(np->avl_balance == AVL_BACK && child);

            if (child->avl_balance == AVL_BACK) { /* single LL */
                /*
                 * ``A'' just got inserted;
                 * np points to ``E'', child to ``C'',
                 * and it is already AVL_BACK --
                 * child will get promoted to top of subtree.

                np->	     -E			C
                	     / \	       / \
                child->	   -C   F	     -B   E
                	   / \		     /   / \
                	 -B   D		    A   D   F
                	 /
                	A

                	Note that child->avl_parent and
                	avl_balance get set in common code.
                 */
                np->avl_parent = child;
                np->avl_balance = AVL_BALANCE;
                np->avl_back = child->avl_forw;
                if (child->avl_forw)
                    child->avl_forw->avl_parent = np;
                child->avl_forw = np;
            } else {
                /*
                 * double LR
                 *
                 * child's avl_forw node gets promoted to
                 * the top of the subtree.

                np->	     -E		      C
                	     / \	     / \
                child->	   +B   F	   -B   E
                	   / \		   /   / \
                	  A  +C		  A   D   F
                	       \
                		D

                 */
                register avlnode_t *tmp = child->avl_forw;

                CERT(child->avl_balance == AVL_FORW && tmp);

                child->avl_forw = tmp->avl_back;
                if (tmp->avl_back)
                    tmp->avl_back->avl_parent = child;

                tmp->avl_back = child;
                child->avl_parent = tmp;

                np->avl_back = tmp->avl_forw;
                if (tmp->avl_forw)
                    tmp->avl_forw->avl_parent = np;

                tmp->avl_forw = np;
                np->avl_parent = tmp;

                if (tmp->avl_balance == AVL_BACK)
                    np->avl_balance = AVL_FORW;
                else
                    np->avl_balance = AVL_BALANCE;

                if (tmp->avl_balance == AVL_FORW)
                    child->avl_balance = AVL_BACK;
                else
                    child->avl_balance = AVL_BALANCE;

                /*
                 * Set child to point to tmp since it is
                 * now the top of the subtree, and will
                 * get attached to the subtree parent in
                 * the common code below.
                 */
                child = tmp;
            }

        } else { /* growth == AVL_BACK */

            /*
             * This code is the mirror image of AVL_FORW above.
             */

            child = np->avl_forw;
            CERT(np->avl_balance == AVL_FORW && child);

            if (child->avl_balance == AVL_FORW) { /* single RR */
                np->avl_parent = child;
                np->avl_balance = AVL_BALANCE;
                np->avl_forw = child->avl_back;
                if (child->avl_back)
                    child->avl_back->avl_parent = np;
                child->avl_back = np;
            } else {
                /*
                 * double RL
                 */
                register avlnode_t *tmp = child->avl_back;

                ASSERT(child->avl_balance == AVL_BACK && tmp);

                child->avl_back = tmp->avl_forw;
                if (tmp->avl_forw)
                    tmp->avl_forw->avl_parent = child;

                tmp->avl_forw = child;
                child->avl_parent = tmp;

                np->avl_forw = tmp->avl_back;
                if (tmp->avl_back)
                    tmp->avl_back->avl_parent = np;

                tmp->avl_back = np;
                np->avl_parent = tmp;

                if (tmp->avl_balance == AVL_FORW)
                    np->avl_balance = AVL_BACK;
                else
                    np->avl_balance = AVL_BALANCE;

                if (tmp->avl_balance == AVL_BACK)
                    child->avl_balance = AVL_FORW;
                else
                    child->avl_balance = AVL_BALANCE;

                child = tmp;
            }
        }

        child->avl_parent = parent;
        child->avl_balance = AVL_BALANCE;

        if (parent) {
            if (parent->avl_back == np)
                parent->avl_back = child;
            else
                parent->avl_forw = child;
        } else {
            ASSERT(*rootp == np);
            *rootp = child;
        }

        break;
    }
}