vtkPVMultiClientsInformation.cxx 6.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*=========================================================================

  Program:   ParaView
  Module:    vtkPVMultiClientsInformation.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkPVMultiClientsInformation.h"

#include "vtkClientServerStream.h"
18
#include "vtkCompositeMultiProcessController.h"
19 20 21 22 23 24
#include "vtkMultiProcessController.h"
#include "vtkObjectFactory.h"
#include "vtkPVConfig.h"
#include "vtkPVServerOptions.h"
#include "vtkPVServerOptionsInternals.h"
#include "vtkPVSession.h"
25 26
#include "vtkProcessModule.h"
#include "vtkToolkits.h"
27 28 29 30 31 32 33 34 35

vtkStandardNewMacro(vtkPVMultiClientsInformation);

//----------------------------------------------------------------------------
vtkPVMultiClientsInformation::vtkPVMultiClientsInformation()
{
  this->MultiClientEnable = 0;
  this->ClientIds = NULL;
  this->ClientId = 0;
36
  this->MasterId = 0;
37 38 39 40 41 42
  this->NumberOfClients = 1;
}

//----------------------------------------------------------------------------
vtkPVMultiClientsInformation::~vtkPVMultiClientsInformation()
{
Kitware Robot's avatar
Kitware Robot committed
43 44
  if (this->ClientIds)
  {
45 46
    delete[] this->ClientIds;
    this->ClientIds = NULL;
Kitware Robot's avatar
Kitware Robot committed
47
  }
48 49 50 51 52
}

//----------------------------------------------------------------------------
void vtkPVMultiClientsInformation::PrintSelf(ostream& os, vtkIndent indent)
{
Kitware Robot's avatar
Kitware Robot committed
53
  this->Superclass::PrintSelf(os, indent);
54 55
  os << indent << "Multi-client: " << this->MultiClientEnable << endl;
  os << indent << "ClientId: " << this->ClientId << endl;
56
  os << indent << "MasterId: " << this->MasterId << endl;
57 58
  os << indent << "NumberOfClients: " << this->NumberOfClients << endl;
  os << indent << "Client list: ";
Kitware Robot's avatar
Kitware Robot committed
59 60
  for (int i = 0; i < this->NumberOfClients; i++)
  {
61
    os << this->GetClientId(i) << " ";
Kitware Robot's avatar
Kitware Robot committed
62
  }
63 64 65 66
  os << endl;
}

//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
67
void vtkPVMultiClientsInformation::DeepCopy(vtkPVMultiClientsInformation* info)
68 69 70
{
  this->MultiClientEnable = info->MultiClientEnable;
  this->ClientId = info->GetClientId();
71
  this->MasterId = info->GetMasterId();
72
  this->NumberOfClients = info->GetNumberOfClients();
Kitware Robot's avatar
Kitware Robot committed
73 74
  if (this->ClientIds)
  {
75 76
    delete[] this->ClientIds;
    this->ClientIds = NULL;
Kitware Robot's avatar
Kitware Robot committed
77 78 79
  }
  if (info->ClientIds)
  {
80
    this->ClientIds = new int[this->NumberOfClients];
Kitware Robot's avatar
Kitware Robot committed
81 82
    for (int cc = 0; cc < this->NumberOfClients; cc++)
    {
83 84
      this->ClientIds[cc] = info->GetClientId(cc);
    }
Kitware Robot's avatar
Kitware Robot committed
85
  }
86 87 88 89 90 91
}

//----------------------------------------------------------------------------
void vtkPVMultiClientsInformation::CopyFromObject(vtkObject* vtkNotUsed(obj))
{
  vtkProcessModule* pm = vtkProcessModule::GetProcessModule();
Kitware Robot's avatar
Kitware Robot committed
92 93
  if (!pm)
  {
94 95
    vtkWarningMacro("ProcessModule is not available.");
    return;
Kitware Robot's avatar
Kitware Robot committed
96
  }
97
  vtkPVOptions* options = pm->GetOptions();
Kitware Robot's avatar
Kitware Robot committed
98 99
  vtkPVServerOptions* serverOptions = vtkPVServerOptions::SafeDownCast(options);
  this->MultiClientEnable = serverOptions ? serverOptions->GetMultiClientMode() : 0;
100

luz paz's avatar
luz paz committed
101
  // Retrieve the current client connection ID
102 103
  vtkPVSession* session = vtkPVSession::SafeDownCast(pm->GetSession());
  vtkCompositeMultiProcessController* ctrl;
Kitware Robot's avatar
Kitware Robot committed
104 105
  if (this->ClientIds)
  {
106 107
    delete[] this->ClientIds;
    this->ClientIds = NULL;
Kitware Robot's avatar
Kitware Robot committed
108 109 110 111
  }
  if (session && (ctrl = vtkCompositeMultiProcessController::SafeDownCast(
                    session->GetController(vtkPVSession::CLIENT))))
  {
112
    this->ClientId = ctrl->GetActiveControllerID();
113
    this->MasterId = ctrl->GetMasterController();
114 115
    this->NumberOfClients = ctrl->GetNumberOfControllers();
    this->ClientIds = new int[this->NumberOfClients];
Kitware Robot's avatar
Kitware Robot committed
116 117
    for (int cc = 0; cc < this->NumberOfClients; cc++)
    {
118 119
      this->ClientIds[cc] = ctrl->GetControllerId(cc);
    }
Kitware Robot's avatar
Kitware Robot committed
120
  }
121
  else
Kitware Robot's avatar
Kitware Robot committed
122
  {
123
    this->ClientId = 0;
124
    this->MasterId = 0;
125 126
    this->NumberOfClients = 1;
    this->MultiClientEnable = false;
Kitware Robot's avatar
Kitware Robot committed
127
  }
128 129 130 131 132 133 134 135 136 137
}

