// Responsible for tracking and taking care of slaves // // Copyright (C) 2000 Stephen A. Torri // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Questions, comments or improvements should be sent to: // s.torri@lancaster.ac.uk or storri@stratos.net // #include #include //------------------------------------- // pre: Slave_ptr is initialized and a valid reference // post: Slave_List_Node created and initialized // function: Create a Slave_List_Node that we can add to a list // // slv_ptr - valid slave pointer to the slave being added to the slave list //------------------------------------- Slave_List_Node::Slave_List_Node(Comms::Slave_ptr slv_ptr) { next = NULL; prev = NULL; node_slv_ptr = Comms::Slave::_duplicate(slv_ptr); } //------------------------------------- // pre: Slave_ptr is initialized and a valid reference // post: Slave_List_Node created and initialized // function: Create a Slave_List_Node that we can add to a list //------------------------------------- Slave_List::Slave_List(){ head = NULL; end = NULL; size = 0; } Slave_List::~Slave_List(){} //------------------------------------- // pre: Slave exists in list and a set position // post: Slave is removed from the list // function: remove the node at "position" if position < size // // position - place in list where the target Slave exists //------------------------------------- Comms::Slave_ptr Slave_List::removeSlave(int position){ Comms::Slave_ptr slv_ptr = Comms::Slave::_nil(); if( position < size) { // get reference to where head points (1st node which is position 0) Slave_List_Node* iterator = head; Slave_List_Node* deleteNode = head; if (position == 0) { // beginning of list // get reference of the node we wish to delete //deleteNode = iterator; // move iterator to node after "target" //iterator = deleteNode->next; // change reference of head to the node after "target" or NULL head = deleteNode->next; // if iterator is not NULL then set prev to point to the head if ( head != NULL) { head->prev = NULL; } else { // no items in list // head == NULL // end == NULL end = head; } } else { // else if (position == size - 1) { // end of list for (int i = 0; i < position; i++){ iterator = iterator->next; deleteNode = iterator; } // confirm we are at the end if ( iterator->next == NULL) { // move the iterator back one node iterator = deleteNode->prev; // point the iterator node to NULL (end) iterator->next = NULL; // make end point to the new end of list end = iterator; } else { // we are some where after position 0 but before end // move the iterator back one iterator = deleteNode->prev; // iterator->next = deleteNode->next; position after removed iterator->next = deleteNode->next; // move interator passed removed node iterator = deleteNode->next; // iterator->prev = deleteNode->prev; position before removed iterator->prev = deleteNode->prev; } } deleteNode->next = NULL; deleteNode->prev = NULL; slv_ptr = Comms::Slave::_duplicate(deleteNode->node_slv_ptr); CORBA::release(deleteNode->node_slv_ptr); delete deleteNode; size--; } return slv_ptr; } //------------------------------------- // pre: Slave exists in list // post: Slave that matches slv_ptr is removed // function: Removes slaves from list that match slv_ptr // // slv_ptr - reference to the Slave to be removed //------------------------------------- Comms::Slave_ptr Slave_List::removeSlave(Comms::Slave_ptr slv_ptr){ int position = indexOf(slv_ptr); return removeSlave(position); } //------------------------------------- // pre: Slave list is created and initialized // post: Return integer marking the position of the Slave // function: Find slaves in list. If it is found we return // the position otherwise we return -1 (not found). // // slv_ptr - reference to the slave we wish to find. //------------------------------------- int Slave_List::indexOf (Comms::Slave_ptr slv_ptr){ Slave_List_Node* iterator = head; // start at first node int position = -1; for (int i = 0; i < size; i++) { if ( iterator->node_slv_ptr->_is_equivalent(slv_ptr) ) { position = i; break; } iterator = iterator->next; } return position; } //------------------------------------- // pre: Slave list created and initialized // post: Slave added to list // function: Add a new slave to the slave list // // slv_ptr - reference to the slave to be added //------------------------------------- void Slave_List::addSlave(Comms::Slave_ptr slv_ptr){ dout << "Slave_List::addSlave - entered addSlave" << endl; if (CORBA::is_nil(slv_ptr)) { cerr << "Slave_List::addSlave - slv_ptr is nil" << endl; throw 0; } else { dout << "Slave_List: addSlave - adding node to list" << endl; Slave_List_Node* temp = new Slave_List_Node(slv_ptr); if(temp != NULL){ dout << "Slave_List::addSlave - temp is not NULL" << endl; temp->prev = NULL; dout << "Slave_List::addSlave - temp->prev = head" << endl; temp->next = head; if(head != NULL){ head->prev = temp; } if(end == NULL){ end = temp; } head = temp; size++; dout << "Slave_List::addSlave - size++" << endl; } } } //------------------------------------- // pre: Slave list created, initialized and contains at least one slave // post: First slave removed from list. // function: This removes the first slave from the list. //------------------------------------- Comms::Slave_ptr Slave_List::getSlave(){ Comms::Slave_ptr slave = removeSlave(0); #if DEBUG if (CORBA::is_nil(slave)){ cout << "Slave_List::getSlave - Failed to remove slave" << endl; cout << "\tFunction: Slave_List::getSlave()" << endl; } #endif return slave; } //------------------------------------- // pre: Slave list created and initialized. // post: Slave list size returned. // function: Get the size of the slave list and return it. //------------------------------------- int Slave_List::getSize() { return size; }