Ejemplo n.º 1
0
int syncHostnameFiles()
{

    //open current directory and send all files to all the connected peers
    char *directory = "./";
    char *filename = stringConcat(myHostName, ".txt");

    //send file for all peers in connectionList
    struct list *listIterator = connectionList;
    struct host *destination;
    while (listIterator != NULL) {
        destination = (struct host *) listIterator->value;
        //if destination is the master server do not send
        if (masterServer != NULL && destination->sockfd == masterServer->sockfd) {
            listIterator = listIterator->next;
            continue;
        }
        filename = stringConcat(directory, filename);
        putFile(destination->id, filename);
        listIterator = listIterator->next;
    }
    //printf("Sent file: %s\n", filename);

    return 0;
}
Ejemplo n.º 2
0
char* concatSansStringH(char* str1, char* str2) {
	/****
	*	Retourne la concatenation str1 et str2, la plus petite est en premier
	*	SANS <string.h>
	*****/

	// Test de longueur pour savoir quel str sera en premier
	if (stringSize(str1) > stringSize(str2)) {
		return stringConcat(str2,str1);
	} else {
		return stringConcat(str1,str2);
	}
}
Ejemplo n.º 3
0
static bool
_predicate (void)
{
	bool fnval = true;
	uint32_t ints[TupleLen];
	TupleTuple tuples[TupleLen];

	int i;
	for (i = 0; i < TupleLen; i++) {
		ints[i] = i;
	}

	String *intermediate = stringAllocateEmpty(sizeof (ints[0]) * TupleLen);
	stringConcat (intermediate, ints, sizeof (ints[0]) * TupleLen);

	for (i = 0; i < TupleLen; i++) {
		tuples[i] = intermediate;
	}

	String *in = stringAllocateEmpty(sizeof (tuples[0]) * TupleLen), *out;
	stringConcat (in, tuples, sizeof (tuples[0]) * TupleLen);

	uint8_t buf[1024 * 1024];
	EtnBufferEncoder *e = etnBufferEncoderNew(buf, sizeof(buf));
	EtnLength etnEncodedSize;
	etnEncode((EtnEncoder *) e, EtnToValue(&TupleTupleType, &in), &etnEncodedSize);

	EtnBufferDecoder *d = etnBufferDecoderNew(buf, etnEncodedSize);
	etnDecode((EtnDecoder *) d, EtnToValue(&TupleTupleType, &out));

	int j;
	for (i = 0; i < TupleLen; i++) {
		for (j = 0; j < TupleLen; j++) {
			if (j != ((uint32_t *) ((String **) out->ptr)[i]->ptr)[j]) {
				fnval = false;
				goto done;
			}
		}
	}

done:
	free (e);
	etnBufferDecoderFree (d);
	stringUnhook (&in);
	stringUnhook (&out);

	return fnval;
}
int main(void)
{
	str_t str1,str2;

	str1=stringCrea(20,"hello ");
	str2=stringCrea(20,"world");
	stringPrint(str1);
	putchar('\n');
	printf("stringLeng:%d\n",stringLeng(str1));

	switch(stringComp(str1,str2)){
		case 0:puts("str1 = str2");break;
		case 1:puts("str1 > str2");break;
		case -1:puts("str1 < str2");break;
	}

	stringConcat(str1,str2);
	stringPrint(str1);
	putchar('\n');

	stringCopy(str1,str2);
	stringPrint(str1);
	putchar('\n');

	return(0);
}
Ejemplo n.º 5
0
int syncAll() {
    //open current directory and send all files to all the connected peers
    DIR *d;
    struct dirent *dir;
    char *directory = "./";
    char *filename = "";
    d = opendir(directory);
    if (d != NULL) {
        while ((dir = readdir(d)) != NULL) {
            //for each file
            if (dir->d_type == DT_REG) {
                //for all peers in connectionList
                struct list *listIterator = connectionList;
                struct host *destination;
                while (listIterator != NULL) {
                    destination = (struct host *) listIterator->value;
                    //if destination is the master server do not send
                    if (masterServer != NULL && destination->sockfd == masterServer->sockfd) {
                        listIterator = listIterator->next;
                        continue;
                    }
                    filename = stringConcat(directory, dir->d_name);
                    putFile(destination->id, filename);
                    listIterator = listIterator->next;
                }
                printf("Sent file: %s\n", filename);
            }
        }
        closedir(d);
    }
    else {
        printf("Unable to open directory.\n");
    }
    return 0;
}
Ejemplo n.º 6
0
int receiveFileAsynchronously(int connectionId, struct packet *recvPacket)
{
    struct list *connection = getNodeByID(connectionList, connectionId);
    struct host *source = (struct host *) connection->value;

    if (connection->filePointer == NULL) {
        printf("Started receiving file: %s at %s from %s/%s.\n$",
               recvPacket->header->fileName, printCurrentTime(), source->hostName, source->port);
        fflush(stdout);
        // Create file where data will be stored
        char *filename = recvPacket->header->fileName;
        char *tempfilename = stringConcat("./", filename);
        FILE *fp = fopen(tempfilename, "wb"); //for testing it should be filename
        if (fp == NULL) {
            printf("Error opening file.\n");
            deletePacketAndMessage(recvPacket);
            return -1;
        }

        //update the File pointer to the connection
        connection->filePointer = fp;

        //write the packet received
        int written_bytes = fwrite(recvPacket->message, 1, recvPacket->header->length, connection->filePointer);
        deletePacketAndMessage(recvPacket);

    }
    else {
        //write the packet received
        int written_bytes = fwrite(recvPacket->message, 1, recvPacket->header->length, connection->filePointer);
        deletePacketAndMessage(recvPacket);
    }
    return 0;
}
Ejemplo n.º 7
0
/*
 * Converts a float to a string.
 */
