LocalThread.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 
00023 #include "mcs.hh"
00024 using namespace mcs;
00025 
00026 
00027 //--------------------------------------------------------
00028 mcs::LocalThread::LocalThread(Thread* parent) :
00029   BaseThread(parent, "LLL"),
00030   recv(true)
00031 {
00032   MCS_DEBUG_SETUP(0, "LocalThread");
00033   Log(MCS_OK( MSG_THREAD_CREATE, "LocalThread" ));
00034 
00035   client = serverClients();
00036 }
00037 
00038 
00039 mcs::LocalThread::~LocalThread()
00040 {
00041   Log(MCS_OK( MSG_THREAD_DESTROY, "LocalThread" ));
00042 }
00043 
00044 
00045 bool mcs::LocalThread::userID2clientID(int userid, int* cid)
00046 {
00047   int i;
00048 
00049   MCS_DEBUG_ENTER(<< this->id());
00050 
00051   for (i=0; i<env->max_users; i++) {
00052     if (client[i]) {
00053       if (client[i]->userID() == userid) {
00054     *cid = i;
00055     return true;
00056       }
00057     }
00058   }
00059 
00060   MCS_DEBUG_LEAVE(<< this->id());
00061   return false;
00062 }
00063 
00064 
00065 void mcs::LocalThread::dataDispatcher()
00066 {
00067     int userid, cid;
00068 
00069     MCS_DEBUG_ENTER(<< this->id());
00070 
00071     while (env->server->dispatch.count() > 0) {
00072       Data d = env->server->dispatch.pop();
00073       userid = d.getDestID();
00074 
00075       if (userid == MCS_ID_LOCAL)
00076     recv.addField(d);
00077       else
00078     if (userID2clientID(userid, &cid)) {
00079       client[cid]->recv.addField(d);
00080       client[cid]->wakeUpClient();
00081     }
00082     else
00083       Log(MCS_WARN( MSG_UNKNOWN_USERID, userid));
00084     }
00085 
00086     MCS_DEBUG_LEAVE(<< this->id());
00087 }
00088 
00089 
00090 void mcs::LocalThread::run()
00091 {
00092   try{
00093 
00094     while (state() == MCS_STATE_RUNNING) {
00095       while (env->server->dispatch.count() == 0)
00096     sleep_ms(500);
00097 
00098       dataDispatcher();
00099     }
00100   }
00101   catch(Event e) {
00102     Log(e);
00103     if (e.type() == FATAL)
00104       return;
00105   }
00106 }
00107 
00108 
00109 
00110 void mcs::LocalThread::hk_log(UserThread* p, Event e)
00111 {
00112     if (env->server->cb_log)
00113         (*(env->server->cb_log))(p, e);
00114 
00115     if (env->server->cbwa_log)
00116         (*(env->server->cbwa_log))();
00117 }
00118 
00119 
00120 UserThread** mcs::LocalThread::serverClients()
00121 {
00122     //Friendness of LocalThread to Server
00123     return ((Server* ) parent())->pClient;
00124 }

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012