Browse Source

Test

etomic
jl777 8 years ago
parent
commit
46bd3b309e
  1. 22
      iguana/exchanges/LP_commands.c
  2. 69
      iguana/exchanges/LP_forwarding.c

22
iguana/exchanges/LP_commands.c

@ -41,7 +41,7 @@ double LP_query(char *method,struct LP_quoteinfo *qp,char *base,char *rel,bits25
jaddstr(reqjson,"method",method);
if ( strcmp(method,"price") != 0 )
printf("QUERY.(%s)\n",jprint(reqjson,0));
LP_pubkey_send(qp->srchash,jprint(reqjson,1),1);
LP_forward(qp->srchash,jprint(reqjson,1),1);
for (i=0; i<30; i++)
{
if ( (price= LP_pricecache(qp,base,rel,qp->txid,qp->vout)) != 0. )
@ -99,7 +99,7 @@ int32_t LP_connectstart(int32_t pubsock,struct LP_utxoinfo *utxo,cJSON *argjson,
retstr = jprint(retjson,1);
if ( pubsock >= 0 )
LP_send(pubsock,retstr,1);
else LP_pubkey_send(utxo->S.otherpubkey,retstr,1);
else LP_forward(utxo->S.otherpubkey,retstr,1);
retval = 0;
} else printf("error launching swaploop\n");
} else printf("printf error nn_connect to %s\n",pairstr);
@ -208,7 +208,7 @@ int32_t LP_tradecommand(char *myipaddr,int32_t pubsock,cJSON *argjson,uint8_t *d
retstr = jprint(retjson,1);
if ( pubsock >= 0 )
LP_send(pubsock,retstr,1);
else LP_pubkey_send(utxo->S.otherpubkey,retstr,1);
else LP_forward(utxo->S.otherpubkey,retstr,1);
utxo->T.published = (uint32_t)time(NULL);
} else printf("null price\n");
} else printf("swappending.%u swap.%p\n",utxo->T.swappending,utxo->S.swap);
@ -400,6 +400,18 @@ forward(pubkey,hexstr)\n\
retstr = LP_pricestr(jstr(argjson,"base"),jstr(argjson,"rel"));
else if ( strcmp(method,"orderbook") == 0 )
retstr = LP_orderbook(jstr(argjson,"base"),jstr(argjson,"rel"));
else if ( strcmp(method,"forward") == 0 )
{
cJSON *reqjson = jduplicate(argjson);
jdelete(reqjson,"method");
if ( jstr(reqjson,"method2") != 0 && strncmp("forward",jstr(reqjson,"method2"),strlen("forward")) != 0 )
{
jaddstr(reqjson,"method",jstr(argjson,"method2"));
if ( LP_forward(jbits256(argjson,"pubkey"),jprint(reqjson,1),1) > 0 )
retstr = clonestr("{\"result\":\"success\"}");
else retstr = clonestr("{\"error\":\"error forwarding\"}");
} else retstr = clonestr("{\"error\":\"cant recurse forwards\"}");
}
else if ( strcmp(method,"getpeers") == 0 )
retstr = LP_peers();
else if ( IAMLP != 0 )
@ -413,8 +425,8 @@ forward(pubkey,hexstr)\n\
retstr = LP_register(jbits256(argjson,"pubkey"),jstr(argjson,"pushaddr"));
else if ( strcmp(method,"lookup") == 0 )
retstr = LP_lookup(jbits256(argjson,"pubkey"));
else if ( strcmp(method,"forward") == 0 )
retstr = LP_forward(jbits256(argjson,"pubkey"),jstr(argjson,"hexstr"));
else if ( strcmp(method,"forwardhex") == 0 )
retstr = LP_forwardhex(jbits256(argjson,"pubkey"),jstr(argjson,"hexstr"));
else if ( strcmp(method,"notify") == 0 )
retstr = clonestr("{\"result\":\"success\",\"notify\":\"received\"}");
else if ( strcmp(method,"notified") == 0 )

69
iguana/exchanges/LP_forwarding.c

