Local server services

View on GitHub
Sample viewer app

Demonstrates how to start and stop the Local Server and start and stop a local map, feature, and geoprocessing service running on the Local Server.

screenshot

Use case

For executing offline geoprocessing tasks in your ArcGIS Runtime apps via an offline (local) server.

How to use the sample

Click Start Local Server to start the Local Server. Click Stop Local Server to stop the Local Server.

The Map Service combo box lets you to pick a local service that is available.

After browsing for the desired file, click Start Service to start the selected service.

When the running service's url appears, select it and click Open Url. To stop this running service, click Stop Service.

How it works

  1. Create it with LocalServer::instance and use LocalServer::start() to start the server asynchronously.
  2. LocalServer::statusChanged() fires whenever the running status of the Local Server changes. Wait for the server to be in the LocalServerStatus::Started state.
  3. Create and run a local service. Here is an example of running a LocalMapService:
    • new LocalMapService(Url) creates a local map service with the given URL path to map package (mpkx file).
    • LocalMapService::start() starts the service asynchronously.
    • The service is added to the LocalServer automatically.

To stop a LocalServer and stop any LocalServices that are added to it:

  1. Get any services that are currently running on the local server with LocalServer::services().
  2. Loop through all services and stop the selected service (from the dropdown) if it is started.
  3. Use LocalService::status() to check if the service's status is LocalServerStatus::Started.
  4. LocalService::stop() stops the service asynchronously.
  5. LocalService::statusChanged() fires whenever the running status of the local service has changed. Wait for all services to be in the LocalServerStatus::Stopped state.
  6. Stop the local server with LocalServer::stop().

Relevant API

  • LocalFeatureService
  • LocalGeoprocessingService
  • LocalMapService
  • LocalServer
  • LocalServerStatus
  • LocalService

Offline Data

Read more about how to set up the sample's offline data here.

Link Local Location
PointsOfInterest map package <userhome>/ArcGIS/Runtime/Data/mpkx/PointsofInterest.mpkx
Contour geoprocessing package <userhome>/ArcGIS/Runtime/Data/gpkx/Contour.gpkx

Additional information

Local Server can be downloaded for Windows and Linux platforms. Local Server is not supported on macOS.

Tags

feature, geoprocessing, local services, map, server, service

Sample Code

LocalServerServices.cppLocalServerServices.cppLocalServerServices.hLocalServerServices.qml
Use dark colors for code blocksCopy
                                                                                                                                                                                                                                                                                                                          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// [WriteFile Name=LocalServerServices, Category=LocalServer]
// [Legal]
// Copyright 2017 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "LocalServerServices.h"

#include "Basemap.h"
#include "LocalServer.h"
#include "LocalMapService.h"
#include "LocalFeatureService.h"
#include "LocalGeoprocessingService.h"

#include <QDesktopServices>
#include <QDir>
#include <QTemporaryDir>

using namespace Esri::ArcGISRuntime;

LocalServerServices::LocalServerServices(QQuickItem* parent) :
  QQuickItem(parent),
  m_dataPath(QDir::homePath() + "/ArcGIS/Runtime/Data")
{
  // Create a temporary directory for the local server if one has not already been created
  if (!LocalServer::appDataPath().isEmpty() && !LocalServer::tempDataPath().isEmpty())
    return;

  // create temp/data path
  const QString tempPath = LocalServerServices::shortestTempPath() + "/EsriQtSample";

  // create the directory
  m_tempDir = std::make_unique<QTemporaryDir>(tempPath);

  // set the temp & app data path for the local server
  LocalServer::instance()->setTempDataPath(m_tempDir->path());
  LocalServer::instance()->setAppDataPath(m_tempDir->path());
}

LocalServerServices::~LocalServerServices() = default;

void LocalServerServices::init()
{
  qmlRegisterType<LocalServerServices>("Esri.Samples", 1, 0, "LocalServerServicesSample");
}

void LocalServerServices::componentComplete()
{
  QQuickItem::componentComplete();

  QString dataPath = QDir::homePath() + "/ArcGIS/Runtime/Data";
  // create a map service
  m_localMapService = new LocalMapService(dataPath + "/mpkx/PointsofInterest.mpkx", this);
  // create a feature service
  m_localFeatureService = new LocalFeatureService(dataPath + "/mpkx/PointsofInterest.mpkx", this);
  // create a gp service
  m_localGPService = new LocalGeoprocessingService(dataPath + "/gpkx/Contour.gpkx", this);

  if (LocalServer::instance()->isInstallValid())
  {
    if (LocalServer::status() == LocalServerStatus::Started)
      LocalServer::stop();

    connectSignals();
  }
}

