void init(int max_sim_time) {
  
  #ifdef STANDALONE 
    printf(" Initializing TraCI...\n");
  #else
    LOG_N(OMG, " Initializing TraCI...\n");
  #endif
  char *objID = " ";
 // size_t size = strlen(objID);

  int noSubscribedVars = 2;
  writeUnsignedByte(0);
  writeInt(1 + 4 + 1 + 4 + 4 + 4 + (int) strlen(objID) + 1 + noSubscribedVars);
  writeUnsignedByte(CMD_SUBSCRIBE_SIM_VARIABLE); // command id
  writeInt(0); // begin time
  writeInt(max_sim_time*1000); // end time
  writeString(objID); // object id
  writeUnsignedByte(noSubscribedVars); // variable number
  writeUnsignedByte(VAR_DEPARTED_VEHICLES_IDS);
  writeUnsignedByte(VAR_ARRIVED_VEHICLES_IDS);

  // send request message
  sendExact(storageLength(storageStart));
  extractCommandStatus(receiveExact(), CMD_SUBSCRIBE_SIM_VARIABLE, description);
  if (departed == NULL) {
    departed = (string_list*) malloc(sizeof(string_list)); // departed MUST point to HEAD
    departed->string = NULL;
    departed->next = NULL;
  }
  if (arrived == NULL) {
    arrived = (string_list*) malloc(sizeof(string_list));  // arrived MUST point to HEAD
    arrived->string = NULL;
    arrived->next = NULL;
  } 
    
  processSubscriptions();

  reset();
}
示例#2
0
static TACommandVerdict gzgetc_cmd(TAThread thread,TAInputStream stream)
{
    void* file;
    int res, errnum;

    file = readPointer(&stream);

    START_TARGET_OPERATION(thread);

    res = gzgetc(file);

    END_TARGET_OPERATION(thread);

    gzerror(file, &errnum);

    writeInt(thread, errnum);
    writeInt(thread, res);

    sendResponse(thread);

    return taDefaultVerdict;
}
示例#3
0
文件: memRoutines.c 项目: Xarfg/2
void memorySnapshot(){
  
  int userMemoryUsage = 0;
  int allocMemoryUsage = 0;
  long long unsigned lastUsedByte = (long long unsigned)memoryAdress;
  long long unsigned firstUsedByte = (long long unsigned)memoryAdress;
  long long unsigned firstBlockByte = (long long unsigned)memoryAdress;
  long long unsigned lastBlockByte = (long long unsigned)memoryAdress;
    
  struct ab* a = headAllocatedB;
  int fd = dataFile;

  while (a!=NULL){
    userMemoryUsage += a->size;
    firstBlockByte = (long long unsigned)a; 
    lastBlockByte = firstBlockByte + a->size + sizeof(struct ab);
    
    if ( firstBlockByte <= firstUsedByte ){
      firstUsedByte = firstUsedByte;
    }
    if ( lastUsedByte <= lastBlockByte ){
      lastUsedByte = lastBlockByte;
    }
    allocMemoryUsage = lastUsedByte - firstUsedByte;
    
    a = a->next;
  }

  writeInt(fd, instant);
  write(fd," ",1);
  writeInt(fd, userMemoryUsage);
  write(fd," ",1);
  writeInt(fd, allocMemoryUsage);
  write(fd,"\n",1);

  instant++;
  return ;

}
示例#4
0
static TACommandVerdict wcsxfrm_cmd(TAThread thread, TAInputStream stream)
{
    wchar_t* ws1;
    wchar_t* ws2;
    wchar_t* xxx = L"";
    size_t n;
    size_t res;
    int save_errno;
    int overflow = 0;
    int i;

    // Prepare
    ws2 = ta_wcsalign(readWString(&stream)); //align on copy
    n = readSize(&stream);
    ws1 = ta_alloc_memory((n + BUFFER_TAIL) * sizeof(wchar_t));
    for (i=0; i<BUFFER_TAIL; ++i)
        ws1[n + i] = (wchar_t)0xDDDDDDDD;

    START_TARGET_OPERATION(thread);

    // Execute
    errno = 0;
    res = wcsxfrm(ws1, ws2, n);
    save_errno = errno;

    END_TARGET_OPERATION(thread);

    for (i=0; i<BUFFER_TAIL; ++i)
        overflow |= (ws1[n + i] != (wchar_t)0xDDDDDDDD);

    if (overflow)
    {
        sendException(thread, "wcsxfrm.02 failed: writing outside of buffer limits occured!");
        return taDefaultVerdict;
    }

    // Response
    if (res >= n)
        writeWString(thread, xxx);
    else
        writeWString(thread, ws1);
    writeSize(thread, res);
    writeInt(thread, save_errno);

    sendResponse(thread);

    ta_dealloc_memory(ws1);
    ta_dealloc_memory(ws2);

    return taDefaultVerdict;
}
int main()
{

    int n;
    n=readlong();
    int i,j;

    for(i=2; i<=100; i++)
    {
        number[0][i]=0;
    }

    for(i=1; i<=n; i++)
    {
        int a;
        a=readlong();
        for(j=1; j<=100; j++)
            number[i][j]=number[i-1][j];

        number[i][a]++;
    }

    int t;
    t=readlong();
    int l,r;
    long long MOD;
    while(t--)
    {
        l=readlong();
        r=readlong();
        MOD=readlong();


        long long ans=1;
        if(MOD==1)
            printf("%d\n",0);
        else
        {
            for(i=1; i<=100; i++)
            {
                if(number[r][i]-number[l-1][i])
                {
                    ans=ans*p(i,number[r][i]-number[l-1][i],MOD);
                    ans=ans%MOD;
                }
            }
            writeInt(ans);
        }
    }
    return 0;
}
示例#6
0
/* Display the amount of money stored in the user's cloud provider account. */
int clientWalletBalance(BIO *conn) {
	//send the code which triggers the server to call serverWalletBalance()
	if(writeInt(conn, WALLET_BALANCE_CODE) == -1) return -1;

	//read the balance back from the server
	int balance = readInt(conn);
	if(balance == -1) {
		fprintf(stderr, "Failed to get balance.\n");
		return -1;
	}
	
	printf("You have %d cloud dollars in your cloud wallet.\n", balance);
	return 0;
}
示例#7
0
static TACommandVerdict getpriority_cmd(TAThread thread,TAInputStream stream)
{
   int which;
   int who  ;
   int res  ;

   // Prepare
   which = readInt( & stream );
   who   = readInt( & stream );
   errno = 0;

   // Execute
   START_TARGET_OPERATION(thread);
   res = getpriority( which, who );
   END_TARGET_OPERATION(thread);

   // Response
   writeInt( thread, res   );
   writeInt( thread, errno );
   sendResponse( thread );

   return taDefaultVerdict;
}
示例#8
0
// interrupt service routine to manage interrupt vector table
void handleInterrupt21(int ax, int bx, int cx, int dx){
  if (ax==0){
    printString(bx);
  }
  else if (ax==1){
    readString(bx);
  }
  else if (ax==2){
    readSector(bx,cx);
  }
  else if (ax==3){
    readFile(bx,cx,dx);
  }
  else if (ax==4){
    runProgram(bx,cx);
  }
  else if (ax==5){
   stop();
  }
  else if (ax==6){
    writeSector(bx,cx);
  }
  else if (ax==7) {
    deleteFile(bx);
  }
  else if (ax==8){
    writeFile(bx,cx,dx);
  }
  else if (ax==11){
    interrupt(25,0,0,0,0);
  }
  else if (ax==12){
    clearScreen(bx,cx);
  }
  else if (ax==13){
    writeInt(bx);
  }
  else if (ax==14){
    ReadInt(bx);
  }
  else if (ax==15){
      error(bx);
  }
  else if (ax==66){
      printChar(bx);
  }
  else {
    printString("Incorrect service call\0");
  }
}
示例#9
0
static TACommandVerdict gzprintf_cmd(TAThread thread,TAInputStream stream)
{
    void* file;
    char* s;
    int res, errnum;

    file = readPointer(&stream);
    s = readString(&stream);

    START_TARGET_OPERATION(thread);

    res = gzprintf(file, s);

    END_TARGET_OPERATION(thread);

    writeInt(thread, errnum);
    writeInt(thread, errno);
    writeInt(thread, res);

    sendResponse(thread);

    return taDefaultVerdict;
}
示例#10
0
static TACommandVerdict nice_cmd(TAThread thread,TAInputStream stream)
{
   int incr;
   int res ;

   // ta_debug_printf( "[%3d]", nice( 0 ) );
   // Prepare
   incr = readInt( & stream );
   errno = 0;

   // Execute
   START_TARGET_OPERATION(thread);
   res = nice( incr );
   END_TARGET_OPERATION(thread);

   // ta_debug_printf( "[%3d|%3d|%3d|%3d|%3d|%3d]\n", res, incr, getuid(), geteuid(), getgid(), getegid() );
   // Response
   writeInt( thread, res   );
   writeInt( thread, errno );
   sendResponse( thread );

   return taDefaultVerdict;
}
示例#11
0
int MQTTSPacket_send_regAck(Clients* client, int msgId, int topicId, char returnCode)
{
	MQTTS_RegAck packet;
	int rc = 0;
	char *buf, *ptr;
	int datalen = 5;

	FUNC_ENTRY;
	packet.header.len = 7;
	packet.header.type = MQTTS_REGACK;

	ptr = buf = malloc(datalen);
	writeInt(&ptr, topicId);
	writeInt(&ptr, msgId);
	writeChar(&ptr, returnCode);

	rc = MQTTSPacket_send(client, packet.header, buf, datalen);
	free(buf);

	Log(LOG_PROTOCOL, 52, NULL, client->socket, client->addr, client->clientID, msgId, topicId, returnCode, rc);
	FUNC_EXIT_RC(rc);
	return rc;
}
void writeString(char *s)
{
  int count=0;
  char* s_copy=s;

  for(; *s!='\0'; s++)
    count++;

  writeInt(count);

  for(; *s_copy!='\0'; s_copy++)
    writeChar(*s_copy);

}
示例#13
0
文件: bt.cpp 项目: ankitsingh/DBMS
void BPlusTree::insert_in_leaf( TreeNode* L, int key, int rid) {

    BufferManager *bm = BufferManager::getInstance();

    if(key < L->getKey(1)) {
        cout<<"inif2"<<endl;
        //inserting the key-pointer pair at the start of the node
        moveBytes( L->getBuffPage(), 8, 16, (L->getTotalPairs())*8 );
        writeInt( L->getBuffPage(), 0, L->getTotalPairs() + 1 );
        cout<<readInt( L->getBuffPage(), 0)<<endl;
    }
    else {
        cout<<"inelse2"<<endl;
        int totPairs = L->getTotalPairs();
        int i = totPairs;

        //finding the location to insert
        while(key<L->getKey(i)){
            cout<<"inwhile\n";
            i--;
            if(key>L->getKey(i))
                break;
        }
        cout<<"outofwhile\n";
        i++;
        //inserting the key-pointer pair at the correct location
        cout<<i<<" "<<L->getTotalPairs()<<"\n";
        moveBytes( L->getBuffPage(), 16+8*i, 8+8*i, (L->getTotalPairs())-i );
        cout<<"moved\n";
        writeInt( L->getBuffPage(), 8+8*i, rid );
        cout<<"write\n";
        writeInt( L->getBuffPage(), 12+8*i, key );
        cout<<"write1\n";
        writeInt( L->getBuffPage(), 0, L->getTotalPairs() + 1 );
        cout<<"written all data";
    }
}
示例#14
0
static TACommandVerdict getgrent_cmd(TAThread thread,TAInputStream stream)
{
    struct group* result;
    int i=0;
    
    errno = 0;

    START_TARGET_OPERATION_WHICH_MAY_CONTAIN_CANCELPOINT(thread);
    result = getgrent();
    END_TARGET_OPERATION_WHICH_MAY_CONTAIN_CANCELPOINT(thread);
 

    writePointer(thread,(void*)result);
    
    if(result)
    {
        writeString(thread, result->gr_name);
        writeInt(thread, result->gr_gid);
        while ((result->gr_mem)[i]!=NULL)
        {
            i++;
        }
        writeInt(thread, i);
        i=0;
        while ((result->gr_mem)[i]!=NULL)
        {
            writeString(thread, (result->gr_mem)[i]);
            i++;
        }
    } 
    
    writeInt(thread,errno);
    sendResponse(thread);
    
    
    return taDefaultVerdict;
}
示例#15
0
static TACommandVerdict setkey_cmd(TAThread thread,TAInputStream stream)

