Skip to content

Commit

Permalink
Compatibility with maven 3.3, trailing whitespace
Browse files Browse the repository at this point in the history
Requires Java 1.6+
Optional $CC $PREFIX
  • Loading branch information
denji committed Jul 21, 2015
1 parent 7b53e34 commit 7a1d12a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 127 deletions.
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
# MinGW32 to cross-compile for windows. To install and
# configure MinGW32 on linux, see http://www.mingw.org


# This is where the mingw32 compiler exists in Ubuntu 8.04.
# Your compiler location may vary.
WIN32_CC=/usr/bin/i586-mingw32msvc-gcc

CC=gcc
CC?=gcc
CFLAGS=-Wall -pedantic -s -O3
SRCDIR=nailgun-client
PREFIX=/usr/local
PREFIX?=/usr/local

ng: ${SRCDIR}/ng.c
@echo "Building ng client. To build a Windows binary, type 'make ng.exe'"
Expand All @@ -19,12 +18,12 @@ ng: ${SRCDIR}/ng.c
install: ng
install -d ${PREFIX}/bin
install ng ${PREFIX}/bin

ng.exe: ${SRCDIR}/ng.c
${WIN32_CC} -o ng.exe ${SRCDIR}/ng.c -lwsock32 -O3 ${CFLAGS}
# any idea why the command line is so sensitive to the order of
# the arguments? If CFLAGS is at the beginning, it won't link.

clean:
@echo ""
@echo "If you have a Windows binary, 'make clean' won't delete it."
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ nailgun
Nailgun is a client, protocol, and server for running Java programs from
the command line without incurring the JVM startup overhead.

Programs run in the server (which is implemented in Java), and are
Programs run in the server (which is implemented in Java), and are
triggered by the client (written in C), which handles all I/O.

The server and examples are built using maven. From the project directory,
"mvn clean install" will do it.

The client is built using make. From the project directory,
The client is built using make. From the project directory,
"make && sudo make install" will do it. To create the windows client
you will additionally need to "make ng.exe".

Expand Down
100 changes: 47 additions & 53 deletions nailgun-client/ng.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
/*
Copyright 2004-2012, Martian Software, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,7 +12,6 @@
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.
*/

/**
Expand Down Expand Up @@ -158,9 +156,9 @@ void handleError () {
LPVOID lpMsgBuf;
int error = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
Expand All @@ -174,7 +172,7 @@ void handleError () {

/* Free the buffer. */
LocalFree( lpMsgBuf );

