/trunk/FreeSpeech/data-flow/src/Receive.cc

# · C++ · 95 lines · 52 code · 21 blank · 22 comment · 4 complexity · 65a2f830aa43352ea4797ab567fd5f51 MD5 · raw file

  1. // Copyright (C) 1999 Jean-Marc Valin & Dominic Letourneau
  2. #include "Node.h"
  3. #include "ObjectParser.h"
  4. #include <iostream>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9. #include <string.h>
  10. class Receive;
  11. DECLARE_NODE(Receive)
  12. /*Node
  13. * @name Receive
  14. * @category IO
  15. * @description Receive data from a TCP/IP network (Not working yet)
  16. * @output_name OUTPUT
  17. * @output_description No description available
  18. * @parameter_name VALUE
  19. * @parameter_description No description available
  20. END*/
  21. /** A constant node contains a value that will never changes. */
  22. class Receive : public Node
  23. {
  24. protected:
  25. /**The value of the constant*/
  26. ObjectRef value;
  27. /**The ID of the 'value' output*/
  28. int outputID;
  29. int sock;
  30. struct sockaddr_in addr;
  31. public:
  32. /**Constructor, takes the name of the node and a set of parameters*/
  33. Receive(string nodeName, ParameterSet params)
  34. : Node(nodeName, params)
  35. //, value (parameters.get("VALUE"))
  36. {
  37. outputID = addOutput("OUTPUT");
  38. cerr << "setting addr\n";
  39. addr.sin_family = AF_INET;
  40. addr.sin_port = htons (5248);
  41. struct hostent *hostinfo;
  42. hostinfo = gethostbyname ("localhost");
  43. addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;
  44. cerr << "creating socket\n";
  45. sock = socket (AF_INET, SOCK_STREAM, 6);
  46. cerr << "calling bind\n";
  47. if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)))
  48. {
  49. perror ("error opening socket");
  50. throw new NodeException(NULL, "can't connect socket",__FILE__,__LINE__);
  51. }
  52. cerr << "calling connect\n";
  53. if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)))
  54. {
  55. perror ("error opening socket");
  56. throw new NodeException(NULL, "can't connect socket",__FILE__,__LINE__);
  57. }
  58. cerr << "socket opened\n";
  59. }
  60. /**Ask for the node's output which ID (number) is output_id
  61. and for the 'count' iteration */
  62. virtual ObjectRef getOutput(int output_id, int count)
  63. {
  64. if (output_id==outputID) return nilObject;
  65. else throw new NodeException (this, "Receive: Unknown output id", __FILE__, __LINE__);
  66. }
  67. protected:
  68. /**Default constructor, should not be used*/
  69. Receive() {throw new GeneralException("Receive copy constructor should not be called",__FILE__,__LINE__);}
  70. };