1 /*
2
3 This file is part of the Maude 2 interpreter.
4
5 Copyright 1997-2003 SRI International, Menlo Park, CA 94025, USA.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
20
21 */
22
23 //
24 // Implementation for class SocketOidSymbol.
25 //
26
27 // utility stuff
28 #include "macros.hh"
29 #include "vector.hh"
30
31 // forward declarations
32 #include "interface.hh"
33 #include "core.hh"
34 #include "freeTheory.hh"
35 #include "NA_Theory.hh"
36 #include "S_Theory.hh"
37 #include "builtIn.hh"
38 #include "objectSystem.hh"
39
40 // interface class definitions
41 #include "symbol.hh"
42 #include "dagNode.hh"
43 #include "term.hh"
44
45 // core class definitions
46 //#include "rewritingContext.hh"
47 #include "symbolMap.hh"
48
49 // free theory class definitions
50 //#include "freeNet.hh"
51 #include "freeDagNode.hh"
52
53 // built in class definitions
54 #include "succSymbol.hh"
55 #include "stringSymbol.hh"
56 #include "stringDagNode.hh"
57 //#include "stringOpSymbol.hh"
58 #include "bindingMacros.hh"
59
60 // object system class definitions
61 #include "objectSystemRewritingContext.hh"
62
63 // socket class definitions
64 #include "socketManagerSymbol.hh"
65
66 // our stuff
67 #include "socketStuff.cc"
68 #include "socketAsync.cc"
69 #include "socketOutcomes.cc"
70
ActiveSocket()71 SocketManagerSymbol::ActiveSocket::ActiveSocket()
72 {
73 textArray = 0; // make it safe for deletion
74 }
75
~ActiveSocket()76 SocketManagerSymbol::ActiveSocket::~ActiveSocket()
77 {
78 delete [] textArray; // just in case we end up being delete while we're still waiting to send stuff
79 }
80
SocketManagerSymbol(int id)81 SocketManagerSymbol::SocketManagerSymbol(int id)
82 : ExternalObjectManagerSymbol(id)
83 {
84 #define MACRO(SymbolName, SymbolClass, NrArgs) \
85 SymbolName = 0;
86 #include "socketSignature.cc"
87 #undef MACRO
88 }
89
90 bool
attachData(const Vector<Sort * > & opDeclaration,const char * purpose,const Vector<const char * > & data)91 SocketManagerSymbol::attachData(const Vector<Sort*>& opDeclaration,
92 const char* purpose,
93 const Vector<const char*>& data)
94 {
95 NULL_DATA(purpose, SocketManagerSymbol, data);
96 return ExternalObjectManagerSymbol::attachData(opDeclaration, purpose, data);
97 }
98
99 bool
attachSymbol(const char * purpose,Symbol * symbol)100 SocketManagerSymbol::attachSymbol(const char* purpose, Symbol* symbol)
101 {
102 Assert(symbol != 0, "null symbol for " << purpose);
103 #define MACRO(SymbolName, SymbolClass, NrArgs) \
104 BIND_SYMBOL(purpose, symbol, SymbolName, SymbolClass*)
105 #include "socketSignature.cc"
106 #undef MACRO
107 return ExternalObjectManagerSymbol::attachSymbol(purpose, symbol);
108 }
109
110 void
copyAttachments(Symbol * original,SymbolMap * map)111 SocketManagerSymbol::copyAttachments(Symbol* original, SymbolMap* map)
112 {
113 SocketManagerSymbol* orig = safeCast(SocketManagerSymbol*, original);
114 #define MACRO(SymbolName, SymbolClass, NrArgs) \
115 COPY_SYMBOL(orig, SymbolName, map, SymbolClass*)
116 #include "socketSignature.cc"
117 #undef MACRO
118 ExternalObjectManagerSymbol::copyAttachments(original, map);
119 }
120
121 void
getDataAttachments(const Vector<Sort * > & opDeclaration,Vector<const char * > & purposes,Vector<Vector<const char * >> & data)122 SocketManagerSymbol::getDataAttachments(const Vector<Sort*>& opDeclaration,
123 Vector<const char*>& purposes,
124 Vector<Vector<const char*> >& data)
125 {
126 int nrDataAttachments = purposes.length();
127 purposes.resize(nrDataAttachments + 1);
128 purposes[nrDataAttachments] = "SocketManagerSymbol";
129 data.resize(nrDataAttachments + 1);
130 ExternalObjectManagerSymbol::getDataAttachments(opDeclaration, purposes, data);
131 }
132
133 void
getSymbolAttachments(Vector<const char * > & purposes,Vector<Symbol * > & symbols)134 SocketManagerSymbol::getSymbolAttachments(Vector<const char*>& purposes,
135 Vector<Symbol*>& symbols)
136 {
137 #define MACRO(SymbolName, SymbolClass, NrArgs) \
138 APPEND_SYMBOL(purposes, symbols, SymbolName)
139 #include "socketSignature.cc"
140 #undef MACRO
141 ExternalObjectManagerSymbol::getSymbolAttachments(purposes, symbols);
142 }
143
144 bool
handleManagerMessage(DagNode * message,ObjectSystemRewritingContext & context)145 SocketManagerSymbol::handleManagerMessage(DagNode* message, ObjectSystemRewritingContext& context)
146 {
147 //cerr << "SocketManagerSymbol::handleManagerMessage(): saw " << message << endl;
148 Symbol* s = message->symbol();
149 if (s == createClientTcpSocketMsg)
150 return createClientTcpSocket(safeCast(FreeDagNode*, message), context);
151 if (s == createServerTcpSocketMsg)
152 return createServerTcpSocket(safeCast(FreeDagNode*, message), context);
153 return false;
154 }
155
156 bool
handleMessage(DagNode * message,ObjectSystemRewritingContext & context)157 SocketManagerSymbol::handleMessage(DagNode* message, ObjectSystemRewritingContext& context)
158 {
159 //cerr << "SocketManagerSymbol::handleMessage(): saw " << message << endl;
160 Symbol* s = message->symbol();
161 if (s == acceptClientMsg)
162 return acceptClient(safeCast(FreeDagNode*, message), context);
163 if (s == sendMsg)
164 return send(safeCast(FreeDagNode*, message), context);
165 if (s == receiveMsg)
166 return receive(safeCast(FreeDagNode*, message), context);
167 if (s == closeSocketMsg)
168 return closeSocket(safeCast(FreeDagNode*, message), context);
169 return false;
170 }
171