예제 #1
0
파일: testaux.c 프로젝트: 1000copy/bstring
END_TEST

START_TEST(core_012)
{
	struct tagbstring t = bsStatic("Hello world");
	struct bStream * s;
	int ret = 0;
	bstring b, c;
	s = bsFromBstr(&t);
	ck_assert(s != NULL);
	b = bfromcstr("");
	ck_assert(b != NULL);
	ret = bsread(b, s, 6);
	ck_assert_int_eq(ret, 0);
	ret = biseqcstr(b, "Hello ");
	ck_assert_int_eq(ret, 1);
	if (b) {
		b->slen = 0;
	}
	ret = bsread(b, s, 6);
	ck_assert_int_eq(ret, 0);
	ret = biseqcstr(b, "world");
	ck_assert_int_eq(ret, 1);
	c = bsclose(s);
	ck_assert(c == NULL);
	ret = bdestroy(b);
	ck_assert_int_eq(ret, BSTR_OK);
}
예제 #2
0
int gen_keys(DArray * keys, int num_keys)
{
    int i = 0;
    FILE *urand = fopen("/dev/urandom", "r");
    check(urand != NULL, "Failed to open /dev/urandom");
    int result = -1; // default to failure condition
    int rc = 0;

    struct bStream *stream = bsopen((bNread) fread, urand);
    check(stream != NULL, "Failed to open /dev/urandom");

    bstring key = bfromcstr("");

    // FNV1a histogram
    for (i = 0; i < num_keys; i++) {
        rc = bsread(key, stream, BUFFER_LEN);
        check(rc >= 0, "Failed to read from /dev/urandom.");

        DArray_push(keys, bstrcpy(key));
    }

    result = 0; // all good

error: // fallthrough
    if(stream) bsclose(stream);
    if(urand) fclose(urand);
    if(key) bdestroy(key);
    return result;
}
예제 #3
0
int gen_keys(DArray *keys, int num_keys) {
	int i = 0;
	FILE *urand = fopen("/dev/urandom", "r");
	check(urand != NULL, "Failed to open /dev/urandom");

	struct bStream *stream = bsopen((bNread)fread, urand);
	check(stream != NULL, "Failed to open /dev/urandom");

	bstring key = bfromcstr("");
	int rc = 0;

	// FNVla histogram
	for(i = 0; i < num_keys; i++) {
		rc = bsread(key, stream, BUFFER_LEN);
		check(rc >= 0, "Failed to read from /dev/urandom.");

		DArray_push(keys, bstrcpy(key));
	}

	bsclose(stream);
	fclose(urand);
	return 0;

error:
	return -1;
}
예제 #4
0
int test12 (void) {
struct tagbstring t = bsStatic ("Hello world");
struct bStream * s;
bstring b;
int ret = 0;

	printf ("TEST: bsFromBstr.\n");

	ret = bsread (b = bfromcstr (""), s = bsFromBstr (&t), 6);
	ret += 1 != biseqcstr (b, "Hello ");
	if (b) b->slen = 0;
	ret = bsread (b, s, 6);
	ret += 1 != biseqcstr (b, "world");

	bdestroy (b);
	bsclose (s);

	printf ("\t# failures: %d\n", ret);

	return ret;
}
예제 #5
0
static int l_bsread( lua_State* L )
{
  void* bs = lua_touserdata( L, lua_upvalueindex( 1 ) );
  const char* string;
  size_t size;
  
  string = bsread( L, bs, &size );
  
  if ( string )
  {
    lua_pushlstring( L, string, size );
    return 1;
  }
  
  free( bs );
  return 0;
}
예제 #6
0
void brGetPages(int *sockfd,int nrOfServers,struct SiderHederFormat *SiderHeder,struct SiderFormat *Sider, 
	int *pageNr,int alreadynr) {

	int i,n;
	int net_status;

	#ifdef DEBUG_TIME
		struct timeval start_time, end_time;
		gettimeofday(&start_time, NULL);
	#endif

	#ifdef DEBUG
		printf("brGetPages: alreadynr %i, *pageNr %i, nrOfServers %i\n",alreadynr,*pageNr,nrOfServers);
	#endif

	//sejjer om vi har fåt et midlertidig svar på at jobben har begynt
	/****************************************************************/
	for (i=alreadynr;i<nrOfServers+alreadynr;i++) {

		if (sockfd[i] != 0) {

			//motter hedderen for svaret
			if (bsread (&sockfd[i],sizeof(net_status),(char *)&net_status,maxSocketWait_CanDo)) {

				if (net_status != net_CanDo) {
					dprintf("net_status wasen net_CanDo but %i\n",net_status);
					sockfd[i] = 0;
				}
			}
		}			
		
	}
	/****************************************************************/
	#ifdef DEBUG_TIME
		gettimeofday(&end_time, NULL);
		dprintf("Time debug: brGetPages.jobstart pages %f\n",getTimeDifference(&start_time,&end_time));
		gettimeofday(&start_time, NULL);
	#endif


	for (i=alreadynr;i<nrOfServers+alreadynr;i++) {

		//inaliserer til 0 slik at vi ikke tror at vi har noe data fra servere som ikke svarte
		SiderHeder[i].showabal = 0;
	
		//motter hedderen for svaret
		if (sockfd[i] != 0) {

			if (!bsread (&sockfd[i],sizeof(struct SiderHederFormat),(char *)&SiderHeder[i],maxSocketWait_SiderHeder)) {
				dprintf("brGetPages: Failed to bsread SiderHeder");
				continue; // ingen vits i å fortsette å lese fra denne hvis vi ikke fik til å gjøre dette read kallet
			}

#ifdef ATTRIBUTES
			if ((SiderHeder[i].navigation_xml = malloc( sizeof(char) * (SiderHeder[i].navigation_xml_len +1)) ) == NULL) {
				perror("Can't malloc data for navigation_xml");
				continue; // ingen vits å å fortsette mer
			}

			if (!bsread (&sockfd[i], SiderHeder[i].navigation_xml_len, SiderHeder[i].navigation_xml, maxSocketWait_SiderHeder)) {
				dprintf("brGetPages: Failed to bsread navigation_xml");
				continue; // ingen vits å å fortsette mer
			}

			SiderHeder[i].navigation_xml[SiderHeder[i].navigation_xml_len] = '\0';

			#ifdef DEBUG
				printf("\n\nnavigation_xml: %s\n\n", SiderHeder[i].navigation_xml);
			#endif

#endif
		}
	}

	#ifdef DEBUG_TIME
		gettimeofday(&end_time, NULL);
		dprintf("Time debug: brGetPages.reading heder pages %f\n",getTimeDifference(&start_time,&end_time));
		gettimeofday(&start_time, NULL);
	#endif


	for (i=alreadynr;i<nrOfServers+alreadynr;i++) {


			if (sockfd[i] == 0) {
				dprintf("Server nr %i don't have a open socket.",i);
			}
			else {
				int j;

				dprintf("aa: i: %i. Server \"%s\" that has %i pages. Soctet %i\n",
					i,
					SiderHeder[i].servername,
					SiderHeder[i].showabal,
					sockfd[i]);
		


				#ifdef DEBUG
					printf("brGetPages: trying to read %i bytes from server %i\n",sizeof(struct SiderFormat) * SiderHeder[i].showabal,i);
					printf("have %i pages\n",SiderHeder[i].showabal);
				#endif

				for (j = 0; j < SiderHeder[i].showabal; j++) {
					if ((n=bsread(&sockfd[i],sizeof(struct SiderFormat), (char *)&Sider[(*pageNr)],maxSocketWait_SiderHeder))) {
						size_t len;
						int k;
						/* Get urls ... */
						Sider[*pageNr].urls = calloc(Sider[*pageNr].n_urls, sizeof(*(Sider->urls)));
						if (Sider[*pageNr].url == NULL)
							err(1, "calloc(urls)");

						for (k = 0; k < Sider[*pageNr].n_urls; k++) {

							bsread(&sockfd[i], sizeof(len), (char *)&len, maxSocketWait_SiderHeder);
							Sider[*pageNr].urls[k].url = malloc(len+1);
							bsread(&sockfd[i], len, Sider[*pageNr].urls[k].url, maxSocketWait_SiderHeder);
							Sider[*pageNr].urls[k].url[len] = '\0';

							bsread(&sockfd[i], sizeof(len), (char *)&len, maxSocketWait_SiderHeder);
							Sider[*pageNr].urls[k].uri = malloc(len+1);
							bsread(&sockfd[i], len, Sider[*pageNr].urls[k].uri, maxSocketWait_SiderHeder);
							Sider[*pageNr].urls[k].uri[len] = '\0';

							bsread(&sockfd[i], sizeof(len), (char *)&len, maxSocketWait_SiderHeder);
							Sider[*pageNr].urls[k].fulluri = malloc(len+1);
							bsread(&sockfd[i], len, Sider[*pageNr].urls[k].fulluri, maxSocketWait_SiderHeder);
							Sider[*pageNr].urls[k].fulluri[len] = '\0';

							#ifdef DEBUG
								printf("n_urls=%i\n", Sider[*pageNr].n_urls);
								printf("url=\"%s\", uri=\"%s\", fulluri=\"%s\"\n", Sider[*pageNr].urls[k].url, Sider[*pageNr].urls[k].uri, Sider[*pageNr].urls[k].fulluri);
							#endif

						}

						bsread(&sockfd[i], sizeof(len), (char *)&len, maxSocketWait_SiderHeder);
						Sider[*pageNr].attributelen = len;

						Sider[*pageNr].attributes = malloc(len+1);
						bsread(&sockfd[i], len, Sider[*pageNr].attributes, maxSocketWait_SiderHeder);
						Sider[*pageNr].attributes[len] = '\0';

						

						(*pageNr) += 1;
					}
				}

				#ifdef DEBUG
					printf("brGetPages: did read %i element\n",n);
				#endif
			}
	}


	#ifdef DEBUG_TIME
		gettimeofday(&end_time, NULL);
		dprintf("Time debug: brGetPages.reading pages %f\n",getTimeDifference(&start_time,&end_time));
	#endif
}