Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleNetworkServer.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /** @example ExampleNetworkServer.cpp
26  @relates CScommServer
27  @ingroup g_examples
28  @brief this is an example for a network server
29  @author MIP
30 */
31 
32 #ifdef WIN32
33 #include <windows.h>
34 #endif
35 #ifndef WIN32
36 #include <unistd.h>
37 #endif
38 
39 #include <NetworkComm/CScommServer.hh>
40 #include <Base/Common/W32Compat.hh>
41 
42 using namespace std;
43 
44 int main (int /*argc*/, char** /*argv*/)
45 {
46 
47  BIAS::CScommServer server;
48 
49  server.SetVerbose(true); // to see some msg
50  // server.SetLog(true); // uncomment this to get all data send and received
51  // server.SetDebugLevel(D_CS_GETDATA);
52  //server.SetDebugLevel(D_CS_ANALYZE);
53 
54  vector<float> floats;
55  floats.push_back(-3);
56  floats.push_back(-2);
57  floats.push_back(-1);
58  string msg="LPOSHEAD";
59  // register msg POSHEAD to be accepted
60  server.RegisterMsg(msg, BIAS::CS_FLOAT, 3);
61 
62  msg="RPOSHEAD";
63  server.RegisterMsg(msg, BIAS::CS_INT, 1);
64  vector<int> ints,intMsg;
65  intMsg.push_back(-5);
66  intMsg.push_back(+6);
67 
68  // now let the server accept connections
69  server.WaitForConnections();
70 
71  vector<float> recFloats;
72  std::vector<char> binData(8);
73  binData[0]=']';
74  binData[1]='b';
75  binData[2]='i';
76  binData[3]='n';
77  binData[4]='a';
78  binData[5]='r';
79  binData[6]='y';
80  binData[7]='[';
81 
82  bool endless=true;
83  while (endless) {
84  // this msg will not be accepted by the exampleClient,
85  // because it is not registered
86  // server.SendMsg(msg,floats);
87  msg="LPOSHEAD";
88  if (server.GetData(msg, recFloats)==0) {
89  cout<<"New Data from client:"<<endl;
90  for (unsigned int i=0;i<recFloats.size();i++)
91  cout<<recFloats[i]<<" ";
92  cout<<endl;
93  }
94  biassleep(1);
95 
96  msg="RPOSHEAD";
97  if (server.GetData(msg, ints)==0) {
98  cout<<"New Data from client:"<<endl;
99  for (unsigned int i=0;i<ints.size();i++)
100  cout<<ints[i]<<" ";
101  cout<<endl;
102  }
103  // this sends only to exampleClient
104  server.SendMsg("exampleClient","BINDATA",&binData[0],binData.size());
105 
106  // this sends to all connected partners
107  server.SendMsg("RPOSHEAD",intMsg);
108  }
109 }
int RegisterMsg(std::string msgName, EdataType dataType, int amount=1)
only registered msgs are accepted by commPartners Register a msg with this functions.
Definition: CScommBase.cpp:315
void WaitForConnections(unsigned int port=D_CS_DEFAULT_PORT)
Init function, returns instantly and enables the server to accept connections on the given port ...
void SetVerbose(bool on)
gives some information about establising conn, and disconnecting etc.
class for sending/receiving data between clients and serversOnly registered msgs will be accepted at ...
Definition: CScommServer.hh:55
int SendMsg(const std::string &msgName, std::vector< float > &floatData)
sends msg msgName to all connected commPartners.
Definition: CScommBase.cpp:358
int GetData(const std::string &msgName, std::vector< float > &floatData)
returns instantly:&lt;br&gt; 0 if new data has arrived and the data in xxxData 1 if no new data has arri...
Definition: CScommBase.cpp:170