@ -60,7 +60,7 @@ char *LP_register(bits256 pubkey,char *ipaddr)
if ( (ptr= LP_forwardfind(pubkey)) != 0 )
{
ptr->lasttime = (uint32_t)time(NULL);
return(clonestr("{\"error\":\"already registered\"}"));
return(clonestr("{\"error\":\"already registered\",\"registered\":1}"));
}
else if ( (pushsock= nn_socket(AF_SP,NN_PUSH)) < 0 )
return(clonestr("{\"error\":\"out of sockets\"}"));
@ -84,7 +84,33 @@ char *LP_register(bits256 pubkey,char *ipaddr)
}
}
char *LP_forward(bits256 pubkey,char *hexstr)
void LP_forwarding_register(bits256 pubkey,char *pushaddr,int32_t max)
{
char *retstr; cJSON *retjson; struct LP_peerinfo *peer,*tmp; int32_t n=0,retval = -1;
if ( pushaddr == 0 || pushaddr[0] == 0 || bits256_nonz(pubkey) == 0 )
{
printf("LP_forwarding_register illegal pushaddr or null pubkey\n");
return;
}
HASH_ITER(hh,LP_peerinfos,peer,tmp)
{
if ( (retstr= issue_LP_register(peer->ipaddr,peer->port,pubkey,pushaddr)) != 0 )
{
//printf("[%s] LP_register.(%s) returned.(%s)\n",pushaddr,peer->ipaddr,retstr);
if ( (retjson= cJSON_Parse(retstr)) != 0 )
{
if ( jint(retjson,"registered") != 0 && ++n >= max )
retval = 0;
free_json(retjson);
}
free(retstr);
}
if ( retval == 0 )
break;
}
}
char *LP_forwardhex(bits256 pubkey,char *hexstr)
{
struct LP_forwardinfo *ptr=0; uint8_t *data; int32_t datalen=0,sentbytes=0; cJSON *retjson;
if ( hexstr == 0 || hexstr[0] == 0 )
@ -115,35 +141,9 @@ char *LP_forward(bits256 pubkey,char *hexstr)
} else return(clonestr("{\"error\":\"notfound\"}"));
}
void LP_forwarding_register(bits256 pubkey,char *pushaddr,int32_t max)
int32_t LP_forward(bits256 pubkey,char *jsonstr,int32_t freeflag)
{
char *retstr; cJSON *retjson; struct LP_peerinfo *peer,*tmp; int32_t n=0,retval = -1;
if ( pushaddr == 0 || pushaddr[0] == 0 || bits256_nonz(pubkey) == 0 )
{
printf("LP_forwarding_register illegal pushaddr or null pubkey\n");
return;
}
HASH_ITER(hh,LP_peerinfos,peer,tmp)
{
if ( (retstr= issue_LP_register(peer->ipaddr,peer->port,pubkey,pushaddr)) != 0 )
{
printf("[%s] LP_register.(%s) returned.(%s)\n",pushaddr,peer->ipaddr,retstr);
if ( (retjson= cJSON_Parse(retstr)) != 0 )
{
if ( jint(retjson,"registered") != 0 && ++n >= max )
retval = 0;
free_json(retjson);
}
free(retstr);
}
if ( retval == 0 )
break;
}
}
int32_t LP_pubkey_send(bits256 pubkey,char *jsonstr,int32_t freeflag)
{
struct LP_forwardinfo *ptr; struct LP_peerinfo *peer,*tmp; char *hexstr,*retstr; int32_t len,retval = -1; cJSON *retjson;
struct LP_forwardinfo *ptr; struct LP_peerinfo *peer,*tmp; char *hexstr,*retstr; int32_t len,retval = -1; cJSON *retjson,*reqjson;
if ( jsonstr == 0 || jsonstr[0] == 0 || bits256_nonz(pubkey) == 0 )
return(-1);
if ( IAMLP != 0 && (ptr= LP_forwardfind(pubkey)) != 0 && ptr->pushsock >= 0 && ptr->lasttime > time(NULL)-LP_KEEPALIVE )
@ -162,7 +162,7 @@ int32_t LP_pubkey_send(bits256 pubkey,char *jsonstr,int32_t freeflag)
}
free(retstr);
}
if ( retval == 0 )
if ( retval == 0 && peer->pushsock >= 0 )
{
printf("found LPnode.(%s) forward.(%s)\n",peer->ipaddr,jsonstr);
len = (int32_t)strlen(jsonstr) + 1;
@ -170,8 +170,11 @@ int32_t LP_pubkey_send(bits256 pubkey,char *jsonstr,int32_t freeflag)
init_hexbytes_noT(hexstr,(uint8_t *)jsonstr,len);
if ( freeflag != 0 )
free(jsonstr);
if ( peer->pushsock >= 0 )
return(LP_send(peer->pushsock,hexstr,1));
reqjson = cJSON_CreateObject();
jaddstr(reqjson,"method","forwardhex");
jaddstr(reqjson,"hex",hexstr);
free(hexstr);
return(LP_send(peer->pushsock,jprint(reqjson,1),1));
}
}
return(-1);

Loading…
Cancel
Save