Panda3D
 All Classes Functions Variables Enumerations
mayaToEgg_client.cxx
1 // Filename: mayaToEgg.cxx
2 // Adapted by: cbrunner (09Nov09)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "mayaToEgg_client.h"
16 #ifdef _WIN32
17  #include "pystub.h"
18 #endif
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: MayaToEgg::Constructor
22 // Access: Public
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 MayaToEggClient::
26 MayaToEggClient() :
27  SomethingToEgg("Maya", ".mb")
28 {
29  qManager = new QueuedConnectionManager();
30  qReader = new QueuedConnectionReader(qManager, 0);
31  cWriter = new ConnectionWriter(qManager, 0);
32  // We assume the server is local and on port 4242
33  server.set_host("localhost", 4242);
34 }
35 
36 int main(int argc, char *argv[]) {
37  // We don't want pystub on linux, since it gives problems with Maya's python.
38 #ifdef _WIN32
39  // A call to pystub() to force libpystub.so to be linked in.
40  pystub();
41 #endif
42 
43  MayaToEggClient prog;
44  // Open a connection to the server process
45  PT(Connection) con = prog.qManager->open_TCP_client_connection(prog.server,0);
46  if (con.is_null()) {
47  nout << "Failed to open port to server process.\nMake sure maya2egg_server is running on localhost\n";
48  exit(1);
49  }
50 
51  // Add this connection to the readers list
52  prog.qReader->add_connection(con);
53 
54  // Get the current working directory and make sure it's a string
56  string s_cwd = (string)cwd.to_os_specific();
57  NetDatagram datagram;
58 
59  // First part of the datagram is the argc
60  datagram.add_uint8(argc);
61 
62  // Add the rest of the arguments as strings to the datagram
63  int i;
64  for (i = 0; i < argc; i++) {
65  datagram.add_string(argv[i]);
66  }
67 
68  // Lastly, add the current working dir as a string to the datagram
69  datagram.add_string(s_cwd);
70 
71  // Send it and close the connection
72  prog.cWriter->send(datagram, con);
73  con->flush();
74  while (true) {
75  prog.qReader->data_available();
76  if (prog.qManager->reset_connection_available()) {
77  PT(Connection) connection;
78  if (prog.qManager->get_reset_connection(connection)) {
79  prog.qManager->close_connection(con);
80  return 0;
81  }
82  }
83  Thread::sleep(0.1);
84  }
85 }
86 
void add_uint8(PN_uint8 value)
Adds an unsigned 8-bit integer to the datagram.
Definition: datagram.I:138
bool close_connection(const PT(Connection)&connection)
Terminates a UDP or TCP socket previously opened.
A specific kind of Datagram, especially for sending across or receiving from a network.
Definition: netDatagram.h:43
void add_string(const string &str)
Adds a variable-length string to the datagram.
Definition: datagram.I:351
This flavor of ConnectionManager will queue up all of the reset-connection messages from the Connecti...
bool send(const Datagram &datagram, const PT(Connection)&connection, bool block=false)
Enqueues a datagram for transmittal on the indicated socket.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
bool data_available()
Returns true if a datagram is available on the queue; call get_data() to extract the datagram...
bool reset_connection_available() const
Returns true if one of the readers/writers/listeners reported a connection reset recently.
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
string to_os_specific() const
Converts the filename from our generic Unix-like convention (forward slashes starting with the root a...
Definition: filename.cxx:1196
static void sleep(double seconds)
Suspends the current thread for at least the indicated amount of time.
Definition: thread.I:236
bool add_connection(Connection *connection)
Adds a new socket to the list of sockets the ConnectionReader will monitor.
This flavor of ConnectionReader will read from its sockets and queue up all of the datagrams read for...
static Filename get_cwd()
Returns the name of the current working directory.
This is the general base class for a file-converter program that reads some model file format and gen...
Represents a single TCP or UDP socket for input or output.
Definition: connection.h:32