Ejemplo n.º 1
0
static INode *SetUpArguments(KonohaContext *kctx, FuelIRBuilder *builder, kMethod *mtd)
{
	kParam *params = kMethod_GetParam(mtd);
	unsigned i;
	INode *Arg;
	INode *Val;
	INode *Arg0 = NULL;
	if(mtd->typeId != TYPE_void) {
		Arg = CreateArgument(builder, 0);
		Val = CreateLocal(builder, mtd->typeId);
		CreateUpdate(builder, Val, Arg);
		INode_setType(Arg, mtd->typeId);
		IField_setHash((IField *) Val, 0);
		Arg0 = Val;
	}
	for(i = 0; i < params->psize; ++i) {
		enum TypeId type = ConvertToTypeId(kctx, params->paramtypeItems[i].attrTypeId);
		Arg = CreateArgument(builder, i+1);
		Val = CreateLocal(builder, type);
		CreateUpdate(builder, Val, Arg);
		INode_setType(Arg, type);
		IField_setHash((IField *) Val, i+1);
	}
	return Arg0;
}
Ejemplo n.º 2
0
static kbool_t FuelVM_VisitAssignNode(KonohaContext *kctx, KBuilder *builder, kNode *expr, void *thunk)
{
	/*
	 * [LetExpr] := lhs = rhs
	 * expr->NodeList = [NULL, lhs, rhs]
	 **/

	kNode *left = kNode_At(expr, 1);
	kNode *right = kNode_At(expr, 2);
	INode *Node;
	FuelIRBuilder *flbuilder = BLD(builder);
	if(left->node == KNode_Local) {
		enum TypeId type = ConvertToTypeId(kctx, left->attrTypeId);
		if((Node = IRBuilder_FindLocalVarByHash(flbuilder, type, left->index)) == 0) {
			Node = CreateLocal(flbuilder, type);
			IField_setHash((IField *) Node, left->index);
		}
		SUGAR VisitNode(kctx, builder, right, thunk);
		INode *RHS = FuelVM_getExpression(builder);
		//if(RHS->Type != Node->Type) {
		//	INode_setType(Node, RHS->Type);
		//}
		CreateUpdate(flbuilder, Node, RHS);
	}
	//else if(left->node == TEXPR_STACKTOP) {
	//	enum TypeId type = ConvertToTypeId(kctx, left->attrTypeId);
	//	uintptr_t Hash = (uintptr_t) left;
	//	if((Node = IRBuilder_FindLocalVarByHash(flbuilder, type, Hash)) == 0) {
	//		Node = CreateLocal(flbuilder, type);
	//		IField_setHash((IField *) Node, Hash);
	//		ARRAY_add(INodePtr, &BLD(builder)->Stack, Node);
	//	}
	//	SUGAR VisitNode(kctx, builder, right, thunk);
	//	INode *RHS = FuelVM_getExpression(builder);
	//	if(RHS->Type != Node->Type)
	//		INode_setType(Node, RHS->Type);
	//	CreateUpdate(BLD(builder), Node, RHS);
	//}
	else{
		assert(left->node == KNode_Field);
		SUGAR VisitNode(kctx, builder, right, thunk);
		kshort_t index  = (kshort_t)left->index;
		kshort_t xindex = (kshort_t)(left->index >> (sizeof(kshort_t)*8));

		INode *Left;
		if((Left = IRBuilder_FindLocalVarByHash(BLD(builder), TYPE_Object, index)) == 0) {
			Left = CreateLocal(BLD(builder), TYPE_Object);
			IField_setHash((IField *) Left, index);
		}
		enum TypeId type = ConvertToTypeId(kctx, left->attrTypeId);
		Node = CreateField(BLD(builder), FieldScope, type, Left, xindex);
		SUGAR VisitNode(kctx, builder, right, thunk);
		CreateUpdate(BLD(builder), Node, FuelVM_getExpression(builder));
	}
	builder->Value = Node;
	return true;
}
Ejemplo n.º 3
0
zx_status_t MerkleTree::CreateFinalInternal(const void* data, void* tree, Digest* root) {
    zx_status_t rc;
    // Must call CreateInit first.  Must call CreateUpdate with all data first.
    if (!initialized_ || (level_ == 0 && offset_ != length_)) {
        return ZX_ERR_BAD_STATE;
    }
    // Must have root to write and a tree to fill if expecting more than one
    // digest.
    if (!root || (!tree && length_ > kNodeSize)) {
        return ZX_ERR_INVALID_ARGS;
    }
    // Special case: the level is empty.
    if (length_ == 0) {
        if ((rc = DigestInit(&digest_, 0, 0)) != ZX_OK) {
            return rc;
        }
        DigestFinal(&digest_, 0);
    }
    // Consume padding if needed.
    const uint8_t* tail = static_cast<const uint8_t*>(data) + offset_;
    if ((rc = CreateUpdate(tail, length_ - offset_, tree)) != ZX_OK) {
        return rc;
    }
    initialized_ = false;
    // If the top, save the digest as the Merkle tree root and return.
    if (length_ <= kNodeSize) {
        *root = digest_.AcquireBytes();
        digest_.ReleaseBytes();
        return ZX_OK;
    }
    // Finalize the next level up.
    uint8_t* next = static_cast<uint8_t*>(tree) + NextAligned(length_);
    return next_->CreateFinalInternal(tree, next, root);
}
Ejemplo n.º 4
0
static void CreateCond(KonohaContext *kctx, KBuilder *builder, kNode *expr, enum ConditionalOp Op, void *thunk)
{
	kNode *LHS = kNode_At(expr, 1);
	kNode *RHS = kNode_At(expr, 2);

	Block *HeadBB  = CreateBlock(BLD(builder));
	Block *ThenBB  = CreateBlock(BLD(builder));
	Block *MergeBB = CreateBlock(BLD(builder));

	/* [CondExpr]
	 * LogicalAnd case
	 *       | goto Head
	 * Head  | let bval = LHS
	 *       | if(bval) { goto Then } else { goto Merge }
	 * Then  | bval = RHS
	 *       | goto Merge
	 * Merge | ...
	 */

	INode *Node;
	IRBuilder_JumpTo(BLD(builder), HeadBB);
	{ /* Head */
		IRBuilder_setBlock(BLD(builder), HeadBB);
		Node = CreateLocal(BLD(builder), TYPE_boolean);
		SUGAR VisitNode(kctx, builder, LHS, thunk);
		INode *Left = FuelVM_getExpression(builder);
		CreateUpdate(BLD(builder), Node, Left);

		if(Op == LogicalAnd) {
			CreateBranch(BLD(builder), Left, ThenBB, MergeBB);
		} else {
			CreateBranch(BLD(builder), Left, MergeBB, ThenBB);
		}
	}
	{ /* Then */
		IRBuilder_setBlock(BLD(builder), ThenBB);
		SUGAR VisitNode(kctx, builder, RHS, thunk);
		INode *Right = FuelVM_getExpression(builder);
		CreateUpdate(BLD(builder), Node, Right);
		IRBuilder_JumpTo(BLD(builder), MergeBB);
	}

	IRBuilder_setBlock(BLD(builder), MergeBB);
	builder->Value = Node;
}
Ejemplo n.º 5
0
static void CreateCond(KonohaContext *kctx, KBuilder *builder, kUntypedNode *expr, enum ConditionalOp Op, void *thunk)
{
	intptr_t cond;
	kUntypedNode *LHS = kUntypedNode_At(expr, 1);
	kUntypedNode *RHS = kUntypedNode_At(expr, 2);

	bblock_t HeadBB  = new_BasicBlockLABEL(kctx);
	bblock_t ThenBB  = new_BasicBlockLABEL(kctx);
	bblock_t MergeBB = new_BasicBlockLABEL(kctx);

	/* [CondExpr]
	 * LogicalAnd case
	 *       | goto Head
	 * Head  | let bval = LHS
	 *       | if(bval) { goto Then } else { goto Merge }
	 * Then  | bval = RHS
	 *       | goto Merge
	 * Merge | ...
	 */

	MiniVMBuilder_JumpTo(kctx, builder, HeadBB);
	cond = builder->stackbase;
	builder->stackbase += 1;
	{ /* Head */
		MiniVMBuilder_setBlock(kctx, builder, HeadBB);
		KLIB VisitNode(kctx, builder, LHS, thunk);
		CreateUpdate(kctx, builder, KType_Boolean, cond, MiniVM_getExpression(builder));
		if(Op == LogicalAnd)
			CreateBranch(kctx, builder, cond, ThenBB, MergeBB, false);
		else {
			CreateBranch(kctx, builder, cond, ThenBB, MergeBB, true);
		}
	}
	{ /* Then */
		MiniVMBuilder_setBlock(kctx, builder, ThenBB);
		KLIB VisitNode(kctx, builder, RHS, thunk);
		CreateUpdate(kctx, builder, KType_Boolean, cond, MiniVM_getExpression(builder));
		MiniVMBuilder_JumpTo(kctx, builder, MergeBB);
	}

	MiniVMBuilder_setBlock(kctx, builder, MergeBB);
	builder->stackbase -= 1;
	builder->Value = builder->stackbase;
}
Ejemplo n.º 6
0
int ProxyInst::InitProxy() 
{
	if (-1==CreateUpdate())
		return -1;

	NDListener *pLis = GetDeftListener() ;
	nd_assert(pLis) ;
	nd_handle h_listen = pLis->GetHandle() ;


	nd_hook_data(h_listen, listen_data_handle);

	read_remote_proxy(NDInstanceSrv::config_file, &g_remote_proxy ) ;
	return 0;

}
Ejemplo n.º 7
0
static kbool_t MiniVM_VisitAssignNode(KonohaContext *kctx, KBuilder *builder, kUntypedNode *node, void *thunk)
{
	/*
	 * [LetExpr] := lhs = rhs
	 * expr->NodeList = [NULL, lhs, rhs]
	 **/

	intptr_t dst = -1;
	kUntypedNode *left = kUntypedNode_At(node, 1);
	kUntypedNode *right = kUntypedNode_At(node, 2);
	ktypeattr_t type = left->typeAttr;
	ksymbol_t symbol;

	assert(IS_Token(left->TermToken));
	symbol = left->TermToken->symbol;
	if(kUntypedNode_node(left) == KNode_Local) {
		if((dst = MiniVMBuilder_FindLocalVar(kctx, builder, symbol, type, left->index)) == -1) {
			dst = AddLocal(kctx, builder, left->index, symbol, type);
		}
		KLIB VisitNode(kctx, builder, right, thunk);
		CreateUpdate(kctx, builder, type, dst, MiniVM_getExpression(builder));
		builder->Value = dst;
	}
	else {
		khalfword_t index  = (khalfword_t)left->index;
		khalfword_t xindex = (khalfword_t)(left->index >> (sizeof(khalfword_t)*8));
		KClass *lhsClass = KClass_(left->typeAttr);
		KClass *rhsClass = KClass_(right->typeAttr);
		assert(kUntypedNode_node(left) == KNode_Field);
		if((dst = MiniVMBuilder_FindLocalVar(kctx, builder, symbol, KType_Object, index)) == -1) {
			dst = AddLocal(kctx, builder, index, symbol, KType_Object);
		}
		KLIB VisitNode(kctx, builder, right, thunk);
		ASM(XNMOV, OC_(index), xindex, TC_(MiniVM_getExpression(builder), rhsClass), lhsClass);
		if(node->typeAttr != KType_void) {
			builder->Value = builder->stackbase;
			ASM(NMOVx, TC_(builder->stackbase, rhsClass), OC_(index), xindex, lhsClass);
		}
	}
	return true;
}
Ejemplo n.º 8
0
static kbool_t FuelVM_VisitFunctionNode(KonohaContext *kctx, KBuilder *builder, kNode *expr, void *thunk)
{
	/*
	 * [FunctionExpr] := new Function(method, env1, env2, ...)
	 * expr->NodeList = [method, defObj, env1, env2, ...]
	 **/
	enum TypeId Type;
	kMethod *mtd = CallNode_getMethod(expr);
	kObject *obj = expr->NodeList->ObjectItems[1];
	INode *MtdObj = CreateObject(BLD(builder), KType_Method, (void *) mtd);

	Type = ConvertToTypeId(kctx, kObject_class(obj)->typeId);
	INode *NewEnv  = CreateNew(BLD(builder), 0, Type);

	size_t i, ParamSize = kArray_size(expr->NodeList)-2;
	for(i = 0; i < ParamSize; i++) {
		kNode *envN = kNode_At(expr, i+2);
		enum TypeId FieldType = ConvertToTypeId(kctx, envN->attrTypeId);
		INode *Node = CreateField(BLD(builder), FieldScope, FieldType, NewEnv, i);
		SUGAR VisitNode(kctx, builder, envN, thunk);
		CreateUpdate(BLD(builder), Node, FuelVM_getExpression(builder));
	}

	Type = ConvertToTypeId(kctx, expr->attrTypeId);
	INode *NewFunc = CreateNew(BLD(builder), 0, Type);

	kNameSpace *ns = kNode_ns(expr);
	mtd =  KLIB kNameSpace_GetMethodByParamSizeNULL(kctx, ns, KClass_Func, KMethodName_("_Create"), 2, KMethodMatch_NoOption);

	INode *CallMtd = CreateObject(BLD(builder), KType_Method, (void *) mtd);
	INode *Params[4];
	Params[0] = CallMtd;
	Params[1] = NewFunc;
	Params[2] = NewEnv;
	Params[3] = MtdObj;
	builder->Value = CreateICall(BLD(builder), Type, DefaultCall, kNode_uline(expr), Params, 4);

	return true;
}
Ejemplo n.º 9
0
void *srv_thread_function(void *arg) {
	printf("server thread started!\n"); // hooray
	
	
// initializing variables
	ptsrv_thread_args targs = (ptsrv_thread_args) arg;
	sim = targs->sim;
	int PORT = targs->port;
	
	
	
	
	int socketfd;                       /* Socket file descriptor  */
	struct sockaddr_in server_name;     /* server socket's address */
	struct sockaddr_in client_name;     /* client socket's address */

	fd_set active_fd_set, read_fd_set;  /* Array of file descriptors */

	int nbytes;
	char buffer[BUFFER_SIZE];
	
	int socketdest;
	char* pacote;
	char str_aux[80];
	FILE* motd;
	char name[150]; //robot name buffer

	table = start_table();
 /* Create the socket. 

    socket (NAMESPACE, STYLE, PROTOCOL)

      - namespace: local, other, internet: PF_INET || PF_INET6 

      - style: SOCK_STREAM, SOCK_DGRAM

      - protocol: 0 means 'auto' */

	socketfd = socket (PF_INET, SOCK_STREAM, 0); 

	if (socketfd < 0)
   	fatal ();

	/* Give the socket a name. */

	server_name.sin_family = AF_INET;                   /* Ineternet family */
	server_name.sin_port = htons (PORT);                /* set port  */
	server_name.sin_addr.s_addr = htonl (INADDR_ANY);   /* Any internetface */

	/* Bind the socket to the port. */

	if (bind (socketfd, (struct sockaddr *) &server_name, sizeof (server_name)) < 0)
		fatal ();
	
     // 23 apr 2010 - Disabling Nagle algorithm	in the protocol stack, so it sends packages
     // immeadiately, without waiting for more bytes to be available to send, which is more
     // appropriate for this real-time program,
     char flag = 1;
     setsockopt(socketfd, IPPROTO_TCP, 1, (char *) &flag, sizeof(int)); 

	/* Tell the OS to start to listen to the socket. */

	if (listen (socketfd, 1) < 0)
		fatal ();

	/* Initialize the set of active sockets. */

	FD_ZERO (&active_fd_set);
	FD_SET (socketfd, &active_fd_set);

	for (;;) {
		int i,j;

		/* Block until there is activity in the socket. */
		read_fd_set = active_fd_set;

		if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
        fatal ();
	
	
	
	

     
     
     /* Service all the sockets with input pending. */

		for (i = 0; i < FD_SETSIZE; ++i) {
			if (FD_ISSET (i, &read_fd_set)) {
				if (i == socketfd) { /* Connection request on original socket. */
					int new_socket;
					socklen_t size;

					/* Accept the the connection and associate it
					with a new socket. */

					size = sizeof (client_name);
					new_socket = accept (socketfd, (struct sockaddr *) &client_name, &size);
					if (new_socket < 0)
						fatal ();
					fprintf (stderr, "Server: connect from host %s, port %hd.\n", inet_ntoa (client_name.sin_addr), ntohs (client_name.sin_port));
					FD_SET (new_socket, &active_fd_set);      /* Add the new socket to the set of active fd */
					
					// adding new user to the table
					//add_user (table, inet_ntoa (client_name.sin_addr), new_socket);
					add_user (table, new_socket);
				}
				else {                                 /* Data arriving on an already-connected socket. */
					nbytes = read (i, buffer, BUFFER_SIZE);
					buffer[nbytes-1] = 0;
					if (nbytes ==0) {                  /* Cient ended the transmission */ 
						printf ("Client disconnected.\n");
						remove_user (table, i);
						print_table (table);
						close (i);
						FD_CLR (i, &active_fd_set);
					}
					else if (nbytes < 0) {            /* Read error */ 
						fprintf (stderr, "Read error, client disconnected.\n");
						remove_user (table, i);
						print_table (table);
						close (i);
						FD_CLR (i, &active_fd_set);
					}
					else {                            // client message
						//debug
						//printf("%s\n", buffer);
						if (IsPackageValid(buffer, nbytes)) {
							if (NewRobot(buffer, name)) { //new robot connected, get name
								printf("New robot added: %s\n", name);
								set_id(table, i, name); //add the new name to the table
								strcpy(sim->robotnames[sim->num_available_clients], name);
								sim->num_available_clients++;
								print_table (table);
								sim->clientlist_update = true;
								//CreateUpdate(buffer, &nbytes, sim->GetRobot(get_id(table, i)));
								//write(i, buffer, nbytes);
							} else {
								//update settings for this robot
								if (GetUpdate(buffer, nbytes, sim->GetRobot(get_id(table, i))) ) {
									//printf("got update from %s\n", sim->GetRobot(get_id(table, i))->robot_name);
									CreateUpdate(buffer, &nbytes, sim->GetRobot(get_id(table, i)));
									write(i, buffer, nbytes);
								} else {
								    //printf("pacote de ok\n");
								    CreateUpdate(buffer, &nbytes, sim->GetRobot(get_id(table, i)));
								    write(i, buffer, nbytes);
								}
							}
						}
						//nbytes = write (i, buffer, strlen(buffer) + 1);
						
					
						/*switch (get_tipo(buffer)) {
						case 2:                       // mensagem privada;
							nome = get_dest(buffer);
							socketdest = busca_usuario(tabela, nome);
							if (socketdest) {
								nbytes = write (socketdest, buffer, strlen(buffer) + 1);
								nbytes = write (i, buffer, strlen(buffer) + 1);
							}
							free(nome);
						break;
						case 3:                      //mensagem de sauda��o de novo usuario
							nome = get_rem(buffer);
							adiciona_usuario(tabela, nome, i);
							imprime_tabela(tabela);
							free(nome);
							if ((motd = fopen("motd", "r")) != NULL) {   // abre o Message Of The Day
								while (fgets(str_aux,79, motd)) {
									pacote = monta_pacote_sis(str_aux, nome);
									nbytes = write (i, pacote, pacote[0]);
									usleep(2000);
								}
								fclose(motd);
							}
						default: // mensagem comum (tambem repassa para todos os usuarios a mensagem de saudacao - case 3 nao tem break)
							for (j = 3; j < FD_SETSIZE; ++j) {
								if (j != socketfd) nbytes = write (j, buffer, strlen(buffer) + 1);
    						}	
						break;
						case 4:                   // mensagens do sistema
							switch (get_sis(buffer)) {
							case 'l':             // cliente requisitando a listagem de usuarios conectados
								reset_iterator(tabela);
								nome = get_rem(buffer);
								strcpy(str_aux, "Usuarios conectados:\n");
								pacote = monta_pacote_sis(str_aux, nome);
								nbytes = write (i, pacote, pacote[0]);
								free(pacote);
								do {
									usleep(2000);
									strcpy(str_aux, get_next_nome(tabela));
									strcat(str_aux, "\n");
									pacote = monta_pacote_sis(str_aux, nome);
									nbytes = write (i, pacote, pacote[0]);
									free(pacote);
								} while (! iterator_finished(tabela));
								usleep(2000);
								strcpy(str_aux, "Fim da listagem.\n");
								pacote = monta_pacote_sis(str_aux, nome);
								nbytes = write (i, pacote, pacote[0]);
								free(pacote);
							break;
							}
						break;
						
						}*/
					}
				}
			}
		}
	}	
	
}
Ejemplo n.º 10
0
bool Location::SetRadius(iDataConnection* db, float radius)
{
    SetRadius(radius);

    return CreateUpdate(db);
}