summaryrefslogtreecommitdiff
path: root/comm/OLD/ReqMgr.cc
diff options
context:
space:
mode:
authorchatty1993-11-29 11:08:19 +0000
committerchatty1993-11-29 11:08:19 +0000
commitd6bd0c0e8e08660606801e846273d4740947d658 (patch)
tree1bd0c7f4eef00d52768b11762274ca5c886ac0af /comm/OLD/ReqMgr.cc
parent86bc9bfd4d2915bc50999437619346b9c2114a43 (diff)
downloadivy-league-d6bd0c0e8e08660606801e846273d4740947d658.zip
ivy-league-d6bd0c0e8e08660606801e846273d4740947d658.tar.gz
ivy-league-d6bd0c0e8e08660606801e846273d4740947d658.tar.bz2
ivy-league-d6bd0c0e8e08660606801e846273d4740947d658.tar.xz
Renamed into UchMsgMgr
Heavy additions.
Diffstat (limited to 'comm/OLD/ReqMgr.cc')
-rw-r--r--comm/OLD/ReqMgr.cc396
1 files changed, 310 insertions, 86 deletions
diff --git a/comm/OLD/ReqMgr.cc b/comm/OLD/ReqMgr.cc
index 3850723..8d317f2 100644
--- a/comm/OLD/ReqMgr.cc
+++ b/comm/OLD/ReqMgr.cc
@@ -17,49 +17,133 @@
#include <fstream.h>
#include <fcntl.h>
-UchReqMgr :: UchReqMgr ()
+UchMsgMgr :: UchMsgMgr ()
: Name ()
{
}
-UchReqMgr :: ~UchReqMgr ()
+UchMsgMgr :: ~UchMsgMgr ()
{
}
void
-UchReqMgr :: SetName (const char* n)
+UchMsgMgr :: SetName (const char* n)
{
if (Name)
- fprintf (stderr, "client type already set to %s\n", (const char*) Name);
+ fprintf (stderr, "service type already set to %s\n", (const char*) Name);
else
Name = n;
}
void
-UchReqMgr :: DumpHeader (const char* file)
+UchMsgMgr :: DumpEnum (ofstream& f, MsgType::msg_kind k, CcuListOf <MsgType>& l)
{
+ if (l.IsEmpty ())
+ return;
+
+ f << "enum " << Name << MsgType::EnumSuffix [k] << " {\n";
+ CcuListIterOf <MsgType> msg = l;
+ bool first = TRUE;
+ while (++msg) {
+ if (first)
+ first = FALSE;
+ else
+ f << ",\n";
+ f << "\t" << MsgType::EnumPrefix [k] << (*msg)->GetName ();
+ }
+ f << "\n};\n\n";
+}
+
+/*?
+Create the C++ header file containing the declarations of the requests defined in the file that
+has been read.
+?*/
+void
+UchMsgMgr :: DumpServerHeader ()
+{
+ /* build file name: <Name>ServerMsg.h, eg. FooServerMsg.h */
+ char file [128];
+ sprintf (file, "%sServerMsg.h", GetName ());
+
+ DumpDecl (file, isServer);
+}
+
+void
+UchMsgMgr :: DumpClientHeader ()
+{
+ /* build file name: <Name>ClientMsg.h, eg. FooClientMsg.h */
+ char file [128];
+ sprintf (file, "%sClientMsg.h", GetName ());
+
+ DumpDecl (file, isClient);
+}
+
+
+void
+UchMsgMgr :: DumpDecl (const char* file, int dest)
+{
+ char macro [128];
+ sprintf (macro, "%sMsg_H_", GetName ());
+
ofstream f (file, ios::out);
if (!f) {
extern int errno;
fprintf (stderr, "can't write to %s: %s\n", file, sys_errlist [errno]);
return;
}
- f << "/*\n *\tRequests for clients " << Name << "\n";
+ f << "/*\n *\tMessages for service " << Name << "\n";
f << " *\n *\tThis file was generated by reqgen - do not edit\n*/\n\n";
- f << "#ifndef " << Name << "Req_H_\n";
- f << "#define " << Name << "Req_H_\n\n";
+ f << "#ifndef " << macro << "\n";
+ f << "#define " << macro << "\n";
f << "#include <uch.h>\n\n";
-
- CcuListIterOf <RequestType> req (Requests);
- while (++req) {
- (*req)->DumpHeader (f);
- }
- f << "#endif\t/* " << Name << "Req_H_ */\n";
+
+
+ DumpEnum (f, MsgType::isRequest, Requests);
+ DumpEnum (f, MsgType::isAnswer, Answers);
+ DumpEnum (f, MsgType::isEvent, Events);
+
+ CcuListIterOf <MsgType> msg = Requests;
+ while (++msg)
+ (*msg)->DumpDecl (f, dest);
+
+ msg = Answers;
+ while (++msg)
+ (*msg)->DumpDecl (f, dest);
+
+ msg = Events;
+ while (++msg)
+ (*msg)->DumpDecl (f, dest);
+
+ f << "#endif\t/* " << macro << " */\n";
}
+
+/*?
+Create the C++ file containing the code implementing the requests defined in the file read.
+?*/
+void
+UchMsgMgr :: DumpClientSource (const char* suffix)
+{
+ /* build file name: <Name>ClientMsg.<suffix>, eg. FooClientMsg.cc */
+ char file [128];
+ sprintf (file, "%sClientMsg.%s", GetName (), suffix);
+
+ DumpCode (file, isClient);
+}
+
+void
+UchMsgMgr :: DumpServerSource (const char* suffix)
+{
+ /* build file name: <Name>ServerMsg.<suffix>, eg. FooServerMsg.cc */
+ char file [128];
+ sprintf (file, "%sServerMsg.%s", GetName (), suffix);
+
+ DumpCode (file, isServer);
+}
+
void
-UchReqMgr :: DumpSource (const char* file)
+UchMsgMgr :: DumpCode (const char* file, int dest)
{
ofstream f (file, ios::out);
if (!f) {
@@ -68,59 +152,179 @@ UchReqMgr :: DumpSource (const char* file)
return;
}
- f << "/*\n *\tRequests for clients " << Name << "\n";
+ f << "/*\n *\tMessages for service " << Name << "\n";
f << " *\n *\tThis file was generated by reqgen - do not edit\n*/\n\n";
- f << "#include \"" << file << ".h\"\n\n";
-
- CcuListIterOf <RequestType> req (Requests);
+
+ if (dest & isClient) {
+ f << "#include \"" << Name << "ClientMsg.h\"\n";
+ f << "#include \"" << Name << "Service.h\"\n";
+ }
+
+ if (dest & isServer) {
+ f << "#include \"" << Name << "ServerMsg.h\"\n";
+ f << "#include \"" << Name << "Server.h\"\n";
+ }
+ f << "\n";
+
+ CcuListIterOf <MsgType> msg = Requests;
+ while (++msg)
+ (*msg)->DumpCode (f, dest);
+
+ msg = Answers;
+ while (++msg)
+ (*msg)->DumpCode (f, dest);
+
+ msg = Events;
+ while (++msg)
+ (*msg)->DumpCode (f, dest);
+
+ if (dest & isClient)
+ DumpClientCode (f);
+
+ if (dest & isServer)
+ DumpServerCode (f);
+}
+
+void
+UchMsgMgr :: DumpClientCode (ofstream& f)
+{
+ f << "void*\n" << Name << "Service :: ConvertAnswer (UchMsgBuffer& b)\n{\n";
+ f << "\tlword type;\n";
+ f << "\tb.MsgPeek (&type);\n\n";
+ f << "\tUchMessage* a = 0;\n";
+ f << "\tswitch (type) {\n";
+ CcuListIterOf <MsgType> ans = Answers;
+ while (++ans) {
+ f << "\tcase " << "an" << (*ans)->GetName () << ":\n";
+ f << "\t\ta = new " << (*ans)->GetName () << "Ans;\n";
+ f << "\t\tbreak;\n";
+ }
+ f << "\t}\n";
+ f << "\tif (a)\n\t\tb.Get (*a);\n\treturn a;\n}\n\n";
+
+
+ f << "bool\n" << Name << "Service :: NewMessage (UchMsgBuffer& b, bool)\n{\n";
+ f << "\tlword type;\n";
+ f << "\tb.MsgPeek (&type);\n\n";
+ f << "\tUchMessage* m = 0;\n";
+ f << "\tswitch (type) {\n";
+ CcuListIterOf <MsgType> evt = Events;
+ while (++evt) {
+ f << "\tcase " << "ev" << (*evt)->GetName () << ":\n";
+ f << "\t\tm = new " << (*evt)->GetName () << "Event;\n";
+ f << "\t\tbreak;\n";
+ }
+ f << "\t}\n";
+ f << "\tif (m) {\n\t\tb.Get (*m);\n\t\tPutEvent (m);\n\t\treturn TRUE;\n";
+ f << "\t} else\n\t\treturn FALSE;\n}\n\n";
+}
+
+
+void
+UchMsgMgr :: DumpServerCode (ofstream& f)
+{
+ f << "bool\n" << Name << "Client :: NewMessage (UchMsgBuffer& b, bool)\n{\n";
+ f << "\tlword type;\n";
+ f << "\tb.MsgPeek (&type);\n\n";
+ f << "\tUchMessage* m = 0;\n";
+ f << "\tswitch (type) {\n";
+ CcuListIterOf <MsgType> req = Requests;
while (++req) {
- (*req)->DumpSource (f);
+ f << "\tcase " << "rq" << (*req)->GetName () << ":\n";
+ f << "\t\tm = new " << (*req)->GetName () << "Req;\n";
+ f << "\t\tbreak;\n";
}
+ f << "\t}\n";
+ f << "\tif (m) {\n\t\tb.Get (*m);\n\t\tm->Activate (*this);\n\t\treturn TRUE;\n";
+ f << "\t} else\n\t\treturn FALSE;\n}\n\n";
+
+ f << "UchClient*\n" << Name << "Server :: HandleNew (UchChannel* ch)\n{\n";
+ f << "\treturn new " << Name << "Client (ch);\n}\n\n";
}
+const char* const MsgType::ClassSuffix [3] = {"Req", "Ans", "Event"};
+const char* const MsgType::EnumPrefix[3] = {"rq", "an", "ev"};
+const char* const MsgType::EnumSuffix[3] = {"_requests", "_answers", "_events"};
+/*?
+Dump the declaration of a request type to a file.
+?*/
void
-RequestType :: DumpHeader (ofstream& f)
+MsgType :: DumpDecl (ofstream& f, int dest)
{
- f << "class " << Name << " : public UchMessage {\nprotected:\n";
+ char classname [128];
+ sprintf (classname, "%s%s", GetName (), ClassSuffix [Kind]);
- CcuListIterOf <RequestField> fields (Fields);
+ int emitter, receiver;
+ if (Kind == isRequest) {
+ emitter = (dest & isClient);
+ receiver = (dest & isServer);
+ } else {
+ emitter = (dest & isServer);
+ receiver = (dest & isClient);
+ }
+
+ /* name and base class */
+ f << "class " << classname << " : public UchMessage {\n";
+
+ /* field containing the type */
+ f << "protected:\n";
+ f << "\tlword\tType;\n";
+
+ /* other fields */
+ CcuListIterOf <MsgField> fields (Fields);
while (++fields) {
- RequestField* field = *fields;
+ MsgField* field = *fields;
f << "\t" << field->GetImpl () << "\t" << field->GetName () << ";\n";
}
- f << "public:\n\t\t" << Name << " ();\n";
+ /* default constructor, used in servers */
+ f << "public:\n";
+ if (!DefaultConstructor && (Fields.IsEmpty () || receiver))
+ f << "\t\t" << classname << " ();\n";
- CcuListIterOf <RequestConstructor> constr (Constructors);
- while (++constr) {
- f << "\t\t" << Name << " (";
- CcuListIterOf <RequestField> fields ((*constr)->GetParameters ());
- int first = 1;
- while (++fields) {
- if (first)
- first = 0;
- else
- f << ", ";
- f << (*fields)->GetType ();
+ /* constructors declared in the file */
+ if (emitter) {
+ CcuListIterOf <MsgConstructor> constr (Constructors);
+ while (++constr) {
+ f << "\t\t" << classname << " (";
+ CcuListIterOf <MsgField> fields ((*constr)->GetParameters ());
+ int first = 1;
+ while (++fields) {
+ if (first)
+ first = 0;
+ else
+ f << ", ";
+ f << (*fields)->GetType ();
+ }
+ f << ");\n";
}
- f << ");\n";
}
- f << "\t\t~" << Name << " ();\n";
- f << "\tvoid\tWriteTo (UchMsgBuffer&);\n";
- f << "\tvoid\tReadFrom (UchMsgBuffer&, lword);\n";
- f << "\tvoid\tActivate ();\n";
- CcuListIterOf <RequestField> getter (Getters);
+ /* destructor */
+ f << "\t\t~" << classname << " ();\n";
+
+ /* other methods */
+ if (emitter)
+ f << "\tvoid\tWriteTo (UchMsgBuffer&);\n";
+ if (receiver) {
+ f << "\tvoid\tReadFrom (UchMsgBuffer&, lword);\n";
+ if (Kind == isRequest)
+ f << "\tvoid\tActivate (UchMsgStream&);\n";
+ }
+
+ /* getters defined in the file */
+ CcuListIterOf <MsgField> getter (Getters);
while (++getter) {
- RequestField* field = *getter;
+ MsgField* field = *getter;
f << "inline\t" << field->GetType () << "\tGet" << field->GetName ()
<< " () const { return (" << field->GetType () << ") " << field->GetName () << "; }\n";
}
- CcuListIterOf <RequestField> setter (Setters);
+ /* setters defined in the file */
+ CcuListIterOf <MsgField> setter (Setters);
while (++setter) {
- RequestField* field = *setter;
+ MsgField* field = *setter;
f << "inline\tvoid\tSet" << field->GetName () << " (" << field->GetType ()
<< " f) { " << field->GetName () << " = (" << field->GetImpl () << ") f; }\n";
}
@@ -128,57 +332,77 @@ RequestType :: DumpHeader (ofstream& f)
f << "};\n\n";
}
+/*?
+Dump the code of a request class to a file.
+?*/
void
-RequestType :: DumpSource (ofstream& f)
+MsgType :: DumpCode (ofstream& f, int dest)
{
- f << Name << " :: " << Name << " ()\n: UchMessage ()\n{\n}\n\n";
-
- CcuListIterOf <RequestConstructor> constr (Constructors);
- while (++constr) {
- f << Name << " :: " << Name << " (";
- CcuListIterOf <RequestField> fields ((*constr)->GetParameters ());
- int i = 0;
- while (++fields) {
- if (i > 0)
- f << ", ";
- f << (*fields)->GetType () << " i" << i;
- ++i;
- }
- f << ")\n: UchMessage ()";
- fields.Reset ();
- i = 0;
- while (++fields) {
- f << ",\n " << (*fields)->GetName () << " (i" << i << ")";
- ++i;
- }
- f << "\n{\n}\n\n";
+ char classname [128];
+ sprintf (classname, "%s%s", GetName (), ClassSuffix [Kind]);
+
+ int emitter, receiver;
+ if (Kind == isRequest) {
+ emitter = (dest & isClient);
+ receiver = (dest & isServer);
+ } else {
+ emitter = (dest & isServer);
+ receiver = (dest & isClient);
}
- f << Name << " :: ~" << Name << " ()\n{\n}\n\n";
+ /* the default constructor*/
+ if (!DefaultConstructor && (Fields.IsEmpty () || receiver)) {
+ f << classname << " :: " << classname << " ()\n: UchMessage (),\n";
+ f << " Type (" << EnumPrefix [Kind] << Name << ")\n{\n}\n\n";
+ }
- f << "#ifdef SERVER\n\n";
- f << "void\n" << Name << " :: ReadFrom (UchMsgBuffer& b, lword l)\n{\n";
- f << "\tb";
- CcuListIterOf <RequestField> field (Fields);
- while (++field) {
- f << " >> " << (*field)->GetName ();
+ /* other constructors, used in emitters */
+ if (emitter) {
+ CcuListIterOf <MsgConstructor> constr (Constructors);
+ while (++constr) {
+ f << classname << " :: " << classname << " (";
+ CcuListIterOf <MsgField> fields ((*constr)->GetParameters ());
+ int i = 0;
+ while (++fields) {
+ if (i > 0)
+ f << ", ";
+ f << (*fields)->GetType () << " i" << i;
+ ++i;
+ }
+ f << ")\n: UchMessage (),\n Type (" << EnumPrefix [Kind] << Name << ")";
+ fields.Reset ();
+ i = 0;
+ while (++fields) {
+ f << ",\n " << (*fields)->GetName () << " (i" << i << ")";
+ ++i;
+ }
+ f << "\n{\n}\n\n";
+ }
}
- f << ";\n";
- f << "}\n\n";
- f << "#endif\t/* SERVER */\n\n";
- f << "#ifdef CLIENT\n\n";
+ /* destructor */
+ f << classname << " :: ~" << classname << " ()\n{\n}\n\n";
- f << "void\n" << Name << " :: " << "WriteTo (UchMsgBuffer& b)\n{\n";
- f << "\tb";
- field.Reset ();
- while (++field) {
- f << " << " << (*field)->GetName ();
+
+ CcuListIterOf <MsgField> field (Fields);
+ if (receiver) {
+ f << "void\n" << classname << " :: ReadFrom (UchMsgBuffer& b, lword)\n{\n";
+ f << "\tlword type;\n\tb >> type";
+ while (++field)
+ f << " >> " << (*field)->GetName ();
+ f << ";\n";
+ f << "}\n\n";
}
- f << ";\n";
- f << "}\n\n";
- f << "#endif\t/* CLIENT */\n";
+ if (emitter) {
+ f << "void\n" << classname << " :: " << "WriteTo (UchMsgBuffer& b)\n{\n";
+ f << "\tb << Type";
+ field.Reset ();
+ while (++field)
+ f << " << " << (*field)->GetName ();
+ f << ";\n";
+ f << "}\n\n";
+ }
}