1 /* Copyright (C) 2000-2007 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: coal.pike,v 1.2 2009/08/07 15:22:36 nicke Exp $
19 inherit "coal/protocoll";
23 #include <attributes.h>
24 class coal : public protocoll{
30 * COAL protocol implementation file
35 #define CDEBUG(s) werror(s+"\n")
47 * Create the socket and initialize the variables.
52 mCommandServer = ([ ]);
54 iTransfer = COAL_TRANSFER_NONE;
55 iLastResponse = time();
56 objectCache = ([ 0: _Server, ]);
59 "Object was not found",
61 "COAL: wrong filetype specified",
62 "Security: Access violation",
63 "Target was not found",
64 "Error loading object",
66 "account has expired",
68 "Command not understood",
69 "Execution error (internal server error)",
71 "Wrong number of arguments to function",
72 "Argument type does not match",
79 * Called when the socket disconnects and also calls disconnect
80 * function of the user object.
87 CDEBUG("Transfer finished...\n");
89 if ( functionp(oTransfer) ) {
102 * Receive a message on the socket.
104 * @param string str - the string received on the socket.
108 receive_message(string str)
110 int t_id, obj_id, command;
118 iLastResponse = time();
120 str = sLastPacket + str;
123 if ( iTransfer == COAL_TRANSFER_SEND ) {
127 else if ( iTransfer == COAL_TRANSFER_RCV ) {
129 if ( iTransferSize != -1 && i > iTransferSize ) {
130 sLastPacket = copy_value(str[iTransferSize..]);
131 str = str[..iTransferSize-1];
133 oTransfer->write(str);
134 if ( iTransferSize != -1 ) {
135 iTransferSize -= strlen(str);
136 if ( iTransferSize <= 0 ) {
141 iTransfer = COAL_TRANSFER_NONE;
144 if ( iTransfer == COAL_TRANSFER_RCV )
148 mixed err = catch(cmds = receive_binary(str));
151 // receive binary throws errors with parsed start parameters
152 if ( sizeof(err) > 2 )
153 SEND_ERROR(E_ERROR_PROTOCOL, err[0], err[2], err[4], 0, err[3],
156 SEND_ERROR(E_ERROR_PROTOCOL, err[0], 0, 0, 0, 0, ({ }), err[1]);
160 while ( arrayp(cmds) ) {
161 command = cmds[HL_CMD][COALLINE_COMMAND] & COMMAND_RAW;
162 obj_id = cmds[HL_CMD][COALLINE_OBJECT];
163 args = cmds[HL_ARGS];
165 t_id = cmds[HL_CMD][COALLINE_TID];
166 obj = objectCache[obj_id];
168 obj = find_object(obj_id);
169 objectCache[obj_id] = obj;
172 func = mCommandServer[command];
173 cid = (objectp(obj) ? obj->get_object_class() : 0);
175 CDEBUG("RCVD: " + command + ","+obj_id+","+t_id+")");
176 if ( functionp(func) )
178 mixed message = catch {
179 result = func(t_id, obj, args);
182 iTransfer = 0; // set back transfer mode !
183 if ( arrayp(message) ) {
184 if ( sizeof(message) == 3 ) {
185 SEND_ERROR(message[2], message[0], t_id, obj_id, cid,
187 master()->describe_backtrace(message[1]));
191 FATAL(master()->describe_backtrace(message[1]));
192 SEND_ERROR(E_ERROR, message[0],
193 t_id, obj_id, cid, command, args,
194 master()->describe_backtrace(message[1]));
197 else if ( objectp(message) ) {
198 if ( !functionp(message->display) ) {
200 FATAL(master()->describe_backtrace(message[1]));
202 SEND_ERROR(E_ERROR, message[0],
203 t_id, obj_id, cid, command, args,
204 master()->describe_backtrace(message[1]));
208 SEND_ERROR(result, "Exception",t_id,obj_id,cid,
213 SEND_ERROR(E_FUNCTION|E_NOTEXIST, "Command does not exist !",
214 t_id, obj_id, cid,command, args, 0);
215 cmds = receive_binary(str);
224 SEND_COAL(0, COAL_LOGOUT, 0, 0, ({ }));
229 void close_connection()
236 string get_socket_name() { return "coal"; }
237 int get_last_response() { return iLastResponse; }