MCS  0.3.3-alpha7
LocalThread.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 
23 #include "mcs.hh"
24 using namespace mcs;
25 
26 
27 //--------------------------------------------------------
29  BaseThread(parent, "LLL"),
30  recv(true)
31 {
32  MCS_DEBUG_SETUP(0, "LocalThread");
33  Log(MCS_OK( MSG_THREAD_CREATE, "LocalThread" ));
34 
35  client = serverClients();
36 }
37 
38 
40 {
41  Log(MCS_OK( MSG_THREAD_DESTROY, "LocalThread" ));
42 }
43 
44 
45 bool mcs::LocalThread::userID2clientID(int userid, int* cid)
46 {
47  int i;
48 
49  MCS_DEBUG_ENTER(<< this->id());
50 
51  for (i=0; i<env->max_users; i++) {
52  if (client[i]) {
53  if (client[i]->userID() == userid) {
54  *cid = i;
55  return true;
56  }
57  }
58  }
59 
60  MCS_DEBUG_LEAVE(<< this->id());
61  return false;
62 }
63 
64 
66 {
67  int userid, cid;
68 
69  MCS_DEBUG_ENTER(<< this->id());
70 
71  while (env->server->dispatch.count() > 0) {
72  Data d = env->server->dispatch.pop();
73  userid = d.getDestID();
74 
75  if (userid == MCS_ID_LOCAL)
76  recv.addField(d);
77  else
78  if (userID2clientID(userid, &cid)) {
79  client[cid]->recv.addField(d);
80  client[cid]->wakeUpClient();
81  }
82  else
83  Log(MCS_WARN( MSG_UNKNOWN_USERID, userid));
84  }
85 
86  MCS_DEBUG_LEAVE(<< this->id());
87 }
88 
89 
91 {
92  try{
93 
94  while (state() == MCS_STATE_RUNNING) {
95  while (env->server->dispatch.count() == 0)
96  sleep_ms(500);
97 
99  }
100  }
101  catch(Event e) {
102  Log(e);
103  if (e.type() == FATAL)
104  return;
105  }
106 }
107 
108 
109 
111 {
112  if (env->server->cb_log)
113  (*(env->server->cb_log))(p, e);
114 
115  if (env->server->cbwa_log)
116  (*(env->server->cbwa_log))();
117 }
118 
119 
121 {
122  //Friendness of LocalThread to Server
123  return ((Server* ) parent())->pClient;
124 }
void(* cbwa_log)()
Pointer to a void callback function without arguments, called by hk_log().
Definition: mcs.hh:7304
RetValue Log(Event e)
Logging facility.
Definition: BaseThread.cc:29
void(* cb_log)(UserThread *p, Event e)
Pointer to a callback function, called by hk_log().
Definition: mcs.hh:7286
Base class for MCS threaded objects.
Definition: mcs.hh:6098
The server side client thread.
Definition: mcs.hh:6365
Main server class for a MCS-based application.
Definition: mcs.hh:7166
void dataDispatcher()
Dispatch Data objects in the "send" vector to UserThread object&#39;s "send" vectors. ...
Definition: LocalThread.cc:65
Record dispatch
Record of Data objects to be sent to various threads.
Definition: mcs.hh:7322
Hold informations about an event.
Definition: mcs.hh:814
static Env * env
Pointer to the actual Env object, this can be seen in all threaded object.
Definition: mcs.hh:6116
Thread * parent()
Returns the address of the parent.
Definition: Thread.cc:316
void sleep_ms(unsigned int millisec)
A millisecond resolution sleep function.
Definition: Thread.cc:603
int max_users
How many clients can be connected at the same time.
Definition: mcs.hh:6949
virtual void hk_log(UserThread *p, Event e)
Virtual method called each time the BaseThread.Log() method is executed.
Definition: LocalThread.cc:110
void wakeUpClient(Event *e=NULL)
Send a message to the client.
Definition: UserThread.cc:282
UserThread ** serverClients()
Returns the Server.pClient variable.
Definition: LocalThread.cc:120
RetValue type()
Return the event type.
Definition: Event.cc:99
virtual void run()
Thread body.
Definition: LocalThread.cc:90
Main include file for all MCS based applications.
void addField(Data *d)
Wrapper around Dynamic_Array.push.
Definition: Record.cc:364
#define MCS_STATE_RUNNING
Thread state: the separate thread is executing the run() method.
Definition: mcs.hh:2409
~LocalThread()
Destructor.
Definition: LocalThread.cc:39
#define MCS_WARN(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:961
A general purpose data type.
Definition: mcs.hh:3092
Record recv
Record of Data objects received from the client or other threads.
Definition: mcs.hh:6713
#define MCS_OK(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:958
Record recv
Record of Data objects received from other threads.
Definition: mcs.hh:6843
A class to create separate threads.
Definition: mcs.hh:2487
bool userID2clientID(int userid, int *cid)
Search for a client ID (CID) associated with a destination ID.
Definition: LocalThread.cc:45
int count()
Wrapper around Dynamic_Array.count.
Definition: Record.cc:359
int state()
Return the state of the thread.
Definition: Thread.cc:307
LocalThread(const LocalThread &)
Declared to avoid using of default copy constructor.
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018