{

    char* key;

    int i;

   

    // Prepare

    key=(char*)readPointer(&stream);



    ta_debug_printf("setkey:\n");

    for(i=0;i<64;i++)

        ta_debug_printf("%d ", key[i]);

    ta_debug_printf("\n");



    START_TARGET_OPERATION(thread);

    errno=0;

    setkey(key);

    END_TARGET_OPERATION(thread);

    

    // Response

    writeInt(thread, errno);



    sendResponse(thread);

    

    return taDefaultVerdict;

}
示例#16
0
/*
 * LIST FILES : CLIENT SIDE
 *
 * Handle the client side of the list files funcitonality
 */ 
int clientListFiles(BIO *conn) {
	//send the code which causes the server to call serverListFiles() */
	if(writeInt(conn, LIST_FILES_CODE) == -1) return -1;

	//catch the server's response, a single string listing all files
	//from the folder
	char buffer[BUFFER_SIZE];
	if(readPacket(conn, buffer, sizeof(buffer)) < 1) return -1;

	//display the file listing
	printf("Your files:\n");
	printf("%s", buffer);
	
	return 1;
}
示例#17
0
int MQTTSPacket_send_subAck(Clients* client, MQTTS_Subscribe* sub, int topicId, int qos, char returnCode)
{
	MQTTS_SubAck packet;
	int rc = 0;
	char *buf, *ptr;
	int datalen = 6;

	FUNC_ENTRY;
	packet.header.len = 8;
	packet.header.type = MQTTS_SUBACK;

	ptr = buf = malloc(datalen);
	packet.flags.QoS = qos;
	writeChar(&ptr, packet.flags.all);
	writeInt(&ptr, topicId);
	writeInt(&ptr, sub->msgId);
	writeChar(&ptr, returnCode);
	rc = MQTTSPacket_send(client, packet.header, buf, datalen);
	free(buf);

	Log(LOG_PROTOCOL, 68, NULL, client->socket, client->addr, client->clientID, sub->msgId, topicId, returnCode, rc);
	FUNC_EXIT_RC(rc);
	return rc;
}
示例#18
0
static TACommandVerdict insertln_cmd(TAThread thread,TAInputStream stream)
{
    int res;
    
    START_TARGET_OPERATION(thread);
    
    res = insertln();
    
    END_TARGET_OPERATION(thread);
    
    writeInt(thread, res);
    sendResponse(thread);
    
    return taDefaultVerdict;
}
示例#19
0
int MQTTSPacket_send_register(Clients* client, int topicId, char* topicName, int msgId)
{
	MQTTS_Register packet;
	int rc = 0;
	char *buf, *ptr;
	int datalen = 4 + strlen(topicName);

	FUNC_ENTRY;
	packet.header.len = datalen+2;
	packet.header.type = MQTTS_REGISTER;

	ptr = buf = malloc(datalen);

	writeInt(&ptr, topicId);
	writeInt(&ptr, msgId);
	memcpy(ptr, topicName, strlen(topicName));

	rc = MQTTSPacket_send(client, packet.header, buf, datalen);
	free(buf);

	Log(LOG_PROTOCOL, 50, NULL, client->socket, client->addr, client->clientID, msgId, topicId, topicName, rc);
	FUNC_EXIT_RC(rc);
	return rc;
}
示例#20
0
static TACommandVerdict mkfifo_cmd(TAThread thread,TAInputStream stream)
{
    char* path;
    int mode;
    int res;

    // Prepare
    path = readString(&stream);
    mode = readFilePermissions(&stream);
    errno = 0;

    START_TARGET_OPERATION(thread);

    res = mkfifo(path, mode);

    END_TARGET_OPERATION(thread);

    // Response
    writeInt(thread,res);
    writeInt(thread,errno);
    sendResponse(thread);

    return taDefaultVerdict;
}
示例#21
0
void deleteByOffset(DeletionHandler *dh, long offset) {
	long head = readLong(dh->bfr, 0L); // read head

	seekBinaryFile(dh->bfr->bf, offset); // point to offset
	int size = readInt(dh->bfr, getStreamOffset(dh->bfr->bf));
	size *= -1; // delete

	seekBinaryFile(dh->bfw->bf, offset);
	writeInt(dh->bfw, size, getStreamOffset(dh->bfw->bf)); // write deleted

	seekBinaryFile(dh->bfw->bf, (offset + sizeof(int)));
	writeLong(dh->bfw, head, getStreamOffset(dh->bfw->bf)); // write head

	writeLong(dh->bfw, offset, 0L); // updated head with new offset
}
示例#22
0
static TACommandVerdict writev_fifo_cmd(TAThread thread,TAInputStream stream)
{
    int fildes;
    int size;
    int i=0;
    void* curPointer;
    size_t curSize;
    struct iovec *iov;
    ssize_t res;


    // Prepare
    fildes = readInt(&stream);
    size = readInt(&stream);

    iov=(struct iovec *)ta_alloc_memory(size*sizeof(struct iovec));

    for (i=0;i<size;i++)
    {
        curPointer=readPointer(&stream);
        curSize=readSize(&stream);
        iov[i].iov_base=curPointer;
        iov[i].iov_len=curSize;
    }

    BEFORE_BLOCKED_TARGET_OPERATION(thread);

    writeString(thread,"Ok");
    sendResponse(thread);
    errno = 0;


    START_TARGET_OPERATION(thread);
    res = writev(fildes, iov, size);
    END_TARGET_OPERATION(thread);


    writeDeferredReaction(thread, "writev_fifo_return");

    // Response
    writeSSize(thread,res);
    writeInt(thread,errno);
    sendResponse(thread);

    ta_dealloc_memory(iov);

    return taDefaultVerdict;
}
示例#23
0
static TACommandVerdict z_stream_size_cmd(TAThread thread,TAInputStream stream)
{
    int res;

    START_TARGET_OPERATION(thread);

    res = sizeof(z_stream);

    END_TARGET_OPERATION(thread);

    // Response
    writeInt(thread, res);
    sendResponse(thread);

    return taDefaultVerdict;
}
int main()
{
	generate_hashmap();
	
	int n;
	n=readlong();
	int i,j;
	
	for(i=0;i<25;i++)
	{
		number[0][i]=0;
	}
	
	for(i=1;i<=n;i++)
	{
		int a;
		a=readlong();
		for(j=0;j<25;j++)
			number[i][j]=number[i-1][j]+hashmap[a][j];
	}
	
	int t;
	t=readlong();
	int l,r;
		long long MOD;
	while(t--)
	{
		l=readlong();
		r=readlong();
		MOD=readlong();
		
		
		long long ans=1;
		
		for(i=0;i<25;i++)
			{
				if(number[r][i]-number[l-1][i])
				{	
					ans=ans*p(prime[i],number[r][i]-number[l-1][i],MOD);
					ans=ans%MOD;
				}
			}
		writeInt(ans);
	}
	
	return 0;
}
示例#25
0
/**
 * Send an MQTT acknowledgement packet down a socket.
 * @param type the MQTT packet type e.g. SUBACK
 * @param msgid the MQTT message id to use
 * @param dup boolean - whether to set the MQTT DUP flag
 * @param socket the open socket to send the data to
 * @return the completion code (e.g. TCPSOCKET_COMPLETE)
 */