void LocalServerServices::connectSignals()
{
  // local server status
  connect(LocalServer::instance(), &LocalServer::statusChanged, this, [this]()
  {
    // append to the status string
    switch (LocalServer::status())
    {
      case LocalServerStatus::Starting:
      {
        m_serverStatus.append("Server Status: STARTING\n");
        break;
      }
      case LocalServerStatus::Started:
      {
        m_serverStatus.append("Server Status: STARTED\n");
        m_isServerRunning = true;
        emit isServerRunningChanged();
        break;
      }
      case LocalServerStatus::Stopping:
      {
        m_serverStatus.append("Server Status: STOPPING\n");
        break;
      }
      case LocalServerStatus::Stopped:
      {
        m_serverStatus.append("Server Status: STOPPED\n");
        m_isServerRunning = false;
        emit isServerRunningChanged();
        break;
      }
      case LocalServerStatus::Failed:
      {
        m_serverStatus.append("Server Status: FAILED\n");
        break;
      }
      default:
        break;
    }
    emit serverStatusChanged();
  });

  connect(m_localMapService, &LocalMapService::statusChanged, this, [this]()
  {
    updateStatus(m_localMapService, "Map");
  });

  connect(m_localFeatureService, &LocalFeatureService::statusChanged, this, [this]()
  {
    updateStatus(m_localFeatureService, "Feature");
  });

  connect(m_localGPService, &LocalGeoprocessingService::statusChanged, this, [this]()
  {
    updateStatus(m_localGPService, "Geoprocessing");
  });
}

// start the server
void LocalServerServices::startLocalServer()
{
  if (m_isServerRunning)
    return;

  // clear all the status messages
  m_serverStatus.clear();
  // start local server
  LocalServer::start();
}

// stop the server
void LocalServerServices::stopLocalServer()
{
  LocalServer::stop();
}

// start a service
void LocalServerServices::startService(const QString& serviceName, const QUrl& filePath)
{
  QString path;
  if (!filePath.isEmpty())
    path = QUrl(filePath).toLocalFile();

  if (serviceName == "Map Service")
  {
    if (path.isEmpty())
    {
      if (m_localMapService->status() == LocalServerStatus::Started)
        return;

      m_localMapService->start();
    }
    else
    {
      LocalMapService* mapService = new LocalMapService(path, this);
      connect(mapService, &LocalMapService::statusChanged, this, [this, mapService]()
      {
        updateStatus(mapService, "Map");
      });
      mapService->start();
    }
  }
  else if (serviceName == "Feature Service")
  {
    if (path.isEmpty())
    {
      if (m_localFeatureService->status() == LocalServerStatus::Started)
        return;

      m_localFeatureService->start();
    }
    else
    {
      LocalFeatureService* featureService = new LocalFeatureService(path, this);
      connect(featureService, &LocalFeatureService::statusChanged, this, [this, featureService]()
      {
        updateStatus(featureService, "Feature");
      });
      featureService->start();
    }
  }
  else if (serviceName == "Geoprocessing Service")
  {
    if (path.isEmpty())
    {
      if (m_localGPService->status() == LocalServerStatus::Started)
        return;

      m_localGPService->start();
    }
    else
    {
      LocalGeoprocessingService* gpService = new LocalGeoprocessingService(path, this);
      connect(gpService, &LocalGeoprocessingService::statusChanged, this, [this, gpService]()
      {
        updateStatus(gpService, "Geoprocessing");
      });
      gpService->start();
    }
  }
}

// stop any service
void LocalServerServices::stopService(const QUrl& serviceUrl)
{
  if (serviceUrl.isEmpty())
    return;

  LocalService* serviceToStop = m_servicesHash[serviceUrl];
  serviceToStop->stop();
}

// open the link in a browser
void LocalServerServices::openURL(const QString& serviceURL)
{
  QDesktopServices::openUrl(QUrl(serviceURL));
}

// check if any one of the services is running
bool LocalServerServices::isAnyServiceRunning()
{
  return LocalServer::services().size() > 0;
}

// get the current list of services from local server
void LocalServerServices::getCurrentServices()
{
  // get the service names by looping through the services
  m_services.clear();
  for (const LocalService* service : LocalServer::services())
  {
    m_services << service->url().toString();
  }
  emit servicesChanged();
}

// get the current status of any service
void LocalServerServices::updateStatus(LocalService* service, const QString& serviceName)
{
  switch (service->status())
  {
    case LocalServerStatus::Starting:
    {
      m_serverStatus.append(serviceName + " Service Status: STARTING\n");
      break;
    }
    case LocalServerStatus::Started:
    {
      m_serverStatus.append(serviceName + " Service Status: STARTED\n");
      m_isServiceRunning = true;
      emit isServiceRunningChanged();

      getCurrentServices();
      m_servicesHash.insert(service->url(), service);
      break;
    }
    case LocalServerStatus::Stopping:
    {
      m_serverStatus.append(serviceName + " Service Status: STOPPING\n");
      break;
    }
    case LocalServerStatus::Stopped:
    {
      m_serverStatus.append(serviceName + " Service Status: STOPPED\n");
      if (!isAnyServiceRunning())
      {
        m_isServiceRunning = false;
        emit isServiceRunningChanged();
      }

      getCurrentServices();
      m_servicesHash.remove(service->url());
      break;
    }
    default:
      break;
  }
  emit serverStatusChanged();
}

QString LocalServerServices::shortestTempPath()
{
  // get tmp and home paths
  const QString tmpPath = QDir::tempPath();
  const QString homePath = QDir::homePath();

  // return whichever is shorter, temp or home path
  if (homePath.length() > tmpPath.length())
    return tmpPath;
  else
    return homePath;
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.