00001
00002
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 extern "C" {
00050 #include <unistd.h>
00051 #include <stdlib.h>
00052 #include <string.h>
00053 }
00054
00055 #include <fstream>
00056 #include <sstream>
00057
00058 #include "consmgr.h"
00059
00060 #include "sockconn.h"
00061 #include "udslistener.h"
00062 #include "udssockaddr.h"
00063
00064
00065 static int uds_test();
00066 static void accept_and_send_on_connection(Listener *listener,
00067 SocketAddressP &path);
00068 static void *socket_sender_thread(void *path);
00069 static void socket_sender(SocketAddressP &path);
00070
00071 int
00072 main(int argc, char *argv[])
00073 {
00074 int ret;
00075
00076
00077 ret = uds_test();
00078
00079 exit(0);
00080 }
00081
00082 static int
00083 uds_test()
00084 {
00085 char *ptmp = strdup("/tmp/test-sock.XXXXXX");
00086 std::string path(mktemp(ptmp));
00087 free(ptmp);
00088
00089 if (path.length() == 0) {
00090 cerr << "mktemp() failed to return a path, unable to continue." << endl;
00091 return(1);
00092 }
00093
00094 SocketAddressP addr(new UDSSocketAddress(path));
00095
00096 cerr << "Opening a socket on \"" << path << "\" for listening." << endl;
00097
00098 Listener *listener = new UDSListener(addr);
00099
00100
00101
00102 accept_and_send_on_connection(listener, addr);
00103 accept_and_send_on_connection(listener, addr);
00104
00105 listener->disconnect();
00106 delete listener;
00107
00108 return(0);
00109 }
00110
00111 void
00112 accept_and_send_on_connection(Listener *listener, SocketAddressP &path)
00113 {
00114
00115 pthread_t tid_uds_sender;
00116 int ret = pthread_create(&tid_uds_sender, NULL, socket_sender_thread, &path);
00117 PTHREAD_CHECK_AND_THROW(ret, "pthread_create(socket_sender_thread)");
00118
00119
00120 ConnectorP incoming = listener->listen_for_incoming();
00121
00122 cerr << "Got a connection on " << listener->get_id() << ": ";
00123 cerr << incoming->get_id() << endl;
00124
00125
00126
00127 try {
00128 while (1) {
00129 Data *b = incoming->read();
00130 std::cout << *b;
00131 delete b;
00132 }
00133 } catch (...) {
00134 std::cout << endl << "Connection closed." << endl;
00135 }
00136
00137 ret = pthread_join(tid_uds_sender, NULL);
00138 }
00139
00142 void *
00143 socket_sender_thread(void *path)
00144 {
00145 socket_sender(*(SocketAddressP*)path);
00146 return NULL;
00147 }
00148
00153 void
00154 socket_sender(SocketAddressP &path)
00155 {
00156 Connector *conn = new SocketConnector(path);
00157
00158 Data b("TEST DATA 12345");
00159 int ret = conn->write(b);
00160 std::cout << "socket_sender wrote " << ret << " bytes" << std::endl;
00161 delete conn;
00162 }