broker.cc (5834B)
1 /* -*- Mode: c++; -*- */ 2 /* -------------------------------------------------------------------- 3 * Filename: 4 * broker.cc 5 * 6 * Description: 7 * <---> 8 * 9 * Authors: 10 * Andreas Aardal Hanssen <andreas-binc curly bincimap spot org> 11 * 12 * Bugs: 13 * 14 * ChangeLog: 15 * 16 * -------------------------------------------------------------------- 17 * Copyright 2002-2005 Andreas Aardal Hanssen 18 * 19 * This program is free software; you can redistribute it and/or modify 20 * it under the terms of the GNU General Public License as published by 21 * the Free Software Foundation; either version 2 of the License, or 22 * (at your option) any later version. 23 * 24 * This program is distributed in the hope that it will be useful, 25 * but WITHOUT ANY WARRANTY; without even the implied warranty of 26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27 * GNU General Public License for more details. 28 * 29 * You should have received a copy of the GNU General Public License 30 * along with this program; if not, write to the Free Software 31 * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. 32 * -------------------------------------------------------------------- 33 */ 34 #include <map> 35 #include <string> 36 37 #include "broker.h" 38 #include "io.h" 39 #include "convert.h" 40 #include "operators.h" 41 #include "recursivedescent.h" 42 #include "session.h" 43 44 using namespace ::std; 45 using namespace Binc; 46 47 //---------------------------------------------------------------------- 48 BrokerFactory::BrokerFactory(void) 49 { 50 brokers[Session::NONAUTHENTICATED] = new Broker(); 51 brokers[Session::AUTHENTICATED] = new Broker(); 52 brokers[Session::SELECTED] = new Broker(); 53 } 54 55 //---------------------------------------------------------------------- 56 BrokerFactory::~BrokerFactory(void) 57 { 58 for (map<int, Broker *>::iterator i = brokers.begin(); 59 i != brokers.end(); ++i) 60 delete i->second; 61 } 62 63 //---------------------------------------------------------------------- 64 BrokerFactory &BrokerFactory::getInstance(void) 65 { 66 static BrokerFactory brokerfactory; 67 return brokerfactory; 68 } 69 70 //---------------------------------------------------------------------- 71 void BrokerFactory::addCapability(const std::string &c) 72 { 73 for (map<int, Broker *>::iterator i = brokers.begin(); 74 i != brokers.end(); ++i) { 75 CapabilityOperator * o; 76 o = dynamic_cast<CapabilityOperator*>(i->second->get("CAPABILITY")); 77 if (o != 0) { 78 o->addCapability(c); 79 break; 80 } 81 } 82 } 83 84 //---------------------------------------------------------------------- 85 void BrokerFactory::assign(const string &fname, Operator *o) 86 { 87 int deletable = true; 88 for (map<int, Broker *>::iterator i = brokers.begin(); 89 i != brokers.end(); ++i) 90 if (i->first & o->getState()) { 91 i->second->assign(fname, o, deletable); 92 deletable = false; 93 } 94 } 95 96 //---------------------------------------------------------------------- 97 Operator *BrokerFactory::getOperator(int state, const string &name) const 98 { 99 if (brokers.find(state) == brokers.end()) 100 return 0; 101 else 102 return brokers.find(state)->second->get(name); 103 } 104 105 //---------------------------------------------------------------------- 106 Broker *BrokerFactory::getBroker(int state) 107 { 108 if (brokers.find(state) == brokers.end()) { 109 setLastError("No appropriate broker for state."); 110 return 0; 111 } 112 113 return brokers[state]; 114 } 115 116 //---------------------------------------------------------------------- 117 Broker::Broker(void) 118 { 119 } 120 121 //---------------------------------------------------------------------- 122 Broker::~Broker(void) 123 { 124 } 125 126 //---------------------------------------------------------------------- 127 void Broker::assign(const string &fname, Operator *o, bool deletable) 128 { 129 deletables[fname] = deletable; 130 operators[fname] = o; 131 } 132 133 //---------------------------------------------------------------------- 134 Operator *Broker::get(const string &name) const 135 { 136 if (operators.find(name) == operators.end()) 137 return 0; 138 139 return operators.find(name)->second; 140 } 141 142 //---------------------------------------------------------------------- 143 Operator::ParseResult Broker::parseStub(Request &command) 144 { 145 Session &session = Session::getInstance(); 146 147 string tag; 148 string cmd; 149 150 switch (expectTag(tag)) { 151 case Operator::ACCEPT: 152 break; 153 case Operator::REJECT: 154 session.setLastError("Syntax error; first token must be a tag"); 155 case Operator::ERROR: 156 return Operator::ERROR; 157 case Operator::TIMEOUT: 158 return Operator::TIMEOUT; 159 } 160 161 switch (expectSPACE()) { 162 case Operator::ACCEPT: 163 break; 164 case Operator::REJECT: 165 session.setLastError("Syntax error; second token must be a SPACE"); 166 case Operator::ERROR: 167 return Operator::ERROR; 168 case Operator::TIMEOUT: 169 return Operator::TIMEOUT; 170 } 171 172 switch (expectAstring(cmd)) { 173 case Operator::ACCEPT: 174 break; 175 case Operator::REJECT: 176 session.setLastError("Syntax error; third token must be a command"); 177 case Operator::ERROR: 178 return Operator::ERROR; 179 case Operator::TIMEOUT: 180 return Operator::TIMEOUT; 181 } 182 183 uppercase(cmd); 184 185 if (cmd == "UID") { 186 command.setUidMode(); 187 188 switch (expectSPACE()) { 189 case Operator::ACCEPT: 190 break; 191 case Operator::REJECT: 192 session.setLastError("Syntax error; after UID there" 193 " must come a SPACE"); 194 case Operator::ERROR: 195 return Operator::ERROR; 196 case Operator::TIMEOUT: 197 return Operator::TIMEOUT; 198 } 199 200 switch (expectAstring(cmd)) { 201 case Operator::ACCEPT: 202 break; 203 case Operator::REJECT: 204 session.setLastError("Syntax error; after UID " 205 "SPACE there must come a command"); 206 case Operator::ERROR: 207 return Operator::ERROR; 208 case Operator::TIMEOUT: 209 return Operator::TIMEOUT; 210 } 211 212 uppercase(cmd); 213 } 214 215 command.setTag(tag); 216 command.setName(cmd); 217 218 return Operator::ACCEPT; 219 }