gclib  2.0.8
Communications API for Galil controllers and PLCs
remote_client.cpp
Go to the documentation of this file.
1 
10 #include "examples.h"
11 
12 #include <iostream> //std::cout
13 #include <vector>
14 #include <string>
15 
16 #ifdef _WIN32
17 #include <conio.h>
18 #elif __linux__
19 #include <ncurses.h>
20 #endif
21 
22 using namespace std;
23 
24 void print_client_message(const char* message)
25 {
26 #ifdef _WIN32
27  std::cout << message << std::endl;
28 #elif __linux__
29  printw(message);
30  printw("\n");
31 #endif
32 }
33 
34 void print_servers_list(const std::vector<std::string>& server_list)
35 {
36  char buf[G_SMALL_BUFFER];
37 
38 
39  if (server_list.size() == 0)
40  {
41  print_client_message("none");
42  }
43  else
44  {
45  for (int i = 0; i < server_list.size(); i++)
46  {
47  std::string test = server_list[i];
48  sprintf(buf, "<%d> %s", i, test.c_str());
49  print_client_message(buf);
50  }
51  }
52 }
53 
54 void servers_to_list(std::vector<std::string>& server_list, std::string servers)
55 {
56  server_list.clear();
57 
58  if (servers.length() == 0)
59  return;
60 
61  int index = 0;
62  std::string server;
63  while (index < servers.length())
64  {
65  if (servers[index] == '\n')
66  {
67  server_list.push_back(server);
68  server.clear();
69  }
70  else
71  {
72  server += servers[index];
73  }
74  index++;
75  }
76 
77  server_list.push_back(server);
78 }
79 
90 {
91  bool loop = true;
92  char servers[G_SMALL_BUFFER];
93  char buf[G_SMALL_BUFFER];
94  std::vector<std::string> server_list;
95 
96  char instructions[] = "<s> List available servers on the network\n"
97  "<h> List available hardware on currently connected server\n"
98  "<0-9> Enter numbers 0-9 to connect to a server by index\n"
99  "<l> Set active server back to local server\n"
100  "<q> Quit\n";
101 
102 #ifdef _WIN32
103  cout << instructions << std::endl;
104 #elif __linux__
105  //These functions set up the ncurses library to capture keyboard input
106  initscr(); // Initialization of ncurses library
107  cbreak(); // Capture one character at a time
108  noecho(); // Do not write back entered characters to the console
109  printw(instructions); //Print instructions to console
110 #endif
111 
112  while (loop)
113  {
114 #ifdef _WIN32
115  char input = _getch(); //Capture keypress
116 #elif __linux__
117  char input = getch(); //Capture keypress
118 #endif
119  if(input == 'q')
120  loop = false;
121  else if (input == 's')
122  {
123  print_client_message("Available Servers:");
124  e(GListServers(servers, G_SMALL_BUFFER));
125 
126  servers_to_list(server_list, servers);
127 
128  print_servers_list(server_list);
129  }
130  else if(input >= '0' && input <= '9')
131  {
132  int index = input - '0';
133  if (server_list.size() > 0 && index < server_list.size())
134  {
135  e(GSetServer(server_list[index].c_str()));
136  sprintf(buf, "Server set to: %s", server_list[index].c_str());
137  print_client_message(buf);
138  }
139  }
140  else if (input == 'l')
141  {
142  e(GSetServer("Local"));
143  print_client_message("Server set to: Local");
144  }
145  else if (input == 'h')
146  {
147  e(GAddresses(buf, G_SMALL_BUFFER));
148  print_client_message(buf);
149  }
150  }
151 
152 #if __linux__
153  endwin(); //Restores terminal to previous state
154 #endif
155 
156  return GALIL_EXAMPLE_OK;
157 }
GCLIB_DLL_EXPORTED GReturn GCALL GListServers(GCStringOut servers, GSize servers_len)
Uses GUtility(), G_UTIL_GCAPS_LIST_SERVERS to provide a list of all available gcaps services on the l...
Definition: gclibo.c:169
GCLIB_DLL_EXPORTED GReturn GCALL GSetServer(GCStringIn server_name)
Uses GUtility(), G_UTIL_GCAPS_SET_SERVER to set the new active server.
Definition: gclibo.c:128
GCLIB_DLL_EXPORTED GReturn GCALL GAddresses(GCStringOut addresses, GSize addresses_len)
Uses GUtility(), G_UTIL_GCAPS_ADDRESSES or G_UTIL_ADDRESSES to provide a listing of all available con...
Definition: gclibo.c:54
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
#define G_SMALL_BUFFER
Most reads from Galil are small. This value will easily hold most, e.g. TH, TZ, etc.
Definition: gclib.h:89
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
GReturn message(GCon g)
Demonstrates how to receive messages from the controller and detect differences in Trace and crashed ...
Definition: message.cpp:14
GReturn remote_client()
Lists available remote servers and allows connection to remote server.