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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
// vmpi_launch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iphelpers.h"
#include "bitbuf.h"
#include "vmpi.h"
bool g_bBroadcast = false;
int PrintUsage()
{
printf( "vmpi_launch -machine <remote machine> -priority <priority> [-mpi_pw <password>] -command \"command line...\"\n" );
printf( "-command must be the last switch..\n" );
return 1;
}
int GetCurMicrosecondsAndSleep( int sleepLen )
{
Sleep( sleepLen );
int retVal;
__asm
{
rdtsc
mov retVal, eax
}
return retVal;
}
const char* FindArg( int argc, char **argv, const char *pName, const char *pDefault )
{
for ( int i=0; i < argc; i++ )
{
if ( stricmp( argv[i], pName ) == 0 )
{
if ( (i+1) < argc )
return argv[i+1];
else
return pDefault;
}
}
return NULL;
}
int ParseArgs( int argc, char **argv, CIPAddr &remoteIP, int &iPriority, int &iFirstArg )
{
if ( FindArg( argc, argv, "-broadcast", "1" ) )
g_bBroadcast = true;
if ( g_bBroadcast == false )
{
const char *pRemoteIPStr = FindArg( argc, argv, "-machine", NULL );
if ( !pRemoteIPStr || !ConvertStringToIPAddr( pRemoteIPStr, &remoteIP ) )
{
printf( "%s is not a valid machine name or IP address.\n", pRemoteIPStr );
return PrintUsage();
}
}
iPriority = 0;
const char *pPriorityStr = FindArg( argc, argv, "-priority", NULL );
if ( pPriorityStr )
iPriority = atoi( pPriorityStr );
if ( iPriority < 0 || iPriority > 1000 )
{
printf( "%s is not a valid priority.\n", pPriorityStr );
return PrintUsage();
}
const char *pCommand = FindArg( argc, argv, "-command", NULL );
if ( !pCommand )
{
return PrintUsage();
}
for ( iFirstArg=1; iFirstArg < argc; iFirstArg++ )
{
if ( argv[iFirstArg] == pCommand )
break;
}
return 0;
}
void SendJobRequest(
ISocket *pSocket,
int argc,
char **argv,
CIPAddr &remoteIP,
int &iPriority,
int &iFirstArg,
int jobID[4] )
{
// Build the packet to send out the job.
char packetData[4096];
bf_write packetBuf;
// Come up with a unique job ID.
jobID[0] = GetCurMicrosecondsAndSleep( 1 );
jobID[1] = GetCurMicrosecondsAndSleep( 1 );
jobID[2] = GetCurMicrosecondsAndSleep( 1 );
jobID[3] = GetCurMicrosecondsAndSleep( 1 );
// Broadcast out to tell all the machines we want workers.
packetBuf.StartWriting( packetData, sizeof( packetData ) );
packetBuf.WriteByte( VMPI_PROTOCOL_VERSION );
const char *pPassword = FindArg( argc, argv, "-mpi_pw", "" );
packetBuf.WriteString( pPassword );
packetBuf.WriteByte( VMPI_LOOKING_FOR_WORKERS );
packetBuf.WriteShort( 0 ); // Tell the port that we're listening on.
// In this case, there is no VMPI master waiting for the app to connect, so
// this parameter doesn't matter.
packetBuf.WriteShort( iPriority );
packetBuf.WriteLong( jobID[0] );
packetBuf.WriteLong( jobID[1] );
packetBuf.WriteLong( jobID[2] );
packetBuf.WriteLong( jobID[3] );
packetBuf.WriteWord( argc-iFirstArg ); // 1 command line argument..
// Write the alternate exe name.
for ( int iArg=iFirstArg; iArg < argc; iArg++ )
packetBuf.WriteString( argv[iArg] );
for ( int iBroadcastPort=VMPI_SERVICE_PORT; iBroadcastPort <= VMPI_LAST_SERVICE_PORT; iBroadcastPort++ )
{
remoteIP.port = iBroadcastPort;
if ( g_bBroadcast == false )
pSocket->SendTo( &remoteIP, packetBuf.GetBasePointer(), packetBuf.GetNumBytesWritten() );
else
pSocket->Broadcast( packetBuf.GetBasePointer(), packetBuf.GetNumBytesWritten(), iBroadcastPort );
}
if ( g_bBroadcast == false )
printf( "Sent command, waiting for reply...\n" );
else
printf( "Sent command\n" );
}
bool WaitForJobStart( ISocket *pSocket, const CIPAddr &remoteIP, const int jobID[4] )
{
while ( 1 )
{
CIPAddr senderAddr;
char data[4096];
int len = -1;
if ( g_bBroadcast == false )
pSocket->RecvFrom( data, sizeof( data ), &senderAddr );
else
pSocket->RecvFrom( data, sizeof( data ), NULL );
if ( len == 19 &&
memcmp( senderAddr.ip, remoteIP.ip, sizeof( senderAddr.ip ) ) == 0 &&
data[1] == VMPI_NOTIFY_START_STATUS &&
memcmp( &data[2], jobID, 16 ) == 0 )
{
if ( data[18] == 0 )
{
// Wasn't able to run.
printf( "Wasn't able to run on target machine.\n" );
return false;
}
else
{
// Ok, the process is running now.
printf( "Process running, waiting for completion...\n" );
return true;
}
}
Sleep( 100 );
}
}
void WaitForJobEnd( ISocket *pSocket, const CIPAddr &remoteIP, const int jobID[4] )
{
while ( 1 )
{
CIPAddr senderAddr;
char data[4096];
int len = pSocket->RecvFrom( data, sizeof( data ), &senderAddr );
if ( len == 18 &&
memcmp( senderAddr.ip, remoteIP.ip, sizeof( senderAddr.ip ) ) == 0 &&
data[1] == VMPI_NOTIFY_END_STATUS &&
memcmp( &data[2], jobID, 16 ) == 0 )
{
int ret = *((int*)&data[2]);
printf( "Finished [%d].\n", ret );
break;
}
Sleep( 100 );
}
}
int main(int argc, char* argv[])
{
if ( argc < 4 )
{
return PrintUsage();
}
// Parse the command line.
CIPAddr remoteIP;
int iFirstArg, iPriority;
int jobID[4];
int ret = ParseArgs( argc, argv, remoteIP, iPriority, iFirstArg );
if ( ret != 0 )
return ret;
// Now send the command to the vmpi service on that machine.
ISocket *pSocket = CreateIPSocket();
if ( !pSocket->BindToAny( 0 ) )
{
printf( "Error binding a socket.\n" );
return 1;
}
SendJobRequest( pSocket, argc, argv, remoteIP, iPriority, iFirstArg, jobID );
// Wait for a reply, positive or negative.
if ( g_bBroadcast == false )
{
if ( !WaitForJobStart( pSocket, remoteIP, jobID ) )
return 2;
WaitForJobEnd( pSocket, remoteIP, jobID );
}
pSocket->Release();
return 0;
}
|