00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #include <Basics.H>
00053 #include <SRPC.H>
00054 #include <MultiSRPC.H>
00055 #include <LimService.H>
00056
00057 using std::ostream;
00058 using std::endl;
00059 using std::cout;
00060 using std::cerr;
00061
00062
00063 const Text IntfName("1235");
00064 const int IntfVersion = 1;
00065 const int ProcId1 = 1;
00066
00067
00068 static Basics::mutex mu;
00069
00070 typedef char *String;
00071
00072 ostream& operator << (ostream &os, const SRPC::failure &f) throw ()
00073
00074 {
00075 mu.lock();
00076 int i;
00077 for (i = 0; i < 70; i++) os << '*'; os << endl;
00078 os << "SRPC failure (error " << f.r << ") :" << endl;
00079 os << " " << f.msg << endl;
00080 for (i = 0; i < 70; i++) os << '*'; os << endl << endl;
00081 mu.unlock();
00082 return os;
00083 }
00084
00085
00086
00087 void ClientCall(SRPC *srpc, int blocks, int sz, String *buff)
00088 throw (SRPC::failure)
00089 {
00090
00091 srpc->start_call(ProcId1, IntfVersion);
00092 srpc->send_int(blocks);
00093 srpc->send_int(sz);
00094 if (blocks > 0) {
00095 for (int i = 0; i < blocks; i++) {
00096 srpc->send_bytes(buff[i], sz);
00097 }
00098 }
00099 srpc->send_end();
00100
00101
00102 int recv_blocks = srpc->recv_int();
00103 int recv_sz = srpc->recv_int();
00104 if (recv_blocks > 0) {
00105 assert(recv_blocks <= blocks);
00106 assert(recv_sz <= sz);
00107 for (int i = 0; i < recv_blocks; i++) {
00108 int len = recv_sz;
00109 srpc->recv_bytes_here(buff[i], len);
00110 assert(len == recv_sz);
00111 }
00112 }
00113 srpc->recv_end();
00114 }
00115
00116 void Client(int blocks, int sz, int numCalls, char *hostname) throw ()
00117 {
00118 MultiSRPC multi(IntfName);
00119 try {
00120
00121 SRPC *srpc;
00122 MultiSRPC::ConnId connId;
00123 connId = multi.Start(hostname, srpc);
00124
00125
00126 String *buff = (String *)NULL;
00127 if (blocks > 0) {
00128 buff = NEW_ARRAY(String, blocks);
00129 for (int i=0; i < blocks; i++) {
00130 buff[i] = NEW_PTRFREE_ARRAY(char, sz);
00131 memset(buff[i], 0, sz);
00132 }
00133 }
00134 for (int callCnt = 0; numCalls < 0 || callCnt < numCalls; callCnt++) {
00135 ClientCall(srpc, blocks, sz, buff);
00136 }
00137
00138
00139 multi.End(connId);
00140 } catch (const SRPC::failure &f) {
00141 cout << f;
00142 }
00143 }
00144
00145
00146
00147 class ServerArgs {
00148 public:
00149 int blocks;
00150 int sz;
00151 String *buff;
00152 ServerArgs(int blocks, int sz) throw ();
00153 };
00154
00155 ServerArgs::ServerArgs(int blocks, int sz) throw ()
00156 : blocks(blocks), sz(sz), buff((String *)NULL)
00157 {
00158 if (this->blocks > 0) {
00159 this->buff = NEW_ARRAY(String, this->blocks);
00160 for (int i = 0; i < this->blocks; i++) {
00161 this->buff[i] = NEW_PTRFREE_ARRAY(char, this->sz);
00162 }
00163 }
00164 }
00165
00166 void ServerFailureCallback( SRPC *srpc, const SRPC::failure &f,
00167 void *arg) throw ()
00168 {
00169 cout << f;
00170 }
00171
00172 void ServerWork(const ServerArgs *args) throw ()
00173 {
00174 if (args->buff != (String *)NULL) {
00175 for (int i = 0; i < args->blocks; i++) {
00176 memset(args->buff[i], 0, args->sz);
00177 }
00178 }
00179 }
00180
00181 void ServerProc1(SRPC *srpc, const ServerArgs *args)
00182 throw (SRPC::failure)
00183 {
00184
00185 int recv_blocks = srpc->recv_int();
00186 int recv_sz = srpc->recv_int();
00187 if (recv_blocks > 0) {
00188 assert(recv_blocks <= args->blocks);
00189 assert(recv_sz <= args->sz);
00190 for (int i = 0; i < recv_blocks; i++) {
00191 int len = recv_sz;
00192 srpc->recv_bytes_here(args->buff[i], len);
00193 assert(len == recv_sz);
00194 }
00195 }
00196 srpc->recv_end();
00197
00198
00199 ServerWork(args);
00200
00201
00202 srpc->send_int(args->blocks);
00203 srpc->send_int(args->sz);
00204 if (args->blocks > 0) {
00205 for (int i = 0; i < args->blocks; i++) {
00206 srpc->send_bytes(args->buff[i], args->sz);
00207 }
00208 }
00209 srpc->send_end();
00210 }
00211
00212 void ServerCallback(SRPC *srpc, int procId, void *arg)
00213 throw (SRPC::failure)
00214 {
00215 const ServerArgs *args = (ServerArgs *)arg;
00216 switch (procId) {
00217 case ProcId1: ServerProc1(srpc, args); break;
00218 default: assert(false);
00219 }
00220 }
00221
00222 void Server(int blocks, int sz) throw ()
00223 {
00224
00225 const int MaxRunning = 10, MaxBlocked = 10;
00226 ServerArgs *args = NEW_CONSTR(ServerArgs, (blocks, sz));
00227 LimService ls(IntfName, IntfVersion, MaxRunning, MaxBlocked,
00228 ServerCallback, ServerFailureCallback, (void *)args);
00229 Basics::thread th = ls.Forked_Run();
00230
00231
00232 mu.lock();
00233 cout << "Server ready..." << endl << endl;
00234 mu.unlock();
00235
00236
00237 (void) th.join();
00238 }
00239
00240
00241
00242 void SyntaxError(char *msg, char *arg = (char *)NULL) throw ()
00243 {
00244 cerr << "Error: " << msg;
00245 if (arg != (char *)NULL) cerr << ": '" << arg << "'";
00246 cerr << endl;
00247 cerr << "SYNTAX: TestNullSRPC -client "
00248 << " [ -blocks b ] [ -sz s ] [ -calls n ] [ host ]" << endl;
00249 cerr << "SYNTAX: TestNullSRPC -server "
00250 << " [ -blocks b ] [ -sz s ]" << endl;
00251 exit(1);
00252 }
00253
00254 int main(int argc, char *argv[])
00255 {
00256 enum Role { ClientRole, ServerRole };
00257 Role role;
00258 if (argc < 2) SyntaxError("too few arguments");
00259 if (strcmp(argv[1], "-client") == 0) {
00260 role = ClientRole;
00261 } else if (strcmp(argv[1], "-server") == 0) {
00262 role = ServerRole;
00263 } else {
00264 SyntaxError("first argument must be '-client' or '-server'");
00265 }
00266
00267
00268 int blocks = 0, sz = 1024;
00269 int numCalls = -1;
00270 char *hostname = "localhost";
00271 int arg;
00272 for (arg = 2; arg < argc && *argv[arg] == '-'; arg++) {
00273 if (strcmp(argv[arg], "-blocks") == 0) {
00274 if (++arg >= argc) SyntaxError("no argument for '-blocks'");
00275 if (sscanf(argv[arg], "%d", &blocks) != 1)
00276 SyntaxError("illegal argument to '-blocks'", argv[arg]);
00277 } else if (strcmp(argv[arg], "-sz") == 0) {
00278 if (++arg >= argc) SyntaxError("no argument for '-sz'");
00279 if (sscanf(argv[arg], "%d", &sz) != 1)
00280 SyntaxError("illegal argument to '-sz'", argv[arg]);
00281 } else if (strcmp(argv[arg], "-calls") == 0) {
00282 if (role == ServerRole)
00283 SyntaxError("'-calls' not understood by server");
00284 if (++arg >= argc) SyntaxError("no argument for '-calls'");
00285 if (sscanf(argv[arg], "%d", &numCalls) != 1)
00286 SyntaxError("illegal argument to '-calls'", argv[arg]);
00287 } else {
00288 SyntaxError("unrecognized switch", argv[arg]);
00289 }
00290 }
00291 switch (role) {
00292 case ClientRole:
00293 if (arg < argc) hostname = argv[arg++];
00294 if (arg < argc) SyntaxError("too many arguments");
00295 Client(blocks, sz, numCalls, hostname);
00296 break;
00297 case ServerRole:
00298 if (argc > arg) SyntaxError("too many arguments");
00299 Server(blocks, sz);
00300 break;
00301 default:
00302 assert(false);
00303 }
00304 return 0;
00305 }