Imported Upstream version 2.6.1
[debian/amanda] / xfer-src / xmsg.c
1 /*
2  * Copyright (c) 2008 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1 as
6  * published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  *
17  * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  */
20
21 #include "amxfer.h"
22 #include "amanda.h"
23
24 /* TODO: use glib chunk allocator */
25
26 /* NOTE TO IMPLEMENTERS:
27  *
28  * When adding a new attribute, make changes in the following places:
29  *  - add the attribute to the XMsg struct in xmsg.h
30  *  - add the attribute to the comments for the appropriate xmsg_types
31  *  - free the attribute in xmsg_free.
32  */
33
34 /*
35  * Methods
36  */
37
38 XMsg *
39 xmsg_new(
40     XferElement *elt,
41     xmsg_type type,
42     int version)
43 {
44     XMsg *msg = g_new0(XMsg, 1);
45     msg->elt = elt;
46     msg->type = type;
47     msg->version = version;
48
49     /* messages hold a reference to the XferElement, to avoid dangling
50      * pointers. */
51     g_object_ref((GObject *)elt);
52
53     return msg;
54 }
55
56 void
57 xmsg_free(
58     XMsg *msg)
59 {
60     /* unreference the source */
61     g_object_unref((GObject *)msg->elt);
62
63     /* and free any allocated attributes */
64     if (msg->repr) g_free(msg->repr);
65     if (msg->message) g_free(msg->message);
66
67     /* then free the XMsg itself */
68     g_free(msg);
69 }
70
71 char *
72 xmsg_repr(
73     XMsg *msg)
74 {
75     if (!msg) return "(nil)"; /* better safe than sorry */
76
77     /* this just shows the "header" fields for now */
78     if (!msg->repr) {
79         char *typ = NULL;
80         switch (msg->type) {
81             case XMSG_INFO: typ = "INFO"; break;
82             case XMSG_ERROR: typ = "ERROR"; break;
83             case XMSG_DONE: typ = "DONE"; break;
84             case XMSG_CANCEL: typ = "CANCEL"; break;
85             default: typ = "**UNKNOWN**"; break;
86         }
87
88         msg->repr = vstrallocf("<XMsg@%p type=XMSG_%s elt=%s version=%d>",
89             msg, typ, xfer_element_repr(msg->elt), msg->version);
90     }
91
92     return msg->repr;
93 }