NEURON
bbslocal.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include "bbsconf.h"
3 #include <InterViews/resource.h>
4 #include "oc2iv.h"
5 #include "bbslocal.h"
6 #include "bbslsrv.h"
7 #include <nrnmpi.h>
8 
9 #if defined(HAVE_STL)
10 #if defined(HAVE_SSTREAM) // the standard ...
11 #include <map>
12 #include <set>
13 #include <string>
14 #else
15 #include <pair.h>
16 #include <map.h>
17 #endif
18 
19 struct ltint {
20  bool operator()(int i, int j) const {
21  return i < j;
22  }
23 };
24 
25 class KeepArgs: public std::map<int, const MessageValue*, ltint> {};
26 
27 #endif
28 
32 
34  if (!server_) {
35  server_ = new BBSLocalServer();
36  posting_ = nil;
37  taking_ = nil;
38  }
39  start();
40 #if defined(HAVE_STL)
41  keepargs_ = new KeepArgs();
42 #endif
43 }
44 
46  // need to unref anything in keepargs_;
47 #if defined(HAVE_STL)
48  delete keepargs_;
49 #endif
50 }
51 
53 
54 void BBSLocal::perror(const char* s) {
55  hoc_execerror("BBSLocal error in ", s);
56 }
57 
59  int i;
60  if (!taking_ || taking_->upkint(&i))
61  perror("upkint");
62  return i;
63 }
64 
66  double x;
67  if (!taking_ || taking_->upkdouble(&x)) {
68  perror("upkdouble");
69  }
70  return x;
71 }
72 
73 void BBSLocal::upkvec(int n, double* x) {
74  if (!taking_ || taking_->upkvec(n, x)) {
75  perror("upkdouble");
76  }
77 }
78 
80  int len;
81  char* s;
82  if (!taking_ || taking_->upkint(&len)) {
83  perror("upkstr length");
84  }
85  s = new char[len + 1];
86  if (taking_->upkstr(s)) {
87  perror("upkstr string");
88  }
89  return s;
90 }
91 
92 char* BBSLocal::upkpickle(size_t* n) {
93  int len;
94  char* s;
95  if (!taking_ || taking_->upkint(&len)) {
96  perror("upkpickle length");
97  }
98  s = new char[len];
99  if (taking_->upkpickle(s, n)) {
100  perror("upkpickle data");
101  }
102  assert(*n == len);
103  return s;
104 }
105 
108  posting_ = new MessageValue();
109  posting_->ref();
110 }
111 
112 void BBSLocal::pkint(int i) {
113  if (!posting_ || posting_->pkint(i)) {
114  perror("pkint");
115  }
116 }
117 
118 void BBSLocal::pkdouble(double x) {
119  if (!posting_ || posting_->pkdouble(x)) {
120  perror("pkdouble");
121  }
122 }
123 
124 void BBSLocal::pkvec(int n, double* x) {
125  if (!posting_ || posting_->pkvec(n, x)) {
126  perror("pkdouble");
127  }
128 }
129 
130 void BBSLocal::pkstr(const char* s) {
131  if (!posting_ || posting_->pkint(strlen(s))) {
132  perror("pkstr length");
133  }
134  if (!posting_ || posting_->pkstr(s)) {
135  perror("pkstr string");
136  }
137 }
138 
139 void BBSLocal::pkpickle(const char* s, size_t n) {
140  if (!posting_ || posting_->pkint(n)) {
141  perror("pkpickle size");
142  }
143  if (!posting_ || posting_->pkpickle(s, n)) {
144  perror("pkpickle data");
145  }
146 }
147 
148 void BBSLocal::post(const char* key) {
151  posting_ = nil;
152 }
153 
154 bool BBSLocal::look_take(const char* key) {
156  taking_ = nil;
157  bool b = server_->look_take(key, &taking_);
158  return b;
159 }
160 
161 bool BBSLocal::look(const char* key) {
163  taking_ = nil;
164  bool b = server_->look(key, &taking_);
165  return b;
166 }
167 
168 void BBSLocal::take(const char* key) { // blocking
169  int id;
170  for (;;) {
172  taking_ = nil;
173  if (server_->look_take(key, &taking_)) {
174  return;
175  } else if ((id = server_->look_take_todo(&taking_)) != 0) {
176  execute(id);
177  } else {
178  perror("take blocking");
179  }
180  }
181 }
182 
183 void BBSLocal::post_todo(int parentid) {
184  server_->post_todo(parentid, posting_);
186  posting_ = nil;
187 }
188 
189 void BBSLocal::post_result(int id) {
192  posting_ = nil;
193 }
194 
197  taking_ = nil;
198  int id = server_->look_take_result(pid, &taking_);
199  return id;
200 }
201 
204  taking_ = nil;
205  int id = server_->look_take_todo(&taking_);
206  return id;
207 }
208 
211  taking_ = nil;
212  int id = look_take_todo();
213  if (id == 0) {
214  perror("take_todo blocking");
215  }
216  return id;
217 }
218 
221 #if defined(HAVE_STL)
222  keepargs_->insert(std::pair<const int, const MessageValue*>(userid, posting_));
223 #endif
224  posting_ = nil;
225 }
226 
228 #if defined(HAVE_STL)
229  KeepArgs::iterator i = keepargs_->find(userid);
230  assert(i != keepargs_->end());
232  taking_ = (MessageValue*) ((*i).second);
233  keepargs_->erase(i);
234  taking_->init_unpack();
236 #endif
237 }
238 
240  BBSImpl::done();
241 }
242 
244  if (started_) {
245  return;
246  }
247  BBSImpl::start();
248  mytid_ = 1;
249  is_master_ = true;
250 }
#define nil
Definition: enter-scope.h:36
int working_id_
Definition: bbsimpl.h:54
static bool is_master_
Definition: bbsimpl.h:60
virtual void return_args(int userid)
Definition: ocbbs.cpp:1385
static int mytid_
Definition: bbsimpl.h:62
virtual void execute(int id)
Definition: bbs.cpp:282
static bool started_
Definition: bbsimpl.h:61
virtual void start()
Definition: bbs.cpp:506
virtual void done()
Definition: bbs.cpp:487
virtual void start()
Definition: bbslocal.cpp:243
virtual void post_result(int id)
Definition: bbslocal.cpp:189
virtual void save_args(int)
Definition: bbslocal.cpp:219
virtual int look_take_result(int pid)
Definition: bbslocal.cpp:195
virtual void perror(const char *)
Definition: bbslocal.cpp:54
virtual void pkvec(int, double *)
Definition: bbslocal.cpp:124
virtual void pkpickle(const char *, size_t)
Definition: bbslocal.cpp:139
virtual void context()
Definition: bbslocal.cpp:52
virtual int upkint()
Definition: bbslocal.cpp:58
KeepArgs * keepargs_
Definition: bbslocal.h:49
virtual void upkvec(int, double *)
Definition: bbslocal.cpp:73
BBSLocal()
Definition: bbslocal.cpp:33
virtual void pkbegin()
Definition: bbslocal.cpp:106
virtual int take_todo()
Definition: bbslocal.cpp:209
virtual double upkdouble()
Definition: bbslocal.cpp:65
virtual char * upkstr()
Definition: bbslocal.cpp:79
virtual void pkint(int)
Definition: bbslocal.cpp:112
virtual char * upkpickle(size_t *size)
Definition: bbslocal.cpp:92
virtual void return_args(int)
Definition: bbslocal.cpp:227
virtual bool look_take(const char *)
Definition: bbslocal.cpp:154
virtual ~BBSLocal()
Definition: bbslocal.cpp:45
virtual int look_take_todo()
Definition: bbslocal.cpp:202
virtual void take(const char *)
Definition: bbslocal.cpp:168
virtual void pkdouble(double)
Definition: bbslocal.cpp:118
virtual void post_todo(int parentid)
Definition: bbslocal.cpp:183
virtual bool look(const char *)
Definition: bbslocal.cpp:161
virtual void pkstr(const char *)
Definition: bbslocal.cpp:130
virtual void post(const char *)
Definition: bbslocal.cpp:148
virtual void done()
Definition: bbslocal.cpp:239
bool look(const char *key, MessageValue **)
Definition: bbslsrv.cpp:296
void post_result(int id, MessageValue *)
Definition: bbslsrv.cpp:348
void post_todo(int parentid, MessageValue *)
Definition: bbslsrv.cpp:331
int look_take_result(int pid, MessageValue **)
Definition: bbslsrv.cpp:386
void post(const char *key, MessageValue *)
Definition: bbslsrv.cpp:318
bool look_take(const char *key, MessageValue **)
Definition: bbslsrv.cpp:274
int look_take_todo(MessageValue **)
Definition: bbslsrv.cpp:362
int pkdouble(double)
Definition: bbslsrv.cpp:168
int upkdouble(double *)
Definition: bbslsrv.cpp:212
int pkint(int)
Definition: bbslsrv.cpp:161
int upkstr(char *)
Definition: bbslsrv.cpp:233
int upkint(int *)
Definition: bbslsrv.cpp:203
int upkvec(int, double *)
Definition: bbslsrv.cpp:221
int upkpickle(char *, size_t *)
Definition: bbslsrv.cpp:242
int pkpickle(const char *, size_t)
Definition: bbslsrv.cpp:194
int pkstr(const char *)
Definition: bbslsrv.cpp:186
int pkvec(int, double *)
Definition: bbslsrv.cpp:175
void init_unpack()
Definition: bbslsrv.cpp:157
virtual void ref() const
Definition: resource.cpp:47
virtual void unref() const
Definition: resource.cpp:52
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:754
#define assert(ex)
Definition: hocassrt.h:32
#define id
Definition: md1redef.h:33
#define i
Definition: md1redef.h:12
static double map(void *v)
Definition: mlinedit.cpp:47
int const size_t const size_t n
Definition: nrngsl.h:11
size_t j
static MessageValue * taking_
Definition: bbslocal.cpp:30
static MessageValue * posting_
Definition: bbslocal.cpp:29
static BBSLocalServer * server_
Definition: bbslocal.cpp:31
static double userid(void *v)
Definition: ocbbs.cpp:227
#define key
Definition: spt2queue.cpp:20