Jamoma API  0.6.0.a19
Max/library/Internals/j.receive.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup implementationMaxLibrary
4  *
5  * @brief Jamoma For Max Shared Library
6  *
7  * @details Receive messages from remote
8  *
9  * @authors Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2006, Tim Place @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "Jamoma.h"
18 
19 
20 /** Receive Object */
21 typedef struct _receive{
22  t_object ob; ///< REQUIRED: Our object
23  void *address_out; ///< outlet used to output address of received data
24  void *data_out; ///< outlet used to output received data
25  TTReceiverPtr receiver; ///< the TTObject class to receive data
26 
27 } t_receive;
28 
29 // Prototypes
30 
31 /** Object instantiation.
32  @param s Pointer to symbol bassed as message argument to the object.
33  @param argc The number of arguments passed to the object.
34  @param argv Pointer to arguments as an array of atoms.
35  @return Pointer to the newly created object.
36  */
37 void *receive_new(t_symbol *s, long argc, t_atom *argv);
38 
39 
40 /** This method is called when the object is free (deleted).
41  @param x Pointer to the object.
42  */
43 void receive_free(t_receive *x);
44 
45 
46 /** Method for displaying assist strings for inlets and outlets.
47  @param x Pointer to this object.
48  @param b
49  @param msg
50  @param argc
51  @param argv
52  */
53 void receive_assist(t_receive *x, void *b, long msg, long arg, char *dst);
54 
55 t_max_err receive_setaddress(t_receive *x, void *attr, long argc, t_atom *argv);
56 t_max_err receive_getaddress(t_receive *x, void *attr, AtomCount *argc, AtomPtr *argv);
57 
58 t_max_err receive_setattribute(t_receive *x, void *attr, long argc, t_atom *argv);
59 t_max_err receive_getattribute(t_receive *x, void *attr, AtomCount *argc, AtomPtr *argv);
60 
61 t_max_err receive_setenable(t_receive *x, void *attr, long argc, t_atom *argv);
62 t_max_err receive_getenable(t_receive *x, void *attr, AtomCount *argc, AtomPtr *argv);
63 
64 void receive_return_address(t_receive *x, t_symbol *mess, long argc, t_atom *argv);
65 void receive_return_value(t_receive *x, t_symbol *mess, long argc, t_atom *argv);
66 
67 void receive_build(t_receive *x, t_symbol *address);
68 
69 // ask the value to the binded node
70 void receive_get(t_receive *x);
71 
72 // ask the value of any node without binding on it
73 void receive_symbol(t_receive *x, t_symbol *mess, long argc, t_atom *argv);
74 
75 // Globals
76 static t_class *s_receive_class; // Required: Global pointer the j.receive class
77 
78 
79 /************************************************************************************/
80 
81 void receive_initclass()
82 {
83  t_class *c;
84  t_object *attr;
85 
86  // Define our class
87  c = class_new( "j.receive",
88  (method)receive_new,
89  (method)receive_free,
90  sizeof(t_receive),
91  (method)0L,
92  A_GIMME,
93  0);
94 
95  // Make methods accessible for our class:
96  class_addmethod(c, (method)receive_return_address, "return_address", A_CANT, 0);
97  class_addmethod(c, (method)receive_return_value, "return_value", A_CANT, 0);
98  class_addmethod(c, (method)receive_assist, "assist", A_CANT, 0);
99  class_addmethod(c, (method)object_obex_dumpout, "dumpout", A_CANT, 0);
100 
101  // ask the value to the binded node
102  class_addmethod(c, (method)receive_get, "bang", 0);
103 
104  // ask the value of any node without binding on it
105  class_addmethod(c, (method)receive_symbol, "anything", A_GIMME, 0);
106 
107  // ATTRIBUTE: address
108  attr = attr_offset_new("address", _sym_symbol, 0, (method)receive_getaddress, (method)receive_setaddress, NULL);
109  class_addattr(c, attr);
110 
111  // ATTRIBUTE: attribute
112  attr = attr_offset_new("attribute", _sym_symbol, 0, (method)receive_getattribute, (method)receive_setattribute, NULL);
113  class_addattr(c, attr);
114 
115  // ATTRIBUTE: enable
116  attr = attr_offset_new("enable", _sym_long, 0, (method)receive_getenable, (method)receive_setenable, NULL);
117  class_addattr(c, attr);
118 
119  // Finalize our class
120  class_register(CLASS_BOX, c);
121  s_receive_class = c;
122 }
123 
124 
125 /************************************************************************************/
126 // Object Life
127 
128 // Create
129 void *receive_new(t_symbol *s, long argc, t_atom *argv)
130 {
131  long attrstart = attr_args_offset(argc, argv); // support normal arguments
132  t_receive *x = (t_receive *)object_alloc(s_receive_class);
133 
134  if(x){
135  x->address_out = outlet_new(x,NULL); // anything outlet
136  x->data_out = outlet_new(x, NULL); // anything outlet
137 
138  attr_args_process(x, argc, argv); // handle attribute args
139 
140  // If no address was specified as an attribute
141  if(attrstart > 0)
142  ;//jamoma_receiver_create((ObjectPtr)x, atom_getsym(argv), &x->receiver);
143 
144  }
145  return x;
146 }
147 
148 
149 // Destroy
151 {
152  ;
153 }
154 
155 /************************************************************************************/
156 // Methods bound to input/inlets
157 
158 // Method for Assistance Messages
159 void receive_assist(t_receive *x, void *b, long msg, long arg, char *dst)
160 {
161  if(msg==1) // Inlets
162  strcpy(dst, "(signal) input to the module");
163  else if(msg==2){ // Outlets
164  if(arg == 0)
165  strcpy(dst, "output from remote");
166  else
167  strcpy(dst, "dumpout");
168  }
169 }
170 
171 
172 // ATTRIBUTE: address
173 t_max_err receive_setaddress(t_receive *x, void *attr, long argc, t_atom *argv)
174 {
175  TTValue v;
176 
177  if (atom_gettype(argv) == A_SYM) {
178 
179  v.append(TT(atom_getsym(argv)->s_name));
180  x->receiver->setAttributeValue(TTSymbol("Address"), v);
181 
182  }
183  else
184  return MAX_ERR_GENERIC;
185 
186  return MAX_ERR_NONE;
187 }
188 
189 t_max_err receive_getaddress(t_receive *x, void *attr, AtomCount *argc, AtomPtr *argv)
190 {
191  TTValue v;
192  TTSymbol s;
193 
194  *argc = 1;
195 
196  if (!(*argv)) { // otherwise use memory passed in
197  *argv = (t_atom *)sysmem_newptr(sizeof(t_atom));
198 
199  x->receiver->getAttributeValue(TTSymbol("Address"), v);
200  s = NULL;
201  v.get(0, s);
202  atom_setsym(*argv, gensym((char*)s.c_str()));
203  }
204  else
205  return MAX_ERR_GENERIC;
206 
207  return MAX_ERR_NONE;
208 }
209 
210 
211 // ATTRIBUTE: attribute
212 t_max_err receive_setattribute(t_receive *x, void *attr, long argc, t_atom *argv)
213 {
214  TTValue v;
215 
216  if (atom_gettype(argv) == A_SYM) {
217 
218  v.append(TT(atom_getsym(argv)->s_name));
219  x->receiver->setAttributeValue(TTSymbol("Attribute"), v);
220 
221  }
222  else
223  return MAX_ERR_GENERIC;
224 
225  return MAX_ERR_NONE;
226 }
227 
228 t_max_err receive_getattribute(t_receive *x, void *attr, AtomCount *argc, AtomPtr *argv)
229 {
230  TTValue v;
231  TTSymbol s;
232 
233  *argc = 1;
234 
235  if (!(*argv)) { // otherwise use memory passed in
236  *argv = (t_atom *)sysmem_newptr(sizeof(t_atom));
237 
238  x->receiver->getAttributeValue(TTSymbol("Attribute"), v);
239  s = NULL;
240  v.get(0, s);
241  atom_setsym(*argv, gensym((char*)s.c_str()));
242  }
243  else
244  return MAX_ERR_GENERIC;
245 
246  return MAX_ERR_NONE;
247 }
248 
249 // ATTRIBUTE: enable
250 t_max_err receive_setenable(t_receive *x, void *attr, long argc, t_atom *argv)
251 {
252  TTValue v;
253 
254  if (atom_gettype(argv) == A_LONG) {
255 
256  if (atom_getlong(argv))
257  v.append((TTBoolean)kTTBoolYes);
258  else
259  v.append((TTBoolean)kTTBoolNo);
260 
261  x->receiver->setAttributeValue(TTSymbol("Enable"), v);
262 
263  }
264  else
265  return MAX_ERR_GENERIC;
266 
267  return MAX_ERR_NONE;
268 }
269 
270 t_max_err receive_getenable(t_receive *x, void *attr, AtomCount *argc, AtomPtr *argv)
271 {
272  TTValue v;
273  TTBoolean b;
274 
275  *argc = 1;
276 
277  if (!(*argv)) { // otherwise use memory passed in
278  *argv = (t_atom *)sysmem_newptr(sizeof(t_atom));
279 
280  x->receiver->getAttributeValue(TTSymbol("Enable"), v);
281  b = NULL;
282  v.get(0, b);
283  atom_setlong(*argv, b);
284  }
285  else
286  return MAX_ERR_GENERIC;
287 
288  return MAX_ERR_NONE;
289 }
290 
291 // ask the value to the binded node
292 void receive_get(t_receive *x)
293 {
294  x->receiver->sendMessage(TTSymbol("get"));
295 }
296 
297 // ask the value of any node without binding on it
298 void receive_symbol(t_receive *x, t_symbol* msg, long argc, t_atom* argv)
299 {
300  TTReceiverPtr aTempReceiver;
301 
302  // Make a temporary receiver
303  if (msg->s_name[0] == C_SEPARATOR)
304  {
305  /*if (jamoma_receiver_create((ObjectPtr)x, msg, &aTempReceiver)) {
306  aTempReceiver->sendMessage(TTSymbol("get"));
307  TTObjectRelease(TTObjectHandle(&aTempReceiver));
308  }*/
309  }
310 }
311 
312 void receive_return_address(t_receive *x, SymbolPtr msg, AtomCount argc, AtomPtr argv)
313 {
314  outlet_anything(x->address_out, msg, argc, argv);
315 }
316 
317 void receive_return_value(t_receive *x, SymbolPtr msg, AtomCount argc, AtomPtr argv)
318 {
319  outlet_anything(x->data_out, msg, argc, argv);
320 }
321 
TTErr sendMessage(const TTSymbol name)
TODO: Document this function.
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTReceiver ...
Definition: TTReceiver.h:27
TTErr getAttributeValue(const TTSymbol name, TTValue &value)
Get an attribute value for an object.
void receive_free(t_receive *x)
This method is called when the object is free (deleted).
TTReceiverPtr receiver
the TTObject class to receive data
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
void * receive_new(t_symbol *s, long argc, t_atom *argv)
Object instantiation.
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void append(const T &anElementValueToAppend)
Insert a single TTElement at the end.
Definition: TTValue.h:243
void * data_out
outlet used to output received data
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
void get(const TTUInt16 index, T &returnedElementValue) const
DEPRECATED.
Definition: TTValue.h:591
t_object ob
REQUIRED: Our object.
const char * c_str() const
Return a pointer to the internal string as a C-string.
Definition: TTSymbol.h:77
void receive_assist(t_receive *x, void *b, long msg, long arg, char *dst)
Method for displaying assist strings for inlets and outlets.
void * address_out
outlet used to output address of received data
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34