int MQTTPacket_send_ack(int type, int msgid, int dup, int socket)
{
	Header header;
	int rc;
	char *buf = malloc(2);
	char *ptr = buf;

	FUNC_ENTRY;
	header.byte = 0;
	header.bits.type = type;
	header.bits.dup = dup;
	writeInt(&ptr, msgid);
	if ((rc = MQTTPacket_send(socket, header, buf, 2)) != TCPSOCKET_INTERRUPTED)
		free(buf);
	FUNC_EXIT_RC(rc);
	return rc;
}
int MQTTSPacket_send(int socket, char* addr, MQTTSHeader header, char* buffer, int buflen)
{
	int rc = 0;
	char *data = NULL;
	char *ptr = NULL;
	PacketBuffer buf;

	FUNC_ENTRY;
	if (header.len > 256)
	{
		header.len += 2;
		buflen += 2;
		data = malloc(header.len);
		ptr = data;
		*ptr++ = 0x01;
		writeInt(&ptr, header.len);
	}
	else
	{
		data = malloc(header.len);
		ptr = data;
		*ptr++ = header.len;
	}
	*ptr++ = header.type;

	memcpy(ptr, buffer, buflen);

	buf.data = data;
	buf.len = buflen + 2;
	rc = MQTTSPacket_sendPacketBuffer(socket, addr, buf);

	if (rc == SOCKET_ERROR)
	{
		Socket_error("sendto", socket);
/*		if (err == EWOULDBLOCK || err == EAGAIN)
			rc = TCPSOCKET_INTERRUPTED;
*/
	}
	else
		rc = 0;

	free(data);

	FUNC_EXIT_RC(rc);
	return rc;
}
PacketBuffer MQTTSPacketSerialize_advertise(unsigned char gateway_id, short duration)
{
	MQTTSHeader header;
	PacketBuffer buf;
	
	FUNC_ENTRY;
	header.len = 5;
	header.type = MQTTS_ADVERTISE;
	
	buf = MQTTSPacketSerialize_header(header);

	writeChar(&buf.ptr, gateway_id);
	writeInt(&buf.ptr, duration);
	
	FUNC_EXIT;
	return buf;
}
示例#28
0
void
WKBWriter::writeCoordinateSequence(const CoordinateSequence& cs,
                                   bool sized)
{
    std::size_t size = cs.getSize();
    bool is3d = false;
    if(outputDimension > 2) {
        is3d = true;
    }

    if(sized) {
        writeInt(static_cast<int>(size));
    }
    for(std::size_t i = 0; i < size; i++) {
        writeCoordinate(cs, i, is3d);
    }
}
示例#29
0
/* Add horizontal advance width. */
static void glyphWidth(abfGlyphCallbacks *cb, float hAdv) {
    ufwCtx h = cb->direct_ctx;

    if (h->err.code != 0)
        return; /* Pending error */
    else if (h->path.state != 1) {
        /* Call sequence error */
        h->err.code = ufwErrBadCall;
        return;
    }

    writeStr(h, "\t<advance width=\"");
    writeInt(h, (long)roundf(hAdv));
    writeLine(h, "\"/>");

    h->path.state = 2;
}
示例#30
0
/**
 * Send an MQTT subscribe packet down a socket.
 * @param topics list of topics
 * @param qoss list of corresponding QoSs
 * @param msgid the MQTT message id to use
 * @param dup boolean - whether to set the MQTT DUP flag
 * @param socket the open socket to send the data to
 * @param clientID the string client identifier, only used for tracing
 * @return the completion code (e.g. TCPSOCKET_COMPLETE)
 */
