Isis 3 Programmer Reference
SocketThread.cpp
1 #include "SocketThread.h"
2 #include "IException.h"
3 #include "Application.h"
4 
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 #include <sys/socket.h>
9 #include <sys/un.h>
10 #include <sys/wait.h>
11 #include <fcntl.h>
12 
13 #include <QObject>
14 #include <QThread>
15 
16 #include "IString.h"
17 
18 namespace Isis {
19 
27  }
28 
31  // wait();
32  }
33 
36  std::string p_socketFile = ("/tmp/isis_qview_" + Application::UserName()).toLatin1().data();
37  struct sockaddr_un p_socketName;
38  p_socketName.sun_family = AF_UNIX;
39  strcpy(p_socketName.sun_path, p_socketFile.c_str());
40  int p_socket;
41 
42  // Create a socket
43  if((p_socket = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
44  std::string msg = "Unable to create socket";
45  std::cerr << msg << std::endl;
46  remove(p_socketFile.c_str());
47  return;
48  }
49 
50  // Setting a timeout didn't work for Mac, so we're using a non-blocking mode
51  // instead.
52  fcntl(p_socket, F_SETFL, O_NONBLOCK);
53 
54  // Bind the file to the socket
55  int status = bind(p_socket, (struct sockaddr *)&p_socketName, sizeof(p_socketName));
56  if(status < 0) {
57  std::string msg = "Unable to bind to socket [" + p_socketFile + "]";
58  std::cerr << msg << std::endl;
59  remove(p_socketFile.c_str());
60  return;
61  }
62 
63  // Set up to listen to the socket
64  if(listen(p_socket, 5) < 0) {
65  std::string msg = "Unable to listen to socket [" + p_socketFile + "]";
66  std::cerr << msg << std::endl;
67  remove(p_socketFile.c_str());
68  return;
69  }
70 
71  p_done = false;
72 
73  while(!p_done) {
74  // Accept Socket
75  socklen_t len = sizeof(&p_socketName);
76  int childSocket = accept(p_socket, (struct sockaddr *)&p_socketName, &len);
77  if (childSocket < 0)
78  if (errno == EWOULDBLOCK) {
79  msleep(100);
80  continue; // probably timed out, we cant do anything about this anyways
81  }
82 
83  // Receive Data
84  int bytes;
85  // This used to be char buf[1024*1024]; but when that line existed the
86  // mac OS's would crash unpredictably, even when the code on that
87  // line wasn't executed.
88  QScopedPointer< char, QScopedPointerArrayDeleter<char> > buf(
89  new char[1024*1024]);
90  if((bytes = recv(childSocket, buf.data(), 1024 * 1024, 0)) < 0) {
91  std::string msg = "Unable to read from socket [" + p_socketFile + "]";
92  std::cerr << msg << std::endl;
93  remove(p_socketFile.c_str());
94  return;
95  }
96 
97  // Push everything onto our string buffer
98  IString buffer;
99  for(int i = 0; i < bytes; i++) buffer += buf.data()[i];
100  QChar escape(27);
101  IString escape2 = QString(escape);
102  while(buffer.size() > 0) {
103  IString token = buffer.Token(escape2);
104  if(token == "raise") {
105  emit focusApp();
106  }
107  else emit newImage(token.c_str());
108  }
109  };
110  }
111 }
void focusApp()
Application has focus signal.
void run()
Starts the socket thread.
IString Token(const IString &separator)
Returns the first token in the IString.
Definition: IString.cpp:912
~SocketThread()
Destroys the SocketThread object.
SocketThread(QObject *parent=0)
Constructor for the SocketThread.
static QString UserName()
Returns the user name.
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
void newImage(const QString &image)
New image signal.