Browse Source

test

release/v0.1
jl777 9 years ago
parent
commit
9baf9a96b3
  1. 118
      iguana/iguana_json.c
  2. 27
      iguana/ramchain_api.c
  3. 1
      includes/cJSON.h
  4. 3
      includes/iguana_apideclares.h

118
iguana/iguana_json.c

@ -307,8 +307,122 @@ cJSON *iguana_peersjson(struct iguana_info *coin,int32_t addronly)
else return(array); else return(array);
} }
cJSON *SuperNET_peerarray(struct iguana_info *coin,int32_t max,int32_t supernetflag)
{
int32_t i,r,j,n = 0; struct iguana_peer *addr; cJSON *array = cJSON_CreateArray();
r = rand();
for (j=0; j<IGUANA_MAXPEERS; j++)
{
i = (r + j) % IGUANA_MAXPEERS;
addr = &coin->peers.active[i];
if ( addr->usock >= 0 && (supernetflag == 0 || addr->supernet != 0) )
{
jaddistr(array,addr->ipaddr);
if ( ++n >= max )
break;
}
}
if ( n == 0 )
{
free_json(array);
return(0);
}
return(array);
}
int32_t SuperNET_coinpeers(struct iguana_info *coin,cJSON *SNjson,cJSON *rawjson,int32_t max)
{
cJSON *array;
if ( (array= SuperNET_peerarray(coin,max,1)) != 0 )
{
max -= cJSON_GetArraySize(array);
jadd(SNjson,coin->symbol,array);
}
if ( max > 0 && (array= SuperNET_peerarray(coin,max,0)) != 0 )
{
max -= cJSON_GetArraySize(array);
jadd(rawjson,coin->symbol,array);
}
return(max);
}
void SuperNET_remotepeer(struct supernet_info *myinfo,struct iguana_info *coin,char *symbol,char *ipaddr,int32_t supernetflag)
{
printf("got %s remotepeer.(%s) supernet.%d\n",symbol,ipaddr,supernetflag);
}
void SuperNET_parsepeers(struct supernet_info *myinfo,cJSON *array,int32_t n,int32_t supernetflag)
{
int32_t i,j,m; cJSON *coinarray; char *symbol,*ipaddr; struct iguana_info *ptr;
if ( (array= jarray(&n,array,"supernet")) != 0 )
{
for (i=0; i<n; i++)
{
if ( (symbol= jfieldstr(jitem(array,i))) != 0 )
{
ptr = iguana_coinfind(symbol);
if ( (coinarray= jarray(&m,array,symbol)) != 0 )
{
for (j=0; j<m; j++)
{
if ( (ipaddr= jstr(jitem(coinarray,j),0)) != 0 )
SuperNET_remotepeer(myinfo,ptr,symbol,ipaddr,supernetflag);
}
}
}
}
}
}
#include "../includes/iguana_apidefs.h" #include "../includes/iguana_apidefs.h"
STRING_ARG(SuperNET,mypeers,jsonstr)
{
cJSON *argjson,*SNarray,*rawarray; int32_t n;
if ( (argjson= cJSON_Parse(jsonstr)) != 0 )
{
if ( (SNarray= jarray(&n,argjson,"supernet")) != 0 )
SuperNET_parsepeers(myinfo,SNarray,n,1);
if ( (rawarray= jarray(&n,argjson,"rawpeers")) != 0 )
SuperNET_parsepeers(myinfo,SNarray,n,1);
free_json(argjson);
return(clonestr("{\"result\":\"peers parsed\"}"));
}
return(clonestr("{\"error\":\"couldnt parse jsonstr\"}"));
}
STRING_ARG(SuperNET,getpeers,activecoin)
{
int32_t i,max = 64;
cJSON *SNjson,*rawjson,*retjson = cJSON_CreateObject();
printf("inside SuperNET coin.%p\n",coin);
SNjson = cJSON_CreateObject();
rawjson = cJSON_CreateObject();
if ( coin != 0 )
max = SuperNET_coinpeers(coin,SNjson,rawjson,max);
else
{
for (i=0; i<IGUANA_MAXCOINS&&max>0; i++)
if ( Coins[i] != 0 )
max = SuperNET_coinpeers(Coins[i],SNjson,rawjson,max);
}
if ( max != 64 )
{
jaddstr(retjson,"agent","SuperNET");
jaddstr(retjson,"method","mypeers");
jaddstr(retjson,"result","peers found");
jadd(retjson,"supernet",SNjson);
jadd(retjson,"rawpeers",rawjson);
}
else
{
jaddstr(retjson,"error","no peers");
free_json(SNjson);
free_json(rawjson);
}
return(jprint(retjson,1));
}
STRING_ARG(iguana,peers,activecoin) STRING_ARG(iguana,peers,activecoin)
{ {
if ( coin != 0 ) if ( coin != 0 )
@ -540,7 +654,9 @@ TWO_STRINGS(SuperNET,html,agentform,htmlfile)
char *SuperNET_parser(struct supernet_info *myinfo,char *agent,char *method,cJSON *json,char *remoteaddr) char *SuperNET_parser(struct supernet_info *myinfo,char *agent,char *method,cJSON *json,char *remoteaddr)
{ {
char *coinstr; struct iguana_info *coin = 0; char *coinstr; struct iguana_info *coin = 0;
if ( (coinstr= jstr(json,"coin")) != 0 ) if ( (coinstr= jstr(json,"activecoin")) != 0 )
coin = iguana_coinfind(coinstr);
if ( coin == 0 && (coinstr= jstr(json,"coin")) != 0 )
coin = iguana_coinfind(coinstr); coin = iguana_coinfind(coinstr);
if ( strcmp(agent,"ramchain") == 0 && coin == 0 ) if ( strcmp(agent,"ramchain") == 0 && coin == 0 )
return(clonestr("{\"error\":\"ramchain needs coin\"}")); return(clonestr("{\"error\":\"ramchain needs coin\"}"));

27
iguana/ramchain_api.c

@ -16,33 +16,6 @@
#include "iguana777.h" #include "iguana777.h"
#include "../includes/iguana_apidefs.h" #include "../includes/iguana_apidefs.h"
STRING_ARG(ramchain,getpeers,activecoin)
{
int32_t i,r,j,iter,n; struct iguana_peer *addr; cJSON *array = cJSON_CreateArray();
cJSON *retjson = cJSON_CreateObject();
printf("inside getpeers coin.%p\n",coin);
if ( coin != 0 )
{
r = rand();
for (iter=n=0; iter<2; iter++)
{
for (j=0; j<IGUANA_MAXPEERS; j++)
{
i = (r + j) % IGUANA_MAXPEERS;
addr = &coin->peers.active[i];
if ( addr->usock >= 0 && (iter == 1 || addr->supernet != 0) )
{
jaddistr(array,addr->ipaddr);
if ( ++n >= 64 )
break;
}
}
}
}
jadd(retjson,"peers",array);
return(jprint(retjson,1));
}
ZERO_ARGS(ramchain,getinfo) ZERO_ARGS(ramchain,getinfo)
{ {
cJSON *retjson = cJSON_CreateObject(); cJSON *retjson = cJSON_CreateObject();

1
includes/cJSON.h

@ -160,6 +160,7 @@ extern "C"
/* When assigning an integer value, it needs to be propagated to valuedouble too. */ /* When assigning an integer value, it needs to be propagated to valuedouble too. */
#define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
#define jfieldstr get_cJSON_fieldname
char *cJSON_str(cJSON *json); char *cJSON_str(cJSON *json);
char *jstr(cJSON *json,char *field); char *jstr(cJSON *json,char *field);

3
includes/iguana_apideclares.h

@ -16,6 +16,8 @@
ZERO_ARGS(SuperNET,help); ZERO_ARGS(SuperNET,help);
TWO_STRINGS(SuperNET,html,agentform,htmlfile); TWO_STRINGS(SuperNET,html,agentform,htmlfile);
STRING_ARG(SuperNET,bitcoinrpc,setcoin); STRING_ARG(SuperNET,bitcoinrpc,setcoin);
STRING_ARG(SuperNET,getpeers,activecoin);
STRING_ARG(SuperNET,mypeers,jsonstr);
STRING_ARG(iguana,peers,activecoin); STRING_ARG(iguana,peers,activecoin);
STRING_AND_INT(iguana,maxpeers,activecoin,max); STRING_AND_INT(iguana,maxpeers,activecoin,max);
@ -28,7 +30,6 @@ TWO_STRINGS(iguana,removenode,activecoin,ipaddr);
TWO_STRINGS(iguana,oneshot,activecoin,ipaddr); TWO_STRINGS(iguana,oneshot,activecoin,ipaddr);
TWO_STRINGS(iguana,nodestatus,activecoin,ipaddr); TWO_STRINGS(iguana,nodestatus,activecoin,ipaddr);
STRING_ARG(ramchain,getpeers,activecoin);
ZERO_ARGS(ramchain,getinfo); ZERO_ARGS(ramchain,getinfo);
ZERO_ARGS(ramchain,getbestblockhash); ZERO_ARGS(ramchain,getbestblockhash);
ZERO_ARGS(ramchain,getblockcount); ZERO_ARGS(ramchain,getblockcount);

Loading…
Cancel
Save