int MQTTPacket_send_subscribe(List* topics, List* qoss, uint64_t msgid, int dup, networkHandles* net, const char* clientID)
{
	Header header;
	char *data, *ptr;
	int rc = -1;
	ListElement *elem = NULL, *qosElem = NULL;
	int datalen;
    int mqtt_version = get_client_mqtt_version_from_network_handler(net);

	FUNC_ENTRY;
	header.bits.type = SUBSCRIBE;
	header.bits.dup = dup;
	header.bits.qos = 1;
	header.bits.retain = 0;

    //8 -> 64 bit msg id. if the msgid is 2 Byte, datalen will be decresed
	datalen = 8 + topics->count * 3; // utf length + char qos == 3
	while (ListNextElement(topics, &elem))
		datalen += strlen((char*)(elem->content));

	ptr = data = malloc(datalen);

    if(mqtt_version == MQTTVERSION_YUNBA_3_1)
    {
        writeInt64(&ptr, msgid);
    }else{
        msgid = (int)msgid;
        writeInt(&ptr, msgid);
        //make sure the datalen is adjusted
        datalen -= 6;
    }
	elem = NULL;
	while (ListNextElement(topics, &elem))
	{
		ListNextElement(qoss, &qosElem);
		writeUTF(&ptr, (char*)(elem->content));
		writeChar(&ptr, *(int*)(qosElem->content));
	}
	rc = MQTTPacket_send(net, header, data, datalen, 1);
	Log(LOG_PROTOCOL, 22, NULL, net->socket, clientID, msgid, rc);
	if (rc != TCPSOCKET_INTERRUPTED)
		free(data);
	FUNC_EXIT_RC(rc);
	return rc;
}