NEURON
bbslocal.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include <InterViews/resource.h>
3 #include "oc2iv.h"
4 #include "bbslocal.h"
5 #include "bbslsrv.hpp"
6 #include <nrnmpi.h>
7 
8 #include <map>
9 #include <set>
10 #include <string>
11 
12 class KeepArgs: public std::map<int, const MessageValue*> {};
13 
17 
19  if (!server_) {
20  server_ = new BBSLocalServer();
21  posting_ = nullptr;
22  taking_ = nullptr;
23  }
24  start();
25  keepargs_ = new KeepArgs();
26 }
27 
29  // need to unref anything in keepargs_;
30  delete keepargs_;
31 }
32 
34 
35 void BBSLocal::perror(const char* s) {
36  hoc_execerror("BBSLocal error in ", s);
37 }
38 
40  int i{};
41  if (!taking_ || taking_->upkint(&i))
42  perror("upkint");
43  return i;
44 }
45 
47  double x{};
48  if (!taking_ || taking_->upkdouble(&x)) {
49  perror("upkdouble");
50  }
51  return x;
52 }
53 
54 void BBSLocal::upkvec(int n, double* x) {
55  if (!taking_ || taking_->upkvec(n, x)) {
56  perror("upkdouble");
57  }
58 }
59 
61  int len{};
62  char* s;
63  if (!taking_ || taking_->upkint(&len)) {
64  perror("upkstr length");
65  }
66  s = new char[len + 1];
67  if (taking_->upkstr(s)) {
68  perror("upkstr string");
69  }
70  return s;
71 }
72 
73 std::vector<char> BBSLocal::upkpickle() {
74  int len{};
75  if (!taking_ || taking_->upkint(&len)) {
76  perror("upkpickle length");
77  }
78  std::vector<char> s{};
79  if (taking_->upkpickle(s)) {
80  perror("upkpickle data");
81  }
82  assert(s.size() == len);
83  return s;
84 }
85 
88  posting_ = new MessageValue();
89  posting_->ref();
90 }
91 
92 void BBSLocal::pkint(int i) {
93  if (!posting_ || posting_->pkint(i)) {
94  perror("pkint");
95  }
96 }
97 
98 void BBSLocal::pkdouble(double x) {
99  if (!posting_ || posting_->pkdouble(x)) {
100  perror("pkdouble");
101  }
102 }
103 
104 void BBSLocal::pkvec(int n, double* x) {
105  if (!posting_ || posting_->pkvec(n, x)) {
106  perror("pkdouble");
107  }
108 }
109 
110 void BBSLocal::pkstr(const char* s) {
111  if (!posting_ || posting_->pkint(strlen(s))) {
112  perror("pkstr length");
113  }
114  if (!posting_ || posting_->pkstr(s)) {
115  perror("pkstr string");
116  }
117 }
118 
119 void BBSLocal::pkpickle(const std::vector<char>& s) {
120  if (!posting_ || posting_->pkint(s.size())) {
121  perror("pkpickle size");
122  }
123  if (!posting_ || posting_->pkpickle(s)) {
124  perror("pkpickle data");
125  }
126 }
127 
128 void BBSLocal::post(const char* key) {
131  posting_ = nullptr;
132 }
133 
134 bool BBSLocal::look_take(const char* key) {
136  taking_ = nullptr;
137  bool b = server_->look_take(key, &taking_);
138  return b;
139 }
140 
141 bool BBSLocal::look(const char* key) {
143  taking_ = nullptr;
144  bool b = server_->look(key, &taking_);
145  return b;
146 }
147 
148 void BBSLocal::take(const char* key) { // blocking
149  int id;
150  for (;;) {
152  taking_ = nullptr;
153  if (server_->look_take(key, &taking_)) {
154  return;
155  } else if ((id = server_->look_take_todo(&taking_)) != 0) {
156  execute(id);
157  } else {
158  perror("take blocking");
159  }
160  }
161 }
162 
163 void BBSLocal::post_todo(int parentid) {
164  server_->post_todo(parentid, posting_);
166  posting_ = nullptr;
167 }
168 
169 void BBSLocal::post_result(int id) {
172  posting_ = nullptr;
173 }
174 
177  taking_ = nullptr;
178  int id = server_->look_take_result(pid, &taking_);
179  return id;
180 }
181 
184  taking_ = nullptr;
185  int id = server_->look_take_todo(&taking_);
186  return id;
187 }
188 
191  taking_ = nullptr;
192  int id = look_take_todo();
193  if (id == 0) {
194  perror("take_todo blocking");
195  }
196  return id;
197 }
198 
201  keepargs_->emplace(userid, posting_);
202  posting_ = nullptr;
203 }
204 
206  KeepArgs::iterator i = keepargs_->find(userid);
207  assert(i != keepargs_->end());
209  taking_ = const_cast<MessageValue*>((*i).second);
210  keepargs_->erase(i);
211  taking_->init_unpack();
213 }
214 
216  BBSImpl::done();
217 }
218 
220  if (started_) {
221  return;
222  }
223  BBSImpl::start();
224  mytid_ = 1;
225  is_master_ = true;
226 }
static MessageValue * taking_
Definition: bbslocal.cpp:15
static MessageValue * posting_
Definition: bbslocal.cpp:14
static BBSLocalServer * server_
Definition: bbslocal.cpp:16
int working_id_
Definition: bbsimpl.h:55
static bool is_master_
Definition: bbsimpl.h:60
virtual void return_args(int userid)
Definition: ocbbs.cpp:1420
static int mytid_
Definition: bbsimpl.h:62
virtual void execute(int id)
Definition: bbs.cpp:261
static bool started_
Definition: bbsimpl.h:61
virtual void start()
Definition: bbs.cpp:471
virtual void done()
Definition: bbs.cpp:464
void start() override
Definition: bbslocal.cpp:219
void take(const char *) override
Definition: bbslocal.cpp:148
void pkstr(const char *) override
Definition: bbslocal.cpp:110
int take_todo() override
Definition: bbslocal.cpp:189
void context() override
Definition: bbslocal.cpp:33
int look_take_result(int pid) override
Definition: bbslocal.cpp:175
void pkpickle(const std::vector< char > &) override
Definition: bbslocal.cpp:119
void pkint(int) override
Definition: bbslocal.cpp:92
~BBSLocal() override
Definition: bbslocal.cpp:28
void post_result(int id) override
Definition: bbslocal.cpp:169
void perror(const char *) override
Definition: bbslocal.cpp:35
char * upkstr() override
Definition: bbslocal.cpp:60
void post(const char *) override
Definition: bbslocal.cpp:128
KeepArgs * keepargs_
Definition: bbslocal.h:48
int look_take_todo() override
Definition: bbslocal.cpp:182
void upkvec(int, double *) override
Definition: bbslocal.cpp:54
void pkvec(int, double *) override
Definition: bbslocal.cpp:104
BBSLocal()
Definition: bbslocal.cpp:18
void pkbegin() override
Definition: bbslocal.cpp:86
void post_todo(int parentid) override
Definition: bbslocal.cpp:163
int upkint() override
Definition: bbslocal.cpp:39
bool look(const char *) override
Definition: bbslocal.cpp:141
void return_args(int) override
Definition: bbslocal.cpp:205
void pkdouble(double) override
Definition: bbslocal.cpp:98
bool look_take(const char *) override
Definition: bbslocal.cpp:134
std::vector< char > upkpickle() override
Definition: bbslocal.cpp:73
double upkdouble() override
Definition: bbslocal.cpp:46
void done() override
Definition: bbslocal.cpp:215
void save_args(int) override
Definition: bbslocal.cpp:199
bool look(const char *key, MessageValue **)
Definition: bbslsrv.cpp:205
void post_result(int id, MessageValue *)
Definition: bbslsrv.cpp:244
void post_todo(int parentid, MessageValue *)
Definition: bbslsrv.cpp:231
int look_take_result(int pid, MessageValue **)
Definition: bbslsrv.cpp:275
void post(const char *key, MessageValue *)
Definition: bbslsrv.cpp:223
bool look_take(const char *key, MessageValue **)
Definition: bbslsrv.cpp:187
int look_take_todo(MessageValue **)
Definition: bbslsrv.cpp:256
int pkdouble(double)
Definition: bbslsrv.cpp:96
int upkdouble(double *)
Definition: bbslsrv.cpp:128
int pkpickle(const std::vector< char > &)
Definition: bbslsrv.cpp:111
int pkint(int)
Definition: bbslsrv.cpp:91
int upkstr(char *)
Definition: bbslsrv.cpp:148
int upkint(int *)
Definition: bbslsrv.cpp:116
int upkvec(int, double *)
Definition: bbslsrv.cpp:137
int upkpickle(std::vector< char > &)
Definition: bbslsrv.cpp:160
int pkstr(const char *)
Definition: bbslsrv.cpp:106
int pkvec(int, double *)
Definition: bbslsrv.cpp:101
void init_unpack()
Definition: bbslsrv.cpp:87
virtual void ref() const
Definition: resource.cpp:42
virtual void unref() const
Definition: resource.cpp:47
#define key
Definition: tqueue.hpp:45
#define id
Definition: md1redef.h:41
#define i
Definition: md1redef.h:19
#define assert(ex)
Definition: hocassrt.h:24
static double map(void *v)
Definition: mlinedit.cpp:43
void hoc_execerror(const char *s1, const char *s2)
Definition: nrnoc_aux.cpp:39
int const size_t const size_t n
Definition: nrngsl.h:10
s
Definition: multisend.cpp:521
static double userid(void *v)
Definition: ocbbs.cpp:196