//----------------------------------------------------------------------------
// Consider an option added if it is a non-default option that the user
// has probably selected.
void vtkPVMultiClientsInformation::AddInformation(vtkPVInformation* info)
{
  vtkPVMultiClientsInformation* serverInfo;
  serverInfo = vtkPVMultiClientsInformation::SafeDownCast(info);
  if (serverInfo)
Kitware Robot's avatar
Kitware Robot committed
138
  {
139
    if (this->NumberOfClients < serverInfo->NumberOfClients)
Kitware Robot's avatar
Kitware Robot committed
140
    {
141
      this->NumberOfClients = serverInfo->NumberOfClients;
Kitware Robot's avatar
Kitware Robot committed
142
    }
143
    if (this->ClientId < serverInfo->ClientId)
Kitware Robot's avatar
Kitware Robot committed
144
    {
145
      this->ClientId = serverInfo->ClientId;
Kitware Robot's avatar
Kitware Robot committed
146
    }
147
    if (this->MasterId < serverInfo->MasterId)
Kitware Robot's avatar
Kitware Robot committed
148
    {
149
      this->MasterId = serverInfo->MasterId;
Kitware Robot's avatar
Kitware Robot committed
150 151 152
    }
    if (this->ClientIds == NULL && serverInfo->ClientIds)
    {
153
      this->ClientIds = new int[serverInfo->NumberOfClients];
Kitware Robot's avatar
Kitware Robot committed
154 155
      for (int cc = 0; cc < serverInfo->NumberOfClients; cc++)
      {
156 157 158
        this->ClientIds[cc] = serverInfo->ClientIds[cc];
      }
    }
Kitware Robot's avatar
Kitware Robot committed
159
  }
160 161 162 163 164 165 166 167 168
}

//----------------------------------------------------------------------------
void vtkPVMultiClientsInformation::CopyToStream(vtkClientServerStream* css)
{
  css->Reset();
  *css << vtkClientServerStream::Reply;
  *css << this->MultiClientEnable;
  *css << this->ClientId;
169
  *css << this->MasterId;
170
  *css << this->NumberOfClients;
Kitware Robot's avatar
Kitware Robot committed
171 172
  for (int cc = 0; cc < this->NumberOfClients; cc++)
  {
173
    *css << this->GetClientId(cc);
Kitware Robot's avatar
Kitware Robot committed
174
  }
175 176 177 178 179 180
  *css << vtkClientServerStream::End;
}

//----------------------------------------------------------------------------
void vtkPVMultiClientsInformation::CopyFromStream(const vtkClientServerStream* css)
{
Kitware Robot's avatar
Kitware Robot committed
181 182
  if (!css->GetArgument(0, 0, &this->MultiClientEnable))
  {
183 184
    vtkErrorMacro("Error parsing MultiClientEnable from message.");
    return;
Kitware Robot's avatar
Kitware Robot committed
185 186 187
  }
  if (!css->GetArgument(0, 1, &this->ClientId))
  {
188 189
    vtkErrorMacro("Error parsing ClientId from message.");
    return;
Kitware Robot's avatar
Kitware Robot committed
190 191 192
  }
  if (!css->GetArgument(0, 2, &this->MasterId))
  {
193 194
    vtkErrorMacro("Error parsing MasterId from message.");
    return;
Kitware Robot's avatar
Kitware Robot committed
195 196 197
  }
  if (!css->GetArgument(0, 3, &this->NumberOfClients))
  {
198 199
    vtkErrorMacro("Error parsing NumberOfClients from message.");
    return;
Kitware Robot's avatar
Kitware Robot committed
200 201 202 203
  }
  if (this->ClientId != 0)
  {
    if (this->ClientIds)
204 205 206
    {
      delete[] this->ClientIds;
      this->ClientIds = NULL;
Kitware Robot's avatar
Kitware Robot committed
207
    }
208 209
    this->ClientIds = new int[this->NumberOfClients];
    for (int cc = 0; cc < this->NumberOfClients; cc++)
Kitware Robot's avatar
Kitware Robot committed
210 211
    {
      if (!css->GetArgument(0, 4 + cc, &this->ClientIds[cc]))
212 213 214 215 216
      {
        vtkErrorMacro("Error parsing ClientIds from message.");
        return;
      }
    }
Kitware Robot's avatar
Kitware Robot committed
217
  }
218 219 220 221 222 223
}

//----------------------------------------------------------------------------
int vtkPVMultiClientsInformation::GetClientId(int idx)
{
  // Invalid internal data
Kitware Robot's avatar
Kitware Robot committed
224 225
  if (this->ClientIds == NULL || !(idx < this->NumberOfClients && idx >= 0))
  {
226
    return this->ClientId;
Kitware Robot's avatar
Kitware Robot committed
227
  }
228 229 230

  return this->ClientIds[idx];
}