tools/tcpService/main.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <QtNetwork/QNetworkInterface>
29 #include <QtNetwork/QTcpSocket>
30 #include <QtCore/QCoreApplication>
31 #include <QtCore/QFile>
32 #include <QtCore/QFileInfo>
33 #include <QtCore/QTime>
34 #include <opencv2/opencv.hpp>
35 #include <find_object/TcpServer.h>
36 
37 void showUsage()
38 {
39  printf("\ntcpService [options] port\n"
40  " Options:\n"
41  " --add \"image.png\" # Add object (file name + id). Set id=0\n"
42  " will make the server generating an id.\n"
43  " --remove # Remove object by ID.\n"
44  " --host #.#.#.# Set host address.\n"
45  " --help Show this help.\n"
46  " Examples:\n"
47  " Add: $ tcpService --add image.png 1 --host 127.0.0.1 4000\n"
48  " Remove: $ tcpService --remove 1 --host 127.0.0.1 4000\n");
49  exit(-1);
50 }
51 
52 int main(int argc, char * argv[])
53 {
54  QString ipAddress;
55  QString fileName;
56  int addId = 0;
57  int removeId = -1;
58  quint16 port = 0;
59 
60  for(int i=1; i<argc-1; ++i)
61  {
62  if(strcmp(argv[i], "--host") == 0 || strcmp(argv[i], "-host") == 0)
63  {
64  ++i;
65  if(i < argc)
66  {
67  ipAddress = argv[i];
68  }
69  else
70  {
71  printf("error parsing --host\n");
72  showUsage();
73  }
74  continue;
75  }
76  if(strcmp(argv[i], "--add") == 0 || strcmp(argv[i], "-add") == 0)
77  {
78  ++i;
79  if(i < argc-1)
80  {
81  fileName = argv[i];
82  ++i;
83  if(i < argc)
84  {
85  addId = atoi(argv[i]);
86  }
87  else
88  {
89  printf("error parsing --add\n");
90  }
91  }
92  else
93  {
94  printf("error parsing --add\n");
95  showUsage();
96  }
97  continue;
98  }
99  if(strcmp(argv[i], "--remove") == 0 || strcmp(argv[i], "-remove") == 0)
100  {
101  ++i;
102  if(i < argc)
103  {
104  removeId = std::atoi(argv[i]);
105  }
106  else
107  {
108  printf("error parsing --remove\n");
109  showUsage();
110  }
111  continue;
112  }
113  if(strcmp(argv[i], "-help") == 0 ||
114  strcmp(argv[i], "--help") == 0)
115  {
116  showUsage();
117  }
118 
119  printf("Unrecognized option: %s\n", argv[i]);
120  showUsage();
121  }
122 
123  port = atoi(argv[argc-1]);
124 
125  if(fileName.isEmpty() && removeId == -1)
126  {
127  printf("Arguments --add or --remove should be set.\n");
128  showUsage();
129  }
130  else if(port <=0)
131  {
132  printf("Port should be set!\n");
133  showUsage();
134  }
135 
136  if(ipAddress.isEmpty())
137  {
138  ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
139  }
140 
141  cv::Mat image;
142  if(!fileName.isEmpty())
143  {
144  image = cv::imread(fileName.toStdString());
145  if(image.empty())
146  {
147  printf("Cannot read image from \"%s\".\n", fileName.toStdString().c_str());
148  showUsage();
149  }
150  fileName = QFileInfo(fileName).fileName();
151  }
152 
153  QCoreApplication app(argc, argv);
154  QTcpSocket request;
155 
156  QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
157  QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
158 
159  printf("Connecting to \"%s:%d\"...\n", ipAddress.toStdString().c_str(), port);
160  request.connectToHost(ipAddress, port);
161 
162  if(!request.waitForConnected())
163  {
164  printf("Connecting to \"%s:%d\"... connection failed!\n", ipAddress.toStdString().c_str(), port);
165  return -1;
166  }
167  else
168  {
169  printf("Connecting to \"%s:%d\"... connected!\n", ipAddress.toStdString().c_str(), port);
170  }
171 
172  QByteArray block;
173  QDataStream out(&block, QIODevice::WriteOnly);
174  out.setVersion(QDataStream::Qt_4_0);
175  out << (quint64)0;
176 
177  if(!image.empty())
178  {
179  // publish image
180  std::vector<unsigned char> buf;
181  cv::imencode(".png", image, buf);
182 
183  out << (quint32)find_object::TcpServer::kAddObject;
184  out << addId;
185  out << fileName;
186  quint64 imageSize = buf.size();
187  out << imageSize;
188  out.writeRawData((char*)buf.data(), (int)buf.size());
189  printf("Add object %d \"%s\"\n", addId, fileName.toStdString().c_str());
190  }
191  else if(removeId)
192  {
194  out << removeId;
195  printf("Remove object %d\n", removeId);
196  }
197 
198  out.device()->seek(0);
199  out << (quint64)(block.size() - sizeof(quint64));
200 
201  qint64 bytes = request.write(block);
202  printf("Service published (%d bytes)!\n", (int)bytes);
203  request.waitForBytesWritten();
204  request.waitForReadyRead();
205 
206  return 0;
207 }
208 
app
int main(int argc, char *argv[])
void showUsage()


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Jun 10 2019 13:21:31