cleanUpAndExit(error);
}
#endif
Expand Down Expand Up @@ -203,22 +201,22 @@ int sendAll(SOCKET s, char *buf, int len) {

while(total < len) {
n = send(s, buf+total, bytesleft, SEND_FLAGS);
if (n == -1) {

if (n == -1) {
break;
}

total += n;
bytesleft -= n;
}

return n==-1 ? 0:total;
return n==-1 ? 0:total;
}

/**
* Sends a chunk noting the specified payload size and chunk type.
* Waits for sending mutex on Win32.
*
*
* @param size the payload size
* @param chunkType the chunk type identifier
*/
Expand Down Expand Up @@ -264,10 +262,10 @@ void sendChunk(unsigned int size, char chunkType, char* buf) {
*/
int sendFileArg(char *filename) {
int i, f;

if ((f = open(filename, O_RDONLY)) < 0) {
perror("--nailgun-filearg");
return 1;
return 1;
}

i = read(f, buf, BUFSIZE);
Expand All @@ -280,7 +278,7 @@ int sendFileArg(char *filename) {
return 1;
}
sendChunk(0, CHUNKTYPE_LONGARG, buf);

close(f);
return 0;
}
Expand All @@ -306,12 +304,12 @@ void sendText(char chunkType, char *text) {
void recvToFD(HANDLE destFD, char *buf, unsigned long len) {
unsigned long bytesRead = 0;
int bytesCopied;

while (bytesRead < len) {
unsigned long bytesRemaining = len - bytesRead;
int bytesToRead = (BUFSIZE < bytesRemaining) ? BUFSIZE : bytesRemaining;
int thisPass = 0;

thisPass = recv(nailgunsocket, buf, bytesToRead, MSG_WAITALL);
if (thisPass == 0 || thisPass == -1) {
perror("recv");
Expand Down Expand Up @@ -341,7 +339,7 @@ void recvToFD(HANDLE destFD, char *buf, unsigned long len) {
}
bytesCopied += bytesWritten;
#endif
}
}
}
}

Expand Down Expand Up @@ -369,15 +367,13 @@ void processExit(char *buf, unsigned long len) {
int exitcode;
int bytesToRead = (BUFSIZE - 1 < len) ? BUFSIZE - 1 : len;
int bytesRead = recvToBuffer(bytesToRead);

if (bytesRead < 0) {
handleSocketClose();
}

buf[bytesRead] = 0;

exitcode = atoi(buf);

cleanUpAndExit(exitcode);
}

Expand Down Expand Up @@ -419,7 +415,6 @@ HANDLE createEvent(BOOL manualReset) {
}

DWORD WINAPI sendHeartbeats(LPVOID lpParameter) {

/* this could be made more efficient by only sending heartbeats when stdin chunks aren't being sent */
for (;;) {
Sleep(HEARTBEAT_TIMEOUT_MILLIS);
Expand Down Expand Up @@ -486,7 +481,7 @@ int processStdin() {
*/
void initSockets () {
WSADATA win_socket_data; /* required to initialise winsock */

WSAStartup(2, &win_socket_data);

/* create flow control event and mutex */
Expand All @@ -502,7 +497,7 @@ void initSockets () {
void initIo () {
/* create non-blocking console io */
AllocConsole();

NG_STDIN_FILENO = GetStdHandle(STD_INPUT_HANDLE);
NG_STDOUT_FILENO = GetStdHandle(STD_OUTPUT_HANDLE);
NG_STDERR_FILENO = GetStdHandle(STD_ERROR_HANDLE);
Expand All @@ -520,7 +515,7 @@ void winStartInput () {
securityAttributes.bInheritHandle = TRUE;
securityAttributes.lpSecurityDescriptor = NULL;
securityAttributes.nLength = 0;

if (!CreateThread(&securityAttributes, 0, &processStdin, NULL, 0, &threadId)) {
handleError();
}
Expand All @@ -546,9 +541,9 @@ void processnailgunstream() {
| ((buf[1] << 16) & 0x00ff0000)
| ((buf[2] << 8) & 0x0000ff00)
| ((buf[3]) & 0x000000ff);

chunkType = buf[4];

switch(chunkType) {
case CHUNKTYPE_STDOUT: recvToFD(NG_STDOUT_FILENO, buf, len);
break;
Expand Down Expand Up @@ -576,7 +571,7 @@ void processnailgunstream() {
*/
int intervalMillis(struct timeval end, struct timeval start) {

return ((end.tv_sec - start.tv_sec) * 1000) +
return ((end.tv_sec - start.tv_sec) * 1000) +
((end.tv_usec - start.tv_usec) /1000);
}

Expand Down Expand Up @@ -630,7 +625,7 @@ void usage(int exitcode) {
fprintf(stderr, " (to execute an aliased class, where \"alias\"\n");
fprintf(stderr, " is both the alias for the class and a symbolic\n");
fprintf(stderr, " link to the ng client)\n\n");

fprintf(stderr, "where options include:\n");
fprintf(stderr, " --nailgun-D<name>=<value> set/override a client environment variable\n");
fprintf(stderr, " --nailgun-version print product version and exit\n");
Expand All @@ -640,7 +635,7 @@ void usage(int exitcode) {
fprintf(stderr, " if set, otherwise localhost)\n");
fprintf(stderr, " --nailgun-port to specify the port of the nailgun server\n");
fprintf(stderr, " (default is NAILGUN_PORT environment variable\n");
fprintf(stderr, " if set, otherwise 2113)\n");
fprintf(stderr, " if set, otherwise 2113)\n");
fprintf(stderr, " --nailgun-filearg FILE places the entire contents of FILE into the\n");
fprintf(stderr, " next argument, which is interpreted as a string\n");
fprintf(stderr, " using the server's default character set. May be\n");
Expand Down Expand Up @@ -673,19 +668,19 @@ int main(int argc, char *argv[], char *env[]) {
#ifdef WIN32
initSockets();
#endif

/* start with environment variable. default to localhost if not defined. */
nailgun_server = getenv("NAILGUN_SERVER");
if (nailgun_server == NULL) {
nailgun_server = "127.0.0.1";
}

/* start with environment variable. default to normal nailgun port if not defined */
nailgun_port = getenv("NAILGUN_PORT");
if (nailgun_port == NULL) {
nailgun_port = NAILGUN_PORT_DEFAULT;
}

/* look at the command used to launch this program. if it was "ng", then the actual
command to issue to the server must be specified as another argument. if it
wasn't ng, assume that the desired command name was symlinked to ng in the user's
Expand All @@ -700,14 +695,14 @@ int main(int argc, char *argv[], char *env[]) {
display usage and exit. Don't handle -h|--help if a command other than
ng or ng.exe was used, since the appropriate nail should then handle
--help. */
if (cmd == NULL &&
(argc == 1 ||
if (cmd == NULL &&
(argc == 1 ||
(argc == 2 && strcmp("--help", argv[1]) == 0) ||
(argc == 2 && strcmp("-h", argv[1]) == 0))) usage(0);

firstArgIndex = 1;

/* quite possibly the lamest commandline parsing ever.
/* quite possibly the lamest commandline parsing ever.
look for the two args we care about (--nailgun-server and
--nailgun-port) and NULL them and their parameters after
reading them if found. later, when we send args to the
Expand Down Expand Up @@ -745,34 +740,34 @@ int main(int argc, char *argv[], char *env[]) {
if (cmd == NULL) {
usage(NAILGUN_BAD_ARGUMENTS);
}
/* jump through a series of connection hoops */

/* jump through a series of connection hoops */
hostinfo = gethostbyname(nailgun_server);

if (hostinfo == NULL) {
fprintf(stderr, "Unknown host: %s\n", nailgun_server);
cleanUpAndExit(NAILGUN_CONNECT_FAILED);
}

port = atoi(nailgun_port);

if ((nailgunsocket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
cleanUpAndExit(NAILGUN_SOCKET_FAILED);
}

server_addr.sin_family = AF_INET;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *(struct in_addr *) hostinfo->h_addr;

memset(&(server_addr.sin_zero), '\0', 8);

if (connect(nailgunsocket, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1) {
perror("connect");
cleanUpAndExit(NAILGUN_CONNECT_FAILED);
}
}

/* ok, now we're connected. first send all of the command line
arguments for the server, if any. remember that we may have
marked some arguments NULL if we read them to specify the
Expand All @@ -789,7 +784,7 @@ int main(int argc, char *argv[], char *env[]) {
}
}

/* now send environment */
/* now send environment */
sendText(CHUNKTYPE_ENV, NAILGUN_FILESEPARATOR);
sendText(CHUNKTYPE_ENV, NAILGUN_PATHSEPARATOR);
#ifndef WIN32
Expand All @@ -803,24 +798,23 @@ int main(int argc, char *argv[], char *env[]) {
for(i = 0; env[i]; ++i) {
sendText(CHUNKTYPE_ENV, env[i]);
}

/* now send the working directory */
cwd = getcwd(NULL, 0);
sendText(CHUNKTYPE_DIR, cwd);
free(cwd);

/* and finally send the command. this marks the point at which
streams are linked between client and server. */
sendText(CHUNKTYPE_CMD, cmd);


/* initialise the std-* handles and the thread to send stdin to the server */
/* initialise the std-* handles and the thread to send stdin to the server */
#ifdef WIN32
initIo();
winStartInput();
#endif

/* stream forwarding loop */
/* stream forwarding loop */
while(1) {
#ifndef WIN32
FD_ZERO(&readfds);
Expand All @@ -837,7 +831,7 @@ int main(int argc, char *argv[], char *env[]) {
if(select (nailgunsocket + 1, &readfds, NULL, NULL, &readtimeout) == -1) {
perror("select");
}

if (FD_ISSET(nailgunsocket, &readfds)) {
#endif
processnailgunstream();
Expand All @@ -857,7 +851,7 @@ int main(int argc, char *argv[], char *env[]) {
sendHeartbeat();
}
#endif
}
}

/* normal termination is triggered by the server, and so occurs in processExit(), above */
}
Loading

0 comments on commit 7a1d12a

Please sign in to comment.