1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkSocketController.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkSocketController.h"
16 
17 #include "vtkObjectFactory.h"
18 #include "vtkProcessGroup.h"
19 #include "vtkSocketCommunicator.h"
20 
21 #if defined(_WIN32) && !defined(__CYGWIN__)
22 # define VTK_WINDOWS_FULL
23 # include "vtkWindows.h"
24 # define WSA_VERSION MAKEWORD(1,1)
25 #endif
26 
27 int vtkSocketController::Initialized = 0;
28 
29 vtkStandardNewMacro(vtkSocketController);
30 
31 //----------------------------------------------------------------------------
vtkSocketController()32 vtkSocketController::vtkSocketController()
33 {
34   this->Communicator = vtkSocketCommunicator::New();
35   this->RMICommunicator = this->Communicator;
36 }
37 
38 //----------------------------------------------------------------------------
~vtkSocketController()39 vtkSocketController::~vtkSocketController()
40 {
41   this->Communicator->Delete();
42   this->Communicator = this->RMICommunicator = 0;
43 }
44 
45 //----------------------------------------------------------------------------
Initialize(int *,char ***)46 void vtkSocketController::Initialize(int* , char***)
47 {
48   if (vtkSocketController::Initialized)
49     {
50     vtkWarningMacro("Already initialized.");
51     return;
52     }
53 
54 #if defined(_WIN32) && !defined(__CYGWIN__)
55   WSAData wsaData;
56   if (WSAStartup(WSA_VERSION, &wsaData))
57     {
58     vtkErrorMacro("Could not initialize sockets !");
59     }
60 #endif
61   vtkSocketController::Initialized = 1;
62 
63 }
64 
65 //----------------------------------------------------------------------------
SetCommunicator(vtkSocketCommunicator * comm)66 void vtkSocketController::SetCommunicator(vtkSocketCommunicator* comm)
67 {
68   if (comm == this->Communicator)
69     {
70     return;
71     }
72   if (this->Communicator)
73     {
74     this->Communicator->UnRegister(this);
75     }
76   this->Communicator = comm;
77   this->RMICommunicator = comm;
78   if (comm)
79     {
80     comm->Register(this);
81     }
82 }
83 
84 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)85 void vtkSocketController::PrintSelf(ostream& os, vtkIndent indent)
86 {
87   this->Superclass::PrintSelf(os,indent);
88 }
89 
90 
91 //----------------------------------------------------------------------------
WaitForConnection(int port)92 int vtkSocketController::WaitForConnection(int port)
93 {
94   return vtkSocketCommunicator::SafeDownCast(this->Communicator)->
95     WaitForConnection(port);
96 }
97 
98 //----------------------------------------------------------------------------
CloseConnection()99 void vtkSocketController::CloseConnection()
100 {
101   vtkSocketCommunicator::SafeDownCast(this->Communicator)->
102     CloseConnection();
103 }
104 
105 //----------------------------------------------------------------------------
ConnectTo(const char * hostName,int port)106 int vtkSocketController::ConnectTo(const char* hostName, int port )
107 {
108   return vtkSocketCommunicator::SafeDownCast(this->Communicator)->
109     ConnectTo(hostName, port);
110 }
111 
112 //----------------------------------------------------------------------------
GetSwapBytesInReceivedData()113 int vtkSocketController::GetSwapBytesInReceivedData()
114 {
115   return vtkSocketCommunicator::SafeDownCast(this->Communicator)->
116     GetSwapBytesInReceivedData();
117 }
118 
119 //-----------------------------------------------------------------------------
CreateCompliantController()120 vtkMultiProcessController *vtkSocketController::CreateCompliantController()
121 {
122   vtkProcessGroup *group = vtkProcessGroup::New();
123   group->Initialize(this->Communicator);
124   group->RemoveAllProcessIds();
125 
126   // This hack creates sub controllers with differing orders of the processes
127   // that will map the ids to be unique on each process.
128   if (vtkSocketCommunicator::SafeDownCast(this->Communicator)->GetIsServer())
129     {
130     group->AddProcessId(1);
131     group->AddProcessId(0);
132     }
133   else
134     {
135     group->AddProcessId(0);
136     group->AddProcessId(1);
137     }
138 
139   vtkMultiProcessController *compliantController
140     = this->CreateSubController(group);
141 
142   group->Delete();
143 
144   return compliantController;
145 }
146