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)) perror("upkint");
61  return i;
62 }
63 
65  double x;
66  if(!taking_ || taking_->upkdouble(&x)) { perror("upkdouble"); }
67  return x;
68 }
69 
70 void BBSLocal::upkvec(int n, double* x) {
71  if(!taking_ || taking_->upkvec(n, x)) { perror("upkdouble"); }
72 }
73 
75  int len;
76  char* s;
77  if( !taking_ || taking_->upkint(&len)) { perror("upkstr length"); }
78  s = new char[len+1];
79  if (taking_->upkstr(s)) { perror("upkstr string"); }
80  return s;
81 }
82 
83 char* BBSLocal::upkpickle(size_t* n) {
84  int len;
85  char* s;
86  if( !taking_ || taking_->upkint(&len)) { perror("upkpickle length"); }
87  s = new char[len];
88  if (taking_->upkpickle(s, n)) { perror("upkpickle data"); }
89  assert(*n == len);
90  return s;
91 }
92 
94  Resource::unref(posting_);
95  posting_ = new MessageValue();
96  posting_->ref();
97 }
98 
99 void BBSLocal::pkint(int i) {
100  if( !posting_ || posting_->pkint(i)) { perror("pkint"); }
101 }
102 
103 void BBSLocal::pkdouble(double x) {
104  if( !posting_ || posting_->pkdouble(x)) { perror("pkdouble"); }
105 }
106 
107 void BBSLocal::pkvec(int n, double* x) {
108  if( !posting_ || posting_->pkvec(n, x)) { perror("pkdouble"); }
109 }
110 
111 void BBSLocal::pkstr(const char* s) {
112  if ( !posting_ || posting_->pkint(strlen(s))) { perror("pkstr length"); }
113  if ( !posting_ || posting_->pkstr(s)) { perror("pkstr string"); }
114 }
115 
116 void BBSLocal::pkpickle(const char* s, size_t n) {
117  if ( !posting_ || posting_->pkint(n)) { perror("pkpickle size"); }
118  if ( !posting_ || posting_->pkpickle(s, n)) { perror("pkpickle data"); }
119 }
120 
121 void BBSLocal::post(const char* key) {
122  server_->post(key, posting_);
123  Resource::unref(posting_);
124  posting_ = nil;
125 }
126 
127 bool BBSLocal::look_take(const char* key) {
128  Resource::unref(taking_);
129  taking_ = nil;
130  bool b = server_->look_take(key, &taking_);
131  return b;
132 }
133 
134 bool BBSLocal::look(const char* key) {
135  Resource::unref(taking_);
136  taking_ = nil;
137  bool b = server_->look(key, &taking_);
138  return b;
139 }
140 
141 void BBSLocal::take(const char* key) { // blocking
142  int id;
143  for (;;) {
144  Resource::unref(taking_);
145  taking_ = nil;
146  if (server_->look_take(key, &taking_)) {
147  return;
148  } else if ((id = server_->look_take_todo(&taking_)) != 0) {
149  execute(id);
150  } else {
151  perror("take blocking");
152  }
153  }
154 }
155 
156 void BBSLocal::post_todo(int parentid) {
157  server_->post_todo(parentid, posting_);
158  Resource::unref(posting_);
159  posting_ = nil;
160 }
161 
162 void BBSLocal::post_result(int id) {
163  server_->post_result(id, posting_);
164  Resource::unref(posting_);
165  posting_ = nil;
166 }
167 
169  Resource::unref(taking_);
170  taking_ = nil;
171  int id = server_->look_take_result(pid, &taking_);
172  return id;
173 }
174 
176  Resource::unref(taking_);
177  taking_ = nil;
178  int id = server_->look_take_todo(&taking_);
179  return id;
180 }
181 
183  Resource::unref(taking_);
184  taking_ = nil;
185  int id = look_take_todo();
186  if (id == 0) {
187  perror("take_todo blocking");
188  }
189  return id;
190 }
191 
193  server_->post_todo(working_id_, posting_);
194 #if defined(HAVE_STL)
195  keepargs_->insert(
196  std::pair<const int, const MessageValue*>(userid, posting_)
197  );
198 #endif
199  posting_ = nil;
200 }
201 
203 #if defined(HAVE_STL)
204  KeepArgs::iterator i = keepargs_->find(userid);
205  assert(i != keepargs_->end());
206  Resource::unref(taking_);
207  taking_ = (MessageValue*)((*i).second);
208  keepargs_->erase(i);
209  taking_->init_unpack();
210  BBSImpl::return_args(userid);
211 #endif
212 }
213 
215  BBSImpl::done();
216 }
217 
219  if (started_) { return; }
220  BBSImpl::start();
221  mytid_ = 1;
222  is_master_ = true;
223 }
virtual void save_args(int)
Definition: bbslocal.cpp:192
static double userid(void *v)
Definition: ocbbs.cpp:225
static MessageValue * posting_
Definition: bbslocal.cpp:29
#define assert(ex)
Definition: hocassrt.h:26
virtual void upkvec(int, double *)
Definition: bbslocal.cpp:70
virtual char * upkpickle(size_t *size)
Definition: bbslocal.cpp:83
void execute(Inst *p)
Definition: code.cpp:2651
virtual void start()
Definition: bbslocal.cpp:218
virtual void post_result(int id)
Definition: bbslocal.cpp:162
virtual void done()
Definition: bbs.cpp:484
int look_take_result(int pid, MessageValue **)
Definition: bbslsrv.cpp:387
void init_unpack()
Definition: bbslsrv.cpp:157
int look_take_todo(MessageValue **)
Definition: bbslsrv.cpp:363
virtual void start()
Definition: bbs.cpp:500
virtual void done()
Definition: bbslocal.cpp:214
virtual void context()
Definition: bbslocal.cpp:52
virtual bool look_take(const char *)
Definition: bbslocal.cpp:127
void post_todo(int parentid, MessageValue *)
Definition: bbslsrv.cpp:332
virtual void perror(const char *)
Definition: bbslocal.cpp:54
virtual void ref() const
Definition: resource.cpp:47
int pkdouble(double)
Definition: bbslsrv.cpp:168
int upkdouble(double *)
Definition: bbslsrv.cpp:212
void start()
Definition: hel2mos.cpp:205
virtual char * upkstr()
Definition: bbslocal.cpp:74
static double map(void *v)
Definition: mlinedit.cpp:46
virtual void post(const char *)
Definition: bbslocal.cpp:121
int const size_t const size_t n
Definition: nrngsl.h:12
_CONST char * s
Definition: system.cpp:74
bool look(const char *key, MessageValue **)
Definition: bbslsrv.cpp:296
virtual void return_args(int userid)
Definition: ocbbs.cpp:1308
bool look_take(const char *key, MessageValue **)
Definition: bbslsrv.cpp:274
virtual ~BBSLocal()
Definition: bbslocal.cpp:45
void post_result(int id, MessageValue *)
Definition: bbslsrv.cpp:349
void hoc_execerror(const char *, const char *)
Definition: hoc.cpp:741
virtual double upkdouble()
Definition: bbslocal.cpp:64
#define key
Definition: spt2queue.cpp:20
virtual int look_take_result(int pid)
Definition: bbslocal.cpp:168
BBSLocal()
Definition: bbslocal.cpp:33
size_t j
virtual void pkstr(const char *)
Definition: bbslocal.cpp:111
virtual void unref() const
Definition: resource.cpp:52
virtual int look_take_todo()
Definition: bbslocal.cpp:175
int pkpickle(const char *, size_t)
Definition: bbslsrv.cpp:194
#define nil
Definition: enter-scope.h:36
static BBSLocalServer * server_
Definition: bbslocal.cpp:31
int pkvec(int, double *)
Definition: bbslsrv.cpp:175
virtual void pkint(int)
Definition: bbslocal.cpp:99
virtual void take(const char *)
Definition: bbslocal.cpp:141
void post(const char *key, MessageValue *)
Definition: bbslsrv.cpp:318
int pkint(int)
Definition: bbslsrv.cpp:161
virtual void pkdouble(double)
Definition: bbslocal.cpp:103
int upkint(int *)
Definition: bbslsrv.cpp:203
int upkpickle(char *, size_t *)
Definition: bbslsrv.cpp:242
#define i
Definition: md1redef.h:12
#define id
Definition: md1redef.h:33
virtual void pkbegin()
Definition: bbslocal.cpp:93
int upkvec(int, double *)
Definition: bbslsrv.cpp:221
virtual int upkint()
Definition: bbslocal.cpp:58
virtual int take_todo()
Definition: bbslocal.cpp:182
int pkstr(const char *)
Definition: bbslsrv.cpp:186
virtual void return_args(int)
Definition: bbslocal.cpp:202
virtual bool look(const char *)
Definition: bbslocal.cpp:134
static MessageValue * taking_
Definition: bbslocal.cpp:30
virtual void pkvec(int, double *)
Definition: bbslocal.cpp:107
virtual void post_todo(int parentid)
Definition: bbslocal.cpp:156
virtual void pkpickle(const char *, size_t)
Definition: bbslocal.cpp:116
int upkstr(char *)
Definition: bbslsrv.cpp:233