void stringFTOA(float value, char* out ) {

	int neg = (value < 0);
	if(neg)
		value *= -1;
	int integer = (int) value;
	stringITOA(integer,out);
	if(neg) {
		char buffer[20];
		stringCopy(out, buffer);
		stringConcat("-",buffer,out);
	}
    char* pt = ".";
	stringConcat(out,pt,out);
	char curr[6];
	stringITOA((int)((value - (int)value)*10000),curr);
	stringConcat(out,curr,out);

}
Ejemplo n.º 8
0
static bool
_predicate (void)
{
	bool fnval;
	Strings in1, out1;
	StructContainingTuple in2, out2;

	in1.field1 = stringAllocateInitialize("Hello, world!");
	in1.field2 = stringAllocateInitialize("Goodbye, cruel world!");

	in2.strings = stringAllocateEmpty(sizeof (Strings));
	stringConcat (in2.strings, &in1, sizeof (Strings));

	uint8_t buf[1024];
	EtnBufferEncoder *e = etnBufferEncoderNew(buf, sizeof(buf));
	EtnLength etnEncodedSize;
	etnEncode((EtnEncoder *) e, EtnToValue(&StructContainingTupleType, &in2), &etnEncodedSize);

	EtnBufferDecoder *d = etnBufferDecoderNew(buf, etnEncodedSize);
	etnDecode((EtnDecoder *) d, EtnToValue(&StructContainingTupleType, &out2));

	out1 = *(Strings *) out2.strings->ptr;

	fnval = ! stringCompare (in1.field1, out1.field1)
	     && ! stringCompare (in1.field2, out1.field2);
	
	free (e);
	etnBufferDecoderFree (d);
	stringUnhook (&in1.field1);
	stringUnhook (&in1.field2);
	stringUnhook (&in2.strings);
	stringUnhook (&out1.field1);
	stringUnhook (&out1.field2);
	stringUnhook (&out2.strings);

	return fnval;
}
Ejemplo n.º 9
0
//--------
// Inline functions
//--------
inline char * stringCopy(const char * s1) { return stringConcat(s1, ""); }
Ejemplo n.º 10
0
bool
Config2Cpp::generateFiles(
	const char * const *	schema,
	int						schemaSize)
{
	char *					msg;
	char *					cppFileName;
	char *					hFileName;
	FILE *					cfgFile;
	FILE *					cppFile;
	FILE *					hFile;

	cppFileName = stringConcat(m_className, m_cppExt);
	hFileName   = stringConcat(m_className, m_hExt);

	//--------
	// Open all the files
	//--------
	cfgFile = fopen(m_cfgFileName, "r");
	if (cfgFile == 0) {
		msg = stringConcat("cannot open '", m_cfgFileName, "'");
		perror(msg);
		delete [] msg;
		delete [] cppFileName;
		delete [] hFileName;
		return false;
	}
	cppFile = fopen(cppFileName, "w");
	if (cppFile == 0) {
		msg = stringConcat("cannot open '", cppFileName, "'");
		perror(msg);
		delete [] msg;
		delete [] cppFileName;
		delete [] hFileName;
		fclose(cfgFile);
		return false;
	}
	hFile = fopen(hFileName, "w");
	if (hFile == 0) {
		msg = stringConcat("cannot open '", hFileName, "'");
		perror(msg);
		delete [] msg;
		delete [] cppFileName;
		delete [] hFileName;
		fclose(cfgFile);
		fclose(cppFile);
		return false;
	}

	//--------
	// Generate the ".h" and ".cpp" files.
	//--------
	printToHeaderFile(hFile, schemaSize);
	printToCppFile(cfgFile, cppFile, schema, schemaSize);

	//--------
	// Tidy up
	//--------
	delete [] cppFileName;
	delete [] hFileName;
	fclose(cfgFile);
	fclose(cppFile);
	fclose(hFile);

	return true;
}